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: Force environments to be different names #5058

Merged
merged 7 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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/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
Loading