Browse Source

fix(api): DRF-ify type validation error message

Peter Thomassen 6 năm trước cách đây
mục cha
commit
80e9db2704
2 tập tin đã thay đổi với 9 bổ sung4 xóa
  1. 5 1
      api/desecapi/serializers.py
  2. 4 3
      api/desecapi/tests/test_rrsets_bulk.py

+ 5 - 1
api/desecapi/serializers.py

@@ -271,7 +271,11 @@ class RRsetSerializer(ConditionalExistenceModelSerializer):
 
 
 class RRsetListSerializer(ListSerializer):
-    default_error_messages = {'not_a_list': 'Invalid input, expected a list of RRsets.'}
+    default_error_messages = {
+        **serializers.Serializer.default_error_messages,
+        **ListSerializer.default_error_messages,
+        **{'not_a_list': 'Expected a list of items but got {input_type}.'},
+    }
 
     @staticmethod
     def _key(data_item):

+ 4 - 3
api/desecapi/tests/test_rrsets_bulk.py

@@ -166,7 +166,7 @@ class AuthenticatedRRSetBulkTestCase(AuthenticatedRRSetBaseTestCase):
 
     def test_bulk_patch_does_not_accept_single_objects(self):
         response = self.client.bulk_patch_rr_sets(domain_name=self.my_empty_domain.name, payload=self.data[0])
-        self.assertContains(response, 'expected a list of RRsets.', status_code=status.HTTP_400_BAD_REQUEST)
+        self.assertContains(response, 'Expected a list of items but got dict.', status_code=status.HTTP_400_BAD_REQUEST)
 
     def test_bulk_patch_full_on_empty_domain(self):
         # Full patch always works
@@ -308,7 +308,7 @@ class AuthenticatedRRSetBulkTestCase(AuthenticatedRRSetBaseTestCase):
 
     def test_bulk_put_does_not_accept_single_objects(self):
         response = self.client.bulk_put_rr_sets(domain_name=self.my_empty_domain.name, payload=self.data[0])
-        self.assertContains(response, 'expected a list of RRsets.', status_code=status.HTTP_400_BAD_REQUEST)
+        self.assertContains(response, 'Expected a list of items but got dict.', status_code=status.HTTP_400_BAD_REQUEST)
 
     def test_bulk_put_full(self):
         # Full PUT always works
@@ -361,4 +361,5 @@ class AuthenticatedRRSetBulkTestCase(AuthenticatedRRSetBaseTestCase):
     def test_bulk_patch_or_post_failure_with_single_rrset(self):
         for method in [self.client.bulk_patch_rr_sets, self.client.bulk_put_rr_sets]:
             response = method(domain_name=self.my_empty_domain.name, payload=self.data[0])
-            self.assertContains(response, 'Invalid input, expected a list of RRsets.', status_code=400)
+            self.assertContains(response, 'Expected a list of items but got dict.',
+                                status_code=status.HTTP_400_BAD_REQUEST)