From 2e7ae91029d80f81a89fff9685902d75d6b9094c Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Mon, 3 Sep 2018 11:14:37 +0100 Subject: [PATCH] Persist canonical domain in email branding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When saving an email branding it’s possible we might not enter the canonical domain for an organisation into the domain field. Because we’re going to use the canonical domain to look up the brandings this will cause a mismatch. Rather than validate this and show an error, let’s just save the correct thing instead. From the user’s perspective this means everything will just work (ie a user with a given email address will automatically get the right branding for their organisation). --- app/main/forms.py | 12 +++++- tests/app/main/views/test_email_branding.py | 43 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index ba7a665b7..536a9b672 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -43,7 +43,7 @@ from app.main.validators import ( ValidGovEmail, ) from app.notify_client.models import permissions, roles -from app.utils import guess_name_from_email_address +from app.utils import AgreementInfo, guess_name_from_email_address def get_time_value_and_label(future_time): @@ -720,10 +720,18 @@ class ServicePreviewBranding(StripWhitespaceForm): branding_style = HiddenField('branding_style') +class GovernmentDomainField(StringField): + validators = [KnownGovernmentDomain()] + + def post_validate(self, form, validation_stopped): + if self.data and not self.errors: + self.data = AgreementInfo(self.data).canonical_domain + + class ServiceUpdateEmailBranding(StripWhitespaceForm): name = StringField('Name of brand') text = StringField('Text') - domain = StringField('Domain', validators=[KnownGovernmentDomain()]) + domain = GovernmentDomainField('Domain') colour = StringField( 'Colour', validators=[ diff --git a/tests/app/main/views/test_email_branding.py b/tests/app/main/views/test_email_branding.py index 3e41924ee..797f5a8e5 100644 --- a/tests/app/main/views/test_email_branding.py +++ b/tests/app/main/views/test_email_branding.py @@ -150,6 +150,49 @@ def test_cant_create_new_email_branding_with_unknown_domain( assert page.select_one('.error-message').text.strip() == ( 'Not a known government domain (you might need to update domains.yml)' ) + assert page.select_one('input[name=domain]')['value'] == ( + 'example.gov.uk' + ) + + +@pytest.mark.parametrize('posted_domain, persisted_domain', [ + ('voa.gsi.gov.uk', 'voa.gov.uk'), + ('voa.gov.uk', 'voa.gov.uk'), + ('hmcts.net', 'hmcts.gov.uk'), +]) +def test_persists_canonical_domain_when_adding_email_branding( + client_request, + mocker, + fake_uuid, + mock_create_email_branding, + posted_domain, + persisted_domain, +): + mocker.patch('app.main.views.email_branding.persist_logo') + mocker.patch('app.main.views.email_branding.delete_temp_files_created_by') + data = { + 'logo': None, + 'colour': '#ff0000', + 'text': 'new text', + 'name': 'new name', + 'domain': posted_domain, + 'brand_type': 'org', + } + client_request.login(platform_admin_user(fake_uuid)) + client_request.post( + '.create_email_branding', + content_type='multipart/form-data', + _data=data, + ) + + assert mock_create_email_branding.call_args == call( + logo=data['logo'], + 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(