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

Merge branch 'fix-sponsor-endpoint' into 'master'

Make sponsororanizationalunit endpoint match doc

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