From 76c8b15f593c4398b2290a6155c998a687e67c03 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 27 Oct 2017 13:53:55 +0100 Subject: [PATCH 1/4] Update the billable units with the page count from the response file for letter notifications. --- app/celery/tasks.py | 18 +++++---- app/dao/notifications_dao.py | 1 - tests/app/celery/test_ftp_update_tasks.py | 13 ++++--- .../notification_dao/test_notification_dao.py | 37 ++++++++++++++++++- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 23101dccd..abafa6b78 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -33,20 +33,19 @@ from app.dao.jobs_dao import ( dao_get_job_by_id, all_notifications_are_created_for_job, dao_get_all_notifications_for_job, - dao_update_job_status) + dao_update_job_status +) from app.dao.notifications_dao import ( get_notification_by_id, - update_notification_status_by_reference, dao_update_notifications_for_job_to_sent_to_dvla, dao_update_notifications_by_reference, - dao_get_last_notification_added_for_job_id) + dao_get_last_notification_added_for_job_id +) from app.dao.provider_details_dao import get_current_provider from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service from app.dao.services_dao import dao_fetch_service_by_id, fetch_todays_total_message_count from app.dao.templates_dao import dao_get_template_by_id from app.models import ( - Job, - Notification, DVLA_RESPONSE_STATUS_SENT, EMAIL_TYPE, JOB_STATUS_CANCELLED, @@ -449,9 +448,12 @@ def update_letter_notifications_statuses(self, filename): for update in notification_updates: status = NOTIFICATION_DELIVERED if update.status == DVLA_RESPONSE_STATUS_SENT \ else NOTIFICATION_TECHNICAL_FAILURE - notification = update_notification_status_by_reference( - update.reference, - status + notification = dao_update_notifications_by_reference( + references=[update.reference], + update_dict={"status": status, + "billable_units": update.page_count, + "updated_at": datetime.utcnow() + } ) if not notification: diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index fad0edb16..1c010c86b 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -493,7 +493,6 @@ def dao_update_notifications_for_job_to_sent_to_dvla(job_id, provider): @statsd(namespace="dao") @transactional def dao_update_notifications_by_reference(references, update_dict): - now = datetime.utcnow() updated_count = Notification.query.filter( Notification.reference.in_(references) ).update( diff --git a/tests/app/celery/test_ftp_update_tasks.py b/tests/app/celery/test_ftp_update_tasks.py index eef0c8c36..a330e9c3c 100644 --- a/tests/app/celery/test_ftp_update_tasks.py +++ b/tests/app/celery/test_ftp_update_tasks.py @@ -9,12 +9,9 @@ from app.models import ( Notification, NOTIFICATION_CREATED, NOTIFICATION_DELIVERED, - NOTIFICATION_TECHNICAL_FAILURE, NOTIFICATION_SENDING, - NOTIFICATION_STATUS_LETTER_RECEIVED, NOTIFICATION_TECHNICAL_FAILURE ) -from app.dao.notifications_dao import dao_update_notifications_by_reference from app.celery.tasks import ( process_updates_from_file, update_dvla_job_to_error, @@ -96,8 +93,10 @@ def test_update_letter_notifications_statuses_builds_updates_list(notify_api, mo def test_update_letter_notifications_statuses_persisted(notify_api, mocker, sample_letter_template): - sent_letter = create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING) - failed_letter = create_notification(sample_letter_template, reference='ref-bar', status=NOTIFICATION_SENDING) + sent_letter = create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING, + billable_units=0) + failed_letter = create_notification(sample_letter_template, reference='ref-bar', status=NOTIFICATION_SENDING, + billable_units=0) valid_file = '{}|Sent|1|Unsorted\n{}|Failed|2|Sorted'.format( sent_letter.reference, failed_letter.reference) @@ -106,7 +105,11 @@ def test_update_letter_notifications_statuses_persisted(notify_api, mocker, samp update_letter_notifications_statuses(filename='foo.txt') assert sent_letter.status == NOTIFICATION_DELIVERED + assert sent_letter.billable_units == 1 + assert sent_letter.updated_at assert failed_letter.status == NOTIFICATION_TECHNICAL_FAILURE + assert failed_letter.billable_units == 2 + assert failed_letter.updated def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references( diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index d4f89abac..562fd8555 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -51,7 +51,7 @@ from app.dao.notifications_dao import ( set_scheduled_notification_to_processed, update_notification_status_by_id, update_notification_status_by_reference, - dao_get_last_notification_added_for_job_id) + dao_get_last_notification_added_for_job_id, dao_update_notifications_by_reference) from app.dao.services_dao import dao_update_service from tests.app.db import ( @@ -2065,3 +2065,38 @@ def test_dao_get_last_notification_added_for_job_id_no_notifications(sample_temp def test_dao_get_last_notification_added_for_job_id_no_notifications(sample_template, fake_uuid): assert dao_get_last_notification_added_for_job_id(fake_uuid) is None + + +def test_dao_update_notifications_by_reference_updated_notificaitons_and_history(sample_template): + notification_0 = create_notification(template=sample_template, reference='noref') + notification_1 = create_notification(template=sample_template, reference='ref') + notification_2 = create_notification(template=sample_template, reference='ref') + + updated_count = dao_update_notifications_by_reference(references=['ref'], + update_dict={"status": "delivered", + "billable_units": 2} + ) + assert updated_count == 2 + updated_1 = Notification.query.get(notification_1.id) + assert updated_1.billable_units == 2 + assert updated_1.status == 'delivered' + updated_2 = Notification.query.get(notification_2.id) + assert updated_2.billable_units == 2 + assert updated_2.status == 'delivered' + + updated_history_1 = NotificationHistory.query.get(notification_1.id) + assert updated_history_1.billable_units == 2 + assert updated_history_1.status == 'delivered' + updated_history_2 = Notification.query.get(notification_2.id) + assert updated_history_2.billable_units == 2 + assert updated_history_2.status == 'delivered' + + assert notification_0 == Notification.query.get(notification_0.id) + + +def test_dao_update_notifications_by_reference_returns_zero_when_no_notifications_to_update(notify_db): + updated_count = dao_update_notifications_by_reference(references=['ref'], + update_dict={"status": "delivered", + "billable_units": 2} + ) + assert updated_count == 0 From 46f5896b3be49b2e496d74c8c97fdd18a8f059ba Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Thu, 26 Oct 2017 16:39:33 +0100 Subject: [PATCH 2/4] add new email_auth service permission --- app/models.py | 12 +++++++++-- .../0129_add_email_auth_permission_.py | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/0129_add_email_auth_permission_.py diff --git a/app/models.py b/app/models.py index 8cc6cc19d..614d3b5c6 100644 --- a/app/models.py +++ b/app/models.py @@ -168,9 +168,17 @@ class DVLAOrganisation(db.Model): INTERNATIONAL_SMS_TYPE = 'international_sms' INBOUND_SMS_TYPE = 'inbound_sms' SCHEDULE_NOTIFICATIONS = 'schedule_notifications' +EMAIL_AUTH = 'email_auth' -SERVICE_PERMISSION_TYPES = [EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, INTERNATIONAL_SMS_TYPE, INBOUND_SMS_TYPE, - SCHEDULE_NOTIFICATIONS] +SERVICE_PERMISSION_TYPES = [ + EMAIL_TYPE, + SMS_TYPE, + LETTER_TYPE, + INTERNATIONAL_SMS_TYPE, + INBOUND_SMS_TYPE, + SCHEDULE_NOTIFICATIONS, + EMAIL_AUTH, +] class ServicePermissionTypes(db.Model): diff --git a/migrations/versions/0129_add_email_auth_permission_.py b/migrations/versions/0129_add_email_auth_permission_.py new file mode 100644 index 000000000..94b135f9c --- /dev/null +++ b/migrations/versions/0129_add_email_auth_permission_.py @@ -0,0 +1,21 @@ +""" + +Revision ID: 0129_add_email_auth_permission +Revises: 0128_noti_to_sms_sender +Create Date: 2017-10-26 14:33:41.336861 + +""" +from alembic import op + + +revision = '0129_add_email_auth_permission' +down_revision = '0128_noti_to_sms_sender' + + +def upgrade(): + op.execute("INSERT INTO service_permission_types VALUES ('email_auth')") + + +def downgrade(): + op.execute("DELETE FROM service_permissions WHERE permission = 'email_auth'") + op.execute("DELETE FROM service_permission_types WHERE name = 'email_auth'") From 8a4c8802ef8744b6e691e69adf256008adaddcea Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Fri, 27 Oct 2017 14:34:55 +0100 Subject: [PATCH 3/4] Add notification email reply_to script --- .../0130_service_email_reply_to_row.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 migrations/versions/0130_service_email_reply_to_row.py diff --git a/migrations/versions/0130_service_email_reply_to_row.py b/migrations/versions/0130_service_email_reply_to_row.py new file mode 100644 index 000000000..0adfa2692 --- /dev/null +++ b/migrations/versions/0130_service_email_reply_to_row.py @@ -0,0 +1,33 @@ +"""empty message + +Revision ID: 0130_service_email_reply_to_row +Revises: 0129_add_email_auth_permission +Create Date: 2017-08-29 14:09:41.042061 + +""" + +# revision identifiers, used by Alembic. +revision = '0130_service_email_reply_to_row' +down_revision = '0129_add_email_auth_permission' + +from alembic import op + + +NOTIFY_SERVICE_ID = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553' +EMAIL_REPLY_TO_ID = 'b3a58d57-2337-662a-4cba-40792a9322f2' + + +def upgrade(): + op.execute(""" + INSERT INTO service_email_reply_to + (id, service_id, email_address, is_default, created_at) + VALUES + ('{}','{}', 'notify+1@digital.cabinet-office.gov.uk', 'f', NOW()) + """.format(EMAIL_REPLY_TO_ID, NOTIFY_SERVICE_ID)) + + +def downgrade(): + op.execute(""" + DELETE FROM service_email_reply_to + WHERE id = '{}' + """.format(EMAIL_REPLY_TO_ID)) From ca10c72fe6d099811fd15e0c0d3ca2ff279c91e7 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 27 Oct 2017 16:00:07 +0100 Subject: [PATCH 4/4] Fix typo --- tests/app/celery/test_ftp_update_tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app/celery/test_ftp_update_tasks.py b/tests/app/celery/test_ftp_update_tasks.py index a330e9c3c..d9b1b22b9 100644 --- a/tests/app/celery/test_ftp_update_tasks.py +++ b/tests/app/celery/test_ftp_update_tasks.py @@ -109,7 +109,7 @@ def test_update_letter_notifications_statuses_persisted(notify_api, mocker, samp assert sent_letter.updated_at assert failed_letter.status == NOTIFICATION_TECHNICAL_FAILURE assert failed_letter.billable_units == 2 - assert failed_letter.updated + assert failed_letter.updated_at def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references(