From 53724dacd165ad4862a4db49b6a4c26fa774d2cb Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 30 Apr 2020 12:11:37 +0100 Subject: [PATCH] Tell template preview whether a letter can be sent internationally MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a service has permission to send international letters then the admin app should tell template preview, so that template preview knows what rules to apply when it’s validating the address of the letter. We don’t need to wait for template preview to start looking at this query string argument – it will just ignore it for now. --- app/main/views/uploads.py | 7 ++++++- app/template_previews.py | 7 +++++-- tests/app/main/views/test_uploads.py | 13 ++++++++++++- tests/app/test_template_previews.py | 14 +++++++++++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/main/views/uploads.py b/app/main/views/uploads.py index 5166d1ed8..263fd43bf 100644 --- a/app/main/views/uploads.py +++ b/app/main/views/uploads.py @@ -115,7 +115,12 @@ def upload_letter(service_id): file_location = get_transient_letter_file_location(service_id, upload_id) try: - response = sanitise_letter(BytesIO(pdf_file_bytes)) + response = sanitise_letter( + BytesIO(pdf_file_bytes), + allow_international_letters=current_service.has_permission( + 'international_letters' + ), + ) response.raise_for_status() except RequestException as ex: if ex.response is not None and ex.response.status_code == 400: diff --git a/app/template_previews.py b/app/template_previews.py index 7bf488cc5..1a1353e89 100644 --- a/app/template_previews.py +++ b/app/template_previews.py @@ -94,9 +94,12 @@ def get_page_count_for_letter(template, values=None): return page_count -def sanitise_letter(pdf_file): +def sanitise_letter(pdf_file, *, allow_international_letters): return requests.post( - '{}/precompiled/sanitise'.format(current_app.config['TEMPLATE_PREVIEW_API_HOST']), + '{}/precompiled/sanitise?allow_international_letters={}'.format( + current_app.config['TEMPLATE_PREVIEW_API_HOST'], + 'true' if allow_international_letters else 'false', + ), data=pdf_file, headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])} ) diff --git a/tests/app/main/views/test_uploads.py b/tests/app/main/views/test_uploads.py index 1c0d6b248..98246762a 100644 --- a/tests/app/main/views/test_uploads.py +++ b/tests/app/main/views/test_uploads.py @@ -143,16 +143,22 @@ def test_get_upload_letter(client_request): assert normalize_spaces(page.find('label', class_='file-upload-button').text) == 'Choose file' +@pytest.mark.parametrize('extra_permissions, expected_allow_international', ( + ([], False), + (['international_letters'], True), +)) def test_post_upload_letter_redirects_for_valid_file( mocker, active_user_with_permissions, service_one, client_request, fake_uuid, + extra_permissions, + expected_allow_international, ): mocker.patch('uuid.uuid4', return_value=fake_uuid) antivirus_mock = mocker.patch('app.main.views.uploads.antivirus_client.scan', return_value=True) - mocker.patch( + mock_sanitise = mocker.patch( 'app.main.views.uploads.sanitise_letter', return_value=Mock( content='The sanitised content', @@ -169,6 +175,7 @@ def test_post_upload_letter_redirects_for_valid_file( mocker.patch('app.main.views.uploads.service_api_client.get_precompiled_template') service_one['restricted'] = False + service_one['permissions'] += extra_permissions client_request.login(active_user_with_permissions, service=service_one) with open('tests/test_pdf_files/one_page_pdf.pdf', 'rb') as file: @@ -188,6 +195,10 @@ def test_post_upload_letter_redirects_for_valid_file( filename='tests/test_pdf_files/one_page_pdf.pdf', recipient='The Queen', ) + mock_sanitise.assert_called_once_with( + ANY, + allow_international_letters=expected_allow_international, + ) assert 'The Queen' in page.find('div', class_='js-stick-at-bottom-when-scrolling').text assert page.find('h1').text == 'tests/test_pdf_files/one_page_pdf.pdf' diff --git a/tests/app/test_template_previews.py b/tests/app/test_template_previews.py index 4fdd73bbf..1f205412b 100644 --- a/tests/app/test_template_previews.py +++ b/tests/app/test_template_previews.py @@ -164,13 +164,21 @@ def test_from_example_template_makes_request(mocker): ) -def test_sanitise_letter_calls_template_preview_sanitise_endoint_with_file(mocker): +@pytest.mark.parametrize('allow_international_letters, expected_url', ( + (False, 'http://localhost:9999/precompiled/sanitise?allow_international_letters=false'), + (True, 'http://localhost:9999/precompiled/sanitise?allow_international_letters=true'), +)) +def test_sanitise_letter_calls_template_preview_sanitise_endoint_with_file( + mocker, + allow_international_letters, + expected_url, +): request_mock = mocker.patch('app.template_previews.requests.post') - sanitise_letter('pdf_data') + sanitise_letter('pdf_data', allow_international_letters=allow_international_letters) request_mock.assert_called_once_with( - 'http://localhost:9999/precompiled/sanitise', + expected_url, headers={'Authorization': 'Token my-secret-key'}, data='pdf_data' )