Show letter preview once file is uploaded

This shows the sanitised letter preview if the file had no validation
errors or the preview with the overlay if it failed validation.
This commit is contained in:
Katie Smith
2019-09-09 10:59:32 +01:00
parent 8a322b844b
commit 7368245c9a
9 changed files with 228 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import base64
from functools import partial
from unittest.mock import Mock
@@ -79,6 +80,44 @@ def test_from_database_object_makes_request(
request_mock.assert_called_once_with(expected_url, json=data, headers=headers)
@pytest.mark.parametrize('page_number, expected_url', [
('1', 'http://localhost:9999/precompiled-preview.png?hide_notify=true'),
('2', 'http://localhost:9999/precompiled-preview.png'),
])
def test_from_valid_pdf_file_makes_request(mocker, page_number, expected_url):
mocker.patch('app.template_previews.extract_page_from_pdf', return_value=b'pdf page')
request_mock = mocker.patch(
'app.template_previews.requests.post',
return_value=Mock(content='a', status_code='b', headers={'c': 'd'})
)
response = TemplatePreview.from_valid_pdf_file(b'pdf file', page_number)
assert response == ('a', 'b', {'c': 'd'}.items())
request_mock.assert_called_once_with(
expected_url,
data=base64.b64encode(b'pdf page').decode('utf-8'),
headers={'Authorization': 'Token my-secret-key'},
)
def test_from_invalid_pdf_file_makes_request(mocker):
mocker.patch('app.template_previews.extract_page_from_pdf', return_value=b'pdf page')
request_mock = mocker.patch(
'app.template_previews.requests.post',
return_value=Mock(content='a', status_code='b', headers={'c': 'd'})
)
response = TemplatePreview.from_invalid_pdf_file(b'pdf file', '1')
assert response == ('a', 'b', {'c': 'd'}.items())
request_mock.assert_called_once_with(
'http://localhost:9999/precompiled/overlay.png?page_number=1',
data=b'pdf page',
headers={'Authorization': 'Token my-secret-key'},
)
@pytest.mark.parametrize('template_type', [
'email', 'sms'
])