mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 21:48:49 -04: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:
@@ -15,14 +15,14 @@ def test_should_return_correct_details_for_delivery():
|
||||
def test_should_return_correct_details_for_bounced():
|
||||
assert aws_responses.response_code_to_message('Bounce') == 'Bounced'
|
||||
assert aws_responses.response_code_to_notification_status('Bounce') == 'bounce'
|
||||
assert aws_responses.response_code_to_notification_statistics_status('Bounce') == 'failed'
|
||||
assert aws_responses.response_code_to_notification_statistics_status('Bounce') == 'failure'
|
||||
assert not aws_responses.response_code_to_notification_success('Bounce')
|
||||
|
||||
|
||||
def test_should_return_correct_details_for_complaint():
|
||||
assert aws_responses.response_code_to_message('Complaint') == 'Complaint'
|
||||
assert aws_responses.response_code_to_notification_status('Complaint') == 'complaint'
|
||||
assert aws_responses.response_code_to_notification_statistics_status('Complaint') == 'failed'
|
||||
assert aws_responses.response_code_to_notification_statistics_status('Complaint') == 'failure'
|
||||
assert not aws_responses.response_code_to_notification_success('Complaint')
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ def test_should_return_correct_details_for_delivery():
|
||||
def test_should_return_correct_details_for_bounced():
|
||||
assert responses.response_code_to_message('1') == 'Declined'
|
||||
assert responses.response_code_to_notification_status('1') == 'failed'
|
||||
assert responses.response_code_to_notification_statistics_status('1') == 'failed'
|
||||
assert responses.response_code_to_notification_statistics_status('1') == 'failure'
|
||||
assert not responses.response_code_to_notification_success('1')
|
||||
|
||||
|
||||
|
||||
@@ -221,7 +221,6 @@ def sample_job(notify_db,
|
||||
}
|
||||
job = Job(**data)
|
||||
dao_create_job(job)
|
||||
print(job.created_at)
|
||||
return job
|
||||
|
||||
|
||||
|
||||
@@ -32,26 +32,98 @@ def test_should_by_able_to_update_reference_by_id(sample_notification):
|
||||
assert Notification.query.get(sample_notification.id).reference == 'reference'
|
||||
|
||||
|
||||
def test_should_by_able_to_update_status_by_reference(sample_notification):
|
||||
assert Notification.query.get(sample_notification.id).status == "sent"
|
||||
update_notification_reference_by_id(sample_notification.id, 'reference')
|
||||
update_notification_status_by_reference('reference', 'delivered')
|
||||
assert Notification.query.get(sample_notification.id).status == 'delivered'
|
||||
def test_should_by_able_to_update_status_by_reference(sample_email_template):
|
||||
data = {
|
||||
'to': '+44709123456',
|
||||
'service': sample_email_template.service,
|
||||
'service_id': sample_email_template.service.id,
|
||||
'template': sample_email_template,
|
||||
'created_at': datetime.utcnow()
|
||||
}
|
||||
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification, sample_email_template.template_type)
|
||||
|
||||
assert Notification.query.get(notification.id).status == "sent"
|
||||
update_notification_reference_by_id(notification.id, 'reference')
|
||||
update_notification_status_by_reference('reference', 'delivered', 'delivered')
|
||||
assert Notification.query.get(notification.id).status == 'delivered'
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_email_template.service.id
|
||||
).one().emails_delivered == 1
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_email_template.service.id
|
||||
).one().emails_requested == 1
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_email_template.service.id
|
||||
).one().emails_error == 0
|
||||
|
||||
|
||||
def test_should_by_able_to_update_status_by_id(sample_notification):
|
||||
assert Notification.query.get(sample_notification.id).status == 'sent'
|
||||
count = update_notification_status_by_id(sample_notification.id, 'delivered')
|
||||
count = update_notification_status_by_id(sample_notification.id, 'delivered', 'delivered')
|
||||
assert count == 1
|
||||
assert Notification.query.get(sample_notification.id).status == 'delivered'
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_notification.service.id
|
||||
).one().sms_delivered == 1
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_notification.service.id
|
||||
).one().sms_requested == 1
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_notification.service.id
|
||||
).one().sms_error == 0
|
||||
|
||||
|
||||
def test_should_be_able_to_record_statistics_failure_for_sms(sample_notification):
|
||||
assert Notification.query.get(sample_notification.id).status == 'sent'
|
||||
count = update_notification_status_by_id(sample_notification.id, 'delivered', 'failure')
|
||||
assert count == 1
|
||||
assert Notification.query.get(sample_notification.id).status == 'delivered'
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_notification.service.id
|
||||
).one().sms_delivered == 0
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_notification.service.id
|
||||
).one().sms_requested == 1
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=sample_notification.service.id
|
||||
).one().sms_error == 1
|
||||
|
||||
|
||||
def test_should_be_able_to_record_statistics_failure_for_email(sample_email_template):
|
||||
data = {
|
||||
'to': '+44709123456',
|
||||
'service': sample_email_template.service,
|
||||
'service_id': sample_email_template.service.id,
|
||||
'template': sample_email_template,
|
||||
'created_at': datetime.utcnow()
|
||||
}
|
||||
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification, sample_email_template.template_type)
|
||||
|
||||
update_notification_reference_by_id(notification.id, 'reference')
|
||||
count = update_notification_status_by_reference('reference', 'bounce', 'failure')
|
||||
assert count == 1
|
||||
assert Notification.query.get(notification.id).status == 'bounce'
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=notification.service.id
|
||||
).one().emails_delivered == 0
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=notification.service.id
|
||||
).one().emails_requested == 1
|
||||
assert NotificationStatistics.query.filter_by(
|
||||
service_id=notification.service.id
|
||||
).one().emails_error == 1
|
||||
|
||||
|
||||
def test_should_return_zero_count_if_no_notification_with_id():
|
||||
assert update_notification_status_by_id(str(uuid.uuid4()), 'delivered') == 0
|
||||
assert update_notification_status_by_id(str(uuid.uuid4()), 'delivered', 'delivered') == 0
|
||||
|
||||
|
||||
def test_should_return_zero_count_if_no_notification_with_reference():
|
||||
assert update_notification_status_by_reference('something', 'delivered') == 0
|
||||
assert update_notification_status_by_reference('something', 'delivered', 'delivered') == 0
|
||||
|
||||
|
||||
def test_should_be_able_to_get_statistics_for_a_service(sample_template):
|
||||
|
||||
@@ -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