mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-30 19:28:50 -04:00
remove letters from app/template
This commit is contained in:
@@ -1131,587 +1131,3 @@ def test_update_redact_template_400s_if_no_created_by(admin_request, sample_temp
|
||||
|
||||
assert sample_template.redact_personalisation is False
|
||||
assert sample_template.template_redacted.updated_at == original_updated_time
|
||||
|
||||
|
||||
def test_preview_letter_template_by_id_invalid_file_type(
|
||||
sample_letter_notification,
|
||||
admin_request):
|
||||
|
||||
resp = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=sample_letter_notification.service_id,
|
||||
template_id=sample_letter_notification.template_id,
|
||||
notification_id=sample_letter_notification.id,
|
||||
file_type='doc',
|
||||
_expected_status=400
|
||||
)
|
||||
|
||||
assert ['file_type must be pdf or png'] == resp['message']['content']
|
||||
|
||||
|
||||
@freeze_time('2012-12-12')
|
||||
@pytest.mark.parametrize('file_type', ('png', 'pdf'))
|
||||
def test_preview_letter_template_by_id_valid_file_type(
|
||||
notify_api,
|
||||
sample_letter_notification,
|
||||
admin_request,
|
||||
file_type,
|
||||
):
|
||||
sample_letter_notification.created_at = datetime.utcnow()
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
content = b'\x00\x01'
|
||||
|
||||
mock_post = request_mock.post(
|
||||
'http://localhost/notifications-template-preview/preview.{}'.format(file_type),
|
||||
content=content,
|
||||
headers={'X-pdf-page-count': '1'},
|
||||
status_code=200
|
||||
)
|
||||
|
||||
resp = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=sample_letter_notification.service_id,
|
||||
notification_id=sample_letter_notification.id,
|
||||
file_type=file_type,
|
||||
)
|
||||
|
||||
post_json = mock_post.last_request.json()
|
||||
assert post_json['template']['id'] == str(sample_letter_notification.template_id)
|
||||
assert post_json['values'] == {
|
||||
'address_line_1': 'A1',
|
||||
'address_line_2': 'A2',
|
||||
'address_line_3': 'A3',
|
||||
'address_line_4': 'A4',
|
||||
'address_line_5': 'A5',
|
||||
'address_line_6': 'A6',
|
||||
'postcode': 'A_POST',
|
||||
}
|
||||
assert post_json['date'] == '2012-12-12T00:00:00'
|
||||
assert post_json['filename'] is None
|
||||
assert base64.b64decode(resp['content']) == content
|
||||
|
||||
|
||||
@freeze_time('2012-12-12')
|
||||
def test_preview_letter_template_by_id_shows_template_version_used_by_notification(
|
||||
notify_api,
|
||||
sample_letter_notification,
|
||||
sample_letter_template,
|
||||
admin_request
|
||||
):
|
||||
sample_letter_notification.created_at = datetime.utcnow()
|
||||
assert sample_letter_notification.template_version == 1
|
||||
|
||||
# Create a new template history to check that our preview doesn't use the newest version
|
||||
# but instead the one linked with the notification
|
||||
sample_letter_template.content = 'new content'
|
||||
dao_update_template(sample_letter_template)
|
||||
versions = dao_get_template_versions(sample_letter_notification.service.id, sample_letter_template.id)
|
||||
assert len(versions) == 2
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
content = b'\x00\x01'
|
||||
|
||||
mock_post = request_mock.post(
|
||||
'http://localhost/notifications-template-preview/preview.png',
|
||||
content=content,
|
||||
headers={'X-pdf-page-count': '1'},
|
||||
status_code=200
|
||||
)
|
||||
|
||||
admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=sample_letter_notification.service_id,
|
||||
notification_id=sample_letter_notification.id,
|
||||
file_type='png',
|
||||
)
|
||||
|
||||
post_json = mock_post.last_request.json()
|
||||
assert post_json['template']['id'] == str(sample_letter_notification.template_id)
|
||||
assert post_json['template']['version'] == '1'
|
||||
|
||||
|
||||
def test_preview_letter_template_by_id_template_preview_500(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_letter_notification):
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
import requests_mock
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
content = b'\x00\x01'
|
||||
|
||||
mock_post = request_mock.post(
|
||||
'http://localhost/notifications-template-preview/preview.pdf',
|
||||
content=content,
|
||||
headers={'X-pdf-page-count': '1'},
|
||||
status_code=404
|
||||
)
|
||||
|
||||
resp = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=sample_letter_notification.service_id,
|
||||
notification_id=sample_letter_notification.id,
|
||||
file_type='pdf',
|
||||
_expected_status=500
|
||||
)
|
||||
|
||||
assert mock_post.last_request.json()
|
||||
assert 'Status code: 404' in resp['message']
|
||||
assert 'Error generating preview letter for {}'.format(sample_letter_notification.id) in resp['message']
|
||||
|
||||
|
||||
def test_preview_letter_template_precompiled_pdf_file_type(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker():
|
||||
|
||||
content = b'\x00\x01'
|
||||
|
||||
mock_get_letter_pdf = mocker.patch(
|
||||
'app.template.rest.get_letter_pdf_and_metadata',
|
||||
return_value=(content, {
|
||||
"message": "",
|
||||
"invalid_pages": "",
|
||||
"page_count": "1"
|
||||
})
|
||||
)
|
||||
|
||||
resp = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type='pdf'
|
||||
)
|
||||
|
||||
assert mock_get_letter_pdf.called_once_with(notification)
|
||||
assert base64.b64decode(resp['content']) == content
|
||||
|
||||
|
||||
def test_preview_letter_template_precompiled_s3_error(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker():
|
||||
|
||||
mocker.patch('app.template.rest.get_letter_pdf_and_metadata',
|
||||
side_effect=botocore.exceptions.ClientError(
|
||||
{'Error': {'Code': '403', 'Message': 'Unauthorized'}},
|
||||
'GetObject'
|
||||
))
|
||||
|
||||
request = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type='pdf',
|
||||
_expected_status=500
|
||||
)
|
||||
|
||||
assert request['message'] == "Error extracting requested page from PDF file for notification_id {} type " \
|
||||
"<class 'botocore.exceptions.ClientError'> An error occurred (403) " \
|
||||
"when calling the GetObject operation: Unauthorized".format(notification.id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"requested_page, message, expected_post_url",
|
||||
[
|
||||
# page defaults to 1, page is valid, no overlay shown
|
||||
("", "", 'precompiled-preview.png'),
|
||||
# page is valid, no overlay shown
|
||||
("1", "", 'precompiled-preview.png'),
|
||||
# page is invalid but not because content is outside printable area so no overlay
|
||||
("1", "letter-not-a4-portrait-oriented", 'precompiled-preview.png'),
|
||||
# page is invalid, overlay shown
|
||||
("1", "content-outside-printable-area", 'precompiled/overlay.png?page_number=1'),
|
||||
# page is valid, no overlay shown
|
||||
("2", "content-outside-printable-area", 'precompiled-preview.png'),
|
||||
# page is invalid, overlay shown
|
||||
("3", "content-outside-printable-area", 'precompiled/overlay.png?page_number=3'),
|
||||
]
|
||||
)
|
||||
def test_preview_letter_template_precompiled_for_png_shows_overlay_on_pages_with_content_outside_printable_area(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker,
|
||||
requested_page,
|
||||
message,
|
||||
expected_post_url,
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
|
||||
pdf_content = b'\x00\x01'
|
||||
expected_returned_content = b'\x00\x02'
|
||||
|
||||
metadata = {
|
||||
"message": message,
|
||||
"invalid_pages": "[1,3]",
|
||||
"page_count": "4"
|
||||
}
|
||||
|
||||
mock_get_letter_pdf = mocker.patch(
|
||||
'app.template.rest.get_letter_pdf_and_metadata',
|
||||
return_value=(pdf_content, metadata)
|
||||
)
|
||||
|
||||
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content)
|
||||
|
||||
mock_post = request_mock.post(
|
||||
'http://localhost/notifications-template-preview/{}'.format(expected_post_url),
|
||||
content=expected_returned_content,
|
||||
headers={'X-pdf-page-count': '4'},
|
||||
status_code=200
|
||||
)
|
||||
|
||||
response = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
page=requested_page,
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type="png",
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
mock_post.last_request.json()
|
||||
assert mock_get_letter_pdf.called_once_with(notification)
|
||||
assert base64.b64decode(response['content']) == expected_returned_content
|
||||
assert response["metadata"] == metadata
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_pages",
|
||||
[
|
||||
"[1,3]",
|
||||
"[2,4]", # it shouldn't make a difference if the error was on the first page or not
|
||||
]
|
||||
)
|
||||
def test_preview_letter_template_precompiled_for_pdf_shows_overlay_on_all_pages_if_content_outside_printable_area(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker,
|
||||
invalid_pages,
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
|
||||
pdf_content = b'\x00\x01'
|
||||
expected_returned_content = b'\x00\x02'
|
||||
|
||||
metadata = {
|
||||
"message": "content-outside-printable-area",
|
||||
"invalid_pages": invalid_pages,
|
||||
"page_count": "4"
|
||||
}
|
||||
|
||||
mock_get_letter_pdf = mocker.patch(
|
||||
'app.template.rest.get_letter_pdf_and_metadata',
|
||||
return_value=(pdf_content, metadata)
|
||||
)
|
||||
|
||||
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content)
|
||||
|
||||
mock_post = request_mock.post(
|
||||
'http://localhost/notifications-template-preview/precompiled/overlay.pdf',
|
||||
content=expected_returned_content,
|
||||
headers={'X-pdf-page-count': '4'},
|
||||
status_code=200
|
||||
)
|
||||
|
||||
response = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type="pdf",
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
mock_post.last_request.json()
|
||||
assert mock_get_letter_pdf.called_once_with(notification)
|
||||
assert base64.b64decode(response['content']) == expected_returned_content
|
||||
assert response["metadata"] == metadata
|
||||
|
||||
|
||||
@pytest.mark.parametrize('page_number,expect_preview_url', [
|
||||
('', 'http://localhost/notifications-template-preview/precompiled-preview.png?hide_notify=true'),
|
||||
('1', 'http://localhost/notifications-template-preview/precompiled-preview.png?hide_notify=true'),
|
||||
('2', 'http://localhost/notifications-template-preview/precompiled-preview.png')
|
||||
])
|
||||
def test_preview_letter_template_precompiled_png_file_type_hide_notify_tag_only_on_first_page(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker,
|
||||
page_number,
|
||||
expect_preview_url
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
pdf_content = b'\x00\x01'
|
||||
png_content = b'\x00\x02'
|
||||
encoded = base64.b64encode(png_content).decode('utf-8')
|
||||
|
||||
mocker.patch(
|
||||
'app.template.rest.get_letter_pdf_and_metadata',
|
||||
return_value=(pdf_content, {
|
||||
"message": "",
|
||||
"invalid_pages": "",
|
||||
"page_count": "2"
|
||||
})
|
||||
)
|
||||
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=png_content)
|
||||
mock_get_png_preview = mocker.patch('app.template.rest._get_png_preview_or_overlaid_pdf', return_value=encoded)
|
||||
|
||||
admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type='png',
|
||||
page=page_number
|
||||
)
|
||||
|
||||
mock_get_png_preview.assert_called_once_with(
|
||||
expect_preview_url, encoded, notification.id, json=False
|
||||
)
|
||||
|
||||
|
||||
def test_preview_letter_template_precompiled_png_template_preview_500_error(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
|
||||
pdf_content = b'\x00\x01'
|
||||
png_content = b'\x00\x02'
|
||||
|
||||
mocker.patch('app.template.rest.get_letter_pdf_and_metadata', return_value=(pdf_content, {
|
||||
"message": "",
|
||||
"invalid_pages": "",
|
||||
"page_count": "1"
|
||||
}))
|
||||
|
||||
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content)
|
||||
|
||||
mock_post = request_mock.post(
|
||||
'http://localhost/notifications-template-preview/precompiled-preview.png',
|
||||
content=png_content,
|
||||
headers={'X-pdf-page-count': '1'},
|
||||
status_code=500
|
||||
)
|
||||
|
||||
admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type='png',
|
||||
_expected_status=500
|
||||
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
mock_post.last_request.json()
|
||||
|
||||
|
||||
def test_preview_letter_template_precompiled_png_template_preview_400_error(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
|
||||
pdf_content = b'\x00\x01'
|
||||
png_content = b'\x00\x02'
|
||||
|
||||
mocker.patch('app.template.rest.get_letter_pdf_and_metadata', return_value=(pdf_content, {
|
||||
"message": "",
|
||||
"invalid_pages": "",
|
||||
"page_count": "1"
|
||||
}))
|
||||
|
||||
mocker.patch('app.template.rest.extract_page_from_pdf', return_value=pdf_content)
|
||||
|
||||
mock_post = request_mock.post(
|
||||
'http://localhost/notifications-template-preview/precompiled-preview.png',
|
||||
content=png_content,
|
||||
headers={'X-pdf-page-count': '1'},
|
||||
status_code=404
|
||||
)
|
||||
|
||||
admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type='png',
|
||||
_expected_status=500
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
mock_post.last_request.json()
|
||||
|
||||
|
||||
def test_preview_letter_template_precompiled_png_template_preview_pdf_error(
|
||||
notify_api,
|
||||
client,
|
||||
admin_request,
|
||||
sample_service,
|
||||
mocker
|
||||
):
|
||||
|
||||
template = create_template(sample_service,
|
||||
template_type='letter',
|
||||
template_name='Pre-compiled PDF',
|
||||
subject='Pre-compiled PDF',
|
||||
hidden=True)
|
||||
|
||||
notification = create_notification(template)
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'TEMPLATE_PREVIEW_API_HOST': 'http://localhost/notifications-template-preview',
|
||||
'TEMPLATE_PREVIEW_API_KEY': 'test-key'
|
||||
}):
|
||||
with requests_mock.Mocker() as request_mock:
|
||||
|
||||
pdf_content = b'\x00\x01'
|
||||
png_content = b'\x00\x02'
|
||||
|
||||
mocker.patch('app.template.rest.get_letter_pdf_and_metadata', return_value=(pdf_content, {
|
||||
"message": "",
|
||||
"invalid_pages": "",
|
||||
"page_count": "1"
|
||||
}))
|
||||
|
||||
error_message = "PDF Error message"
|
||||
mocker.patch('app.template.rest.extract_page_from_pdf', side_effect=PdfReadError(error_message))
|
||||
|
||||
request_mock.post(
|
||||
'http://localhost/notifications-template-preview/precompiled-preview.png',
|
||||
content=png_content,
|
||||
headers={'X-pdf-page-count': '1'},
|
||||
status_code=404
|
||||
)
|
||||
|
||||
request = admin_request.get(
|
||||
'template.preview_letter_template_by_notification_id',
|
||||
service_id=notification.service_id,
|
||||
notification_id=notification.id,
|
||||
file_type='png',
|
||||
_expected_status=500
|
||||
)
|
||||
|
||||
assert request['message'] == "Error extracting requested page from PDF file for notification_id {} type " \
|
||||
"{} {}".format(notification.id, type(PdfReadError()), error_message)
|
||||
|
||||
Reference in New Issue
Block a user