Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(project/serializer): limit edit to only fields that make sense #4846

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ class Meta:
"stale_flags_limit_days",
"edge_v2_migration_status",
)
read_only_fields = (
"enable_dynamo_db",
"edge_v2_migration_status",
)

def update(self, instance: Project, validated_data: dict) -> Project:
# Prevent updates to `organisation` field
validated_data.pop("organisation", None)
return super().update(instance, validated_data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's perhaps better to just add a new serializer for update requests which adds organisation to the read only fields?


def get_migration_status(self, obj: Project) -> str:
if not settings.PROJECT_METADATA_TABLE_NAME_DYNAMO:
Expand Down
31 changes: 31 additions & 0 deletions api/tests/unit/projects/test_unit_projects_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ def test_can_update_project(
assert response.json()["stale_flags_limit_days"] == new_stale_flags_limit_days


def test_can_not_update_project_organisation(
admin_client: APIClient,
project: Project,
organisation: Organisation,
organisation_two: Organisation,
) -> None:
# Given
new_name = "New project name"

data = {
"name": new_name,
"organisation": organisation_two.id,
}
url = reverse("api-v1:projects:project-detail", args=[project.id])

# When
response = admin_client.put(url, data=data)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json()["name"] == new_name
assert response.json()["organisation"] == organisation.id


@pytest.mark.parametrize(
"client",
[(lazy_fixture("admin_master_api_key_client")), (lazy_fixture("admin_client"))],
Expand Down Expand Up @@ -733,6 +757,9 @@ def test_update_project(client, project, mocker, settings, organisation):
"organisation": organisation.id,
"only_allow_lower_case_feature_names": False,
"feature_name_regex": feature_name_regex,
# read only fields should not be updated
"enable_dynamo_db": not project.enable_dynamo_db,
"edge_v2_migration_status": project.edge_v2_migration_status + "random-string",
}

# When
Expand All @@ -742,6 +769,10 @@ def test_update_project(client, project, mocker, settings, organisation):
assert response.status_code == status.HTTP_200_OK
assert response.json()["only_allow_lower_case_feature_names"] is False
assert response.json()["feature_name_regex"] == feature_name_regex
assert response.json()["enable_dynamo_db"] == project.enable_dynamo_db
assert (
response.json()["edge_v2_migration_status"] == project.edge_v2_migration_status
)


@pytest.mark.parametrize(
Expand Down
Loading