Skip to content

Commit

Permalink
Add missing User API (url, view, serializer, tests)
Browse files Browse the repository at this point in the history
Signed-off-by: kot0dama <89980752+kot0dama@users.noreply.github.com>
  • Loading branch information
kot0dama committed Apr 9, 2024
1 parent 9e6719f commit 53a93a3
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
18 changes: 18 additions & 0 deletions httprequest_lego_provider/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# See LICENSE file for licensing details.
"""Serializers."""

# imported-auth-user has to be disabled as the import is needed for UserSerializer
# pylint:disable=imported-auth-user
from django.contrib.auth.models import User
from rest_framework import serializers

from .models import Domain, DomainUserPermission
Expand Down Expand Up @@ -35,3 +38,18 @@ class Meta:

model = DomainUserPermission
fields = "__all__"


class UserSerializer(serializers.ModelSerializer):
"""Serializer for the User objects."""

class Meta:
"""Serializer configuration.
Attributes:
model: the model to serialize.
fields: fields to serialize.
"""

model = User
fields = "__all__"
57 changes: 57 additions & 0 deletions httprequest_lego_provider/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,60 @@ def test_post_domain_user_permission_when_logged_in_as_admin_user(

assert DomainUserPermission.objects.filter(user=99, domain=domain) is not None
assert response.status_code == 201


@pytest.mark.django_db
def test_post_user_when_logged_in_as_non_admin_user(client: Client, user_auth_token: str):
"""
arrange: log in a non-admin user.
act: submit a POST request for the user URL.
assert: a 403 is returned and the user is not inserted in the database.
"""
response = client.post(
"/api/v1/users/",
data={"username": "non-existing-user"},
format="json",
headers={"AUTHORIZATION": f"Basic {user_auth_token}"},
)

with pytest.raises(User.DoesNotExist):
User.objects.get(username="non-existing-user")
assert response.status_code == 403


@pytest.mark.django_db
def test_post_user_when_logged_in_as_admin_user(client: Client, admin_user_auth_token: str):
"""
arrange: log in an admin user.
act: submit a POST request for the user URL.
assert: a 201 is returned and the user is inserted in the database.
"""
response = client.post(
"/api/v1/users/",
data={"username": "new-user"},
format="json",
headers={"AUTHORIZATION": f"Basic {admin_user_auth_token}"},
)

assert User.objects.get(username="new-user") is not None
assert response.status_code == 201


@pytest.mark.django_db
def test_post_user_when_logged_in_as_admin_user_and_user_invalid(
client: Client, admin_user_auth_token: str
):
"""
arrange: log in a admin user.
act: submit a POST request with an invalid value for the user URL.
assert: a 400 is returned.
"""
existing = User.objects.all()[0]
response = client.post(
"/api/v1/users/",
data={"username": existing.username},
format="json",
headers={"AUTHORIZATION": f"Basic {admin_user_auth_token}"},
)

assert response.status_code == 400
1 change: 1 addition & 0 deletions httprequest_lego_provider/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
router = DefaultRouter()
router.register("domains", views.DomainViewSet)
router.register("domain-user-permissions", views.DomainUserPermissionViewSet)
router.register("users", views.UserViewSet)

urlpatterns = [
path("cleanup", views.handle_cleanup, name="cleanup"),
Expand Down
19 changes: 18 additions & 1 deletion httprequest_lego_provider/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from typing import Optional

# imported-auth-user has to be disabled as the import is needed for UserViewSet
# pylint:disable=imported-auth-user
from django.contrib.auth.models import User
from django.http import HttpRequest, HttpResponse
from rest_framework import viewsets
from rest_framework.decorators import api_view
Expand All @@ -15,7 +18,7 @@
from .dns import remove_dns_record, write_dns_record
from .forms import CleanupForm, PresentForm
from .models import Domain, DomainUserPermission
from .serializers import DomainSerializer, DomainUserPermissionSerializer
from .serializers import DomainSerializer, DomainUserPermissionSerializer, UserSerializer


@api_view(["POST"])
Expand Down Expand Up @@ -101,3 +104,17 @@ class DomainUserPermissionViewSet(viewsets.ModelViewSet):
queryset = DomainUserPermission.objects.all()
serializer_class = DomainUserPermissionSerializer
permission_classes = [IsAdminUser]


class UserViewSet(viewsets.ModelViewSet):
"""Views for the User.
Attributes:
queryset: query for the objects in the model.
serializer_class: class used for serialization.
permission_classes: list of classes to match permissions.
"""

queryset = User.objects.all().order_by("-date_joined")
serializer_class = UserSerializer
permission_classes = [IsAdminUser]

0 comments on commit 53a93a3

Please sign in to comment.