Refactor to move preview logic to API

* Moved the notifications code to go to admin to get the the template

preview document rather than go to template preview.

This will remove the logic from admin and place it in api so it is
easier to expand on later when there are precompiled PDFs

* Added some error handling if API returns an API error.

Caught the error and displayed an error PNG so it is obvious something
failed. Currently it displayed a thumbnail of a png over the top of the
loading page, and therefore it wasn't obvious of the state.
This commit is contained in:
Ken Tsang
2018-03-08 12:25:07 +00:00
parent 61b8c9040a
commit 4628b99445
4 changed files with 66 additions and 22 deletions

View File

@@ -1,9 +1,11 @@
import base64
from functools import partial
from unittest.mock import mock_open
import pytest
from flask import url_for
from freezegun import freeze_time
from notifications_utils.template import LetterImageTemplate
from notifications_python_client.errors import APIError
from tests.conftest import (
SERVICE_ONE_ID,
mock_get_notification,
@@ -163,9 +165,11 @@ def test_should_show_image_of_letter_notification(
mock_get_notification(mocker, fake_uuid, template_type='letter')
mocked_preview = mocker.patch(
'app.main.views.templates.TemplatePreview.from_utils_template',
return_value='foo'
mocker.patch(
'app.notify_client.notification_api_client.NotificationApiClient.get',
return_value={
'content': base64.b64encode(b'foo').decode('utf-8')
}
)
response = logged_in_client.get(url_for(
@@ -177,8 +181,32 @@ def test_should_show_image_of_letter_notification(
assert response.status_code == 200
assert response.get_data(as_text=True) == 'foo'
assert isinstance(mocked_preview.call_args[0][0], LetterImageTemplate)
assert mocked_preview.call_args[0][1] == filetype
def test_should_show_preview_error_image_letter_notification_on_preview_error(
logged_in_client,
fake_uuid,
mocker,
):
mock_get_notification(mocker, fake_uuid, template_type='letter')
mocker.patch(
'app.notify_client.notification_api_client.NotificationApiClient.get',
side_effect=APIError
)
mocker.patch("builtins.open", mock_open(read_data="preview error image"))
response = logged_in_client.get(url_for(
'main.view_letter_notification_as_preview',
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
filetype='png'
))
assert response.status_code == 200
assert response.get_data(as_text=True) == 'preview error image'
def test_should_404_for_unknown_extension(