diff --git a/app/letters/utils.py b/app/letters/utils.py index 57395fc87..fc84b50ac 100644 --- a/app/letters/utils.py +++ b/app/letters/utils.py @@ -6,7 +6,7 @@ from flask import current_app 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 @@ -48,14 +48,23 @@ def get_letter_pdf_filename(reference, crown, is_scan_letter=False, postage=SECO 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( - folder='' if is_test_letter else - '{}/'.format(notification.created_at.date()), + folder=folder, reference=notification.reference ).upper() - return upload_file_name + return bucket_name, upload_file_name def get_reference_from_filename(filename): @@ -122,18 +131,12 @@ def get_file_names_from_error_bucket(): def get_letter_pdf(notification): - is_test_letter = notification.key_type == KEY_TYPE_TEST and notification.template.is_precompiled_letter - if is_test_letter: - bucket_name = current_app.config['TEST_LETTERS_BUCKET_NAME'] - else: - bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME'] + bucket_name, prefix = get_bucket_name_and_prefix_for_notification(notification) s3 = boto3.resource('s3') bucket = s3.Bucket(bucket_name) - item = next(x for x in bucket.objects.filter( - Prefix=get_bucket_prefix_for_notification(notification, is_test_letter) - )) + item = next(x for x in bucket.objects.filter(Prefix=prefix)) obj = s3.Object( bucket_name=bucket_name, diff --git a/tests/app/letters/test_letter_utils.py b/tests/app/letters/test_letter_utils.py index e7068dc9f..f2077b786 100644 --- a/tests/app/letters/test_letter_utils.py +++ b/tests/app/letters/test_letter_utils.py @@ -7,33 +7,39 @@ from freezegun import freeze_time from moto import mock_s3 from app.letters.utils import ( - get_bucket_prefix_for_notification, + get_bucket_name_and_prefix_for_notification, get_letter_pdf_filename, get_letter_pdf, upload_letter_pdf, 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 FROZEN_DATE_TIME = "2018-03-14 17:00:00" -@pytest.fixture() -def sample_precompiled_letter_notification_using_test_key(sample_letter_notification): +@pytest.fixture(name='sample_precompiled_letter_notification') +def _sample_precompiled_letter_notification(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' with freeze_time(FROZEN_DATE_TIME): sample_letter_notification.created_at = datetime.utcnow() 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( folder=sample_notification.created_at.date(), reference=sample_notification.reference @@ -41,19 +47,44 @@ def test_get_bucket_prefix_for_notification_valid_notification(sample_notificati @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 ): - bucket_prefix = get_bucket_prefix_for_notification( - sample_precompiled_letter_notification_using_test_key, is_test_letter=True) + bucket, bucket_prefix = get_bucket_name_and_prefix_for_notification( + sample_precompiled_letter_notification_using_test_key) + assert bucket == current_app.config['TEST_LETTERS_BUCKET_NAME'] assert bucket_prefix == 'NOTIFY.{}'.format( 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): - get_bucket_prefix_for_notification(None) + get_bucket_name_and_prefix_for_notification(None) @pytest.mark.parametrize('crown_flag,expected_crown_text', [