Use the new error messages when uploading a letter

We now use the pattern of showing a box at the top of the page with the
error. The error message has a heading and can have additional details.
Error messages and the invalid pages get stored in the S3 metadata.
This commit is contained in:
Katie Smith
2019-10-15 15:56:06 +01:00
parent 3223956ba2
commit 3b3f74bbf0
7 changed files with 122 additions and 43 deletions

View File

@@ -789,7 +789,7 @@ def test_letter_validation_preview_doesnt_call_template_preview_when_no_file(moc
validate_letter.assert_not_called()
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.find('span', class_='error-message').text.strip() == "You need to upload a file to submit"
assert page.find('span', class_='error-message').text.strip() == "You need to choose a file to upload"
def test_letter_validation_preview_doesnt_call_template_preview_when_file_not_pdf(mocker, platform_admin_client):
@@ -805,7 +805,7 @@ def test_letter_validation_preview_doesnt_call_template_preview_when_file_not_pd
antivirus_scan.assert_not_called()
validate_letter.assert_not_called()
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.find('span', class_='error-message').text.strip() == "Letters must be saved as a PDF"
assert page.find('span', class_='error-message').text.strip() == "Save your letter as a PDF and try again."
def test_letter_validation_preview_doesnt_call_template_preview_when_file_doesnt_pass_virus_scan(

View File

@@ -23,6 +23,7 @@ def test_get_upload_letter(client_request):
assert page.find('h1').text == 'Upload a letter'
assert page.find('input', class_='file-upload-field')
assert page.select('button[type=submit]')
assert normalize_spaces(page.find('label', class_='file-upload-button').text) == 'Choose file'
def test_post_upload_letter_redirects_for_valid_file(mocker, client_request):
@@ -88,6 +89,7 @@ def test_post_upload_letter_shows_letter_preview_for_valid_file(mocker, client_r
_follow_redirects=True,
)
assert page.find('h1').text == 'tests/test_pdf_files/one_page_pdf.pdf'
assert len(page.select('.letter-postage')) == 1
assert normalize_spaces(page.select_one('.letter-postage').text) == ('Postage: second class')
assert page.select_one('.letter-postage')['class'] == ['letter-postage', 'letter-postage-second']
@@ -111,7 +113,9 @@ def test_post_upload_letter_shows_error_when_file_is_not_a_pdf(client_request):
_data={'file': file},
_expected_status=200
)
assert page.find('span', class_='error-message').text.strip() == "Letters must be saved as a PDF"
assert page.find('h1').text == 'Wrong file type'
assert page.find('div', class_='banner-dangerous').find('p').text == 'Save your letter as a PDF and try again.'
assert normalize_spaces(page.find('label', class_='file-upload-button').text) == 'Upload your file again'
def test_post_upload_letter_shows_error_when_no_file_uploaded(client_request):
@@ -121,7 +125,8 @@ def test_post_upload_letter_shows_error_when_no_file_uploaded(client_request):
_data={'file': ''},
_expected_status=200
)
assert page.find('span', class_='error-message').text.strip() == "You need to upload a file to submit"
assert page.find('div', class_='banner-dangerous').find('h1').text == 'You need to choose a file to upload'
assert normalize_spaces(page.find('label', class_='file-upload-button').text) == 'Upload your file again'
def test_post_upload_letter_shows_error_when_file_contains_virus(mocker, client_request):
@@ -134,8 +139,8 @@ def test_post_upload_letter_shows_error_when_file_contains_virus(mocker, client_
_data={'file': file},
_expected_status=400
)
assert page.find('h1').text == 'Upload a letter'
assert normalize_spaces(page.select('.banner-dangerous')[0].text) == 'Your file has failed the virus check'
assert page.find('div', class_='banner-dangerous').find('h1').text == 'Your file contains a virus'
assert normalize_spaces(page.find('label', class_='file-upload-button').text) == 'Upload your file again'
def test_post_choose_upload_file_when_file_is_too_big(mocker, client_request):
@@ -148,8 +153,9 @@ def test_post_choose_upload_file_when_file_is_too_big(mocker, client_request):
_data={'file': file},
_expected_status=400
)
assert page.find('h1').text == 'Upload a letter'
assert normalize_spaces(page.select('.banner-dangerous')[0].text) == 'Your file must be smaller than 2MB'
assert page.find('div', class_='banner-dangerous').find('h1').text == 'Your file is too big'
assert page.find('div', class_='banner-dangerous').find('p').text == 'Files must be smaller than 2MB.'
assert normalize_spaces(page.find('label', class_='file-upload-button').text) == 'Upload your file again'
def test_post_choose_upload_file_when_file_is_malformed(mocker, client_request):
@@ -162,8 +168,11 @@ def test_post_choose_upload_file_when_file_is_malformed(mocker, client_request):
_data={'file': file},
_expected_status=400
)
assert page.find('h1').text == 'Upload a letter'
assert normalize_spaces(page.select('.banner-dangerous')[0].text) == 'Your file must be a valid PDF'
assert page.find('div', class_='banner-dangerous').find('h1').text == "Theres a problem with your file"
assert page.find(
'div', class_='banner-dangerous'
).find('p').text == 'Notify cannot read this PDF.Save a new copy of your file and try again.'
assert normalize_spaces(page.find('label', class_='file-upload-button').text) == 'Upload your file again'
def test_post_upload_letter_with_invalid_file(mocker, client_request):
@@ -173,10 +182,15 @@ def test_post_upload_letter_with_invalid_file(mocker, client_request):
mock_sanitise_response = Mock()
mock_sanitise_response.raise_for_status.side_effect = RequestException(response=Mock(status_code=400))
mock_sanitise_response.json = lambda: {
"message": "content-outside-printable-area",
"invalid_pages": [1]
}
mocker.patch('app.main.views.uploads.sanitise_letter', return_value=mock_sanitise_response)
mocker.patch('app.main.views.uploads.service_api_client.get_precompiled_template')
mocker.patch('app.main.views.uploads.get_letter_metadata', return_value={
'filename': 'tests/test_pdf_files/one_page_pdf.pdf', 'page_count': '1', 'status': 'invalid'})
'filename': 'tests/test_pdf_files/one_page_pdf.pdf', 'page_count': '1', 'status': 'invalid',
'message': 'content-outside-printable-area', 'invalid_pages': '[1]'})
with open('tests/test_pdf_files/one_page_pdf.pdf', 'rb') as file:
file_contents = file.read()
@@ -194,13 +208,16 @@ def test_post_upload_letter_with_invalid_file(mocker, client_request):
file_location='service-{}/fake-uuid.pdf'.format(SERVICE_ONE_ID),
status='invalid',
page_count=1,
filename='tests/test_pdf_files/one_page_pdf.pdf'
filename='tests/test_pdf_files/one_page_pdf.pdf',
invalid_pages=[1],
message='content-outside-printable-area'
)
assert page.find('h1').text == 'tests/test_pdf_files/one_page_pdf.pdf'
assert normalize_spaces(
page.find(id='validation-error-message').text
) == 'Validation failed'
assert page.find('div', class_='banner-dangerous').find('h1').text == 'We cannot print your letter'
assert page.find(
'div', class_='banner-dangerous').find('p').text == (
'The content appears outside the printable area on page 1 Files must meet our letter specification.'
)
assert not page.find('button', {'type': 'submit'})
@@ -216,10 +233,12 @@ def test_post_upload_letter_shows_letter_preview_for_invalid_file(mocker, client
mocker.patch('app.main.views.uploads.upload_letter_to_s3')
mock_sanitise_response = Mock()
mock_sanitise_response.raise_for_status.side_effect = RequestException(response=Mock(status_code=400))
mock_sanitise_response.json = lambda: {"message": "template preview error"}
mocker.patch('app.main.views.uploads.sanitise_letter', return_value=mock_sanitise_response)
mocker.patch('app.main.views.uploads.service_api_client.get_precompiled_template', return_value=letter_template)
mocker.patch('app.main.views.uploads.get_letter_metadata', return_value={
'filename': 'tests/test_pdf_files/one_page_pdf.pdf', 'page_count': '1', 'status': 'invalid'})
'filename': 'tests/test_pdf_files/one_page_pdf.pdf', 'page_count': '1', 'status': 'invalid',
'message': 'template-preview-error'})
with open('tests/test_pdf_files/one_page_pdf.pdf', 'rb') as file:
page = client_request.post(
@@ -274,6 +293,7 @@ def test_uploaded_letter_preview(mocker, client_request):
original_filename='my_letter.pdf',
page_count=1,
status='valid',
error={}
)
assert page.find('h1').text == 'my_letter.pdf'
@@ -334,7 +354,8 @@ def test_send_uploaded_letter_when_metadata_states_pdf_is_invalid(mocker, servic
mock_send = mocker.patch('app.main.views.uploads.notification_api_client.send_precompiled_letter')
mocker.patch(
'app.main.views.uploads.get_letter_metadata',
return_value={'filename': 'my_file.pdf', 'page_count': '3', 'status': 'invalid'}
return_value={'filename': 'my_file.pdf', 'page_count': '3', 'status': 'invalid',
'message': 'error', 'invalid_pages': '[1]'}
)
service_one['permissions'] = ['letter', 'upload_letters']