File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -111,6 +111,15 @@ class CreateUpdateEnvironmentSerializer(
111
111
invalid_plans = ("free" ,)
112
112
field_names = ("minimum_change_request_approvals" ,)
113
113
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
+
114
123
def get_subscription (self ) -> typing .Optional [Subscription ]:
115
124
view = self .context ["view" ]
116
125
Original file line number Diff line number Diff line change @@ -588,6 +588,33 @@ def test_should_create_environments(
588
588
).exists ()
589
589
590
590
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
+
591
618
def test_create_environment_without_required_metadata_returns_400 (
592
619
project ,
593
620
admin_client_new ,
You can’t perform that action at this time.
0 commit comments