From b36676a2928b491d47c63f79a7b5b2c5d0808b65 Mon Sep 17 00:00:00 2001 From: olamidepeterojo Date: Sat, 28 Dec 2024 01:28:16 +0100 Subject: [PATCH] stop application edit after creation Signed-off-by: olamidepeterojo --- .../experiments/api/v5/serializers.py | 16 ++++++++++++ .../test_nimbus_experiment_serializer.py | 26 ++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/experimenter/experimenter/experiments/api/v5/serializers.py b/experimenter/experimenter/experiments/api/v5/serializers.py index 9f1ca16c51..62fe9f7bc5 100644 --- a/experimenter/experimenter/experiments/api/v5/serializers.py +++ b/experimenter/experimenter/experiments/api/v5/serializers.py @@ -234,6 +234,11 @@ def validate_name(self, value): def validate(self, data): data = super().validate(data) + + if self.instance and "application" in data: + raise serializers.ValidationError( + {"application": "Application cannot be updated after creation."} + ) feature_values = data.get("feature_values") @@ -894,6 +899,12 @@ def validate_status_next(self, value): return value def validate(self, data): + + if self.instance and "application" in data and self.instance.application != data["application"]: + raise serializers.ValidationError( + {"application": "Application cannot be updated after creation."} + ) + data = super().validate(data) non_is_archived_fields = set(data.keys()) - set( @@ -955,6 +966,11 @@ def validate(self, data): return data def update(self, experiment, validated_data): + if "application" in validated_data and experiment.application != validated_data["application"]: + raise serializers.ValidationError( + {"application": "Application cannot be updated after creation."} + ) + if ( experiment.is_rollout and validated_data.get("population_percent") != experiment.population_percent diff --git a/experimenter/experimenter/experiments/tests/api/v5/test_serializers/test_nimbus_experiment_serializer.py b/experimenter/experimenter/experiments/tests/api/v5/test_serializers/test_nimbus_experiment_serializer.py index ccc769cd18..6d22d2524e 100644 --- a/experimenter/experimenter/experiments/tests/api/v5/test_serializers/test_nimbus_experiment_serializer.py +++ b/experimenter/experimenter/experiments/tests/api/v5/test_serializers/test_nimbus_experiment_serializer.py @@ -299,7 +299,7 @@ def test_saves_existing_experiment_with_changelog(self): ) self.assertEqual(experiment.changes.count(), 1) - data = { + invalid_data = { "application": NimbusExperiment.Application.DESKTOP, "hypothesis": "New Hypothesis", "name": "New Name", @@ -310,11 +310,31 @@ def test_saves_existing_experiment_with_changelog(self): } serializer = NimbusExperimentSerializer( - experiment, data=data, context={"user": self.user} + experiment, data=invalid_data, context={"user": self.user} ) - self.assertTrue(serializer.is_valid(), serializer.errors) + self.assertFalse(serializer.is_valid()) + self.assertIn("application", serializer.errors) + self.assertEqual( + serializer.errors["application"], + ["Application cannot be updated after creation."], + ) + valid_data = { + "hypothesis": "New Hypothesis", + "name": "New Name", + "public_description": "New public description", + "changelog_message": "test changelog message", + "feature_configs": [feature_config.id], + "prevent_pref_conflicts": True, + } + + serializer = NimbusExperimentSerializer( + experiment, data=valid_data, context={"user": self.user}, partial=True + ) + + self.assertTrue(serializer.is_valid(), serializer.errors) + experiment = serializer.save() self.assertEqual(experiment.changes.count(), 2) self.assertEqual(experiment.application, NimbusExperiment.Application.DESKTOP)