def test_external_domain_subdomain_is_reserved(self, domain, error_message, mock_gandi_api): """ Validate that the user cannot register a reserved subdomain. """ with self.assertRaises(ValidationError) as exc: validate_available_subdomain(domain) self.assertEqual(exc.exception.message, error_message) self.assertEqual(exc.exception.code, 'reserved') self.assertFalse(mock_gandi_api.filter_dns_records.called)
def validate_subdomain(self, value): """ Prevent users from registering with a subdomain which is in use. """ is_new_instance = self.instance is None is_changed = not is_new_instance and self.instance.subdomain != self.initial_data.get( "subdomain") if is_new_instance or is_changed: validate_available_subdomain(value) return value
def test_validate_available_subdomain(self, mock_gandi_api): """ Validate that a correct subdomain does not cause a validation error. """ subdomain = 'newsubdomain' internal_domains = ['some', 'internal', 'domain'] mock_gandi_api.filter_dns_records.return_value = [{ 'name': f'{domain}.example.com' } for domain in internal_domains] validate_available_subdomain(subdomain) mock_gandi_api.filter_dns_records.assert_called_once_with( settings.DEFAULT_INSTANCE_BASE_DOMAIN)
def test_subdomain_is_taken_by_instance(self, mock_gandi_api): """ Validate that a taken subdomain raises validation error. """ subdomain = "mysubdomain" mock_gandi_api.filter_dns_records.return_value = [] instance_factory(sub_domain=subdomain) with self.assertRaises(ValidationError) as exc: validate_available_subdomain(subdomain) self.assertEqual(exc.exception.message, 'This domain is already taken.') self.assertEqual(exc.exception.code, 'unique') self.assertFalse(mock_gandi_api.filter_dns_records.called)
def test_subdomain_cannot_be_validated(self, mock_gandi_api): """ Validate validation error raised when we cannot check the domain's DNS records. It is better to raise an error to the user then mess up our domains. """ subdomain = 'newsubdomain' mock_gandi_api.filter_dns_records.side_effect = ValueError() with self.assertRaises(ValidationError) as exc: validate_available_subdomain(subdomain) self.assertEqual(exc.exception.message, 'The domain cannot be validated.') self.assertEqual(exc.exception.code, 'cannot_validate') mock_gandi_api.filter_dns_records.assert_called_once_with( settings.DEFAULT_INSTANCE_BASE_DOMAIN)
def test_subdomain_is_taken(self, subdomain, registered_subdomain, mock_gandi_api): """ Validate that a taken subdomain raises validation error. """ mock_gandi_api.filter_dns_records.return_value = [{ 'name': f'{registered_subdomain}.example.com' }] with self.assertRaises(ValidationError) as exc: validate_available_subdomain(subdomain) self.assertEqual(exc.exception.message, 'This domain is already taken.') self.assertEqual(exc.exception.code, 'unique') mock_gandi_api.filter_dns_records.assert_called_once_with( settings.DEFAULT_INSTANCE_BASE_DOMAIN)