diff --git a/app/notifications/process_notifications.py b/app/notifications/process_notifications.py index 100790f83..20c77d188 100644 --- a/app/notifications/process_notifications.py +++ b/app/notifications/process_notifications.py @@ -18,6 +18,7 @@ from app.models import ( EMAIL_TYPE, KEY_TYPE_TEST, SMS_TYPE, + LETTER_TYPE, NOTIFICATION_CREATED, Notification, ScheduledNotification @@ -105,6 +106,8 @@ def persist_notification( notification.rate_multiplier = recipient_info.billable_units elif notification_type == EMAIL_TYPE: notification.normalised_to = format_email_address(notification.to) + elif notification_type == LETTER_TYPE: + notification.postage = service.postage # if simulated create a Notification model to return but do not persist the Notification to the dB if not simulated: diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 2dea9b865..6f9082408 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -982,6 +982,32 @@ def test_save_letter_saves_letter_to_database(mocker, notify_db_session): assert notification_db.reply_to_text == contact_block.contact_block +@pytest.mark.parametrize('postage', ['first', 'second']) +def test_save_letter_saves_letter_to_database_with_correct_postage(mocker, sample_letter_job, postage): + sample_letter_job.service.postage = postage + + mocker.patch('app.celery.tasks.letters_pdf_tasks.create_letters_pdf.apply_async') + + notification_json = _notification_json( + template=sample_letter_job.template, + to='Foo', + personalisation={'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': 'Flob'}, + job_id=sample_letter_job.id, + row_number=1 + ) + notification_id = uuid.uuid4() + + save_letter( + sample_letter_job.service_id, + notification_id, + encryption.encrypt(notification_json), + ) + + notification_db = Notification.query.one() + assert notification_db.id == notification_id + assert notification_db.postage == postage + + def test_save_letter_saves_letter_to_database_right_reply_to(mocker, notify_db_session): service = create_service() create_letter_contact(service=service, contact_block="Address contact", is_default=True) diff --git a/tests/app/notifications/test_process_letter_notifications.py b/tests/app/notifications/test_process_letter_notifications.py index a607bf68b..48fc2962b 100644 --- a/tests/app/notifications/test_process_letter_notifications.py +++ b/tests/app/notifications/test_process_letter_notifications.py @@ -25,6 +25,7 @@ def test_create_letter_notification_creates_notification(sample_letter_template, assert notification.key_type == sample_api_key.key_type assert notification.reference is not None assert notification.client_reference is None + assert notification.postage == 'second' def test_create_letter_notification_sets_reference(sample_letter_template, sample_api_key): diff --git a/tests/app/v2/notifications/test_post_letter_notifications.py b/tests/app/v2/notifications/test_post_letter_notifications.py index 72afe5214..cbf9ca471 100644 --- a/tests/app/v2/notifications/test_post_letter_notifications.py +++ b/tests/app/v2/notifications/test_post_letter_notifications.py @@ -76,12 +76,11 @@ def test_post_letter_notification_returns_201(client, sample_letter_template, mo assert Job.query.count() == 0 notification = Notification.query.one() assert notification.status == NOTIFICATION_CREATED - notification_id = notification.id - assert resp_json['id'] == str(notification_id) + assert resp_json['id'] == str(notification.id) assert resp_json['reference'] == reference assert resp_json['content']['subject'] == sample_letter_template.subject assert resp_json['content']['body'] == sample_letter_template.content - assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri'] + assert 'v2/notifications/{}'.format(notification.id) in resp_json['uri'] assert resp_json['template']['id'] == str(sample_letter_template.id) assert resp_json['template']['version'] == sample_letter_template.version assert ( @@ -95,6 +94,28 @@ def test_post_letter_notification_returns_201(client, sample_letter_template, mo mock.assert_called_once_with([str(notification.id)], queue=QueueNames.CREATE_LETTERS_PDF) +@pytest.mark.parametrize('postage', ['first', 'second']) +def test_post_letter_notification_sets_postage(client, sample_letter_template, mocker, postage): + sample_letter_template.service.postage = postage + mocker.patch('app.celery.tasks.letters_pdf_tasks.create_letters_pdf.apply_async') + data = { + 'template_id': str(sample_letter_template.id), + 'personalisation': { + 'address_line_1': 'Her Royal Highness Queen Elizabeth II', + 'address_line_2': 'Buckingham Palace', + 'address_line_3': 'London', + 'postcode': 'SW1 1AA', + 'name': 'Lizzie' + } + } + + resp_json = letter_request(client, data, service_id=sample_letter_template.service_id) + + assert validate(resp_json, post_letter_response) == resp_json + notification = Notification.query.one() + assert notification.postage == postage + + @pytest.mark.parametrize('env', [ 'staging', 'live', @@ -441,8 +462,10 @@ def test_post_precompiled_letter_with_invalid_base64(client, notify_user, mocker assert not Notification.query.first() -def test_post_precompiled_letter_notification_returns_201(client, notify_user, mocker): +@pytest.mark.parametrize('postage', ['first', 'second']) +def test_post_precompiled_letter_notification_returns_201(client, notify_user, mocker, postage): sample_service = create_service(service_permissions=['letter', 'precompiled_letter']) + sample_service.postage = postage s3mock = mocker.patch('app.v2.notifications.post_notifications.upload_letter_pdf') mocker.patch('app.v2.notifications.post_notifications.pdf_page_count', return_value=5) mocker.patch("app.letters.rest.notify_celery.send_task") @@ -464,6 +487,7 @@ def test_post_precompiled_letter_notification_returns_201(client, notify_user, m assert notification.billable_units == 3 assert notification.status == NOTIFICATION_PENDING_VIRUS_CHECK + assert notification.postage == postage resp_json = json.loads(response.get_data(as_text=True)) assert resp_json == {'id': str(notification.id), 'reference': 'letter-reference'} diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index 707ab2a04..2ea9582da 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -58,6 +58,7 @@ def test_post_sms_notification_returns_201(client, sample_template_with_placehol assert len(notifications) == 1 assert notifications[0].status == NOTIFICATION_CREATED notification_id = notifications[0].id + assert notifications[0].postage is None assert resp_json['id'] == str(notification_id) assert resp_json['reference'] == reference assert resp_json['content']['body'] == sample_template_with_placeholders.content.replace("(( Name))", "Jo") @@ -309,6 +310,7 @@ def test_post_email_notification_returns_201(client, sample_email_template_with_ assert validate(resp_json, post_email_response) == resp_json notification = Notification.query.one() assert notification.status == NOTIFICATION_CREATED + assert notification.postage is None assert resp_json['id'] == str(notification.id) assert resp_json['reference'] == reference assert notification.reference is None