Skip to content
Snippets Groups Projects
Commit 3e7f3a7b authored by Tore.Brede's avatar Tore.Brede
Browse files

Fixing test

parent c5922515
No related branches found
No related tags found
1 merge request!100Remove register endpoint
import pytest import pytest
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse 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 @pytest.mark.django_db
def test_invite_guest(client): def test_invite_guest(client, user_sponsor, unit_foo, role_type_foo):
data = {"first_name": "Foo", "last_name": "Bar", "email": "test@example.com", "role": { data = {'first_name': 'Foo', 'last_name': 'Bar', 'email': 'test@example.com', 'role': {
"start_date": "2019-08-06", 'start_date': '2019-08-06',
"end_date": "2019-08-10", 'end_date': '2019-08-10',
"id": 1 'orgunit_id': unit_foo.id,
'type': role_type_foo.id
}} }}
url = reverse("gregui-v1:invite-create") url = reverse('gregui-v1:invite-create')
response = client.post(url, data, format="json")
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.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'
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 import pytest
# from greg.models import ( from greg.models import Sponsor, OrganizationalUnit, RoleType
# Consent, from gregui.models import GregUserProfile
# Notification,
# Person, # faker spams the logs with localisation warnings
# Sponsor, # see https://github.com/joke2k/faker/issues/753
# SponsorOrganizationalUnit, logging.getLogger("faker").setLevel(logging.ERROR)
# Identity,
# Role,
# RoleType,
# OrganizationalUnit,
# ConsentType,
# )
@pytest.fixture @pytest.fixture
def client() -> APIClient: def client() -> APIClient:
client = APIClient() client = APIClient()
return client 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment