add flag to return pdf content via api

this is only applicable when getting a single notification by id. it's
also ignored if the notification isn't a letter.

Otherwise, it overwrites the 'body' field in the response.

If the notification's pdf is available, it returns that, base64
encoded. If the pdf is not available, it returns an empty string.

The pdf won't be available if the notification's status is:
* pending-virus-scan
* virus-scan-failed
* validation-failed
* technical-failure

The pdf will be retrieved from the correct s3 bucket based on its type
This commit is contained in:
Leo Hemsted
2019-09-03 16:49:03 +01:00
parent 5cdce44e42
commit 22aff482a8
4 changed files with 62 additions and 2 deletions

View File

@@ -80,6 +80,16 @@ def test_get_bucket_name_and_prefix_for_notification_precompiled_letter_using_te
sample_precompiled_letter_notification_using_test_key.reference).upper()
@freeze_time(FROZEN_DATE_TIME)
def test_get_bucket_name_and_prefix_for_notification_templated_letter_using_test_key(sample_letter_notification):
sample_letter_notification.key_type = KEY_TYPE_TEST
bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification(sample_letter_notification)
assert bucket == current_app.config['TEST_LETTERS_BUCKET_NAME']
assert bucket_prefix == 'NOTIFY.{}'.format(sample_letter_notification.reference).upper()
@freeze_time(FROZEN_DATE_TIME)
def test_get_bucket_name_and_prefix_for_failed_validation(sample_precompiled_letter_notification):
sample_precompiled_letter_notification.status = NOTIFICATION_VALIDATION_FAILED

View File

@@ -649,3 +649,38 @@ def test_get_notifications_renames_letter_statuses(client, sample_letter_templat
json_response = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert json_response['status'] == expected_status
@pytest.mark.parametrize('status,expected_body,mock_called', [
('created', 'Zm9v', True),
('sending', 'Zm9v', True),
('pending-virus-check', '', False),
('virus-scan-failed', '', False),
('validation-failed', '', False),
('technical-failure', '', False),
])
def test_get_notification_only_returns_pdf_content_if_right_status(
client,
sample_letter_notification,
mocker,
status,
expected_body,
mock_called
):
mock_get_letter_pdf = mocker.patch('app.v2.notifications.get_notifications.get_letter_pdf', return_value=b'foo')
sample_letter_notification.status = status
auth_header = create_authorization_header(service_id=sample_letter_notification.service_id)
response = client.get(
path=url_for(
'v2_notifications.get_notification_by_id',
notification_id=sample_letter_notification.id,
return_pdf_content='true'
),
headers=[('Content-Type', 'application/json'), auth_header]
)
json_response = json.loads(response.get_data(as_text=True))
assert response.status_code == 200
assert json_response['body'] == expected_body
assert mock_get_letter_pdf.called == mock_called