Skip to content

Commit 15ebefd

Browse files
authored
Enforce readonly name field for Test_Type instances and add dynamic serializer selection (#14090)
* Enforce readonly name field for existing Test_Type instances in form * Add TestTypeCreateSerializer and enforce readonly name field in TestTypeSerializer * Add dynamic serializer selection in TestTypesViewSet for create action * Update test payload to set 'active' field instead of 'name' * Update TestTypeTest payload to use 'name' and modify update_fields to 'active' * Add test to verify 'name' field is read-only in TestType
1 parent e507578 commit 15ebefd

4 files changed

Lines changed: 34 additions & 2 deletions

File tree

dojo/api_v2/serializers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,8 +1472,15 @@ class Meta:
14721472
exclude = ("inherited_tags",)
14731473

14741474

1475+
class TestTypeCreateSerializer(serializers.ModelSerializer):
1476+
1477+
class Meta:
1478+
model = Test_Type
1479+
exclude = ("dynamically_generated",)
1480+
1481+
14751482
class TestTypeSerializer(serializers.ModelSerializer):
1476-
tags = TagListSerializerField(required=False)
1483+
name = serializers.ReadOnlyField()
14771484

14781485
class Meta:
14791486
model = Test_Type

dojo/api_v2/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,6 +2263,11 @@ class TestTypesViewSet(
22632263
def get_queryset(self):
22642264
return Test_Type.objects.all().order_by("id")
22652265

2266+
def get_serializer_class(self):
2267+
if self.action == "create":
2268+
return serializers.TestTypeCreateSerializer
2269+
return serializers.TestTypeSerializer
2270+
22662271

22672272
# @extend_schema_view(**schema_with_prefetch())
22682273
# Nested models with prefetch make the response schema too long for Swagger UI

dojo/forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,17 @@ class Meta:
324324
model = Test_Type
325325
exclude = ["dynamically_generated"]
326326

327+
def __init__(self, *args, **kwargs):
328+
super().__init__(*args, **kwargs)
329+
330+
if self.instance.pk:
331+
self.fields["name"].widget.attrs["readonly"] = True
332+
333+
def clean_name(self):
334+
if self.instance.pk:
335+
return self.instance.name
336+
return self.cleaned_data["name"]
337+
327338

328339
class Development_EnvironmentForm(forms.ModelForm):
329340
class Meta:

unittests/test_rest_framework.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3452,11 +3452,20 @@ def __init__(self, *args, **kwargs):
34523452
self.payload = {
34533453
"name": "Test_1",
34543454
}
3455-
self.update_fields = {"name": "Test_2"}
3455+
self.update_fields = {"active": False}
34563456
self.test_type = TestType.CONFIGURATION_PERMISSIONS
34573457
self.deleted_objects = 1
34583458
BaseClass.RESTEndpointTest.__init__(self, *args, **kwargs)
34593459

3460+
def test_name_read_only(self):
3461+
current_objects = self.client.get(self.url, format="json").data
3462+
relative_url = self.url + "{}/".format(current_objects["results"][-1]["id"])
3463+
payload = {"name": "New name"}
3464+
response = self.client.patch(relative_url, payload, format="json")
3465+
self.assertEqual(200, response.status_code, response.content[:1000])
3466+
# See that the request was politley ignored and that name did not change
3467+
self.assertEqual(current_objects["results"][-1]["name"], response.data["name"])
3468+
34603469

34613470
class ConfigurationPermissionTest(BaseClass.BaseClassTest):
34623471
fixtures = ["dojo_testdata.json"]

0 commit comments

Comments
 (0)