diff --git a/greg/api/views/sponsor.py b/greg/api/views/sponsor.py index adb8cf21b380286036ef465bf4d4f04edb0304dc..570b8d2bc20a1a99b127e29beecb574f15418259 100644 --- a/greg/api/views/sponsor.py +++ b/greg/api/views/sponsor.py @@ -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): diff --git a/greg/tests/api/test_sponsor.py b/greg/tests/api/test_sponsor.py index 58d5f7b63e96621da83f7083747ccea28ce2dcc4..2df5dca54234157a682a9952ecebc18658331981 100644 --- a/greg/tests/api/test_sponsor.py +++ b/greg/tests/api/test_sponsor.py @@ -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"] diff --git a/greg/tests/management/test_import_usernames.py b/greg/tests/management/test_import_usernames.py index 16735e1aded43d69507834df39e615754cc36255..008538cf7679ed414dda2351dee652dd68867cf8 100644 --- a/greg/tests/management/test_import_usernames.py +++ b/greg/tests/management/test_import_usernames.py @@ -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"