From 642ab1ad1e20877b508c5ea4d3e61cfb101c6989 Mon Sep 17 00:00:00 2001 From: Ali Zaidi Date: Wed, 6 May 2020 12:21:16 +0100 Subject: [PATCH] Add support for CSV files --- app/clients/document_download.py | 9 ++++---- app/v2/notifications/post_notifications.py | 2 +- .../notifications/test_post_notifications.py | 21 +++++++++++++------ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/clients/document_download.py b/app/clients/document_download.py index f6d2968e6..2f4742e38 100644 --- a/app/clients/document_download.py +++ b/app/clients/document_download.py @@ -1,5 +1,3 @@ -import base64 - import requests from flask import current_app @@ -26,15 +24,16 @@ class DocumentDownloadClient: def get_upload_url(self, service_id): return "{}/services/{}/documents".format(self.api_host, service_id) - def upload_document(self, service_id, file_contents): + def upload_document(self, service_id, file_contents, is_csv=None): try: response = requests.post( self.get_upload_url(service_id), headers={ 'Authorization': "Bearer {}".format(self.auth_token), }, - files={ - 'document': base64.b64decode(file_contents) + json={ + 'document': file_contents, + 'is_csv': is_csv or False, } ) diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index acd37f1a8..a789bc851 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -325,7 +325,7 @@ def process_document_uploads(personalisation_data, service, simulated=False): else: try: personalisation_data[key] = document_download_client.upload_document( - service.id, personalisation_data[key]['file'] + service.id, personalisation_data[key]['file'], personalisation_data[key].get('is_csv') ) except DocumentDownloadError as e: raise BadRequestError(message=e.message, status_code=e.status_code) diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index 159c5d9ef..53e31f13d 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -775,7 +775,16 @@ def test_post_email_notification_with_archived_reply_to_id_returns_400(client, s assert 'BadRequestError' in resp_json['errors'][0]['error'] -def test_post_notification_with_document_upload(client, notify_db_session, mocker): +@pytest.mark.parametrize( + 'csv_param', + ( + {'is_csv': None}, + {'is_csv': False}, + {'is_csv': True}, + {}, + ) +) +def test_post_notification_with_document_upload(client, notify_db_session, mocker, csv_param): service = create_service(service_permissions=[EMAIL_TYPE]) service.contact_link = 'contact.me@gov.uk' template = create_template( @@ -786,14 +795,14 @@ def test_post_notification_with_document_upload(client, notify_db_session, mocke mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') document_download_mock = mocker.patch('app.v2.notifications.post_notifications.document_download_client') - document_download_mock.upload_document.side_effect = lambda service_id, content: f'{content}-link' + document_download_mock.upload_document.side_effect = lambda service_id, content, is_csv: f'{content}-link' data = { "email_address": service.users[0].email_address, "template_id": template.id, "personalisation": { - "first_link": {"file": "abababab"}, - "second_link": {"file": "cdcdcdcd"} + "first_link": {"file": "abababab", **csv_param}, + "second_link": {"file": "cdcdcdcd", **csv_param} } } @@ -808,8 +817,8 @@ def test_post_notification_with_document_upload(client, notify_db_session, mocke assert validate(resp_json, post_email_response) == resp_json assert document_download_mock.upload_document.call_args_list == [ - call(service.id, 'abababab'), - call(service.id, 'cdcdcdcd') + call(service.id, 'abababab', csv_param.get('is_csv')), + call(service.id, 'cdcdcdcd', csv_param.get('is_csv')) ] notification = Notification.query.one()