Skip to content
Snippets Groups Projects
test_invite_guest.py 2.28 KiB
import datetime
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 Identity, Person, Role, Invitation, InvitationLink
from gregui.api.views.invitation import InvitationView


@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": (
                datetime.datetime.today() + datetime.timedelta(days=1)
            ).strftime("%Y-%m-%d"),
            "end_date": (
                datetime.datetime.today() + datetime.timedelta(days=10)
            ).strftime("%Y-%m-%d"),
            "orgunit": unit_foo.id,
            "type": role_type_foo.id,
        },
    }
    url = reverse("gregui-v1:invitation")

    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 = InvitationView.as_view()
    response = view(request)

    assert response.status_code == status.HTTP_201_CREATED

    assert Person.objects.count() == 1
    person = Person.objects.first()
    assert person.first_name == "Foo"
    assert person.last_name == "Bar"

    assert Identity.objects.filter(
        person=person,
        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