mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -05:00
Merge pull request #1773 from alphagov/ken-preview-precompiled-letters-test-key
Preview precompiled letters sent using an API test key
This commit is contained in:
@@ -28,25 +28,6 @@ def test_should_have_decorated_tasks_functions():
|
||||
assert create_letters_pdf.__wrapped__.__name__ == 'create_letters_pdf'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('crown_flag,expected_crown_text', [
|
||||
(True, 'C'),
|
||||
(False, 'N'),
|
||||
])
|
||||
@freeze_time("2017-12-04 17:29:00")
|
||||
def test_get_letter_pdf_filename_returns_correct_filename(
|
||||
notify_api, mocker, crown_flag, expected_crown_text):
|
||||
filename = get_letter_pdf_filename(reference='foo', crown=crown_flag)
|
||||
|
||||
assert filename == '2017-12-04/NOTIFY.FOO.D.2.C.{}.20171204172900.PDF'.format(expected_crown_text)
|
||||
|
||||
|
||||
@freeze_time("2017-12-04 17:31:00")
|
||||
def test_get_letter_pdf_filename_returns_tomorrows_filename(notify_api, mocker):
|
||||
filename = get_letter_pdf_filename(reference='foo', crown=True)
|
||||
|
||||
assert filename == '2017-12-05/NOTIFY.FOO.D.2.C.C.20171204173100.PDF'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('personalisation', [{'name': 'test'}, None])
|
||||
def test_get_letters_pdf_calls_notifications_template_preview_service_correctly(
|
||||
notify_api, mocker, client, sample_letter_template, personalisation):
|
||||
|
||||
@@ -1,6 +1,26 @@
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
|
||||
from app.letters.utils import get_bucket_prefix_for_notification
|
||||
import boto3
|
||||
from flask import current_app
|
||||
from freezegun import freeze_time
|
||||
from moto import mock_s3
|
||||
|
||||
from app.letters.utils import get_bucket_prefix_for_notification, get_letter_pdf_filename, get_letter_pdf
|
||||
from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEST, PRECOMPILED_TEMPLATE_NAME
|
||||
|
||||
FROZEN_DATE_TIME = "2018-03-14 17:00:00"
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@freeze_time(FROZEN_DATE_TIME)
|
||||
def sample_precompiled_letter_notification_using_test_key(sample_letter_notification):
|
||||
sample_letter_notification.template.hidden = True
|
||||
sample_letter_notification.template.name = PRECOMPILED_TEMPLATE_NAME
|
||||
sample_letter_notification.key_type = KEY_TYPE_TEST
|
||||
sample_letter_notification.reference = 'foo'
|
||||
sample_letter_notification.created_at = datetime.utcnow()
|
||||
return sample_letter_notification
|
||||
|
||||
|
||||
def test_get_bucket_prefix_for_notification_valid_notification(sample_notification):
|
||||
@@ -13,6 +33,70 @@ def test_get_bucket_prefix_for_notification_valid_notification(sample_notificati
|
||||
).upper()
|
||||
|
||||
|
||||
@freeze_time(FROZEN_DATE_TIME)
|
||||
def test_get_bucket_prefix_for_notification_precompiled_letter_using_test_key(
|
||||
sample_precompiled_letter_notification_using_test_key
|
||||
):
|
||||
bucket_prefix = get_bucket_prefix_for_notification(
|
||||
sample_precompiled_letter_notification_using_test_key, is_test_letter=True)
|
||||
|
||||
assert bucket_prefix == 'NOTIFY.{}'.format(
|
||||
sample_precompiled_letter_notification_using_test_key.reference).upper()
|
||||
|
||||
|
||||
def test_get_bucket_prefix_for_notification_invalid_notification():
|
||||
with pytest.raises(AttributeError):
|
||||
get_bucket_prefix_for_notification(None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('crown_flag,expected_crown_text', [
|
||||
(True, 'C'),
|
||||
(False, 'N'),
|
||||
])
|
||||
@freeze_time("2017-12-04 17:29:00")
|
||||
def test_get_letter_pdf_filename_returns_correct_filename(
|
||||
notify_api, mocker, crown_flag, expected_crown_text):
|
||||
filename = get_letter_pdf_filename(reference='foo', crown=crown_flag)
|
||||
|
||||
assert filename == '2017-12-04/NOTIFY.FOO.D.2.C.{}.20171204172900.PDF'.format(expected_crown_text)
|
||||
|
||||
|
||||
@freeze_time("2017-12-04 17:29:00")
|
||||
def test_get_letter_pdf_filename_returns_correct_filename_for_test_letters(
|
||||
notify_api, mocker):
|
||||
filename = get_letter_pdf_filename(reference='foo', crown='C', is_test_letter=True)
|
||||
|
||||
assert filename == 'NOTIFY.FOO.D.2.C.C.20171204172900.PDF'
|
||||
|
||||
|
||||
@freeze_time("2017-12-04 17:31:00")
|
||||
def test_get_letter_pdf_filename_returns_tomorrows_filename(notify_api, mocker):
|
||||
filename = get_letter_pdf_filename(reference='foo', crown=True)
|
||||
|
||||
assert filename == '2017-12-05/NOTIFY.FOO.D.2.C.C.20171204173100.PDF'
|
||||
|
||||
|
||||
@mock_s3
|
||||
@pytest.mark.parametrize('bucket_config_name,filename_format', [
|
||||
('TEST_LETTERS_BUCKET_NAME', 'NOTIFY.FOO.D.2.C.C.%Y%m%d%H%M%S.PDF'),
|
||||
('LETTERS_PDF_BUCKET_NAME', '%Y-%m-%d/NOTIFY.FOO.D.2.C.C.%Y%m%d%H%M%S.PDF')
|
||||
])
|
||||
@freeze_time(FROZEN_DATE_TIME)
|
||||
def test_get_letter_pdf_gets_pdf_from_correct_bucket(
|
||||
sample_precompiled_letter_notification_using_test_key,
|
||||
bucket_config_name,
|
||||
filename_format
|
||||
):
|
||||
if bucket_config_name == 'LETTERS_PDF_BUCKET_NAME':
|
||||
sample_precompiled_letter_notification_using_test_key.key_type = KEY_TYPE_NORMAL
|
||||
|
||||
bucket_name = current_app.config[bucket_config_name]
|
||||
filename = datetime.utcnow().strftime(filename_format)
|
||||
conn = boto3.resource('s3', region_name='eu-west-1')
|
||||
conn.create_bucket(Bucket=bucket_name)
|
||||
s3 = boto3.client('s3', region_name='eu-west-1')
|
||||
s3.put_object(Bucket=bucket_name, Key=filename, Body=b'pdf_content')
|
||||
|
||||
ret = get_letter_pdf(sample_precompiled_letter_notification_using_test_key)
|
||||
|
||||
assert ret == b'pdf_content'
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import uuid
|
||||
from unittest.mock import ANY
|
||||
|
||||
from flask import json
|
||||
from flask import url_for
|
||||
@@ -29,9 +30,13 @@ test_address = {
|
||||
}
|
||||
|
||||
|
||||
def letter_request(client, data, service_id, key_type=KEY_TYPE_NORMAL, _expected_status=201):
|
||||
def letter_request(client, data, service_id, key_type=KEY_TYPE_NORMAL, _expected_status=201, precompiled=False):
|
||||
if precompiled:
|
||||
url = url_for('v2_notifications.post_precompiled_letter_notification')
|
||||
else:
|
||||
url = url_for('v2_notifications.post_notification', notification_type=LETTER_TYPE)
|
||||
resp = client.post(
|
||||
url_for('v2_notifications.post_notification', notification_type=LETTER_TYPE),
|
||||
url,
|
||||
data=json.dumps(data),
|
||||
headers=[
|
||||
('Content-Type', 'application/json'),
|
||||
@@ -382,6 +387,30 @@ def test_post_letter_notification_is_delivered_if_in_trial_mode_and_using_test_k
|
||||
assert not fake_create_letter_task.called
|
||||
|
||||
|
||||
def test_post_letter_notification_is_delivered_and_has_pdf_uploaded_to_test_letters_bucket_using_test_key(
|
||||
client,
|
||||
notify_user,
|
||||
mocker
|
||||
):
|
||||
sample_letter_service = create_service(service_permissions=['letter', 'precompiled_letter'])
|
||||
s3mock = mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf')
|
||||
mocker.patch('app.v2.notifications.post_notifications.pdf_page_count', return_value=1)
|
||||
data = {
|
||||
"reference": "letter-reference",
|
||||
"content": "bGV0dGVyLWNvbnRlbnQ="
|
||||
}
|
||||
letter_request(
|
||||
client,
|
||||
data=data,
|
||||
service_id=str(sample_letter_service.id),
|
||||
key_type=KEY_TYPE_TEST,
|
||||
precompiled=True)
|
||||
|
||||
notification = Notification.query.one()
|
||||
assert notification.status == NOTIFICATION_DELIVERED
|
||||
s3mock.assert_called_once_with(ANY, b'letter-content', is_test_letter=True)
|
||||
|
||||
|
||||
def test_post_letter_notification_persists_notification_reply_to_text(
|
||||
client, notify_db_session, mocker
|
||||
):
|
||||
|
||||
Reference in New Issue
Block a user