mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 14:49:49 -04:00
Set the postage in the filename based on the postage set on the notification.
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
|
from app.models import KEY_TYPE_TEST, SECOND_CLASS, RESOLVE_POSTAGE_FOR_FILE_NAME
|
||||||
from app.utils import convert_utc_to_bst
|
from app.utils import convert_utc_to_bst
|
||||||
|
|
||||||
|
|
||||||
@@ -32,14 +32,14 @@ def get_folder_name(_now, is_test_or_scan_letter=False):
|
|||||||
return folder_name
|
return folder_name
|
||||||
|
|
||||||
|
|
||||||
def get_letter_pdf_filename(reference, crown, is_scan_letter=False):
|
def get_letter_pdf_filename(reference, crown, is_scan_letter=False, postage=SECOND_CLASS):
|
||||||
now = datetime.utcnow()
|
now = datetime.utcnow()
|
||||||
|
|
||||||
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
||||||
folder=get_folder_name(now, is_scan_letter),
|
folder=get_folder_name(now, is_scan_letter),
|
||||||
reference=reference,
|
reference=reference,
|
||||||
duplex="D",
|
duplex="D",
|
||||||
letter_class="2",
|
letter_class=RESOLVE_POSTAGE_FOR_FILE_NAME[postage],
|
||||||
colour="C",
|
colour="C",
|
||||||
crown="C" if crown else "N",
|
crown="C" if crown else "N",
|
||||||
date=now.strftime('%Y%m%d%H%M%S')
|
date=now.strftime('%Y%m%d%H%M%S')
|
||||||
@@ -71,7 +71,9 @@ def upload_letter_pdf(notification, pdf_data, precompiled=False):
|
|||||||
upload_file_name = get_letter_pdf_filename(
|
upload_file_name = get_letter_pdf_filename(
|
||||||
notification.reference,
|
notification.reference,
|
||||||
notification.service.crown,
|
notification.service.crown,
|
||||||
is_scan_letter=precompiled)
|
is_scan_letter=precompiled,
|
||||||
|
postage=notification.postage
|
||||||
|
)
|
||||||
|
|
||||||
if precompiled:
|
if precompiled:
|
||||||
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||||
|
|||||||
@@ -1113,6 +1113,14 @@ NOTIFICATION_STATUS_LETTER_RECEIVED = 'received'
|
|||||||
|
|
||||||
DVLA_RESPONSE_STATUS_SENT = 'Sent'
|
DVLA_RESPONSE_STATUS_SENT = 'Sent'
|
||||||
|
|
||||||
|
FIRST_CLASS = 'first'
|
||||||
|
SECOND_CLASS = 'second'
|
||||||
|
POSTAGE_TYPES = [FIRST_CLASS, SECOND_CLASS]
|
||||||
|
RESOLVE_POSTAGE_FOR_FILE_NAME = {
|
||||||
|
FIRST_CLASS: 1,
|
||||||
|
SECOND_CLASS: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class NotificationStatusTypes(db.Model):
|
class NotificationStatusTypes(db.Model):
|
||||||
__tablename__ = 'notification_status_types'
|
__tablename__ = 'notification_status_types'
|
||||||
|
|||||||
@@ -194,9 +194,7 @@ def test_create_job_returns_400_if_file_is_invalid(
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
print(response.get_data(as_text=True))
|
|
||||||
resp_json = json.loads(response.get_data(as_text=True))
|
resp_json = json.loads(response.get_data(as_text=True))
|
||||||
print(resp_json)
|
|
||||||
assert resp_json['result'] == 'error'
|
assert resp_json['result'] == 'error'
|
||||||
assert resp_json['message'] == 'File is not valid, can\'t create job'
|
assert resp_json['message'] == 'File is not valid, can\'t create job'
|
||||||
mock_job_dao.assert_not_called()
|
mock_job_dao.assert_not_called()
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from app.letters.utils import (
|
|||||||
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
|
||||||
|
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"
|
||||||
|
|
||||||
@@ -67,6 +68,18 @@ def test_get_letter_pdf_filename_returns_correct_filename(
|
|||||||
assert filename == '2017-12-04/NOTIFY.FOO.D.2.C.{}.20171204172900.PDF'.format(expected_crown_text)
|
assert filename == '2017-12-04/NOTIFY.FOO.D.2.C.{}.20171204172900.PDF'.format(expected_crown_text)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('postage,expected_postage', [
|
||||||
|
('second', 2),
|
||||||
|
('first', 1),
|
||||||
|
])
|
||||||
|
@freeze_time("2017-12-04 17:29:00")
|
||||||
|
def test_get_letter_pdf_filename_returns_correct_postage_for_filename(
|
||||||
|
notify_api, postage, expected_postage):
|
||||||
|
filename = get_letter_pdf_filename(reference='foo', crown=True, postage=postage)
|
||||||
|
|
||||||
|
assert filename == '2017-12-04/NOTIFY.FOO.D.{}.C.C.20171204172900.PDF'.format(expected_postage)
|
||||||
|
|
||||||
|
|
||||||
@freeze_time("2017-12-04 17:29:00")
|
@freeze_time("2017-12-04 17:29:00")
|
||||||
def test_get_letter_pdf_filename_returns_correct_filename_for_test_letters(
|
def test_get_letter_pdf_filename_returns_correct_filename_for_test_letters(
|
||||||
notify_api, mocker):
|
notify_api, mocker):
|
||||||
@@ -137,6 +150,33 @@ def test_upload_letter_pdf_to_correct_bucket(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('postage,expected_postage', [
|
||||||
|
('second', 2),
|
||||||
|
('first', 1)
|
||||||
|
])
|
||||||
|
def test_upload_letter_pdf_uses_postage_from_notification(
|
||||||
|
sample_letter_template, mocker, postage, expected_postage
|
||||||
|
):
|
||||||
|
letter_notification = create_notification(template=sample_letter_template, postage=postage)
|
||||||
|
mock_s3 = mocker.patch('app.letters.utils.s3upload')
|
||||||
|
|
||||||
|
filename = get_letter_pdf_filename(
|
||||||
|
reference=letter_notification.reference,
|
||||||
|
crown=letter_notification.service.crown,
|
||||||
|
is_scan_letter=False,
|
||||||
|
postage=letter_notification.postage
|
||||||
|
)
|
||||||
|
|
||||||
|
upload_letter_pdf(letter_notification, b'\x00\x01', precompiled=False)
|
||||||
|
|
||||||
|
mock_s3.assert_called_once_with(
|
||||||
|
bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'],
|
||||||
|
file_location=filename,
|
||||||
|
filedata=b'\x00\x01',
|
||||||
|
region=current_app.config['AWS_REGION']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@mock_s3
|
@mock_s3
|
||||||
@freeze_time(FROZEN_DATE_TIME)
|
@freeze_time(FROZEN_DATE_TIME)
|
||||||
def test_move_failed_pdf_error(notify_api):
|
def test_move_failed_pdf_error(notify_api):
|
||||||
|
|||||||
Reference in New Issue
Block a user