From 3e7f3a7b276bbd31cd786a15175330b9864f9d28 Mon Sep 17 00:00:00 2001 From: Tore Brede <Tore.Brede@uib.no> Date: Mon, 11 Oct 2021 11:01:14 +0200 Subject: [PATCH] Fixing test --- gregui/tests/api/test_invite_guest.py | 37 +++++++++++---- gregui/tests/conftest.py | 65 +++++++++++++++++++++------ 2 files changed, 79 insertions(+), 23 deletions(-) diff --git a/gregui/tests/api/test_invite_guest.py b/gregui/tests/api/test_invite_guest.py index 176aa99c..17314b2a 100644 --- a/gregui/tests/api/test_invite_guest.py +++ b/gregui/tests/api/test_invite_guest.py @@ -1,17 +1,36 @@ 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): - data = {"first_name": "Foo", "last_name": "Bar", "email": "test@example.com", "role": { - "start_date": "2019-08-06", - "end_date": "2019-08-10", - "id": 1 +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") - response = client.post(url, data, format="json") + 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 - assert response.json() == {"id": 1, **data} + + 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 d58676c3..8f908ab7 100644 --- a/gregui/tests/conftest.py +++ b/gregui/tests/conftest.py @@ -1,24 +1,61 @@ -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 -- GitLab