mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
make sure validation_failed notifications still return pdf
it's useful to see them, and previously it was crashing.
This commit is contained in:
@@ -6,7 +6,7 @@ from flask import current_app
|
|||||||
|
|
||||||
from notifications_utils.s3 import s3upload
|
from notifications_utils.s3 import s3upload
|
||||||
|
|
||||||
from app.models import KEY_TYPE_TEST, SECOND_CLASS, RESOLVE_POSTAGE_FOR_FILE_NAME
|
from app.models import KEY_TYPE_TEST, SECOND_CLASS, RESOLVE_POSTAGE_FOR_FILE_NAME, NOTIFICATION_VALIDATION_FAILED
|
||||||
from app.utils import convert_utc_to_bst
|
from app.utils import convert_utc_to_bst
|
||||||
|
|
||||||
|
|
||||||
@@ -48,14 +48,23 @@ def get_letter_pdf_filename(reference, crown, is_scan_letter=False, postage=SECO
|
|||||||
return upload_file_name
|
return upload_file_name
|
||||||
|
|
||||||
|
|
||||||
def get_bucket_prefix_for_notification(notification, is_test_letter=False):
|
def get_bucket_name_and_prefix_for_notification(notification):
|
||||||
|
is_test_letter = notification.key_type == KEY_TYPE_TEST and notification.template.is_precompiled_letter
|
||||||
|
folder = ''
|
||||||
|
if notification.status == NOTIFICATION_VALIDATION_FAILED:
|
||||||
|
bucket_name = current_app.config['INVALID_PDF_BUCKET_NAME']
|
||||||
|
elif is_test_letter:
|
||||||
|
bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME']
|
||||||
|
else:
|
||||||
|
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
||||||
|
folder = '{}/'.format(notification.created_at.date())
|
||||||
|
|
||||||
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
|
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
|
||||||
folder='' if is_test_letter else
|
folder=folder,
|
||||||
'{}/'.format(notification.created_at.date()),
|
|
||||||
reference=notification.reference
|
reference=notification.reference
|
||||||
).upper()
|
).upper()
|
||||||
|
|
||||||
return upload_file_name
|
return bucket_name, upload_file_name
|
||||||
|
|
||||||
|
|
||||||
def get_reference_from_filename(filename):
|
def get_reference_from_filename(filename):
|
||||||
@@ -122,18 +131,12 @@ def get_file_names_from_error_bucket():
|
|||||||
|
|
||||||
|
|
||||||
def get_letter_pdf(notification):
|
def get_letter_pdf(notification):
|
||||||
is_test_letter = notification.key_type == KEY_TYPE_TEST and notification.template.is_precompiled_letter
|
bucket_name, prefix = get_bucket_name_and_prefix_for_notification(notification)
|
||||||
if is_test_letter:
|
|
||||||
bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME']
|
|
||||||
else:
|
|
||||||
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
|
||||||
|
|
||||||
s3 = boto3.resource('s3')
|
s3 = boto3.resource('s3')
|
||||||
bucket = s3.Bucket(bucket_name)
|
bucket = s3.Bucket(bucket_name)
|
||||||
|
|
||||||
item = next(x for x in bucket.objects.filter(
|
item = next(x for x in bucket.objects.filter(Prefix=prefix))
|
||||||
Prefix=get_bucket_prefix_for_notification(notification, is_test_letter)
|
|
||||||
))
|
|
||||||
|
|
||||||
obj = s3.Object(
|
obj = s3.Object(
|
||||||
bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
|
|||||||
@@ -7,33 +7,39 @@ from freezegun import freeze_time
|
|||||||
from moto import mock_s3
|
from moto import mock_s3
|
||||||
|
|
||||||
from app.letters.utils import (
|
from app.letters.utils import (
|
||||||
get_bucket_prefix_for_notification,
|
get_bucket_name_and_prefix_for_notification,
|
||||||
get_letter_pdf_filename,
|
get_letter_pdf_filename,
|
||||||
get_letter_pdf,
|
get_letter_pdf,
|
||||||
upload_letter_pdf,
|
upload_letter_pdf,
|
||||||
ScanErrorType, move_failed_pdf, get_folder_name
|
ScanErrorType, move_failed_pdf, get_folder_name
|
||||||
)
|
)
|
||||||
from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEST, PRECOMPILED_TEMPLATE_NAME
|
from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEST, PRECOMPILED_TEMPLATE_NAME, NOTIFICATION_VALIDATION_FAILED
|
||||||
from tests.app.db import create_notification
|
from tests.app.db import create_notification
|
||||||
|
|
||||||
FROZEN_DATE_TIME = "2018-03-14 17:00:00"
|
FROZEN_DATE_TIME = "2018-03-14 17:00:00"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture(name='sample_precompiled_letter_notification')
|
||||||
def sample_precompiled_letter_notification_using_test_key(sample_letter_notification):
|
def _sample_precompiled_letter_notification(sample_letter_notification):
|
||||||
sample_letter_notification.template.hidden = True
|
sample_letter_notification.template.hidden = True
|
||||||
sample_letter_notification.template.name = PRECOMPILED_TEMPLATE_NAME
|
sample_letter_notification.template.name = PRECOMPILED_TEMPLATE_NAME
|
||||||
sample_letter_notification.key_type = KEY_TYPE_TEST
|
|
||||||
sample_letter_notification.reference = 'foo'
|
sample_letter_notification.reference = 'foo'
|
||||||
with freeze_time(FROZEN_DATE_TIME):
|
with freeze_time(FROZEN_DATE_TIME):
|
||||||
sample_letter_notification.created_at = datetime.utcnow()
|
sample_letter_notification.created_at = datetime.utcnow()
|
||||||
return sample_letter_notification
|
return sample_letter_notification
|
||||||
|
|
||||||
|
|
||||||
def test_get_bucket_prefix_for_notification_valid_notification(sample_notification):
|
@pytest.fixture(name='sample_precompiled_letter_notification_using_test_key')
|
||||||
|
def _sample_precompiled_letter_notification_using_test_key(sample_precompiled_letter_notification):
|
||||||
|
sample_precompiled_letter_notification.key_type = KEY_TYPE_TEST
|
||||||
|
return sample_precompiled_letter_notification
|
||||||
|
|
||||||
bucket_prefix = get_bucket_prefix_for_notification(sample_notification)
|
|
||||||
|
|
||||||
|
def test_get_bucket_name_and_prefix_for_notification_valid_notification(sample_notification):
|
||||||
|
|
||||||
|
bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification(sample_notification)
|
||||||
|
|
||||||
|
assert bucket == current_app.config['LETTERS_PDF_BUCKET_NAME']
|
||||||
assert bucket_prefix == '{folder}/NOTIFY.{reference}'.format(
|
assert bucket_prefix == '{folder}/NOTIFY.{reference}'.format(
|
||||||
folder=sample_notification.created_at.date(),
|
folder=sample_notification.created_at.date(),
|
||||||
reference=sample_notification.reference
|
reference=sample_notification.reference
|
||||||
@@ -41,19 +47,44 @@ def test_get_bucket_prefix_for_notification_valid_notification(sample_notificati
|
|||||||
|
|
||||||
|
|
||||||
@freeze_time(FROZEN_DATE_TIME)
|
@freeze_time(FROZEN_DATE_TIME)
|
||||||
def test_get_bucket_prefix_for_notification_precompiled_letter_using_test_key(
|
def test_get_bucket_name_and_prefix_for_notification_precompiled_letter_using_test_key(
|
||||||
sample_precompiled_letter_notification_using_test_key
|
sample_precompiled_letter_notification_using_test_key
|
||||||
):
|
):
|
||||||
bucket_prefix = get_bucket_prefix_for_notification(
|
bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification(
|
||||||
sample_precompiled_letter_notification_using_test_key, is_test_letter=True)
|
sample_precompiled_letter_notification_using_test_key)
|
||||||
|
|
||||||
|
assert bucket == current_app.config['TEST_LETTERS_BUCKET_NAME']
|
||||||
assert bucket_prefix == 'NOTIFY.{}'.format(
|
assert bucket_prefix == 'NOTIFY.{}'.format(
|
||||||
sample_precompiled_letter_notification_using_test_key.reference).upper()
|
sample_precompiled_letter_notification_using_test_key.reference).upper()
|
||||||
|
|
||||||
|
|
||||||
def test_get_bucket_prefix_for_notification_invalid_notification():
|
@freeze_time(FROZEN_DATE_TIME)
|
||||||
|
def test_get_bucket_name_and_prefix_for_failed_validation(sample_precompiled_letter_notification):
|
||||||
|
sample_precompiled_letter_notification.status = NOTIFICATION_VALIDATION_FAILED
|
||||||
|
bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification(sample_precompiled_letter_notification)
|
||||||
|
|
||||||
|
assert bucket == current_app.config['INVALID_PDF_BUCKET_NAME']
|
||||||
|
assert bucket_prefix == 'NOTIFY.{}'.format(
|
||||||
|
sample_precompiled_letter_notification.reference).upper()
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time(FROZEN_DATE_TIME)
|
||||||
|
def test_get_bucket_name_and_prefix_for_test_noti_with_failed_validation(
|
||||||
|
sample_precompiled_letter_notification_using_test_key
|
||||||
|
):
|
||||||
|
sample_precompiled_letter_notification_using_test_key.status = NOTIFICATION_VALIDATION_FAILED
|
||||||
|
bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification(
|
||||||
|
sample_precompiled_letter_notification_using_test_key
|
||||||
|
)
|
||||||
|
|
||||||
|
assert bucket == current_app.config['INVALID_PDF_BUCKET_NAME']
|
||||||
|
assert bucket_prefix == 'NOTIFY.{}'.format(
|
||||||
|
sample_precompiled_letter_notification_using_test_key.reference).upper()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_bucket_name_and_prefix_for_notification_invalid_notification():
|
||||||
with pytest.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
get_bucket_prefix_for_notification(None)
|
get_bucket_name_and_prefix_for_notification(None)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('crown_flag,expected_crown_text', [
|
@pytest.mark.parametrize('crown_flag,expected_crown_text', [
|
||||||
|
|||||||
Reference in New Issue
Block a user