Skip to content

Commit 22223da

Browse files
authored
fix: Force environments to be different names (#5058)
1 parent 9ea58fa commit 22223da

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

api/environments/serializers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ class CreateUpdateEnvironmentSerializer(
111111
invalid_plans = ("free",)
112112
field_names = ("minimum_change_request_approvals",)
113113

114+
class Meta(EnvironmentSerializerWithMetadata.Meta):
115+
validators = [
116+
serializers.UniqueTogetherValidator(
117+
queryset=EnvironmentSerializerWithMetadata.Meta.model.objects.all(),
118+
fields=("name", "project"),
119+
message="An environment with this name already exists.",
120+
)
121+
]
122+
114123
def get_subscription(self) -> typing.Optional[Subscription]:
115124
view = self.context["view"]
116125

api/tests/unit/environments/test_unit_environments_views.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,33 @@ def test_should_create_environments(
588588
).exists()
589589

590590

591+
def test_environment_matches_existing_environment_name(
592+
project: Project,
593+
admin_client: APIClient,
594+
) -> None:
595+
# Given
596+
url = reverse("api-v1:environments:environment-list")
597+
description = "This is the description"
598+
name = "Test environment"
599+
data = {
600+
"name": name,
601+
"project": project.id,
602+
"description": description,
603+
}
604+
Environment.objects.create(name=name, description=description, project=project)
605+
606+
# When
607+
response = admin_client.post(
608+
url, data=json.dumps(data), content_type="application/json"
609+
)
610+
611+
# Then
612+
assert response.status_code == 400
613+
assert response.json() == {
614+
"non_field_errors": ["An environment with this name already exists."]
615+
}
616+
617+
591618
def test_create_environment_without_required_metadata_returns_400(
592619
project,
593620
admin_client_new,

0 commit comments

Comments
 (0)