mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-20 14:29:51 -04:00
Merge pull request #2294 from alphagov/no-canonical-magic
Validate non canonical domains
This commit is contained in:
@@ -33,6 +33,7 @@ from wtforms.validators import URL, DataRequired, Length, Optional, Regexp
|
|||||||
|
|
||||||
from app.main.validators import (
|
from app.main.validators import (
|
||||||
Blacklist,
|
Blacklist,
|
||||||
|
CanonicalGovernmentDomain,
|
||||||
CsvFileValidator,
|
CsvFileValidator,
|
||||||
DoesNotStartWithDoubleZero,
|
DoesNotStartWithDoubleZero,
|
||||||
KnownGovernmentDomain,
|
KnownGovernmentDomain,
|
||||||
@@ -721,7 +722,10 @@ class ServicePreviewBranding(StripWhitespaceForm):
|
|||||||
|
|
||||||
|
|
||||||
class GovernmentDomainField(StringField):
|
class GovernmentDomainField(StringField):
|
||||||
validators = [KnownGovernmentDomain()]
|
validators = [
|
||||||
|
KnownGovernmentDomain(),
|
||||||
|
CanonicalGovernmentDomain(),
|
||||||
|
]
|
||||||
|
|
||||||
def post_validate(self, form, validation_stopped):
|
def post_validate(self, form, validation_stopped):
|
||||||
if self.data and not self.errors:
|
if self.data and not self.errors:
|
||||||
|
|||||||
@@ -111,3 +111,25 @@ class KnownGovernmentDomain:
|
|||||||
def __call__(self, form, field):
|
def __call__(self, form, field):
|
||||||
if field.data and AgreementInfo(field.data).owner is None:
|
if field.data and AgreementInfo(field.data).owner is None:
|
||||||
raise ValidationError(self.message)
|
raise ValidationError(self.message)
|
||||||
|
|
||||||
|
|
||||||
|
class CanonicalGovernmentDomain:
|
||||||
|
|
||||||
|
message = 'Not {} domain (use {} if appropriate)'
|
||||||
|
|
||||||
|
def __call__(self, form, field):
|
||||||
|
|
||||||
|
if not field.data:
|
||||||
|
return
|
||||||
|
|
||||||
|
domain = AgreementInfo(field.data)
|
||||||
|
|
||||||
|
if not domain.is_canonical:
|
||||||
|
raise ValidationError(
|
||||||
|
self.message.format('a canonical', domain.canonical_domain)
|
||||||
|
)
|
||||||
|
|
||||||
|
if field.data != domain.canonical_domain:
|
||||||
|
raise ValidationError(
|
||||||
|
self.message.format('an organisation-level', domain.canonical_domain)
|
||||||
|
)
|
||||||
|
|||||||
@@ -155,18 +155,31 @@ def test_cant_create_new_email_branding_with_unknown_domain(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('posted_domain, persisted_domain', [
|
@pytest.mark.parametrize('posted_domain, expected_error', [
|
||||||
('voa.gsi.gov.uk', 'voa.gov.uk'),
|
(
|
||||||
('voa.gov.uk', 'voa.gov.uk'),
|
'voa.gsi.gov.uk',
|
||||||
('hmcts.net', 'hmcts.gov.uk'),
|
'Not a canonical domain (use voa.gov.uk if appropriate)',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'hmcts.net',
|
||||||
|
'Not a canonical domain (use hmcts.gov.uk if appropriate)',
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'southend.essex.gov.uk',
|
||||||
|
'Not an organisation-level domain (use essex.gov.uk if appropriate)',
|
||||||
|
),
|
||||||
|
pytest.mark.xfail(
|
||||||
|
('voa.gov.uk', ''),
|
||||||
|
raises=AssertionError
|
||||||
|
),
|
||||||
])
|
])
|
||||||
def test_persists_canonical_domain_when_adding_email_branding(
|
def test_rejects_non_canonical_domain_when_adding_email_branding(
|
||||||
client_request,
|
client_request,
|
||||||
mocker,
|
mocker,
|
||||||
fake_uuid,
|
fake_uuid,
|
||||||
mock_create_email_branding,
|
mock_create_email_branding,
|
||||||
posted_domain,
|
posted_domain,
|
||||||
persisted_domain,
|
expected_error,
|
||||||
):
|
):
|
||||||
mocker.patch('app.main.views.email_branding.persist_logo')
|
mocker.patch('app.main.views.email_branding.persist_logo')
|
||||||
mocker.patch('app.main.views.email_branding.delete_temp_files_created_by')
|
mocker.patch('app.main.views.email_branding.delete_temp_files_created_by')
|
||||||
@@ -179,20 +192,15 @@ def test_persists_canonical_domain_when_adding_email_branding(
|
|||||||
'brand_type': 'org',
|
'brand_type': 'org',
|
||||||
}
|
}
|
||||||
client_request.login(platform_admin_user(fake_uuid))
|
client_request.login(platform_admin_user(fake_uuid))
|
||||||
client_request.post(
|
page = client_request.post(
|
||||||
'.create_email_branding',
|
'.create_email_branding',
|
||||||
content_type='multipart/form-data',
|
content_type='multipart/form-data',
|
||||||
_data=data,
|
_data=data,
|
||||||
|
_expected_status=200,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert mock_create_email_branding.call_args == call(
|
assert page.select_one('.error-message').text.strip() == expected_error
|
||||||
logo=data['logo'],
|
assert mock_create_email_branding.called is False
|
||||||
name=data['name'],
|
|
||||||
text=data['text'],
|
|
||||||
colour=data['colour'],
|
|
||||||
domain=persisted_domain,
|
|
||||||
brand_type=data['brand_type']
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_new_email_branding_when_branding_saved(
|
def test_create_new_email_branding_when_branding_saved(
|
||||||
|
|||||||
Reference in New Issue
Block a user