Browse Source

fix(api): check for null record content, fixes #116

Peter Thomassen 6 years ago
parent
commit
affa811749
2 changed files with 28 additions and 6 deletions
  1. 4 6
      api/desecapi/serializers.py
  2. 24 0
      test/e2e/spec/api_spec.js

+ 4 - 6
api/desecapi/serializers.py

@@ -25,13 +25,10 @@ class RRSerializer(serializers.ModelSerializer):
         model = RR
         fields = ('content',)
 
-    def to_representation(self, instance):
-        return instance.content
-
     def to_internal_value(self, data):
         if not isinstance(data, dict):
             data = {'content': data}
-        return self.Meta.model(**data)
+        return super().to_internal_value(data)
 
 
 class RRsetBulkListSerializer(BulkListSerializer):
@@ -139,7 +136,7 @@ class RRsetSerializer(BulkSerializerMixin, serializers.ModelSerializer):
             if records is None:
                 rrsets[rrset] = None
             else:
-                rr_data = [{'content': x.content, 'rrset': rrset} for x in records]
+                rr_data = [{'content': x.content} for x in records]
 
                 # Use RRSerializer to validate records inputs
                 allow_empty = (method in ('PATCH', 'PUT'))
@@ -154,7 +151,8 @@ class RRsetSerializer(BulkSerializerMixin, serializers.ModelSerializer):
                     continue
 
                 # Blessings have been given, so add RRset to the to-write dict
-                rrsets[rrset] = [rr for rr in rr_serializer.validated_data]
+                rrsets[rrset] = [RR(rrset=rrset, **rr_validated_data)
+                                 for rr_validated_data in rr_serializer.validated_data]
 
             errors.append({})
 

+ 24 - 0
test/e2e/spec/api_spec.js

@@ -538,6 +538,14 @@ describe("API", function () {
                         );
                         return expect(response).to.have.status(422);
                     });
+
+                    it("gives the right response for records contents being null", function () {
+                        var response = chakram.post(
+                            '/domains/' + domain + '/rrsets/',
+                            [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4', null]}]
+                        );
+                        return expect(response).to.have.status(400);
+                    });
                 });
 
             });
@@ -759,6 +767,14 @@ describe("API", function () {
                         );
                         return expect(response).to.have.status(422);
                     });
+
+                    it("gives the right response for records contents being null", function () {
+                        var response = chakram.put(
+                            '/domains/' + domain + '/rrsets/',
+                            [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4', null]}]
+                        );
+                        return expect(response).to.have.status(400);
+                    });
                 });
 
             });
@@ -1032,6 +1048,14 @@ describe("API", function () {
                         );
                         return expect(response).to.have.status(422);
                     });
+
+                    it("gives the right response for records contents being null", function () {
+                        var response = chakram.patch(
+                            '/domains/' + domain + '/rrsets/',
+                            [{'subname': 'a.2', 'ttl': 50, 'type': 'MX', 'records': ['1.2.3.4', null]}]
+                        );
+                        return expect(response).to.have.status(400);
+                    });
                 });
 
             });