mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Update letter utils to handle precompiled letters sent by test API keys
- precompiled letters sent using a test key should be put into a test-letters bucket - also refactored code from test_letter_pdf_tasks
This commit is contained in:
@@ -9,20 +9,27 @@ from app.variables import Retention
|
|||||||
|
|
||||||
|
|
||||||
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
|
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
|
||||||
'{folder}/NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
'{folder}NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
|
||||||
|
|
||||||
PRECOMPILED_BUCKET_PREFIX = '{folder}/NOTIFY.{reference}'
|
PRECOMPILED_BUCKET_PREFIX = '{folder}/NOTIFY.{reference}'
|
||||||
|
|
||||||
|
|
||||||
def get_letter_pdf_filename(reference, crown):
|
def get_folder_name(_now, is_test_letter):
|
||||||
|
if is_test_letter:
|
||||||
|
folder_name = ''
|
||||||
|
else:
|
||||||
|
print_datetime = _now
|
||||||
|
if _now.time() > current_app.config.get('LETTER_PROCESSING_DEADLINE'):
|
||||||
|
print_datetime = _now + timedelta(days=1)
|
||||||
|
folder_name = '{}/'.format(print_datetime.date())
|
||||||
|
return folder_name
|
||||||
|
|
||||||
|
|
||||||
|
def get_letter_pdf_filename(reference, crown, is_test_letter=False):
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
|
|
||||||
print_datetime = now
|
|
||||||
if now.time() > current_app.config.get('LETTER_PROCESSING_DEADLINE'):
|
|
||||||
print_datetime = now + timedelta(days=1)
|
|
||||||
|
|
||||||
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
||||||
folder=print_datetime.date(),
|
folder=get_folder_name(now, is_test_letter),
|
||||||
reference=reference,
|
reference=reference,
|
||||||
duplex="D",
|
duplex="D",
|
||||||
letter_class="2",
|
letter_class="2",
|
||||||
@@ -43,23 +50,28 @@ def get_bucket_prefix_for_notification(notification):
|
|||||||
return upload_file_name
|
return upload_file_name
|
||||||
|
|
||||||
|
|
||||||
def upload_letter_pdf(notification, pdf_data):
|
def upload_letter_pdf(notification, pdf_data, is_test_letter=False):
|
||||||
current_app.logger.info("PDF Letter {} reference {} created at {}, {} bytes".format(
|
current_app.logger.info("PDF Letter {} reference {} created at {}, {} bytes".format(
|
||||||
notification.id, notification.reference, notification.created_at, len(pdf_data)))
|
notification.id, notification.reference, notification.created_at, len(pdf_data)))
|
||||||
|
|
||||||
upload_file_name = get_letter_pdf_filename(
|
upload_file_name = get_letter_pdf_filename(
|
||||||
notification.reference, notification.service.crown)
|
notification.reference, notification.service.crown, is_test_letter)
|
||||||
|
|
||||||
|
if is_test_letter:
|
||||||
|
bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME']
|
||||||
|
else:
|
||||||
|
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
||||||
|
|
||||||
s3upload(
|
s3upload(
|
||||||
filedata=pdf_data,
|
filedata=pdf_data,
|
||||||
region=current_app.config['AWS_REGION'],
|
region=current_app.config['AWS_REGION'],
|
||||||
bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
bucket_name=bucket_name,
|
||||||
file_location=upload_file_name,
|
file_location=upload_file_name,
|
||||||
tags={Retention.KEY: Retention.ONE_WEEK}
|
tags={Retention.KEY: Retention.ONE_WEEK}
|
||||||
)
|
)
|
||||||
|
|
||||||
current_app.logger.info("Uploaded letters PDF {} to {} for notification id {}".format(
|
current_app.logger.info("Uploaded letters PDF {} to {} for notification id {}".format(
|
||||||
upload_file_name, current_app.config['LETTERS_PDF_BUCKET_NAME'], notification.id))
|
upload_file_name, bucket_name, notification.id))
|
||||||
|
|
||||||
|
|
||||||
def get_letter_pdf(notification):
|
def get_letter_pdf(notification):
|
||||||
|
|||||||
@@ -28,25 +28,6 @@ def test_should_have_decorated_tasks_functions():
|
|||||||
assert create_letters_pdf.__wrapped__.__name__ == 'create_letters_pdf'
|
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])
|
@pytest.mark.parametrize('personalisation', [{'name': 'test'}, None])
|
||||||
def test_get_letters_pdf_calls_notifications_template_preview_service_correctly(
|
def test_get_letters_pdf_calls_notifications_template_preview_service_correctly(
|
||||||
notify_api, mocker, client, sample_letter_template, personalisation):
|
notify_api, mocker, client, sample_letter_template, personalisation):
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from app.letters.utils import get_bucket_prefix_for_notification
|
from freezegun import freeze_time
|
||||||
|
|
||||||
|
from app.letters.utils import get_bucket_prefix_for_notification, get_letter_pdf_filename
|
||||||
|
|
||||||
|
|
||||||
def test_get_bucket_prefix_for_notification_valid_notification(sample_notification):
|
def test_get_bucket_prefix_for_notification_valid_notification(sample_notification):
|
||||||
@@ -16,3 +18,30 @@ def test_get_bucket_prefix_for_notification_valid_notification(sample_notificati
|
|||||||
def test_get_bucket_prefix_for_notification_invalid_notification():
|
def test_get_bucket_prefix_for_notification_invalid_notification():
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
get_bucket_prefix_for_notification(None)
|
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'
|
||||||
|
|||||||
Reference in New Issue
Block a user