Merge pull request #2610 from alphagov/get-pdf-contents-via-api

add api endpoint to get pdf for letter
This commit is contained in:
Leo Hemsted
2019-09-17 14:55:34 +01:00
committed by GitHub
9 changed files with 168 additions and 27 deletions

View File

@@ -94,6 +94,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,87 @@ 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
def test_get_pdf_for_notification_returns_pdf_content(
client,
sample_letter_notification,
mocker,
):
mock_get_letter_pdf = mocker.patch('app.v2.notifications.get_notifications.get_letter_pdf', return_value=b'foo')
sample_letter_notification.status = 'created'
auth_header = create_authorization_header(service_id=sample_letter_notification.service_id)
response = client.get(
path=url_for('v2_notifications.get_pdf_for_notification', notification_id=sample_letter_notification.id),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 200
assert response.get_data() == b'foo'
mock_get_letter_pdf.assert_called_once_with(sample_letter_notification)
def test_get_pdf_for_notification_returns_400_if_pdf_not_found(
client,
sample_letter_notification,
mocker,
):
# if no files are returned get_letter_pdf throws StopIteration as the iterator runs out
mock_get_letter_pdf = mocker.patch(
'app.v2.notifications.get_notifications.get_letter_pdf',
side_effect=StopIteration
)
sample_letter_notification.status = 'created'
auth_header = create_authorization_header(service_id=sample_letter_notification.service_id)
response = client.get(
path=url_for('v2_notifications.get_pdf_for_notification', notification_id=sample_letter_notification.id),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 400
assert response.json['errors'] == [{
'error': 'PDFNotReadyError',
'message': 'PDF not available yet, try again later'
}]
mock_get_letter_pdf.assert_called_once_with(sample_letter_notification)
@pytest.mark.parametrize('status, expected_message', [
('virus-scan-failed', 'Document did not pass the virus scan'),
('technical-failure', 'PDF not available for letters in status technical-failure'),
])
def test_get_pdf_for_notification_only_returns_pdf_content_if_right_status(
client,
sample_letter_notification,
mocker,
status,
expected_message
):
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_pdf_for_notification', notification_id=sample_letter_notification.id),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 400
assert response.json['errors'] == [{
'error': 'BadRequestError',
'message': expected_message
}]
assert mock_get_letter_pdf.called is False
def test_get_pdf_for_notification_fails_for_non_letters(client, sample_notification):
auth_header = create_authorization_header(service_id=sample_notification.service_id)
response = client.get(
path=url_for('v2_notifications.get_pdf_for_notification', notification_id=sample_notification.id),
headers=[('Content-Type', 'application/json'), auth_header]
)
assert response.status_code == 400
assert response.json['errors'] == [{'error': 'BadRequestError', 'message': 'Notification is not a letter'}]

View File

@@ -122,7 +122,7 @@ def test_post_letter_notification_sets_postage(
'staging',
'live',
])
def test_post_letter_notification_with_test_key_set_status_to_delivered(
def test_post_letter_notification_with_test_key_creates_pdf_and_sets_status_to_delivered(
notify_api, client, sample_letter_template, mocker, env):
data = {
@@ -148,7 +148,7 @@ def test_post_letter_notification_with_test_key_set_status_to_delivered(
notification = Notification.query.one()
assert not fake_create_letter_task.called
fake_create_letter_task.assert_called_once_with([str(notification.id)], queue='research-mode-tasks')
assert not fake_create_dvla_response_task.called
assert notification.status == NOTIFICATION_DELIVERED
@@ -157,7 +157,7 @@ def test_post_letter_notification_with_test_key_set_status_to_delivered(
'development',
'preview',
])
def test_post_letter_notification_with_test_key_sets_status_to_sending_and_sends_fake_response_file(
def test_post_letter_notification_with_test_key_creates_pdf_and_sets_status_to_sending_and_sends_fake_response_file(
notify_api, client, sample_letter_template, mocker, env):
data = {
@@ -183,7 +183,7 @@ def test_post_letter_notification_with_test_key_sets_status_to_sending_and_sends
notification = Notification.query.one()
assert not fake_create_letter_task.called
fake_create_letter_task.assert_called_once_with([str(notification.id)], queue='research-mode-tasks')
assert fake_create_dvla_response_task.called
assert notification.status == NOTIFICATION_SENDING
@@ -357,7 +357,7 @@ def test_post_letter_notification_doesnt_send_in_trial(client, sample_trial_lett
{'error': 'BadRequestError', 'message': 'Cannot send letters when service is in trial mode'}]
def test_post_letter_notification_is_delivered_if_in_trial_mode_and_using_test_key(
def test_post_letter_notification_is_delivered_but_still_creates_pdf_if_in_trial_mode_and_using_test_key(
client,
sample_trial_letter_template,
mocker
@@ -373,7 +373,7 @@ def test_post_letter_notification_is_delivered_if_in_trial_mode_and_using_test_k
notification = Notification.query.one()
assert notification.status == NOTIFICATION_DELIVERED
assert not fake_create_letter_task.called
fake_create_letter_task.assert_called_once_with([str(notification.id)], queue='research-mode-tasks')
def test_post_letter_notification_is_delivered_and_has_pdf_uploaded_to_test_letters_bucket_using_test_key(