diff --git a/gregui/api/urls.py b/gregui/api/urls.py index 064d0077485326015a084f5459e02f69f688bc72..64977e22dcfe79a5acd97d888aab263ab591b97b 100644 --- a/gregui/api/urls.py +++ b/gregui/api/urls.py @@ -2,7 +2,6 @@ from django.urls import re_path, path from rest_framework.routers import DefaultRouter -from gregui.api.views.guest import GuestRegisterView from gregui.api.views.invitation import ( CheckInvitationView, CreateInvitationView, @@ -15,10 +14,9 @@ router = DefaultRouter(trailing_slash=False) urlpatterns = router.urls urlpatterns += [ - re_path(r"register/$", GuestRegisterView.as_view(), name="guest-register"), re_path(r"roletypes/$", RoleTypeViewSet.as_view(), name="role-types"), re_path(r"units/$", UnitsViewSet.as_view(), name="units"), path("invited/", InvitedGuestView.as_view(), name="invite"), path("invited/<uuid>", CheckInvitationView.as_view()), - path("invite/", CreateInvitationView.as_view()), + path("invite/", CreateInvitationView.as_view(), name="invite-create"), ] diff --git a/gregui/api/views/guest.py b/gregui/api/views/guest.py deleted file mode 100644 index dc9439409c513a61c64eece9bd91edff16b5d86a..0000000000000000000000000000000000000000 --- a/gregui/api/views/guest.py +++ /dev/null @@ -1,11 +0,0 @@ -from rest_framework import permissions -from rest_framework.generics import CreateAPIView - -from greg.models import Person -from gregui.api.serializers.guest import GuestRegisterSerializer - - -class GuestRegisterView(CreateAPIView): - queryset = Person.objects.all() - permission_classes = [permissions.AllowAny] - serializer_class = GuestRegisterSerializer diff --git a/gregui/tests/api/test_guest.py b/gregui/tests/api/test_guest.py deleted file mode 100644 index 29e3f83f48b160ec00a221903a1686421a2bf5eb..0000000000000000000000000000000000000000 --- a/gregui/tests/api/test_guest.py +++ /dev/null @@ -1,13 +0,0 @@ -import pytest - -from rest_framework import status -from rest_framework.reverse import reverse - - -@pytest.mark.django_db -def test_register_guest(client): - data = {"first_name": "Foo", "last_name": "Bar", "email": "test@example.com"} - url = reverse("gregui-v1:guest-register") - response = client.post(url, data) - assert response.status_code == status.HTTP_201_CREATED - assert response.json() == {"id": 1, **data} diff --git a/gregui/tests/api/test_invite_guest.py b/gregui/tests/api/test_invite_guest.py new file mode 100644 index 0000000000000000000000000000000000000000..8b6c2e79deb4ec25b41e40c653f8c35eef8e76fb --- /dev/null +++ b/gregui/tests/api/test_invite_guest.py @@ -0,0 +1,41 @@ +import pytest +from rest_framework import status +from rest_framework.reverse import reverse +from rest_framework.test import APIRequestFactory, force_authenticate + +from greg.models import Person +from gregui.api.views.invitation import CreateInvitationView + + +@pytest.mark.django_db +def test_invite_guest(client, user_sponsor, unit_foo, role_type_foo): + data = { + "first_name": "Foo", + "last_name": "Bar", + "email": "test@example.com", + "role": { + "start_date": "2019-08-06", + "end_date": "2019-08-10", + "orgunit_id": unit_foo.id, + "type": role_type_foo.id, + }, + } + url = reverse("gregui-v1:invite-create") + + all_persons = Person.objects.all() + assert len(all_persons) == 0 + + factory = APIRequestFactory() + request = factory.post(url, data, format="json") + force_authenticate(request, user=user_sponsor) + + view = CreateInvitationView.as_view() + response = view(request) + + assert response.status_code == status.HTTP_201_CREATED + + all_persons = Person.objects.all() + + assert len(all_persons) == 1 + assert all_persons[0].first_name == "Foo" + assert all_persons[0].last_name == "Bar" diff --git a/gregui/tests/conftest.py b/gregui/tests/conftest.py index d58676c39755ffdf612a2c97ce18de057888c486..b71080b0e796c069861d615453d07e824b6e5a7f 100644 --- a/gregui/tests/conftest.py +++ b/gregui/tests/conftest.py @@ -1,24 +1,58 @@ -from rest_framework.test import APIClient +import logging -# from django.contrib.auth import get_user_model +from django.contrib.auth import get_user_model +from rest_framework.authtoken.admin import User +from rest_framework.test import APIClient import pytest -# from greg.models import ( -# Consent, -# Notification, -# Person, -# Sponsor, -# SponsorOrganizationalUnit, -# Identity, -# Role, -# RoleType, -# OrganizationalUnit, -# ConsentType, -# ) +from greg.models import Sponsor, OrganizationalUnit, RoleType +from gregui.models import GregUserProfile + +# faker spams the logs with localisation warnings +# see https://github.com/joke2k/faker/issues/753 +logging.getLogger("faker").setLevel(logging.ERROR) @pytest.fixture def client() -> APIClient: client = APIClient() return client + + +@pytest.fixture +def unit_foo() -> OrganizationalUnit: + ou = OrganizationalUnit.objects.create(orgreg_id="12345", name_en="foo_unit") + return OrganizationalUnit.objects.get(id=ou.id) + + +@pytest.fixture +def role_type_foo() -> RoleType: + rt = RoleType.objects.create(identifier="role_foo", name_en="Role Foo") + return RoleType.objects.get(id=rt.id) + + +@pytest.fixture +def sponsor_guy(unit_foo: OrganizationalUnit) -> Sponsor: + sponsor = Sponsor.objects.create( + feide_id="guy@example.org", first_name="Sponsor", last_name="Guy" + ) + sponsor.units.add(unit_foo, through_defaults={"hierarchical_access": False}) + return Sponsor.objects.get(id=sponsor.id) + + +@pytest.fixture +def user_sponsor(sponsor_guy: Sponsor) -> User: + user_model = get_user_model() + + # Create a user and link him to a sponsor + user = user_model.objects.create( + username="test_sponsor", + email="test@example.org", + first_name="Test", + last_name="Sponsor", + ) + GregUserProfile.objects.create(user=user, sponsor=sponsor_guy) + + # This user is a sponsor for unit_foo + return user