Skip to content

Commit

Permalink
fix: Force environments to be different names (#5058)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachaysan authored Jan 31, 2025
1 parent 9ea58fa commit 22223da
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/environments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ class CreateUpdateEnvironmentSerializer(
invalid_plans = ("free",)
field_names = ("minimum_change_request_approvals",)

class Meta(EnvironmentSerializerWithMetadata.Meta):
validators = [
serializers.UniqueTogetherValidator(
queryset=EnvironmentSerializerWithMetadata.Meta.model.objects.all(),
fields=("name", "project"),
message="An environment with this name already exists.",
)
]

def get_subscription(self) -> typing.Optional[Subscription]:
view = self.context["view"]

Expand Down
27 changes: 27 additions & 0 deletions api/tests/unit/environments/test_unit_environments_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,33 @@ def test_should_create_environments(
).exists()


def test_environment_matches_existing_environment_name(
project: Project,
admin_client: APIClient,
) -> None:
# Given
url = reverse("api-v1:environments:environment-list")
description = "This is the description"
name = "Test environment"
data = {
"name": name,
"project": project.id,
"description": description,
}
Environment.objects.create(name=name, description=description, project=project)

# When
response = admin_client.post(
url, data=json.dumps(data), content_type="application/json"
)

# Then
assert response.status_code == 400
assert response.json() == {
"non_field_errors": ["An environment with this name already exists."]
}


def test_create_environment_without_required_metadata_returns_400(
project,
admin_client_new,
Expand Down

0 comments on commit 22223da

Please sign in to comment.