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

GREG-91: Setting hierarchical access by default to false

parent 1856b17e
No related branches found
No related tags found
1 merge request!126GREG-91: Expand sponsor api
Pipeline #98526 passed
......@@ -79,7 +79,8 @@ class SponsorOrgunitLinkView(
def create(self, request, *args, **kwargs):
(sponsor_id, orgunit_id) = self._extract_sponsor_and_orgunit(kwargs)
hierarchical_access = request.data.get("hierarchical_access")
# Default to false if hierarchical_access is not specified
hierarchical_access = request.data.get("hierarchical_access", "False")
sponsor = Sponsor.objects.get(id=sponsor_id)
sponsor.units.add(
orgunit_id, through_defaults={"hierarchical_access": hierarchical_access}
......@@ -92,7 +93,6 @@ class SponsorOrgunitLinkView(
but the link between the sponsor and the unit"""
(sponsor_id, orgunit_id) = self._extract_sponsor_and_orgunit(kwargs)
sponsor = Sponsor.objects.filter(id=sponsor_id).get()
sponsor.units.remove(orgunit_id)
return Response(status=status.HTTP_204_NO_CONTENT)
......
......@@ -3,7 +3,13 @@ from rest_framework import status
from rest_framework.reverse import reverse
from greg.models import OrganizationalUnit, Sponsor, Person, Identity
from greg.models import (
OrganizationalUnit,
Sponsor,
Person,
Identity,
SponsorOrganizationalUnit,
)
@pytest.mark.django_db
......@@ -66,14 +72,14 @@ def test_add_sponsor_with_unit(client, unit_foo: OrganizationalUnit):
assert len(sponsor_lookup_response_body["results"]) == 1
assert len(sponsor_lookup_response_body["results"][0]["units"]) == 0
data = {"hierarchical_access": "true"}
data = {"hierarchical_access": "True"}
create_sponsor_link_url = reverse(
"v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_id, "orgunit_id": unit_foo.id},
)
response = client.post(create_sponsor_link_url, data=data)
assert response.status_code == status.HTTP_200_OK
assert response.status_code == status.HTTP_204_NO_CONTENT
sponsor_lookup_response = client.get(sponsor_url, kwargs={"id": sponsor_id})
sponsor_lookup_response_body = sponsor_lookup_response.json()
......@@ -91,7 +97,7 @@ def test_remove_sponsor_orgunit_link(
response_get = client.get(sponsor_detail_url).json()
assert len(response_get["units"]) == 0
data = {"hierarchical_access": True}
data = {"hierarchical_access": "True"}
sponsor_orgunit_url = reverse(
"v1:sponsor_orgunit-detail",
......@@ -144,3 +150,40 @@ def test_delete_sponsor_connected_to_identity_not_allowed(
# Check that the sponsor has been deleted
response_get = client.get(sponsor_detail_url)
assert response_get.status_code == status.HTTP_404_NOT_FOUND
@pytest.mark.django_db
def test_add_sponsor_unit_link_with_no_access_parameter(
client, unit_foo: OrganizationalUnit
):
sponsor_url = reverse("v1:sponsor-list")
data = {
"feide_id": "sponsor@example.org",
"first_name": "Test",
"last_name": "Sponsor",
}
response = client.post(sponsor_url, data=data)
sponsor_id = response.json()["id"]
# Do a post with no data
create_sponsor_link_url = reverse(
"v1:sponsor_orgunit-detail",
kwargs={"sponsor_id": sponsor_id, "orgunit_id": unit_foo.id},
)
response = client.post(create_sponsor_link_url)
assert response.status_code == status.HTTP_204_NO_CONTENT
# Check that the unit is attached to the sponsor
sponsor_lookup_response = client.get(sponsor_url, kwargs={"id": sponsor_id})
sponsor_lookup_response_body = sponsor_lookup_response.json()
assert len(sponsor_lookup_response_body["results"][0]["units"]) == 1
attached_unit = sponsor_lookup_response_body["results"][0]["units"][0]
assert attached_unit["id"] == unit_foo.id
# Check that hierarchical_access is set to False for the link between the sponsor and unit
sponsor_organization_unit = SponsorOrganizationalUnit.objects.filter(
sponsor_id=sponsor_id, organizational_unit_id=unit_foo.id
).get()
assert not sponsor_organization_unit.hierarchical_access
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