mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Updated API to get it working with Admin.
* Added missing items from template which are required * Returned the file as a JSON string with the file as a base64 encoded string * Updated tests to match teh desired format
This commit is contained in:
committed by
Ken Tsang
parent
4c8bc9f430
commit
42c1040604
@@ -1,9 +1,10 @@
|
|||||||
|
import base64
|
||||||
from flask import (
|
from flask import (
|
||||||
Blueprint,
|
Blueprint,
|
||||||
current_app,
|
current_app,
|
||||||
jsonify,
|
jsonify,
|
||||||
request
|
request)
|
||||||
)
|
from requests import post as requests_post
|
||||||
|
|
||||||
from app.dao.notifications_dao import get_notification_by_id
|
from app.dao.notifications_dao import get_notification_by_id
|
||||||
from app.dao.templates_dao import (
|
from app.dao.templates_dao import (
|
||||||
@@ -182,23 +183,24 @@ def redact_template(template, data):
|
|||||||
return 'null', 200
|
return 'null', 200
|
||||||
|
|
||||||
|
|
||||||
@template_blueprint.route('/<uuid:template_id>/pdf-preview/<uuid:notification_id>/<file_type>', methods=['GET'])
|
@template_blueprint.route('/preview/<uuid:notification_id>/<file_type>', methods=['GET'])
|
||||||
def preview_letter_template_by_notification_id(service_id, template_id, notification_id, file_type):
|
def preview_letter_template_by_notification_id(service_id, notification_id, file_type):
|
||||||
|
|
||||||
if file_type not in ('pdf', 'png'):
|
if file_type not in ('pdf', 'png'):
|
||||||
raise InvalidRequest({'content': ["file_type must be pdf or png"]}, status_code=404)
|
raise InvalidRequest({'content': ["file_type must be pdf or png"]}, status_code=400)
|
||||||
|
|
||||||
page = request.args.get('page')
|
page = request.args.get('page')
|
||||||
|
|
||||||
template = dao_get_template_by_id(template_id)
|
notification = get_notification_by_id(notification_id)
|
||||||
|
|
||||||
|
template = dao_get_template_by_id(notification.template_id)
|
||||||
|
|
||||||
template_for_letter_print = {
|
template_for_letter_print = {
|
||||||
|
"id": str(notification.template_id),
|
||||||
"subject": template.subject,
|
"subject": template.subject,
|
||||||
"content": template.content
|
"content": template.content,
|
||||||
|
"version": str(template.version)
|
||||||
}
|
}
|
||||||
|
|
||||||
notification = get_notification_by_id(notification_id)
|
|
||||||
|
|
||||||
service = dao_fetch_service_by_id(service_id)
|
service = dao_fetch_service_by_id(service_id)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
@@ -208,7 +210,6 @@ def preview_letter_template_by_notification_id(service_id, template_id, notifica
|
|||||||
'dvla_org_id': service.dvla_organisation_id,
|
'dvla_org_id': service.dvla_organisation_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
from requests import (post as requests_post)
|
|
||||||
resp = requests_post(
|
resp = requests_post(
|
||||||
'{}/preview.{}{}'.format(
|
'{}/preview.{}{}'.format(
|
||||||
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
current_app.config['TEMPLATE_PREVIEW_API_HOST'],
|
||||||
@@ -219,4 +220,10 @@ def preview_letter_template_by_notification_id(service_id, template_id, notifica
|
|||||||
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
headers={'Authorization': 'Token {}'.format(current_app.config['TEMPLATE_PREVIEW_API_KEY'])}
|
||||||
)
|
)
|
||||||
|
|
||||||
return resp.content, resp.status_code, resp.headers.items()
|
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
|
||||||
@@ -6,7 +7,6 @@ from datetime import datetime, timedelta
|
|||||||
import pytest
|
import pytest
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from app.errors import InvalidRequest
|
|
||||||
from app.models import Template, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, TemplateHistory
|
from app.models import Template, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, TemplateHistory
|
||||||
from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template
|
from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template
|
||||||
|
|
||||||
@@ -798,25 +798,25 @@ def test_update_redact_template_400s_if_no_created_by(admin_request, sample_temp
|
|||||||
|
|
||||||
|
|
||||||
def test_preview_letter_template_by_id_invalid_file_type(
|
def test_preview_letter_template_by_id_invalid_file_type(
|
||||||
sample_service,
|
sample_letter_notification,
|
||||||
sample_letter_template,
|
admin_request):
|
||||||
sample_letter_notification):
|
|
||||||
|
|
||||||
with pytest.raises(InvalidRequest) as exc:
|
resp = admin_request.get(
|
||||||
from app.template.rest import preview_letter_template_by_notification_id
|
'template.preview_letter_template_by_notification_id',
|
||||||
preview_letter_template_by_notification_id(
|
service_id=sample_letter_notification.service_id,
|
||||||
sample_service.id,
|
template_id=sample_letter_notification.template_id,
|
||||||
sample_letter_template.id,
|
notification_id=sample_letter_notification.id,
|
||||||
sample_letter_notification.id,
|
file_type='doc',
|
||||||
'doc')
|
_expected_status=400
|
||||||
assert 'file_type must be pdf or png' in str(exc.value)
|
)
|
||||||
|
|
||||||
|
assert ['file_type must be pdf or png'] == resp['message']['content']
|
||||||
|
|
||||||
|
|
||||||
def test_preview_letter_template_by_id_valid_file_type(
|
def test_preview_letter_template_by_id_valid_file_type(
|
||||||
notify_api,
|
notify_api,
|
||||||
client,
|
client,
|
||||||
sample_service,
|
admin_request,
|
||||||
sample_letter_template,
|
|
||||||
sample_letter_notification):
|
sample_letter_notification):
|
||||||
|
|
||||||
with set_config_values(notify_api, {
|
with set_config_values(notify_api, {
|
||||||
@@ -834,25 +834,20 @@ def test_preview_letter_template_by_id_valid_file_type(
|
|||||||
status_code=200
|
status_code=200
|
||||||
)
|
)
|
||||||
|
|
||||||
from app.template.rest import preview_letter_template_by_notification_id
|
resp = admin_request.get(
|
||||||
content, status_code, items = preview_letter_template_by_notification_id(
|
'template.preview_letter_template_by_notification_id',
|
||||||
sample_service.id,
|
service_id=sample_letter_notification.service_id,
|
||||||
sample_letter_template.id,
|
notification_id=sample_letter_notification.id,
|
||||||
sample_letter_notification.id,
|
file_type='pdf'
|
||||||
'pdf'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert status_code == 200
|
assert base64.b64decode(resp['content']) == content
|
||||||
assert content == content
|
|
||||||
assert list(items)[0][0] == 'X-pdf-page-count'
|
|
||||||
assert list(items)[0][1] == '1'
|
|
||||||
|
|
||||||
|
|
||||||
def test_preview_letter_template_by_id_template_preview_404(
|
def test_preview_letter_template_by_id_template_preview_500(
|
||||||
notify_api,
|
notify_api,
|
||||||
client,
|
client,
|
||||||
sample_service,
|
admin_request,
|
||||||
sample_letter_template,
|
|
||||||
sample_letter_notification):
|
sample_letter_notification):
|
||||||
|
|
||||||
with set_config_values(notify_api, {
|
with set_config_values(notify_api, {
|
||||||
@@ -870,12 +865,12 @@ def test_preview_letter_template_by_id_template_preview_404(
|
|||||||
status_code=404
|
status_code=404
|
||||||
)
|
)
|
||||||
|
|
||||||
from app.template.rest import preview_letter_template_by_notification_id
|
resp = admin_request.get(
|
||||||
content, status_code, items = preview_letter_template_by_notification_id(
|
'template.preview_letter_template_by_notification_id',
|
||||||
sample_service.id,
|
service_id=sample_letter_notification.service_id,
|
||||||
sample_letter_template.id,
|
notification_id=sample_letter_notification.id,
|
||||||
sample_letter_notification.id,
|
file_type='pdf',
|
||||||
'pdf'
|
_expected_status=500
|
||||||
)
|
)
|
||||||
|
|
||||||
assert status_code == 404
|
assert resp['message'] == 'Error generating preview for {}'.format(sample_letter_notification.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user