2019-01-31 16:56:35 +00:00
|
|
|
from io import BytesIO
|
2019-02-11 15:31:12 +00:00
|
|
|
from unittest.mock import Mock, call
|
|
|
|
|
from uuid import UUID
|
2019-01-31 16:56:35 +00:00
|
|
|
|
2022-01-06 11:06:55 +00:00
|
|
|
import pytest
|
2019-02-11 15:31:12 +00:00
|
|
|
from botocore.exceptions import ClientError as BotoClientError
|
2020-03-02 18:00:26 +00:00
|
|
|
from flask import url_for
|
2019-02-06 10:28:55 +00:00
|
|
|
from notifications_python_client.errors import HTTPError
|
2019-01-31 16:56:35 +00:00
|
|
|
|
2019-02-11 15:31:12 +00:00
|
|
|
from app.s3_client.s3_logo_client import (
|
|
|
|
|
LETTER_TEMP_LOGO_LOCATION,
|
|
|
|
|
permanent_letter_logo_name,
|
|
|
|
|
)
|
2019-02-07 16:16:22 +00:00
|
|
|
from tests.conftest import normalize_spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_letter_branding_page_shows_full_branding_list(
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_all_letter_branding
|
2019-02-07 16:16:22 +00:00
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.get('.letter_branding')
|
2019-02-07 16:16:22 +00:00
|
|
|
|
2019-02-11 15:31:12 +00:00
|
|
|
links = page.select('.message-name a')
|
|
|
|
|
brand_names = [normalize_spaces(link.text) for link in links]
|
|
|
|
|
hrefs = [link['href'] for link in links]
|
2019-02-07 16:16:22 +00:00
|
|
|
|
|
|
|
|
assert normalize_spaces(
|
|
|
|
|
page.select_one('h1').text
|
|
|
|
|
) == "Letter branding"
|
|
|
|
|
|
2020-02-20 16:55:56 +00:00
|
|
|
assert page.select('.govuk-grid-column-three-quarters a')[-1]['href'] == url_for('main.create_letter_branding')
|
2019-02-07 16:16:22 +00:00
|
|
|
|
2019-04-04 11:09:09 +01:00
|
|
|
assert brand_names == [
|
|
|
|
|
'HM Government',
|
|
|
|
|
'Land Registry',
|
|
|
|
|
'Animal and Plant Health Agency',
|
2019-02-11 15:31:12 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
assert hrefs == [
|
|
|
|
|
url_for('.update_letter_branding', branding_id=str(UUID(int=0))),
|
|
|
|
|
url_for('.update_letter_branding', branding_id=str(UUID(int=1))),
|
|
|
|
|
url_for('.update_letter_branding', branding_id=str(UUID(int=2))),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_letter_branding_shows_the_current_letter_brand(
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_letter_branding_by_id,
|
2019-11-04 11:07:55 +00:00
|
|
|
fake_uuid,
|
2019-02-11 15:31:12 +00:00
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Update letter branding'
|
|
|
|
|
assert page.select_one('#logo-img > img')['src'].endswith('/hm-government.svg')
|
|
|
|
|
assert page.select_one('#name').attrs.get('value') == 'HM Government'
|
2020-12-16 11:24:52 +00:00
|
|
|
assert page.select_one('#file').attrs.get('accept') == '.svg'
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_letter_branding_with_new_valid_file(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-02-11 15:31:12 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
filename = 'new_file.svg'
|
|
|
|
|
expected_temp_filename = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename=filename)
|
|
|
|
|
|
|
|
|
|
mock_s3_upload = mocker.patch('app.s3_client.s3_logo_client.utils_s3upload')
|
|
|
|
|
mocker.patch('app.s3_client.s3_logo_client.uuid.uuid4', return_value=fake_uuid)
|
|
|
|
|
mock_delete_temp_files = mocker.patch('app.main.views.letter_branding.delete_letter_temp_file')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
_data={'file': (BytesIO(''.encode('utf-8')), filename)},
|
|
|
|
|
_follow_redirects=True,
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert page.select_one('#logo-img > img')['src'].endswith(expected_temp_filename)
|
|
|
|
|
assert page.select_one('#name').attrs.get('value') == 'HM Government'
|
|
|
|
|
|
|
|
|
|
assert mock_s3_upload.called
|
2020-08-28 13:26:14 +01:00
|
|
|
assert mock_delete_temp_files.called is False
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_letter_branding_when_uploading_invalid_file(
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-11-04 11:07:55 +00:00
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid,
|
2019-02-11 15:31:12 +00:00
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
_data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
|
|
|
|
|
_follow_redirects=True
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Update letter branding'
|
|
|
|
|
assert page.select_one('.error-message').text.strip() == 'SVG Images only!'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_letter_branding_deletes_any_temp_files_when_uploading_a_file(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid,
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-02-11 15:31:12 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename='temp.svg')
|
|
|
|
|
|
|
|
|
|
mock_s3_upload = mocker.patch('app.s3_client.s3_logo_client.utils_s3upload')
|
|
|
|
|
mock_delete_temp_files = mocker.patch('app.main.views.letter_branding.delete_letter_temp_file')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
logo=temp_logo,
|
|
|
|
|
_data={'file': (BytesIO(''.encode('utf-8')), 'new_uploaded_file.svg')},
|
|
|
|
|
_follow_redirects=True,
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert mock_s3_upload.called
|
|
|
|
|
assert mock_delete_temp_files.called
|
|
|
|
|
assert page.find('h1').text == 'Update letter branding'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_letter_branding_with_original_file_and_new_details(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_all_letter_branding,
|
|
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid
|
|
|
|
|
):
|
|
|
|
|
mock_client_update = mocker.patch('app.main.views.letter_branding.letter_branding_client.update_letter_branding')
|
2020-03-02 18:00:26 +00:00
|
|
|
mock_upload_logos = mocker.patch('app.main.views.letter_branding.upload_letter_svg_logo')
|
2019-02-11 15:31:12 +00:00
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
_data={
|
2019-02-11 15:31:12 +00:00
|
|
|
'name': 'Updated name',
|
|
|
|
|
'operation': 'branding-details'
|
|
|
|
|
},
|
2021-12-30 16:13:49 +00:00
|
|
|
_follow_redirects=True,
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
assert page.find('h1').text == 'Letter branding'
|
2020-08-28 13:26:14 +01:00
|
|
|
assert mock_upload_logos.called is False
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
mock_client_update.assert_called_once_with(
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
filename='hm-government',
|
|
|
|
|
name='Updated name'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-04-04 11:09:09 +01:00
|
|
|
def test_update_letter_branding_shows_form_errors_on_name_fields(
|
2019-02-11 15:31:12 +00:00
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid
|
|
|
|
|
):
|
|
|
|
|
mocker.patch('app.main.views.letter_branding.letter_branding_client.update_letter_branding')
|
|
|
|
|
|
|
|
|
|
logo = permanent_letter_logo_name('hm-government', 'svg')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
logo=logo,
|
|
|
|
|
_data={
|
2019-02-11 15:31:12 +00:00
|
|
|
'name': '',
|
|
|
|
|
'operation': 'branding-details'
|
|
|
|
|
},
|
2021-12-30 16:13:49 +00:00
|
|
|
_follow_redirects=True
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
2020-08-07 14:06:51 +01:00
|
|
|
error_messages = page.find_all('span', class_='govuk-error-message')
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Update letter branding'
|
2019-04-04 11:09:09 +01:00
|
|
|
assert len(error_messages) == 1
|
2020-08-07 14:06:51 +01:00
|
|
|
assert 'This field is required.' in error_messages[0].text.strip()
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
|
2019-04-04 11:09:09 +01:00
|
|
|
def test_update_letter_branding_shows_database_errors_on_name_field(
|
2019-02-11 15:31:12 +00:00
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid,
|
|
|
|
|
):
|
|
|
|
|
mocker.patch('app.main.views.letter_branding.letter_branding_client.update_letter_branding', side_effect=HTTPError(
|
|
|
|
|
response=Mock(
|
|
|
|
|
status_code=400,
|
|
|
|
|
json={
|
|
|
|
|
'result': 'error',
|
|
|
|
|
'message': {
|
2019-04-04 11:09:09 +01:00
|
|
|
'name': {
|
|
|
|
|
'name already in use'
|
2019-02-11 15:31:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
),
|
2019-04-04 11:09:09 +01:00
|
|
|
message={'name': ['name already in use']}
|
2019-02-11 15:31:12 +00:00
|
|
|
))
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
_data={
|
2019-02-11 15:31:12 +00:00
|
|
|
'name': 'my brand',
|
|
|
|
|
'operation': 'branding-details'
|
2021-12-30 16:13:49 +00:00
|
|
|
},
|
|
|
|
|
_expected_status=200,
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
|
2020-08-07 14:06:51 +01:00
|
|
|
error_message = page.find('span', class_='govuk-error-message').text.strip()
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Update letter branding'
|
2020-08-07 14:06:51 +01:00
|
|
|
assert 'name already in use' in error_message
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_letter_branding_with_new_file_and_new_details(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_all_letter_branding,
|
|
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid
|
|
|
|
|
):
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=fake_uuid, unique_id=fake_uuid, filename='new_file.svg')
|
|
|
|
|
|
|
|
|
|
mock_client_update = mocker.patch('app.main.views.letter_branding.letter_branding_client.update_letter_branding')
|
|
|
|
|
mock_persist_logo = mocker.patch('app.main.views.letter_branding.persist_logo')
|
|
|
|
|
mock_delete_temp_files = mocker.patch('app.main.views.letter_branding.delete_letter_temp_files_created_by')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
logo=temp_logo,
|
|
|
|
|
_data={
|
2019-02-11 15:31:12 +00:00
|
|
|
'name': 'Updated name',
|
|
|
|
|
'operation': 'branding-details'
|
|
|
|
|
},
|
2021-12-30 16:13:49 +00:00
|
|
|
_follow_redirects=True
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
assert page.find('h1').text == 'Letter branding'
|
|
|
|
|
|
|
|
|
|
mock_client_update.assert_called_once_with(
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
filename='{}-new_file'.format(fake_uuid),
|
|
|
|
|
name='Updated name'
|
|
|
|
|
)
|
|
|
|
|
mock_persist_logo.assert_called_once_with(
|
|
|
|
|
temp_logo,
|
|
|
|
|
'letters/static/images/letter-template/{}-new_file.svg'.format(fake_uuid)
|
|
|
|
|
)
|
|
|
|
|
mock_delete_temp_files.assert_called_once_with(fake_uuid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_letter_branding_rolls_back_db_changes_and_shows_error_if_saving_to_s3_fails(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-11 15:31:12 +00:00
|
|
|
mock_get_letter_branding_by_id,
|
|
|
|
|
fake_uuid
|
|
|
|
|
):
|
|
|
|
|
mock_client_update = mocker.patch('app.main.views.letter_branding.letter_branding_client.update_letter_branding')
|
2020-03-02 18:00:26 +00:00
|
|
|
mocker.patch('app.main.views.letter_branding.upload_letter_svg_logo', side_effect=BotoClientError({}, 'error'))
|
2019-02-11 15:31:12 +00:00
|
|
|
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=fake_uuid, unique_id=fake_uuid, filename='new_file.svg')
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.update_letter_branding',
|
|
|
|
|
branding_id=fake_uuid,
|
|
|
|
|
logo=temp_logo,
|
|
|
|
|
_data={
|
2019-02-11 15:31:12 +00:00
|
|
|
'name': 'Updated name',
|
|
|
|
|
'operation': 'branding-details'
|
|
|
|
|
},
|
2021-12-30 16:13:49 +00:00
|
|
|
_follow_redirects=True,
|
2019-02-11 15:31:12 +00:00
|
|
|
)
|
|
|
|
|
assert page.find('h1').text == 'Update letter branding'
|
|
|
|
|
assert page.select_one('.error-message').text.strip() == 'Error saving uploaded file - try uploading again'
|
|
|
|
|
|
|
|
|
|
assert mock_client_update.call_count == 2
|
|
|
|
|
assert mock_client_update.call_args_list == [
|
2019-04-04 11:09:09 +01:00
|
|
|
call(branding_id=fake_uuid, filename='{}-new_file'.format(fake_uuid), name='Updated name'),
|
|
|
|
|
call(branding_id=fake_uuid, filename='hm-government', name='HM Government')
|
2019-02-07 16:16:22 +00:00
|
|
|
]
|
2019-01-31 16:56:35 +00:00
|
|
|
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
def test_create_letter_branding_does_not_show_branding_info(
|
|
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
|
|
|
|
):
|
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.get('.create_letter_branding')
|
2019-01-31 16:56:35 +00:00
|
|
|
|
|
|
|
|
assert page.select_one('#logo-img > img') is None
|
2020-08-07 14:06:51 +01:00
|
|
|
assert page.select_one('#name').attrs.get('value') is None
|
2020-12-16 11:24:52 +00:00
|
|
|
assert page.select_one('#file').attrs.get('accept') == '.svg'
|
2019-01-31 16:56:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_letter_branding_when_uploading_valid_file(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-01-31 16:56:35 +00:00
|
|
|
fake_uuid
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-01-31 16:56:35 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
filename = 'test.svg'
|
|
|
|
|
expected_temp_filename = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename=filename)
|
|
|
|
|
|
|
|
|
|
mock_s3_upload = mocker.patch('app.s3_client.s3_logo_client.utils_s3upload')
|
|
|
|
|
mocker.patch('app.s3_client.s3_logo_client.uuid.uuid4', return_value=fake_uuid)
|
|
|
|
|
mock_delete_temp_files = mocker.patch('app.main.views.letter_branding.delete_letter_temp_file')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
_data={'file': (BytesIO("""
|
2020-03-04 14:25:14 +00:00
|
|
|
<svg height="100" width="100">
|
|
|
|
|
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>
|
|
|
|
|
""".encode('utf-8')), filename)},
|
2021-12-30 16:13:49 +00:00
|
|
|
_follow_redirects=True,
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert page.select_one('#logo-img > img').attrs['src'].endswith(expected_temp_filename)
|
|
|
|
|
assert mock_s3_upload.called
|
2020-08-28 13:26:14 +01:00
|
|
|
assert mock_delete_temp_files.called is False
|
2019-01-31 16:56:35 +00:00
|
|
|
|
|
|
|
|
|
2022-01-06 11:06:55 +00:00
|
|
|
@pytest.mark.parametrize('svg_contents, expected_error', (
|
|
|
|
|
(
|
|
|
|
|
'''
|
|
|
|
|
<svg height="100" width="100">
|
|
|
|
|
<image href="someurlgoeshere" x="0" y="0" height="100" width="100"></image></svg>
|
|
|
|
|
''',
|
|
|
|
|
'This SVG has an embedded raster image in it and will not render well',
|
|
|
|
|
),
|
2022-01-06 11:21:12 +00:00
|
|
|
(
|
|
|
|
|
'''
|
|
|
|
|
<svg height="100" width="100">
|
|
|
|
|
<text>Will render differently depending on fonts installed</text>
|
|
|
|
|
</svg>
|
|
|
|
|
''',
|
|
|
|
|
'This SVG has text which has not been converted to paths and may not render well',
|
|
|
|
|
),
|
2022-01-06 11:06:55 +00:00
|
|
|
))
|
|
|
|
|
def test_create_letter_branding_fails_validation_when_uploading_SVG_with_bad_element(
|
2020-03-04 14:25:14 +00:00
|
|
|
mocker,
|
2022-01-06 11:26:43 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2022-01-06 11:06:55 +00:00
|
|
|
fake_uuid,
|
|
|
|
|
svg_contents,
|
|
|
|
|
expected_error,
|
2020-03-04 14:25:14 +00:00
|
|
|
):
|
|
|
|
|
filename = 'test.svg'
|
|
|
|
|
|
|
|
|
|
mock_s3_upload = mocker.patch('app.s3_client.s3_logo_client.utils_s3upload')
|
|
|
|
|
|
2022-01-06 11:26:43 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
_data={'file': (BytesIO(svg_contents.encode('utf-8')), filename)},
|
|
|
|
|
_follow_redirects=True,
|
2020-03-04 14:25:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert normalize_spaces(page.find('h1').text) == "Add letter branding"
|
2022-01-06 11:06:55 +00:00
|
|
|
assert normalize_spaces(page.select_one(".error-message").text) == expected_error
|
2020-03-04 14:25:14 +00:00
|
|
|
|
|
|
|
|
assert page.findAll('div', {'id': 'logo-img'}) == []
|
|
|
|
|
|
2020-08-28 13:26:14 +01:00
|
|
|
assert mock_s3_upload.called is False
|
2020-03-04 14:25:14 +00:00
|
|
|
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
def test_create_letter_branding_when_uploading_invalid_file(
|
|
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
|
|
|
|
):
|
|
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
_data={'file': (BytesIO(''.encode('utf-8')), 'test.png')},
|
|
|
|
|
_follow_redirects=True,
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
assert page.find('h1').text == 'Add letter branding'
|
|
|
|
|
assert page.select_one('.error-message').text.strip() == 'SVG Images only!'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_letter_branding_deletes_temp_files_when_uploading_a_new_file(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-01-31 16:56:35 +00:00
|
|
|
fake_uuid,
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-01-31 16:56:35 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename='temp.svg')
|
|
|
|
|
|
|
|
|
|
mock_s3_upload = mocker.patch('app.s3_client.s3_logo_client.utils_s3upload')
|
|
|
|
|
mock_delete_temp_files = mocker.patch('app.main.views.letter_branding.delete_letter_temp_file')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
logo=temp_logo,
|
|
|
|
|
_data={'file': (BytesIO(''.encode('utf-8')), 'new.svg')},
|
|
|
|
|
_follow_redirects=True
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
assert mock_s3_upload.called
|
|
|
|
|
assert mock_delete_temp_files.called
|
|
|
|
|
assert page.find('h1').text == 'Add letter branding'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_new_letter_branding_shows_preview_of_logo(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-01-31 16:56:35 +00:00
|
|
|
fake_uuid
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-01-31 16:56:35 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename='temp.svg')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.get(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
logo=temp_logo,
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Add letter branding'
|
|
|
|
|
assert page.select_one('#logo-img > img').attrs['src'].endswith(temp_logo)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_letter_branding_shows_an_error_when_submitting_details_with_no_logo(
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-01-31 16:56:35 +00:00
|
|
|
fake_uuid
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
_data={
|
2019-01-31 16:56:35 +00:00
|
|
|
'name': 'Test brand',
|
|
|
|
|
'operation': 'branding-details'
|
2021-12-30 16:13:49 +00:00
|
|
|
},
|
|
|
|
|
_expected_status=200,
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Add letter branding'
|
|
|
|
|
assert page.select_one('.error-message').text.strip() == 'You need to upload a file to submit'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_letter_branding_persists_logo_when_all_data_is_valid(
|
|
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-01-31 16:56:35 +00:00
|
|
|
fake_uuid,
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-01-31 16:56:35 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename='test.svg')
|
|
|
|
|
|
|
|
|
|
mock_letter_client = mocker.patch('app.main.views.letter_branding.letter_branding_client')
|
|
|
|
|
mock_persist_logo = mocker.patch('app.main.views.letter_branding.persist_logo')
|
|
|
|
|
mock_delete_temp_files = mocker.patch('app.main.views.letter_branding.delete_letter_temp_files_created_by')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
logo=temp_logo,
|
|
|
|
|
_data={
|
2019-01-31 16:56:35 +00:00
|
|
|
'name': 'Test brand',
|
|
|
|
|
'operation': 'branding-details'
|
|
|
|
|
},
|
2021-12-30 16:13:49 +00:00
|
|
|
_follow_redirects=True
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
2019-02-07 16:16:22 +00:00
|
|
|
assert page.find('h1').text == 'Letter branding'
|
2019-01-31 16:56:35 +00:00
|
|
|
|
|
|
|
|
mock_letter_client.create_letter_branding.assert_called_once_with(
|
2019-04-04 11:09:09 +01:00
|
|
|
filename='{}-test'.format(fake_uuid), name='Test brand'
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
mock_persist_logo.assert_called_once_with(
|
|
|
|
|
temp_logo,
|
|
|
|
|
'letters/static/images/letter-template/{}-test.svg'.format(fake_uuid)
|
|
|
|
|
)
|
|
|
|
|
mock_delete_temp_files.assert_called_once_with(user_id)
|
|
|
|
|
|
|
|
|
|
|
2019-04-04 11:09:09 +01:00
|
|
|
def test_create_letter_branding_shows_form_errors_on_name_field(
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-01-31 16:56:35 +00:00
|
|
|
fake_uuid
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-01-31 16:56:35 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename='test.svg')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
logo=temp_logo,
|
|
|
|
|
_data={
|
2019-01-31 16:56:35 +00:00
|
|
|
'name': '',
|
|
|
|
|
'operation': 'branding-details'
|
2021-12-30 16:13:49 +00:00
|
|
|
},
|
|
|
|
|
_expected_status=200,
|
2019-01-31 16:56:35 +00:00
|
|
|
)
|
|
|
|
|
|
2020-08-07 14:06:51 +01:00
|
|
|
error_messages = page.find_all('span', class_='govuk-error-message')
|
2019-01-31 16:56:35 +00:00
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Add letter branding'
|
2019-04-04 11:09:09 +01:00
|
|
|
assert len(error_messages) == 1
|
2020-08-07 14:06:51 +01:00
|
|
|
assert 'This field is required.' in error_messages[0].text.strip()
|
2019-01-31 16:56:35 +00:00
|
|
|
|
|
|
|
|
|
2019-04-04 11:09:09 +01:00
|
|
|
def test_create_letter_branding_shows_database_errors_on_name_fields(
|
2019-02-06 10:28:55 +00:00
|
|
|
mocker,
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request,
|
|
|
|
|
platform_admin_user,
|
2019-02-06 10:28:55 +00:00
|
|
|
fake_uuid,
|
|
|
|
|
):
|
2021-12-30 16:13:49 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2019-02-06 10:28:55 +00:00
|
|
|
user_id = session["user_id"]
|
|
|
|
|
|
|
|
|
|
mocker.patch('app.main.views.letter_branding.letter_branding_client.create_letter_branding', side_effect=HTTPError(
|
|
|
|
|
response=Mock(
|
|
|
|
|
status_code=400,
|
|
|
|
|
json={
|
|
|
|
|
'result': 'error',
|
|
|
|
|
'message': {
|
2019-04-04 11:09:09 +01:00
|
|
|
'name': {
|
|
|
|
|
'name already in use'
|
2019-02-06 10:28:55 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
),
|
2019-04-04 11:09:09 +01:00
|
|
|
message={'name': ['name already in use']}
|
2019-02-06 10:28:55 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
temp_logo = LETTER_TEMP_LOGO_LOCATION.format(user_id=user_id, unique_id=fake_uuid, filename='test.svg')
|
|
|
|
|
|
2021-12-30 16:13:49 +00:00
|
|
|
client_request.login(platform_admin_user)
|
|
|
|
|
page = client_request.post(
|
|
|
|
|
'.create_letter_branding',
|
|
|
|
|
logo=temp_logo,
|
|
|
|
|
_data={
|
2019-02-06 10:28:55 +00:00
|
|
|
'name': 'my brand',
|
|
|
|
|
'operation': 'branding-details'
|
2021-12-30 16:13:49 +00:00
|
|
|
},
|
|
|
|
|
_expected_status=200,
|
2019-02-06 10:28:55 +00:00
|
|
|
)
|
|
|
|
|
|
2020-08-07 14:06:51 +01:00
|
|
|
error_message = page.find('span', class_='govuk-error-message').text.strip()
|
2019-02-06 10:28:55 +00:00
|
|
|
|
|
|
|
|
assert page.find('h1').text == 'Add letter branding'
|
2020-08-07 14:06:51 +01:00
|
|
|
assert 'name already in use' in error_message
|