From f7f202b670b25c6488dfc82d12e0fc0d2fad7d7a Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 23 Aug 2018 17:44:34 +0100 Subject: [PATCH] Fix the EmailBranding --- app/main/forms.py | 19 +++----- app/main/views/email_branding.py | 2 - app/main/views/index.py | 9 +--- tests/app/main/views/test_email_branding.py | 45 +++++++------------ tests/app/main/views/test_email_preview.py | 29 +----------- .../test_email_branding_client.py | 10 ++--- tests/conftest.py | 36 ++++++++------- 7 files changed, 51 insertions(+), 99 deletions(-) diff --git a/app/main/forms.py b/app/main/forms.py index da4d2ee2b..fab2f7771 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -728,17 +728,6 @@ class ServiceSelectEmailBranding(StripWhitespaceForm): class ServiceUpdateEmailBranding(StripWhitespaceForm): - def __init__(self, *args, **kwargs): - - super().__init__(*args, **kwargs) - - self.brand_type.choices = filter(None, [ - ('govuk', 'GOV.UK only'), - ('both', 'GOV.UK and branding'), - ('org', 'Branding only'), - ('org_banner', 'Branding banner'), - ]) - name = StringField('Name of brand') text = StringField('Text') domain = StringField('Domain') @@ -750,7 +739,13 @@ class ServiceUpdateEmailBranding(StripWhitespaceForm): ) file = FileField_wtf('Upload a PNG logo', validators=[FileAllowed(['png'], 'PNG Images only!')]) brand_type = RadioField( - "Brand type" + "Brand type", + choices=[ + ('govuk', 'GOV.UK only'), + ('both', 'GOV.UK and branding'), + ('org', 'Branding only'), + ('org_banner', 'Branding banner'), + ] ) diff --git a/app/main/views/email_branding.py b/app/main/views/email_branding.py index 2c3bcd243..5a4a5a963 100644 --- a/app/main/views/email_branding.py +++ b/app/main/views/email_branding.py @@ -30,7 +30,6 @@ def email_branding(): form = ServiceSelectEmailBranding() email_brandings = get_branding_as_value_and_label(brandings) form.email_branding.choices = email_brandings + [('None', 'Create a new email branding')] - if form.validate_on_submit(): if form.email_branding.data != 'None': return redirect(url_for('.update_email_branding', branding_id=form.email_branding.data)) @@ -107,7 +106,6 @@ def update_email_branding(branding_id, logo=None): @user_is_platform_admin def create_email_branding(logo=None): form = ServiceUpdateEmailBranding() - if form.validate_on_submit(): if form.file.data: upload_filename = upload_logo( diff --git a/app/main/views/index.py b/app/main/views/index.py index 3b4232825..ed07c2a15 100644 --- a/app/main/views/index.py +++ b/app/main/views/index.py @@ -78,13 +78,6 @@ def design_content(): return render_template('views/design-patterns-content-guidance.html') -def _set_colour(branding_style, email_branding): - if branding_style in ['both', 'org']: - return email_branding['single_id_colour'] or email_branding['colour'] - elif branding_style == 'org_banner': - return email_branding['banner_colour'] or email_branding['colour'] - - @main.route('/_email') def email_template(): branding_type = request.args.get('branding_type', 'govuk') @@ -98,7 +91,7 @@ def email_template(): brand_banner = False else: email_branding = email_branding_client.get_email_branding(branding_style)['email_branding'] - colour = _set_colour(branding_type, email_branding) + colour = email_branding['colour'] brand_name = email_branding['text'] brand_colour = colour brand_logo = 'https://{}/{}'.format(get_cdn_domain(), email_branding['logo']) diff --git a/tests/app/main/views/test_email_branding.py b/tests/app/main/views/test_email_branding.py index 9bef00beb..b282ccefa 100644 --- a/tests/app/main/views/test_email_branding.py +++ b/tests/app/main/views/test_email_branding.py @@ -51,8 +51,6 @@ def test_edit_email_branding_shows_the_correct_branding_info( assert page.select_one('#name').attrs.get('value') == 'Organisation name' assert page.select_one('#text').attrs.get('value') == 'Organisation text' assert page.select_one('#colour').attrs.get('value') == '#f00' - assert page.select_one('#banner_colour').attrs.get('value') == '#f11' - assert page.select_one('#single_id_colour').attrs.get('value') == '#f22' assert page.select_one('#domain').attrs.get('value') == 'sample.com' @@ -72,8 +70,6 @@ def test_create_email_branding_does_not_show_any_branding_info( assert page.select_one('#name').attrs.get('value') == '' assert page.select_one('#text').attrs.get('value') == '' assert page.select_one('#colour').attrs.get('value') == '' - assert page.select_one('#banner_colour').attrs.get('value') == '' - assert page.select_one('#single_id_colour').attrs.get('value') == '' assert page.select_one('#domain').attrs.get('value') == '' @@ -89,8 +85,7 @@ def test_create_new_email_branding_without_logo( 'text': 'new text', 'name': 'new name', 'domain': 'sample.com', - 'banner_colour': '#FFFF00', - 'single_id_colour': '#00FF00', + 'brand_type': 'govuk' } mock_persist = mocker.patch('app.main.views.email_branding.persist_logo') @@ -108,9 +103,8 @@ def test_create_new_email_branding_without_logo( name=data['name'], text=data['text'], colour=data['colour'], - banner_colour=data['banner_colour'], - single_id_colour=data['single_id_colour'], - domain=data['domain'] + domain=data['domain'], + brand_type=data['brand_type'] ) assert mock_persist.call_args_list == [] @@ -130,8 +124,7 @@ def test_create_new_email_branding_when_branding_saved( 'text': 'new text', 'name': 'new name', 'domain': 'sample.com', - 'banner_colour': '#FFFF00', - 'single_id_colour': '#00FF00', + 'brand_type': 'govuk' } temp_filename = LOGO_LOCATION_STRUCTURE.format( @@ -152,8 +145,7 @@ def test_create_new_email_branding_when_branding_saved( 'text': data['text'], 'cdn_url': 'https://static-logos.cdn.com', 'domain': data['domain'], - 'banner_colour': data['banner_colour'], - 'single_id_colour': data['single_id_colour'], + 'brand_type': data['brand_type'] } ) @@ -163,9 +155,8 @@ def test_create_new_email_branding_when_branding_saved( name=data['name'], text=data['text'], colour=data['colour'], - banner_colour=data['banner_colour'], - single_id_colour=data['single_id_colour'], - domain=data['domain'] + domain=data['domain'], + brand_type=data['brand_type'] ) @@ -207,7 +198,9 @@ def test_deletes_previous_temp_logo_after_uploading_logo( logged_in_platform_admin_client.post( url_for('main.create_email_branding', logo=temp_old_filename, branding_id=fake_uuid), - data={'file': (BytesIO(''.encode('utf-8')), 'test.png')}, + data={'file': (BytesIO(''.encode('utf-8')), 'test.png'), + 'brand_type': 'govuk' + }, content_type='multipart/form-data' ) @@ -231,9 +224,8 @@ def test_update_existing_branding( 'colour': '#0000ff', 'text': 'new text', 'name': 'new name', - 'banner_colour': '#FFFF00', - 'single_id_colour': '#00FF00', 'domain': 'sample.com', + 'brand_type': 'govuk' } temp_filename = LOGO_LOCATION_STRUCTURE.format( @@ -250,8 +242,7 @@ def test_update_existing_branding( content_type='multipart/form-data', data={'colour': data['colour'], 'name': data['name'], 'text': data['text'], 'cdn_url': 'https://static-logos.cdn.com', - 'banner_colour': data['banner_colour'], 'single_id_colour': data['single_id_colour'], - 'domain': data['domain'] + 'domain': data['domain'], 'brand_type': data['brand_type'] } ) @@ -262,9 +253,8 @@ def test_update_existing_branding( name=data['name'], text=data['text'], colour=data['colour'], - banner_colour=data['banner_colour'], - single_id_colour=data['single_id_colour'], domain=data['domain'], + brand_type=data['brand_type'] ) @@ -287,7 +277,8 @@ def test_temp_logo_is_shown_after_uploading_logo( response = logged_in_platform_admin_client.post( url_for('main.create_email_branding'), - data={'file': (BytesIO(''.encode('utf-8')), 'test.png')}, + data={'file': (BytesIO(''.encode('utf-8')), 'test.png'), + 'brand_type': 'govuk'}, content_type='multipart/form-data', follow_redirects=True ) @@ -317,9 +308,9 @@ def test_logo_persisted_when_organisation_saved( resp = logged_in_platform_admin_client.post( url_for('.create_email_branding', logo=temp_filename), + data={'brand_type': 'govuk'}, content_type='multipart/form-data' ) - assert resp.status_code == 302 assert not mocked_upload_logo.called @@ -348,8 +339,7 @@ def test_colour_regex_validation( 'text': 'new text', 'name': 'new name', 'domain': 'sample.com', - 'banner_colour': '#FFFF00', - 'single_id_colour': '#00FF00', + 'brand_type': 'govuk' } mocker.patch('app.main.views.email_branding.delete_temp_files_created_by') @@ -359,5 +349,4 @@ def test_colour_regex_validation( content_type='multipart/form-data', data=data ) - assert response.status_code == expected_status_code diff --git a/tests/app/main/views/test_email_preview.py b/tests/app/main/views/test_email_preview.py index fd14de170..e1f819f52 100644 --- a/tests/app/main/views/test_email_preview.py +++ b/tests/app/main/views/test_email_preview.py @@ -4,8 +4,6 @@ import pytest from bs4 import BeautifulSoup from flask import url_for -from app.main.views.index import _set_colour - @pytest.mark.parametrize( "query_args, result", [ @@ -88,7 +86,7 @@ def test_displays_org_branding_with_banner(client, mock_get_email_branding): assert not page.find("a", attrs={"href": "https://www.gov.uk"}) assert page.find("img", attrs={"src": re.compile("example.png")}) - assert page.select("body > table > tr > td[bgcolor='#f11']") # banner colour is set + assert page.select("body > table > tr > td[bgcolor='#f00']") # banner colour is set assert page.select("body > table table > tr > td > span")[0]\ .get_text().strip() == 'Organisation text' # brand text is set @@ -106,28 +104,5 @@ def test_displays_org_branding_with_banner_without_brand_text( assert not page.find("a", attrs={"href": "https://www.gov.uk"}) assert page.find("img", attrs={"src": re.compile("example.png")}) - assert page.select("body > table > tr > td[bgcolor='#f11']") # banner colour is set + assert page.select("body > table > tr > td[bgcolor='#f00']") # banner colour is set assert not page.select("body > table table > tr > td > span") == 0 # brand text is not set - - -@pytest.mark.parametrize('colour, banner_colour, single_id_colour, branding_type, expected_colour', [ - ('black', 'yellow', 'red', 'org', 'red'), - ('black', 'yellow', None, 'org', 'black'), - ('black', 'yellow', 'red', 'org_banner', 'yellow'), - ('black', None, 'red', 'org_banner', 'black'), - ('black', 'yellow', 'red', 'govuk', None), - ('black', 'yellow', 'red', 'both', 'red'), - ('black', 'yellow', None, 'both', 'black'), -]) -def test_set_colour(colour, banner_colour, single_id_colour, branding_type, expected_colour): - email_branding = { - 'logo': None, - 'colour': colour, - 'text': 'new text', - 'name': 'new name', - 'domain': 'sample.com', - 'banner_colour': banner_colour, - 'single_id_colour': single_id_colour, - } - colour = _set_colour(branding_type, email_branding) - assert colour == expected_colour diff --git a/tests/app/notify_client/test_email_branding_client.py b/tests/app/notify_client/test_email_branding_client.py index a6dbb29f5..61a95683d 100644 --- a/tests/app/notify_client/test_email_branding_client.py +++ b/tests/app/notify_client/test_email_branding_client.py @@ -27,13 +27,12 @@ def test_get_letter_email_branding(mocker): def test_create_email_branding(mocker): org_data = {'logo': 'test.png', 'name': 'test name', 'text': 'test name', 'colour': 'red', - 'banner_colour': 'blue', 'single_id_colour': 'yellow', 'domain': 'sample.com'} + 'domain': 'sample.com', 'brand_type': 'org'} mock_post = mocker.patch('app.notify_client.email_branding_client.EmailBrandingClient.post') EmailBrandingClient().create_email_branding( logo=org_data['logo'], name=org_data['name'], text=org_data['text'], colour=org_data['colour'], - banner_colour=org_data['banner_colour'], single_id_colour=org_data['single_id_colour'], - domain=org_data['domain'] + domain=org_data['domain'], brand_type='org' ) mock_post.assert_called_once_with( @@ -44,13 +43,12 @@ def test_create_email_branding(mocker): def test_update_email_branding(mocker, fake_uuid): org_data = {'logo': 'test.png', 'name': 'test name', 'text': 'test name', 'colour': 'red', - 'banner_colour': 'blue', 'single_id_colour': 'yellow', 'domain': 'sample.com'} + 'domain': 'sample.com', 'brand_type': 'org'} mock_post = mocker.patch('app.notify_client.email_branding_client.EmailBrandingClient.post') EmailBrandingClient().update_email_branding( branding_id=fake_uuid, logo=org_data['logo'], name=org_data['name'], text=org_data['text'], - colour=org_data['colour'], banner_colour=org_data['banner_colour'], - single_id_colour=org_data['single_id_colour'], domain=org_data['domain']) + colour=org_data['colour'], domain=org_data['domain'], brand_type='org') mock_post.assert_called_once_with( url='/email-branding/{}'.format(fake_uuid), diff --git a/tests/conftest.py b/tests/conftest.py index 492c5675d..4ceae0e2d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2441,19 +2441,25 @@ def mock_get_all_email_branding(mocker): def _get_all_email_branding(sort_key=None): if sort_key: return [ - {'id': '1', 'name': 'org 1', 'text': 'org 1', 'colour': 'red', 'logo': 'logo1.png'}, - {'id': '2', 'name': 'org 2', 'text': 'org 2', 'colour': 'orange', 'logo': 'logo2.png'}, - {'id': '3', 'name': 'org 3', 'text': None, 'colour': None, 'logo': 'logo3.png'}, - {'id': '4', 'name': 'org 4', 'text': 'org 4', 'colour': None, 'logo': 'logo4.png'}, - {'id': '5', 'name': 'org 5', 'text': None, 'colour': 'blue', 'logo': 'logo5.png'}, + {'id': '1', 'name': 'org 1', 'text': 'org 1', 'colour': 'red', 'logo': 'logo1.png', + 'brand_type': 'govuk'}, + {'id': '2', 'name': 'org 2', 'text': 'org 2', 'colour': 'orange', 'logo': 'logo2.png', + 'brand_type': 'both'}, + {'id': '3', 'name': 'org 3', 'text': None, 'colour': None, 'logo': 'logo3.png', 'brand_type': 'org'}, + {'id': '4', 'name': 'org 4', 'text': 'org 4', 'colour': None, 'logo': 'logo4.png', + 'brand_type': 'org_banner'}, + {'id': '5', 'name': 'org 5', 'text': None, 'colour': 'blue', 'logo': 'logo5.png', 'brand_type': 'org'}, ] else: return [ - {'id': '1', 'name': 'org 1', 'text': 'org 1', 'colour': 'red', 'logo': 'logo1.png'}, - {'id': '2', 'name': 'org 2', 'text': 'org 2', 'colour': 'orange', 'logo': 'logo2.png'}, - {'id': '3', 'name': 'org 3', 'text': None, 'colour': None, 'logo': 'logo3.png'}, - {'id': '5', 'name': 'org 5', 'text': None, 'colour': 'blue', 'logo': 'logo5.png'}, - {'id': '4', 'name': 'org 4', 'text': 'org 4', 'colour': None, 'logo': 'logo4.png'}, + {'id': '1', 'name': 'org 1', 'text': 'org 1', 'colour': 'red', 'logo': 'logo1.png', + 'brand_type': 'govuk'}, + {'id': '2', 'name': 'org 2', 'text': 'org 2', 'colour': 'orange', 'logo': 'logo2.png', + 'brand_type': 'both'}, + {'id': '3', 'name': 'org 3', 'text': None, 'colour': None, 'logo': 'logo3.png', 'brand_type': 'org'}, + {'id': '5', 'name': 'org 5', 'text': None, 'colour': 'blue', 'logo': 'logo5.png', + 'brand_type': 'org_banner'}, + {'id': '4', 'name': 'org 4', 'text': 'org 4', 'colour': None, 'logo': 'logo4.png', 'brand_type': 'org'}, ] return mocker.patch( @@ -2494,9 +2500,8 @@ def mock_get_email_branding(mocker, fake_uuid): 'text': 'Organisation text', 'id': fake_uuid, 'colour': '#f00', - 'banner_colour': '#f11', - 'single_id_colour': '#f22', 'domain': 'sample.com', + 'brand_type': 'org', } } @@ -2515,8 +2520,7 @@ def mock_get_email_branding_without_brand_text(mocker, fake_uuid): 'text': '', 'id': fake_uuid, 'colour': '#f00', - 'banner_colour': '#f11', - 'single_id_colour': '#f22' + 'brand_type': 'org_banner' } } @@ -2528,7 +2532,7 @@ def mock_get_email_branding_without_brand_text(mocker, fake_uuid): @pytest.fixture(scope='function') def mock_create_email_branding(mocker): - def _create_email_branding(logo, name, text, colour, banner_colour, single_id_colour, domain): + def _create_email_branding(logo, name, text, colour, domain, brand_type): return return mocker.patch( @@ -2538,7 +2542,7 @@ def mock_create_email_branding(mocker): @pytest.fixture(scope='function') def mock_update_email_branding(mocker): - def _update_email_branding(branding_id, logo, name, text, colour, banner_colour, single_id_colour, domain): + def _update_email_branding(branding_id, logo, name, text, colour, domain, brand_type): return return mocker.patch(