From 81da8e762dae3555a8a402b2c86a20eabad1a30b Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Mon, 9 Sep 2019 16:05:00 +0100 Subject: [PATCH] Update for new template preview sanitise response --- app/main/views/uploads.py | 5 ++++- tests/app/main/views/test_uploads.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/main/views/uploads.py b/app/main/views/uploads.py index 5acb8a898..82b0373fc 100644 --- a/app/main/views/uploads.py +++ b/app/main/views/uploads.py @@ -1,3 +1,4 @@ +import base64 import uuid from io import BytesIO @@ -51,6 +52,7 @@ def upload_letter(service_id): return invalid_upload_error('Your file must be smaller than 2MB') try: + # TODO: get page count from the sanitise response once template preview handles malformed files nicely page_count = pdf_page_count(BytesIO(pdf_file_bytes)) except PdfReadError: current_app.logger.info('Invalid PDF uploaded for service_id: {}'.format(service_id)) @@ -70,7 +72,8 @@ def upload_letter(service_id): raise ex else: status = 'valid' - upload_letter_to_s3(response.content, file_location, status) + file_contents = base64.b64decode(response.json()['file'].encode()) + upload_letter_to_s3(file_contents, file_location, status) return redirect( url_for( diff --git a/tests/app/main/views/test_uploads.py b/tests/app/main/views/test_uploads.py index 6638ef3ef..b9b532f84 100644 --- a/tests/app/main/views/test_uploads.py +++ b/tests/app/main/views/test_uploads.py @@ -28,7 +28,10 @@ def test_get_upload_letter(client_request): def test_post_upload_letter_redirects_for_valid_file(mocker, client_request): mocker.patch('uuid.uuid4', return_value='fake-uuid') antivirus_mock = mocker.patch('app.main.views.uploads.antivirus_client.scan', return_value=True) - mocker.patch('app.main.views.uploads.sanitise_letter', return_value=Mock(content='The sanitised content')) + mocker.patch( + 'app.main.views.uploads.sanitise_letter', + return_value=Mock(content='The sanitised content', json=lambda: {'file': 'VGhlIHNhbml0aXNlZCBjb250ZW50'}) + ) mock_s3 = mocker.patch('app.main.views.uploads.upload_letter_to_s3') mocker.patch('app.main.views.uploads.service_api_client.get_precompiled_template') @@ -42,7 +45,7 @@ def test_post_upload_letter_redirects_for_valid_file(mocker, client_request): assert antivirus_mock.called mock_s3.assert_called_once_with( - 'The sanitised content', + b'The sanitised content', 'service-{}/fake-uuid.pdf'.format(SERVICE_ONE_ID), 'valid', ) @@ -64,7 +67,10 @@ def test_post_upload_letter_shows_letter_preview_for_valid_file(mocker, client_r mocker.patch('uuid.uuid4', return_value='fake-uuid') mocker.patch('app.main.views.uploads.antivirus_client.scan', return_value=True) - mocker.patch('app.main.views.uploads.sanitise_letter', return_value=Mock(content='The sanitised content')) + mocker.patch( + 'app.main.views.uploads.sanitise_letter', + return_value=Mock(content='The sanitised content', json=lambda: {'file': 'VGhlIHNhbml0aXNlZCBjb250ZW50'}) + ) mocker.patch('app.main.views.uploads.upload_letter_to_s3') mocker.patch('app.main.views.uploads.pdf_page_count', return_value=3) mocker.patch('app.main.views.uploads.service_api_client.get_precompiled_template', return_value=letter_template)