Add test-letters bucket for precompiled test keys

- uses moto to mock out boto3 for testing
This commit is contained in:
Ken Tsang
2018-03-14 21:27:07 +00:00
parent d3537cca74
commit d40bc28f2a
3 changed files with 68 additions and 6 deletions

View File

@@ -5,13 +5,14 @@ from flask import current_app
from notifications_utils.s3 import s3upload
from app.models import KEY_TYPE_TEST
from app.variables import Retention
LETTERS_PDF_FILE_LOCATION_STRUCTURE = \
'{folder}NOTIFY.{reference}.{duplex}.{letter_class}.{colour}.{crown}.{date}.pdf'
PRECOMPILED_BUCKET_PREFIX = '{folder}/NOTIFY.{reference}'
PRECOMPILED_BUCKET_PREFIX = '{folder}NOTIFY.{reference}'
def get_folder_name(_now, is_test_letter):
@@ -41,9 +42,10 @@ def get_letter_pdf_filename(reference, crown, is_test_letter=False):
return upload_file_name
def get_bucket_prefix_for_notification(notification):
def get_bucket_prefix_for_notification(notification, is_test_letter=False):
upload_file_name = PRECOMPILED_BUCKET_PREFIX.format(
folder=notification.created_at.date(),
folder='' if is_test_letter else
'{}/'.format(notification.created_at.date()),
reference=notification.reference
).upper()
@@ -75,12 +77,16 @@ def upload_letter_pdf(notification, pdf_data, is_test_letter=False):
def get_letter_pdf(notification):
bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
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']
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
for item in bucket.objects.filter(Prefix=get_bucket_prefix_for_notification(notification)):
for item in bucket.objects.filter(Prefix=get_bucket_prefix_for_notification(notification, is_test_letter)):
obj = s3.Object(
bucket_name=bucket_name,
key=item.key

View File

@@ -1,5 +1,6 @@
-r requirements.txt
flake8==3.5.0
moto==1.1.25
pytest==3.4.2
pytest-env==0.6.2
pytest-mock==1.7.1

View File

@@ -1,8 +1,26 @@
import pytest
from datetime import datetime
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
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):
@@ -15,6 +33,17 @@ 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)
@@ -45,3 +74,29 @@ 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'