Procházet zdrojové kódy

fix(api): set correct minimum TTL on domains created via auth action

Peter Thomassen před 5 roky
rodič
revize
aa4ec2f52b

+ 5 - 0
api/desecapi/serializers.py

@@ -481,6 +481,11 @@ class DomainSerializer(serializers.ModelSerializer):
         if not models.Domain.is_registrable(domain_name, user):
             raise serializers.ValidationError('This domain name is unavailable.', code='name_unavailable')
 
+    def create(self, validated_data):
+        if 'minimum_ttl' not in validated_data and models.Domain(name=validated_data['name']).is_locally_registrable:
+            validated_data.update(minimum_ttl=60)
+        return super().create(validated_data)
+
 
 class DonationSerializer(serializers.ModelSerializer):
 

+ 2 - 2
api/desecapi/tests/test_user_management.py

@@ -374,7 +374,7 @@ class UserManagementTestCase(DesecTestCase, PublicSuffixMockMixin):
         return email, password
 
     def _test_registration_with_domain(self, email=None, password=None, domain=None, expect_failure_response=None,
-                                       tampered_domain=None):
+                                       tampered_domain=None, local=False):
         domain = domain or self.random_domain_name()
 
         email, password, response = self.register_user(email, password, domain=domain)
@@ -481,7 +481,7 @@ class NoUserAccountTestCase(UserLifeCycleTestCase):
             self._test_registration_with_domain(domain='co.uk', expect_failure_response=self.assertRegistrationFailureDomainUnavailableResponse)
         local_public_suffix = random.sample(self.AUTO_DELEGATION_DOMAINS, 1)[0]
         with self.get_psl_context_manager(local_public_suffix):
-            self._test_registration_with_domain(domain=self.random_domain_name(suffix=local_public_suffix))
+            self._test_registration_with_domain(domain=self.random_domain_name(suffix=local_public_suffix), local=True)
 
     def test_registration_with_tampered_domain(self):
         PublicSuffixMockMixin.setUpMockPatch(self)

+ 1 - 4
api/desecapi/views.py

@@ -72,11 +72,8 @@ class DomainList(generics.ListCreateAPIView):
         return models.Domain.objects.filter(owner=self.request.user.pk)
 
     def perform_create(self, serializer):
-        domain_kwargs = {'owner': self.request.user}
-        if models.Domain(name=serializer.validated_data['name']).is_locally_registrable:
-            domain_kwargs['minimum_ttl'] = 60
         with PDNSChangeTracker():
-            domain = serializer.save(**domain_kwargs)
+            domain = serializer.save(owner=self.request.user)
 
         # TODO this line raises if the local public suffix is not in our database!
         PDNSChangeTracker.track(lambda: self.auto_delegate(domain))