mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 18:38:14 -04:00
Merge pull request #1725 from alphagov/rc_add_get_template_preview
Add get template preview
This commit is contained in:
+53
-5
@@ -1,10 +1,12 @@
|
|||||||
|
import base64
|
||||||
from flask import (
|
from flask import (
|
||||||
Blueprint,
|
Blueprint,
|
||||||
|
current_app,
|
||||||
jsonify,
|
jsonify,
|
||||||
request,
|
request)
|
||||||
current_app
|
from requests import post as requests_post
|
||||||
)
|
|
||||||
|
|
||||||
|
from app.dao.notifications_dao import get_notification_by_id
|
||||||
from app.dao.templates_dao import (
|
from app.dao.templates_dao import (
|
||||||
dao_update_template,
|
dao_update_template,
|
||||||
dao_create_template,
|
dao_create_template,
|
||||||
@@ -12,8 +14,8 @@ from app.dao.templates_dao import (
|
|||||||
dao_get_template_by_id_and_service_id,
|
dao_get_template_by_id_and_service_id,
|
||||||
dao_get_all_templates_for_service,
|
dao_get_all_templates_for_service,
|
||||||
dao_get_template_versions,
|
dao_get_template_versions,
|
||||||
dao_update_template_reply_to
|
dao_update_template_reply_to,
|
||||||
)
|
dao_get_template_by_id)
|
||||||
from notifications_utils.template import SMSMessageTemplate
|
from notifications_utils.template import SMSMessageTemplate
|
||||||
from app.dao.services_dao import dao_fetch_service_by_id
|
from app.dao.services_dao import dao_fetch_service_by_id
|
||||||
from app.models import SMS_TYPE
|
from app.models import SMS_TYPE
|
||||||
@@ -179,3 +181,49 @@ def redact_template(template, data):
|
|||||||
if not template.redact_personalisation:
|
if not template.redact_personalisation:
|
||||||
dao_redact_template(template, data['created_by'])
|
dao_redact_template(template, data['created_by'])
|
||||||
return 'null', 200
|
return 'null', 200
|
||||||
|
|
||||||
|
|
||||||
|
@template_blueprint.route('/preview/<uuid:notification_id>/<file_type>', methods=['GET'])
|
||||||
|
def preview_letter_template_by_notification_id(service_id, notification_id, file_type):
|
||||||
|
if file_type not in ('pdf', 'png'):
|
||||||
|
raise InvalidRequest({'content': ["file_type must be pdf or png"]}, status_code=400)
|
||||||
|
|
||||||
|
page = request.args.get('page')
|
||||||
|
|
||||||
|
notification = get_notification_by_id(notification_id)
|
||||||
|
|
||||||
|
template = dao_get_template_by_id(notification.template_id)
|
||||||
|
|
||||||
|
template_for_letter_print = {
|
||||||
|
"id": str(notification.template_id),
|
||||||
|
"subject": template.subject,
|
||||||
|
"content": template.content,
|
||||||
|
"version": str(template.version)
|
||||||
|
}
|
||||||
|
|
||||||
|
service = dao_fetch_service_by_id(service_id)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'letter_contact_block': notification.reply_to_text,
|
||||||
|
'template': template_for_letter_print,
|
||||||
|
'values': notification.personalisation,
|
||||||
|
'dvla_org_id': service.dvla_organisation_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = requests_post(
|
||||||
|
'{}/preview.{}{}'.format(
|
||||||
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||||
|
file_type,
|
||||||
|
'?page={}'.format(page) if page else ''
|
||||||
|
),
|
||||||
|
json=data,
|
||||||
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp.status_code != 200:
|
||||||
|
raise InvalidRequest(
|
||||||
|
'Error generating preview for {}'.format(notification_id), status_code=500
|
||||||
|
)
|
||||||
|
|
||||||
|
content = base64.b64encode(resp.content).decode('utf-8')
|
||||||
|
return jsonify({"content": content})
|
||||||
|
|||||||
@@ -157,7 +157,6 @@ def test_create_letters_pdf_handles_s3_errors(mocker, sample_letter_notification
|
|||||||
'Type': 'Sender'
|
'Type': 'Sender'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mock_s3 = mocker.patch('app.letters.utils.s3upload', side_effect=ClientError(error_response, 'operation_name'))
|
mock_s3 = mocker.patch('app.letters.utils.s3upload', side_effect=ClientError(error_response, 'operation_name'))
|
||||||
mock_retry = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.retry')
|
mock_retry = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.retry')
|
||||||
|
|
||||||
|
|||||||
@@ -1258,7 +1258,6 @@ def test_build_dvla_file_retries_if_s3_err(sample_letter_template, mocker):
|
|||||||
'Type': 'Sender'
|
'Type': 'Sender'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mocker.patch('app.celery.tasks.LetterDVLATemplate', return_value='dvla|string')
|
mocker.patch('app.celery.tasks.LetterDVLATemplate', return_value='dvla|string')
|
||||||
mocker.patch('app.celery.tasks.s3upload', side_effect=ClientError(error_response, 'operation_name'))
|
mocker.patch('app.celery.tasks.s3upload', side_effect=ClientError(error_response, 'operation_name'))
|
||||||
retry_mock = mocker.patch('app.celery.tasks.build_dvla_file.retry', side_effect=Retry)
|
retry_mock = mocker.patch('app.celery.tasks.build_dvla_file.retry', side_effect=Retry)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import base64
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
@@ -14,9 +15,9 @@ from tests.app.conftest import (
|
|||||||
sample_template as create_sample_template,
|
sample_template as create_sample_template,
|
||||||
sample_template_without_email_permission,
|
sample_template_without_email_permission,
|
||||||
sample_template_without_letter_permission,
|
sample_template_without_letter_permission,
|
||||||
sample_template_without_sms_permission,
|
sample_template_without_sms_permission)
|
||||||
)
|
|
||||||
from tests.app.db import create_service, create_letter_contact, create_template
|
from tests.app.db import create_service, create_letter_contact, create_template
|
||||||
|
from tests.conftest import set_config_values
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('template_type, subject', [
|
@pytest.mark.parametrize('template_type, subject', [
|
||||||
@@ -794,3 +795,82 @@ def test_update_redact_template_400s_if_no_created_by(admin_request, sample_temp
|
|||||||
|
|
||||||
assert sample_template.redact_personalisation is False
|
assert sample_template.redact_personalisation is False
|
||||||
assert sample_template.template_redacted.updated_at == original_updated_time
|
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']
|
||||||
|
|
||||||
|
|
||||||
|
def test_preview_letter_template_by_id_valid_file_type(
|
||||||
|
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'
|
||||||
|
|
||||||
|
request_mock.post(
|
||||||
|
'http://localhost/notifications-template-preview/preview.pdf',
|
||||||
|
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='pdf'
|
||||||
|
)
|
||||||
|
|
||||||
|
assert base64.b64decode(resp['content']) == content
|
||||||
|
|
||||||
|
|
||||||
|
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'
|
||||||
|
|
||||||
|
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 resp['message'] == 'Error generating preview for {}'.format(sample_letter_notification.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user