Skip to content

Commit d55482a

Browse files
authored
fix: change default org to be immutable (#872)
1 parent 505fd0d commit d55482a

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/aap_eda/api/serializers/organization.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# limitations under the License.
1414

1515
from ansible_base.lib.serializers.common import NamedCommonModelSerializer
16+
from django.conf import settings
1617

18+
from aap_eda.api import exceptions as api_exc
1719
from aap_eda.core.models import Organization
1820

1921
from .fields.ansible_resource import AnsibleResourceFieldSerializer
@@ -37,6 +39,17 @@ class Meta:
3739
"modified_by",
3840
]
3941

42+
def validate(self, data):
43+
# when creating a new org, self.instance is empty
44+
if (
45+
self.instance
46+
and self.instance.name == settings.DEFAULT_ORGANIZATION_NAME
47+
):
48+
raise api_exc.Conflict(
49+
"The default organization cannot be modified."
50+
)
51+
return data
52+
4053

4154
class OrganizationRefSerializer(NamedCommonModelSerializer):
4255
class Meta:

tests/integration/api/test_organization.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,35 @@ def test_retrieve_organization_not_exist(admin_client: APIClient):
136136

137137

138138
@pytest.mark.django_db
139-
def test_partial_update_organization(
140-
default_organization: models.Organization,
141-
admin_client: APIClient,
139+
def test_partial_update_organization_success(
140+
new_organization: models.Organization,
141+
superuser_client: APIClient,
142142
):
143143
new_data = {"name": "new-name", "description": "New Description"}
144-
response = admin_client.patch(
145-
f"{api_url_v1}/organizations/{default_organization.id}/", data=new_data
144+
response = superuser_client.patch(
145+
f"{api_url_v1}/organizations/{new_organization.id}/", data=new_data
146146
)
147147
assert response.status_code == status.HTTP_200_OK
148148

149-
default_organization.refresh_from_db()
150-
assert_organization_data(response.data, default_organization)
149+
new_organization.refresh_from_db()
150+
assert_organization_data(response.data, new_organization)
151+
152+
153+
@pytest.mark.django_db
154+
def test_partial_update_default_organization_exception(
155+
default_organization: models.Organization,
156+
superuser_client: APIClient,
157+
):
158+
new_data = {"name": "new-name", "description": "New Description"}
159+
response = superuser_client.patch(
160+
f"{api_url_v1}/organizations/{default_organization.id}/", data=new_data
161+
)
162+
assert response.status_code == status.HTTP_409_CONFLICT
163+
assert (
164+
response.data["detail"]
165+
== "The default organization cannot be modified."
166+
)
167+
assert models.Organization.objects.get(id=default_organization.id)
151168

152169

153170
@pytest.mark.django_db

tests/integration/dab_rbac/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def get_model_name(self, model):
4747
def get_fixture_object(self, request, name):
4848
if name == "rulebook_process":
4949
name = "activation_instance"
50+
# default org is immutable
51+
elif name == "organization":
52+
return request.getfixturevalue("new_organization")
5053
return request.getfixturevalue(f"default_{name}")
5154

5255
def get_post_data(self, model_obj):

0 commit comments

Comments
 (0)