big commit with letters removal

This commit is contained in:
stvnrlly
2022-12-05 15:33:44 -05:00
parent 92da56fe63
commit 944715ac46
151 changed files with 278 additions and 10116 deletions

View File

@@ -1,122 +0,0 @@
import urllib
import uuid
import boto3
import botocore
import pytest
from flask import current_app
from moto import mock_s3
from app.s3_client.s3_letter_upload_client import (
LetterMetadata,
LetterNotFoundError,
backup_original_letter_to_s3,
get_letter_metadata,
upload_letter_to_s3,
)
pytest.skip(reason="Skipping letter-related features", allow_module_level=True)
def test_backup_original_letter_to_s3(mocker, notify_admin):
s3_mock = mocker.patch('app.s3_client.s3_letter_upload_client.utils_s3upload')
upload_id = uuid.uuid4()
backup_original_letter_to_s3(
'pdf_data',
upload_id=upload_id,
)
s3_mock.assert_called_once_with(
bucket_name=current_app.config['PRECOMPILED_ORIGINALS_BACKUP_LETTERS'],
file_location=f'{str(upload_id)}.pdf',
filedata='pdf_data',
region=current_app.config['AWS_REGION']
)
def test_upload_letter_to_s3(mocker):
s3_mock = mocker.patch('app.s3_client.s3_letter_upload_client.utils_s3upload')
recipient = 'Bugs Bunny\n123 Big Hole\nLooney Town'
upload_letter_to_s3(
'pdf_data',
file_location='service_id/upload_id.pdf',
status='valid',
page_count=3,
filename='my_doc',
recipient=recipient
)
s3_mock.assert_called_once_with(
bucket_name=current_app.config['TRANSIENT_UPLOADED_LETTERS'],
file_location='service_id/upload_id.pdf',
filedata='pdf_data',
metadata={'status': 'valid', 'page_count': '3', 'filename': 'my_doc',
'recipient': urllib.parse.quote(recipient)},
region=current_app.config['AWS_REGION']
)
def test_upload_letter_to_s3_with_message_and_invalid_pages(mocker):
s3_mock = mocker.patch('app.s3_client.s3_letter_upload_client.utils_s3upload')
upload_letter_to_s3(
'pdf_data',
file_location='service_id/upload_id.pdf',
status='invalid',
page_count=3,
filename='my_doc',
message='This file failed',
invalid_pages=[1, 2, 5])
s3_mock.assert_called_once_with(
bucket_name=current_app.config['TRANSIENT_UPLOADED_LETTERS'],
file_location='service_id/upload_id.pdf',
filedata='pdf_data',
metadata={
'status': 'invalid',
'page_count': '3',
'filename': 'my_doc',
'message': 'This file failed',
'invalid_pages': '[1, 2, 5]'
},
region=current_app.config['AWS_REGION']
)
def test_lettermetadata_gets_non_special_keys():
metadata = LetterMetadata({"key": "value", "not_key_to_decode": "%C2%A3"})
assert metadata.get("key") == "value"
assert metadata.get("other_key") is None
assert metadata.get("other_key", "default") == "default"
assert metadata.get("not_key_to_decode") == "%C2%A3"
def test_lettermetadata_unquotes_special_keys():
metadata = LetterMetadata({"filename": "%C2%A3hello", "recipient": "%C2%A3hi"})
assert metadata.get("filename") == "£hello"
assert metadata.get("recipient") == "£hi"
@mock_s3
@pytest.mark.parametrize('will_raise_custom_error,expected_exception', [
(True, LetterNotFoundError),
(False, botocore.exceptions.ClientError)
])
def test_get_letter_s3_object_raises_custom_error(
will_raise_custom_error,
expected_exception
):
bucket_name = current_app.config['TRANSIENT_UPLOADED_LETTERS']
s3 = boto3.client('s3', region_name='us-west-2')
# bucket not existing will trigger some other error
if will_raise_custom_error:
s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={'LocationConstraint': 'us-west-2'}
)
with pytest.raises(expected_exception):
get_letter_metadata('service', 'file')

View File

@@ -6,18 +6,12 @@ import pytest
from app.s3_client import default_access_key, default_region, default_secret_key
from app.s3_client.s3_logo_client import (
EMAIL_LOGO_LOCATION_STRUCTURE,
LETTER_TEMP_LOGO_LOCATION,
LETTER_TEMP_TAG,
TEMP_TAG,
delete_email_temp_file,
delete_email_temp_files_created_by,
delete_letter_temp_file,
delete_letter_temp_files_created_by,
letter_filename_for_db,
permanent_email_logo_name,
persist_logo,
upload_email_logo,
upload_letter_temp_logo,
)
bucket = 'test_bucket'
@@ -40,15 +34,6 @@ def upload_filename(fake_uuid):
temp=TEMP_TAG.format(user_id=fake_uuid), unique_id=upload_id, filename=filename)
@pytest.fixture
def letter_upload_filename(fake_uuid):
return LETTER_TEMP_LOGO_LOCATION.format(
user_id=fake_uuid,
unique_id=upload_id,
filename=svg_filename
)
def test_upload_email_logo_calls_correct_args(client_request, mocker, fake_uuid, upload_filename):
mocker.patch('uuid.uuid4', return_value=upload_id)
mocker.patch.dict('flask.current_app.config', {'LOGO_UPLOAD_BUCKET': bucket_credentials})
@@ -67,25 +52,6 @@ def test_upload_email_logo_calls_correct_args(client_request, mocker, fake_uuid,
)
def test_upload_letter_temp_logo_calls_correct_args(mocker, fake_uuid, letter_upload_filename):
mocker.patch('uuid.uuid4', return_value=upload_id)
mocker.patch.dict('flask.current_app.config', {'LOGO_UPLOAD_BUCKET': bucket_credentials})
mocked_s3_upload = mocker.patch('app.s3_client.s3_logo_client.utils_s3upload')
new_filename = upload_letter_temp_logo(filename=svg_filename, user_id=fake_uuid, filedata=data)
mocked_s3_upload.assert_called_once_with(
filedata=data,
region=region,
bucket_name=bucket,
file_location=letter_upload_filename,
content_type='image/svg+xml',
access_key=default_access_key,
secret_key=default_secret_key,
)
assert new_filename == 'letters/static/images/letter-template/temp-{}_test_uuid-test.svg'.format(fake_uuid)
def test_persist_logo(client_request, mocker, fake_uuid, upload_filename):
mocker.patch.dict('flask.current_app.config', {'LOGO_UPLOAD_BUCKET': bucket_credentials})
mocked_get_s3_object = mocker.patch('app.s3_client.s3_logo_client.get_s3_object')
@@ -124,16 +90,6 @@ def test_permanent_email_logo_name_does_not_change_filenames_with_no_TEMP_TAG():
assert new_name == filename
def test_letter_filename_for_db_when_file_has_a_temp_tag(fake_uuid):
temp_filename = LETTER_TEMP_LOGO_LOCATION.format(user_id=fake_uuid, unique_id=upload_id, filename=svg_filename)
assert letter_filename_for_db(temp_filename, fake_uuid) == 'test_uuid-test'
def test_letter_filename_for_db_when_file_does_not_have_a_temp_tag(fake_uuid):
filename = 'letters/static/images/letter-template/{}-test.svg'.format(fake_uuid)
assert letter_filename_for_db(filename, fake_uuid) == '{}-test'.format(fake_uuid)
def test_delete_email_temp_files_created_by_user(client_request, mocker, fake_uuid):
obj = namedtuple("obj", ["key"])
objs = [obj(key='test1'), obj(key='test2')]
@@ -147,19 +103,6 @@ def test_delete_email_temp_files_created_by_user(client_request, mocker, fake_uu
assert arg == call(objs[index].key)
def test_delete_letter_temp_files_created_by_user(mocker, fake_uuid):
obj = namedtuple("obj", ["key"])
objs = [obj(key='test1'), obj(key='test2')]
mocker.patch('app.s3_client.s3_logo_client.get_s3_objects_filter_by_prefix', return_value=objs)
mocked_delete_s3_object = mocker.patch('app.s3_client.s3_logo_client.delete_s3_object')
delete_letter_temp_files_created_by(fake_uuid)
for index, arg in enumerate(mocked_delete_s3_object.call_args_list):
assert arg == call(objs[index].key)
def test_delete_single_email_temp_file(client_request, mocker, upload_filename):
mocked_delete_s3_object = mocker.patch('app.s3_client.s3_logo_client.delete_s3_object')
@@ -177,23 +120,3 @@ def test_does_not_delete_non_temp_email_file(client_request, mocker):
assert mocked_delete_s3_object.called is False
assert str(error.value) == 'Not a temp file: {}'.format(filename)
def test_delete_single_temp_letter_file(mocker, fake_uuid, upload_filename):
mocked_delete_s3_object = mocker.patch('app.s3_client.s3_logo_client.delete_s3_object')
upload_filename = LETTER_TEMP_TAG.format(user_id=fake_uuid) + svg_filename
delete_letter_temp_file(upload_filename)
mocked_delete_s3_object.assert_called_with(upload_filename)
def test_does_not_delete_non_temp_letter_file(mocker, fake_uuid):
mocked_delete_s3_object = mocker.patch('app.s3_client.s3_logo_client.delete_s3_object')
with pytest.raises(ValueError) as error:
delete_letter_temp_file(svg_filename)
assert mocked_delete_s3_object.called is False
assert str(error.value) == 'Not a temp file: {}'.format(svg_filename)