Skip to content
Snippets Groups Projects
Commit 72717d01 authored by Marte Fossum's avatar Marte Fossum
Browse files

Make sponsororanizationalunit endpoint match doc

parent d77541bf
No related branches found
No related tags found
1 merge request!433Make sponsororanizationalunit endpoint match doc
......@@ -97,21 +97,24 @@ class SponsorOrgunitLinkView(
automatic=automatic,
source=source,
)
serializer = self.serializer_class(sponsor_orgunit, many=True)
return Response(serializer.data)
SponsorOrganizationalUnit.objects.create(
sponsor=Sponsor.objects.get(id=kwargs["sponsor_id"]),
organizational_unit=OrganizationalUnit.objects.get(id=kwargs["orgunit_id"]),
hierarchical_access=hierarchical_access,
automatic=automatic,
source=source,
)
else:
SponsorOrganizationalUnit.objects.create(
sponsor=Sponsor.objects.get(id=kwargs["sponsor_id"]),
organizational_unit=OrganizationalUnit.objects.get(
id=kwargs["orgunit_id"]
),
hierarchical_access=hierarchical_access,
automatic=automatic,
source=source,
)
try:
sponsor_orgunit = self.queryset.get(
sponsor__id=sponsor_id, organizational_unit__id=orgunit_id
)
except SponsorOrganizationalUnit.DoesNotExist:
return Response({})
sponsor_orgunit = self.queryset.filter(
sponsor__id=sponsor_id, organizational_unit__id=orgunit_id
)
serializer = self.serializer_class(sponsor_orgunit, many=True)
serializer = self.serializer_class(sponsor_orgunit)
return Response(serializer.data)
def destroy(self, request, *args, **kwargs):
......@@ -137,10 +140,13 @@ class SponsorOrgunitLinkView(
def retrieve(self, request, *args, **kwargs):
"""Returns a given SponsorOrganizationalUnit object"""
(sponsor_id, orgunit_id) = self._extract_sponsor_and_orgunit(kwargs)
unit = self.queryset.filter(
sponsor__id=sponsor_id, organizational_unit__id=orgunit_id
)
serializer = self.serializer_class(unit, many=True)
try:
unit = self.queryset.get(
sponsor__id=sponsor_id, organizational_unit__id=orgunit_id
)
except SponsorOrganizationalUnit.DoesNotExist:
return Response({})
serializer = self.serializer_class(unit)
return Response(serializer.data)
def _extract_sponsor_and_orgunit(self, request_data):
......
......@@ -176,7 +176,8 @@ def test_add_sponsor_unit_link_with_no_access_parameter(
response = client.post(create_sponsor_link_url)
assert response.status_code == status.HTTP_200_OK
response_body = response.json()
assert len(response_body) == 1
assert response_body["sponsor"] == sponsor_id
assert response_body["organizational_unit"] == unit_foo.id
# Check that the unit is attached to the sponsor
sponsor_lookup_response = client.get(sponsor_url, kwargs={"id": sponsor_id})
......@@ -204,9 +205,10 @@ def test_retrieve_sponsor_orgunit(
"v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_guy.id, "orgunit_id": unit_foo.id},
)
sponsor_unit = client.get(url).json()
response_body = client.get(url).json()
assert len(sponsor_unit) == 1
assert response_body["sponsor"] == sponsor_guy.id
assert response_body["organizational_unit"] == unit_foo.id
@pytest.mark.django_db
......@@ -219,9 +221,9 @@ def test_no_sponsor_orgunit_exists(
"v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_foo.id, "orgunit_id": unit_foo.id},
)
sponsor_unit = client.get(url).json()
response_body = client.get(url).json()
assert len(sponsor_unit) == 0
assert not response_body
@pytest.mark.django_db
......@@ -254,8 +256,8 @@ def test_update_sponsor_orgunit(
"v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_guy.id, "orgunit_id": unit_foo.id},
)
assert sponsor_org_unit.hierarchical_access is False
response_data = client.post(url, data=data).json()[0]
assert not sponsor_org_unit.hierarchical_access
response_data = client.post(url, data=data).json()
assert response_data["hierarchical_access"] == data["hierarchical_access"]
assert response_data["automatic"] == data["automatic"]
......@@ -278,7 +280,7 @@ def test_create_new_sponsor_orgunit(
kwargs={"sponsor_id": sponsor_foo.id, "orgunit_id": unit_foo.id},
)
sponsor_unit = client.get(get_url).json()
assert len(sponsor_unit) == 0
assert not sponsor_unit
post_url = reverse(
"v1:sponsor_orgunit-detail",
......@@ -286,9 +288,8 @@ def test_create_new_sponsor_orgunit(
)
response_data = client.post(post_url, data=data).json()
assert len(response_data) == 1
response_data = response_data[0]
assert response_data["sponsor"] == sponsor_foo.id
assert response_data["organizational_unit"] == unit_foo.id
assert response_data["hierarchical_access"] == data["hierarchical_access"]
assert response_data["automatic"] == data["automatic"]
assert response_data["source"] == data["source"]
......@@ -221,7 +221,6 @@ def test_different_feide_id_already_exists(
assert person_identities[0].value == "foobar@test.no"
call_command("import_usernames_from_cerebrum", "--without-usernames", "--commit")
person_identities = person_three.identities.filter(type=usr_id_type)
print(person_identities)
assert len(person_identities) == 2
assert person_identities[0].value == "foobar@test.no"
assert person_identities[1].value == "foobar@inst.no"
......
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