mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-07-19 05:53:51 -04:00
This continues the work from Template Preview [1], so that we have a complete store of original PDFs to use for testing changes to it. Previously we did store some originals, but these were only invalid PDFs that had failed sanitisation; for valid PDFs, the "transient" bucket only contains the sanitised versions, which the API deletes / moves when the notification is sent [2]. Since the notification is only created at a later stage [3], there's no easy way to get the final name of the PDF we send to DVLA. Instead, we use the "upload_id", which eventually becomes the notification ID [4]. This should be enough to trace the file for specific debugging. Note that we only want to store original PDFs if they're valid (and virus free!), since there's no point testing changes with bad data. [1]: https://github.com/alphagov/notifications-template-preview/pull/545 [2]:c44ec57c17/app/service/send_notification.py (L212)[3]:7930a53a58/app/main/views/uploads.py (L362)[4]:7930a53a58/app/main/views/uploads.py (L373)
92 lines
2.8 KiB
Python
92 lines
2.8 KiB
Python
import urllib
|
|
import uuid
|
|
|
|
from flask import current_app
|
|
|
|
from app.s3_client.s3_letter_upload_client import (
|
|
LetterMetadata,
|
|
backup_original_letter_to_s3,
|
|
upload_letter_to_s3,
|
|
)
|
|
|
|
|
|
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"
|