mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 08:21:13 -05:00
Adds notification stats update into the callback process
- when a provider callback occurs and we update the status of the notification, also update the statistics table Adds: - Mapping object to the clients to handle mapping to various states from the response codes, this replaces the map. - query lookup in the DAO to get the query based on response type / template type Tests around rest class and dao to check correct updating of stats Missing: - multiple client callbacks will keep incrementing the counts of success/failure. This edge case needs to be handle in a future story.
This commit is contained in:
@@ -7,7 +7,7 @@ from flask import json
|
||||
from app.models import Service
|
||||
from app.dao.templates_dao import dao_get_all_templates_for_service
|
||||
from app.dao.services_dao import dao_update_service
|
||||
from app.dao.notifications_dao import get_notification_by_id
|
||||
from app.dao.notifications_dao import get_notification_by_id, dao_get_notification_statistics_for_service
|
||||
from freezegun import freeze_time
|
||||
|
||||
|
||||
@@ -982,7 +982,7 @@ def test_firetext_callback_should_return_404_if_cannot_find_notification_id(noti
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == 'Firetext callback failed: notification {} not found. Status {}'.format(
|
||||
missing_notification_id,
|
||||
'delivered'
|
||||
'Delivered'
|
||||
)
|
||||
|
||||
|
||||
@@ -1007,6 +1007,10 @@ def test_firetext_callback_should_update_notification_status(notify_api, sample_
|
||||
)
|
||||
updated = get_notification_by_id(sample_notification.id)
|
||||
assert updated.status == 'delivered'
|
||||
assert get_notification_by_id(sample_notification.id).status == 'delivered'
|
||||
assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_delivered == 1
|
||||
assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_requested == 1
|
||||
assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_error == 0
|
||||
|
||||
|
||||
def test_firetext_callback_should_update_notification_status_failed(notify_api, sample_notification):
|
||||
@@ -1030,6 +1034,9 @@ def test_firetext_callback_should_update_notification_status_failed(notify_api,
|
||||
)
|
||||
updated = get_notification_by_id(sample_notification.id)
|
||||
assert updated.status == 'failed'
|
||||
assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_delivered == 0
|
||||
assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_requested == 1
|
||||
assert dao_get_notification_statistics_for_service(sample_notification.service_id)[0].sms_error == 1
|
||||
|
||||
|
||||
def test_firetext_callback_should_update_notification_status_sent(notify_api, notify_db, notify_db_session):
|
||||
@@ -1054,6 +1061,42 @@ def test_firetext_callback_should_update_notification_status_sent(notify_api, no
|
||||
)
|
||||
updated = get_notification_by_id(notification.id)
|
||||
assert updated.status == 'sent'
|
||||
assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_delivered == 0
|
||||
assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_requested == 1
|
||||
assert dao_get_notification_statistics_for_service(notification.service_id)[0].sms_error == 0
|
||||
|
||||
|
||||
def test_firetext_callback_should_update_multiple_notification_status_sent(notify_api, notify_db, notify_db_session):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
notification1 = sample_notification(notify_db, notify_db_session, status='delivered')
|
||||
notification2 = sample_notification(notify_db, notify_db_session, status='delivered')
|
||||
notification3 = sample_notification(notify_db, notify_db_session, status='delivered')
|
||||
|
||||
client.post(
|
||||
path='/notifications/sms/firetext',
|
||||
data='mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}'.format(
|
||||
notification1.id
|
||||
),
|
||||
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||
|
||||
client.post(
|
||||
path='/notifications/sms/firetext',
|
||||
data='mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}'.format(
|
||||
notification2.id
|
||||
),
|
||||
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||
|
||||
client.post(
|
||||
path='/notifications/sms/firetext',
|
||||
data='mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}'.format(
|
||||
notification3.id
|
||||
),
|
||||
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||
|
||||
assert dao_get_notification_statistics_for_service(notification1.service_id)[0].sms_delivered == 3
|
||||
assert dao_get_notification_statistics_for_service(notification1.service_id)[0].sms_requested == 3
|
||||
assert dao_get_notification_statistics_for_service(notification1.service_id)[0].sms_error == 0
|
||||
|
||||
|
||||
def test_ses_callback_should_not_need_auth(notify_api):
|
||||
@@ -1126,11 +1169,20 @@ def test_ses_callback_should_fail_if_notification_cannot_be_found(notify_db, not
|
||||
assert json_resp['message'] == 'SES callback failed: notification not found. Status delivered'
|
||||
|
||||
|
||||
def test_ses_callback_should_update_notification_status(notify_api, notify_db, notify_db_session):
|
||||
def test_ses_callback_should_update_notification_status(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification = sample_notification(notify_db, notify_db_session, reference='ref')
|
||||
notification = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
reference='ref'
|
||||
)
|
||||
|
||||
assert get_notification_by_id(notification.id).status == 'sent'
|
||||
|
||||
@@ -1144,6 +1196,64 @@ def test_ses_callback_should_update_notification_status(notify_api, notify_db, n
|
||||
assert json_resp['result'] == 'success'
|
||||
assert json_resp['message'] == 'SES callback succeeded'
|
||||
assert get_notification_by_id(notification.id).status == 'delivered'
|
||||
assert dao_get_notification_statistics_for_service(notification.service_id)[0].emails_delivered == 1
|
||||
assert dao_get_notification_statistics_for_service(notification.service_id)[0].emails_requested == 1
|
||||
assert dao_get_notification_statistics_for_service(notification.service_id)[0].emails_error == 0
|
||||
|
||||
|
||||
def test_ses_callback_should_update_multiple_notification_status_sent(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_email_template):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
notification1 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
reference='ref1',
|
||||
status='delivered')
|
||||
|
||||
notification2 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
reference='ref2',
|
||||
status='delivered')
|
||||
|
||||
notification3 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template=sample_email_template,
|
||||
reference='ref2',
|
||||
status='delivered')
|
||||
|
||||
client.post(
|
||||
path='/notifications/sms/firetext',
|
||||
data='mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}'.format(
|
||||
notification1.id
|
||||
),
|
||||
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||
|
||||
client.post(
|
||||
path='/notifications/sms/firetext',
|
||||
data='mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}'.format(
|
||||
notification2.id
|
||||
),
|
||||
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||
|
||||
client.post(
|
||||
path='/notifications/sms/firetext',
|
||||
data='mobile=441234123123&status=0&time=2016-03-10 14:17:00&reference={}'.format(
|
||||
notification3.id
|
||||
),
|
||||
headers=[('Content-Type', 'application/x-www-form-urlencoded')])
|
||||
|
||||
assert dao_get_notification_statistics_for_service(notification1.service_id)[0].emails_delivered == 3
|
||||
assert dao_get_notification_statistics_for_service(notification1.service_id)[0].emails_requested == 3
|
||||
assert dao_get_notification_statistics_for_service(notification1.service_id)[0].emails_error == 0
|
||||
|
||||
|
||||
def test_should_handle_invite_email_callbacks(notify_api, notify_db, notify_db_session):
|
||||
@@ -1203,3 +1313,7 @@ def ses_missing_notification_id_callback():
|
||||
|
||||
def ses_invalid_notification_type_callback():
|
||||
return b'{\n "Type" : "Notification",\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",\n "Message" : "{\\"notificationType\\":\\"Unknown\\",\\"mail\\":{\\"timestamp\\":\\"2016-03-14T12:35:25.909Z\\",\\"source\\":\\"test@test-domain.com\\",\\"sourceArn\\":\\"arn:aws:ses:eu-west-1:123456789012:identity/testing-notify\\",\\"sendingAccountId\\":\\"123456789012\\",\\"destination\\":[\\"testing@digital.cabinet-office.gov.uk\\"]},\\"delivery\\":{\\"timestamp\\":\\"2016-03-14T12:35:26.567Z\\",\\"processingTimeMillis\\":658,\\"recipients\\":[\\"testing@digital.cabinet-office.gov.uk\\"],\\"smtpResponse\\":\\"250 2.0.0 OK 1457958926 uo5si26480932wjc.221 - gsmtp\\",\\"reportingMTA\\":\\"a6-238.smtp-out.eu-west-1.amazonses.com\\"}}",\n "Timestamp" : "2016-03-14T12:35:26.665Z",\n "SignatureVersion" : "1",\n "Signature" : "X8d7eTAOZ6wlnrdVVPYanrAlsX0SMPfOzhoTEBnQqYkrNWTqQY91C0f3bxtPdUhUtOowyPAOkTQ4KnZuzphfhVb2p1MyVYMxNKcBFB05/qaCX99+92fjw4x9LeUOwyGwMv5F0Vkfi5qZCcEw69uVrhYLVSTFTrzi/yCtru+yFULMQ6UhbY09GwiP6hjxZMVr8aROQy5lLHglqQzOuSZ4KeD85JjifHdKzlx8jjQ+uj+FLzHXPMAPmPU1JK9kpoHZ1oPshAFgPDpphJe+HwcJ8ezmk+3AEUr3wWli3xF+49y8Z2anASSVp6YI2YP95UT8Rlh3qT3T+V9V8rbSVislxA==",\n "SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-bb750dd426d95ee9390147a5624348ee.pem",\n "UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:302763885840:preview-emails:d6aad3ef-83d6-4cf3-a470-54e2e75916da"\n}' # noqa
|
||||
|
||||
|
||||
|
||||
{\n "Type" : "Notification",\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",\n "Message" : "{"notificationType{\n "Type" : "Notification",\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",\n "Message" : "{"notificationType":"Unknown","mail":{"timestamp":"2016-03-14T12:35:25.909Z","source":"test@test-domain.com","sourceArn":"arn:aws:ses:eu-west-1:123456789012:identity/testing-notify","sendingAccountId":"123456789012","destination":["testing@digital.cabinet-office.gov.uk"]},"delivery":{"timestamp":"2016-03-14T12:35:26.567Z","processingTimeMillis":658,"recipients":["testing@digital.cabinet-office.gov.uk"],"smtpResponse":"250 2.0.0 OK 1457958926 uo5si26480932wjc.221 - gsmtp","reportingMTA":"a6-238.smtp-out.eu-west-1.amazonses.com"}}",\n "Timestamp" : "2016-03-14T12:35:26.665Z",\n "SignatureVersion" : "1",\n "Signature" : "X8d7eTAOZ6wlnrdVVPYanrAlsX0SMPfOzhoTEBnQqYkrNWTqQY91C0f3bxtPdUhUtOowyPAOkTQ4KnZuzphfhVb2p1MyVYMxNKcBFB05/qaCX99+92fjw4x9LeUOwyGwMv5F0Vkfi5qZCcEw69uVrhYLVSTFTrzi/yCtru+yFULMQ6UhbY09GwiP6hjxZMVr8aROQy5lLHglqQzOuSZ4KeD85JjifHdKzlx8jjQ+uj+FLzHXPMAPmPU1JK9kpoHZ1oPshAFgPDpphJe+HwcJ8ezmk+3AEUr3wWli3xF+49y8Z2anASSVp6YI2YP95UT8Rlh3qT3T+V9V8rbSVislxA==",\n "SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-bb750dd426d95ee9390147a5624348ee.pem",\n "UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:302763885840:preview-emails:d6aad3ef-83d6-4cf3-a470-54e2e75916da"\n}":"Unknown","mail":{"timestamp":"2016-03-14T12:35:25.909Z","source":"test@test-domain.com","sourceArn":"arn:aws:ses:eu-west-1:123456789012:identity/testing-notify","sendingAccountId":"123456789012","destination":["testing@digital.cabinet-office.gov.uk"]},"delivery":{"timestamp":"2016-03-14T12:35:26.567Z","processingTimeMillis":658,"recipients":["testing@digital.cabinet-office.gov.uk"],"smtpResponse":"250 2.0.0 OK 1457958926 uo5si26480932wjc.221 - gsmtp","reportingMTA":"a6-238.smtp-out.eu-west-1.amazonses.com"}}",\n "Timestamp" : "2016-03-14T12:35:26.665Z",\n "SignatureVersion" : "1",\n "Signature" : "X8d7eTAOZ6wlnrdVVPYanrAlsX0SMPfOzhoTEBnQqYkrNWTqQY91C0f3bxtPdUhUtOowyPAOkTQ4KnZuzphfhVb2p1MyVYMxNKcBFB05/qaCX99+92fjw4x9LeUOwyGwMv5F0Vkfi5qZCcEw69uVrhYLVSTFTrzi/yCtru+yFULMQ6UhbY09GwiP6hjxZMVr8aROQy5lLHglqQzOuSZ4KeD85JjifHdKzlx8jjQ+uj+FLzHXPMAPmPU1JK9kpoHZ1oPshAFgPDpphJe+HwcJ8ezmk+3AEUr3wWli3xF+49y8Z2anASSVp6YI2YP95UT8Rlh3qT3T+V9V8rbSVislxA==",\n "SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-bb750dd426d95ee9390147a5624348ee.pem",\n "UnsubscribeURL" : "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:302763885840:preview-emails:d6aad3ef-83d6-4cf3-a470-54e2e75916da"\n}
|
||||
Reference in New Issue
Block a user