From 1184a9d5c65804ee257367a8acd44900c1e7b387 Mon Sep 17 00:00:00 2001 From: Tore Brede <Tore.Brede@uib.no> Date: Sun, 31 Oct 2021 12:51:18 +0100 Subject: [PATCH] GREG-100: Adding test for deleting invitation --- gregui/api/urls.py | 4 ++-- gregui/api/views/invitation.py | 4 ++-- gregui/tests/api/test_invite_guest.py | 30 +++++++++++++++++++++------ gregui/tests/conftest.py | 7 +++---- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/gregui/api/urls.py b/gregui/api/urls.py index af6ff1ff..46e1cf0f 100644 --- a/gregui/api/urls.py +++ b/gregui/api/urls.py @@ -4,7 +4,7 @@ from rest_framework.routers import DefaultRouter from gregui.api.views.invitation import ( CheckInvitationView, - CreateInvitationView, + InvitationView, InvitedGuestView, ) from gregui.api.views.person import PersonSearchView, PersonView @@ -21,7 +21,7 @@ urlpatterns += [ re_path(r"units/$", UnitsViewSet.as_view(), name="units"), path("invited/", InvitedGuestView.as_view(), name="invited-info"), path("invited/<uuid>", CheckInvitationView.as_view(), name="invite-verify"), - path("invite/", CreateInvitationView.as_view(), name="invite-create"), + path("invite/", InvitationView.as_view(), name="invitation"), path("person/<int:id>", PersonView.as_view(), name="person-get"), path( "person/search/<searchstring>", PersonSearchView.as_view(), name="person-search" diff --git a/gregui/api/views/invitation.py b/gregui/api/views/invitation.py index e93ebd0a..9b0f58ad 100644 --- a/gregui/api/views/invitation.py +++ b/gregui/api/views/invitation.py @@ -20,9 +20,9 @@ from gregui.api.serializers.invitation import InviteGuestSerializer from gregui.models import GregUserProfile -class CreateInvitationView(CreateAPIView, DestroyAPIView): +class InvitationView(CreateAPIView, DestroyAPIView): """ - Invitation creation endpoint + Endpoint for invitation creation and cancelling { diff --git a/gregui/tests/api/test_invite_guest.py b/gregui/tests/api/test_invite_guest.py index 87554ec7..9e95b369 100644 --- a/gregui/tests/api/test_invite_guest.py +++ b/gregui/tests/api/test_invite_guest.py @@ -4,8 +4,8 @@ from rest_framework import status from rest_framework.reverse import reverse from rest_framework.test import APIRequestFactory, force_authenticate -from greg.models import Identity, Person -from gregui.api.views.invitation import CreateInvitationView +from greg.models import Identity, Person, Role, Invitation, InvitationLink +from gregui.api.views.invitation import InvitationView @pytest.mark.django_db @@ -16,16 +16,16 @@ def test_invite_guest(client, user_sponsor, unit_foo, role_type_foo): "email": "test@example.com", "role": { "start_date": ( - datetime.datetime.today() + datetime.timedelta(days=1) + datetime.datetime.today() + datetime.timedelta(days=1) ).strftime("%Y-%m-%d"), "end_date": ( - datetime.datetime.today() + datetime.timedelta(days=10) + datetime.datetime.today() + datetime.timedelta(days=10) ).strftime("%Y-%m-%d"), "orgunit": unit_foo.id, "type": role_type_foo.id, }, } - url = reverse("gregui-v1:invite-create") + url = reverse("gregui-v1:invitation") all_persons = Person.objects.all() assert len(all_persons) == 0 @@ -34,7 +34,7 @@ def test_invite_guest(client, user_sponsor, unit_foo, role_type_foo): request = factory.post(url, data, format="json") force_authenticate(request, user=user_sponsor) - view = CreateInvitationView.as_view() + view = InvitationView.as_view() response = view(request) assert response.status_code == status.HTTP_201_CREATED @@ -49,3 +49,21 @@ def test_invite_guest(client, user_sponsor, unit_foo, role_type_foo): type=Identity.IdentityType.PRIVATE_EMAIL, value="test@example.com", ).exists() + + +@pytest.mark.django_db +def test_invite_cancel(client, invitation_link, invitation, role, log_in, sponsor_guy, user_sponsor): + # TODO: Should all sponsors be allowed to delete arbitrary invitations? + log_in(user_sponsor) + url = reverse("gregui-v1:invitation") + + # Check that the role is there + role = Role.objects.get(id=role.id) + response = client.delete("%s?role_id=%s" % (url, str(role.id))) + + assert response.status_code == status.HTTP_200_OK + + # The role, invitation and connected links should now have been removed + assert Role.objects.filter(id=role.id).count() == 0 + assert Invitation.objects.filter(id=invitation.id).count() == 0 + assert InvitationLink.objects.filter(invitation__id=invitation.id).count() == 0 diff --git a/gregui/tests/conftest.py b/gregui/tests/conftest.py index 1bd22154..fc70bdca 100644 --- a/gregui/tests/conftest.py +++ b/gregui/tests/conftest.py @@ -9,7 +9,6 @@ from django.utils.timezone import make_aware from rest_framework.authtoken.admin import User from rest_framework.test import APIClient - from greg.models import ( Invitation, InvitationLink, @@ -26,6 +25,7 @@ from gregui.models import GregUserProfile # see https://github.com/joke2k/faker/issues/753 logging.getLogger("faker").setLevel(logging.ERROR) + # OIDC stuff @pytest.fixture def claims(): @@ -246,9 +246,8 @@ def greg_sponsors(data): @pytest.fixture -def log_in(client, greg_users): - def _log_in(username): - user = greg_users[username] +def log_in(client): + def _log_in(user): client.force_login(user=user) # It seems like the session was not updated automatically this way session = client.session -- GitLab