mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
move get_pdf functionality to its own endpoint
it felt very awkward when the body of a pdf might be empty, might have things in it, and whether it is empty or not can change even when the status is the same (a created template notification might have a pdf, but might not, we don't know). So move it to its own endpoint, so we can hand craft some 400 errors that appropriately explain what's going on.
This commit is contained in:
@@ -651,36 +651,85 @@ def test_get_notifications_renames_letter_statuses(client, sample_letter_templat
|
||||
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_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': 'BadRequestError',
|
||||
'message': 'PDF not available for letter, try again later'
|
||||
}]
|
||||
mock_get_letter_pdf.assert_called_once_with(sample_letter_notification)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status', [
|
||||
'pending-virus-check',
|
||||
'virus-scan-failed',
|
||||
'technical-failure',
|
||||
])
|
||||
def test_get_notification_only_returns_pdf_content_if_right_status(
|
||||
def test_get_pdf_for_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'
|
||||
),
|
||||
path=url_for('v2_notifications.get_pdf_for_notification', notification_id=sample_letter_notification.id),
|
||||
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
|
||||
assert response.status_code == 400
|
||||
assert response.json['errors'] == [{
|
||||
'error': 'BadRequestError',
|
||||
'message': 'PDF not available for letters in status {}'.format(status)
|
||||
}]
|
||||
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'}]
|
||||
|
||||
Reference in New Issue
Block a user