mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-18 05:31:48 -05:00
requests converts dicts into query parameters anyway if you don't specify so dont try and encode the data ourselves, also hardened up tests
105 lines
7.5 KiB
Python
105 lines
7.5 KiB
Python
from flask import json
|
|
from app.celery.research_mode_tasks import (
|
|
send_sms_response,
|
|
send_email_response,
|
|
mmg_callback,
|
|
firetext_callback,
|
|
ses_notification_callback,
|
|
ses_hard_bounce_callback,
|
|
ses_soft_bounce_callback
|
|
)
|
|
|
|
|
|
def test_make_mmg_callback(notify_api, rmock):
|
|
endpoint = "http://localhost:6011/notifications/sms/mmg"
|
|
rmock.request(
|
|
"POST",
|
|
endpoint,
|
|
json={"status": "success"},
|
|
status_code=200)
|
|
send_sms_response("mmg", "1234", "07811111111")
|
|
|
|
assert rmock.called
|
|
assert rmock.request_history[0].url == endpoint
|
|
assert json.loads(rmock.request_history[0].text)['MSISDN'] == '07811111111'
|
|
|
|
|
|
def test_make_firetext_callback(notify_api, rmock):
|
|
endpoint = "http://localhost:6011/notifications/sms/firetext"
|
|
rmock.request(
|
|
"POST",
|
|
endpoint,
|
|
json="some data",
|
|
status_code=200)
|
|
send_sms_response("firetext", "1234", "07811111111")
|
|
|
|
assert rmock.called
|
|
assert rmock.request_history[0].url == endpoint
|
|
assert 'mobile=07811111111' in rmock.request_history[0].text
|
|
|
|
|
|
def test_make_ses_callback(notify_api, rmock):
|
|
endpoint = "http://localhost:6011/notifications/email/ses"
|
|
rmock.request(
|
|
"POST",
|
|
endpoint,
|
|
json={"status": "success"},
|
|
status_code=200)
|
|
send_email_response("ses", "1234", "test@test.com")
|
|
|
|
assert rmock.called
|
|
|
|
|
|
def test_delivered_mmg_callback():
|
|
data = json.loads(mmg_callback("1234", "07811111111"))
|
|
assert data['MSISDN'] == "07811111111"
|
|
assert data['status'] == "3"
|
|
assert data['reference'] == "mmg_reference"
|
|
assert data['CID'] == "1234"
|
|
|
|
|
|
def test_perm_failure_mmg_callback():
|
|
data = json.loads(mmg_callback("1234", "07822222222"))
|
|
assert data['MSISDN'] == "07822222222"
|
|
assert data['status'] == "5"
|
|
assert data['reference'] == "mmg_reference"
|
|
assert data['CID'] == "1234"
|
|
|
|
|
|
def test_temp_failure_mmg_callback():
|
|
data = json.loads(mmg_callback("1234", "07833333333"))
|
|
assert data['MSISDN'] == "07833333333"
|
|
assert data['status'] == "4"
|
|
assert data['reference'] == "mmg_reference"
|
|
assert data['CID'] == "1234"
|
|
|
|
|
|
def test_delivered_firetext_callback():
|
|
assert firetext_callback('1234', '07811111111') == {
|
|
'mobile': '07811111111',
|
|
'status': '0',
|
|
'time': '2016-03-10 14:17:00',
|
|
'reference': '1234'
|
|
}
|
|
|
|
|
|
def test_failure_firetext_callback():
|
|
assert firetext_callback('1234', '07822222222') == {
|
|
'mobile': '07822222222',
|
|
'status': '1',
|
|
'time': '2016-03-10 14:17:00',
|
|
'reference': '1234'
|
|
}
|
|
|
|
|
|
def test_delivered_ses_callback():
|
|
assert ses_notification_callback("my-reference") == '{ "Type" : "Notification", "MessageId" : "my-reference", "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing", "Message" : "{\\"notificationType\\":\\"Delivery\\",\\"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\\",\\"messageId\\":\\"my-reference\\",\\"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\\"}}", "Timestamp" : "2016-03-14T12:35:26.665Z", "SignatureVersion" : "1", "Signature" : "X8d7eTAOZ6wlnrdVVPYanrAlsX0SMPfOzhoTEBnQqYkrNWTqQY91C0f3bxtPdUhUtOowyPAOkTQ4KnZuzphfhVb2p1MyVYMxNKcBFB05/qaCX99+92fjw4x9LeUOwyGwMv5F0Vkfi5qZCcEw69uVrhYLVSTFTrzi/yCtru+yFULMQ6UhbY09GwiP6hjxZMVr8aROQy5lLHglqQzOuSZ4KeD85JjifHdKzlx8jjQ+uj+FLzHXPMAPmPU1JK9kpoHZ1oPshAFgPDpphJe+HwcJ8ezmk+3AEUr3wWli3xF+49y8Z2anASSVp6YI2YP95UT8Rlh3qT3T+V9V8rbSVislxA==", "SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-bb750dd426d95ee9390147a5624348ee.pem", "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"}' # noqa
|
|
|
|
|
|
def test_ses_hard_bounce_callback():
|
|
assert ses_hard_bounce_callback("my-reference") == '{ "Type" : "Notification", "MessageId" : "my-reference", "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing", "Message" : "{\\"notificationType\\":\\"Bounce\\",\\"bounce\\":{\\"bounceType\\":\\"Permanent\\",\\"bounceSubType\\":\\"General\\"}, \\"mail\\":{\\"messageId\\":\\"my-reference\\",\\"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\\"}}", "Timestamp" : "2016-03-14T12:35:26.665Z", "SignatureVersion" : "1", "Signature" : "X8d7eTAOZ6wlnrdVVPYanrAlsX0SMPfOzhoTEBnQqYkrNWTqQY91C0f3bxtPdUhUtOowyPAOkTQ4KnZuzphfhVb2p1MyVYMxNKcBFB05/qaCX99+92fjw4x9LeUOwyGwMv5F0Vkfi5qZCcEw69uVrhYLVSTFTrzi/yCtru+yFULMQ6UhbY09GwiP6hjxZMVr8aROQy5lLHglqQzOuSZ4KeD85JjifHdKzlx8jjQ+uj+FLzHXPMAPmPU1JK9kpoHZ1oPshAFgPDpphJe+HwcJ8ezmk+3AEUr3wWli3xF+49y8Z2anASSVp6YI2YP95UT8Rlh3qT3T+V9V8rbSVislxA==", "SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-bb750dd426d95ee9390147a5624348ee.pem", "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"}' # noqa
|
|
|
|
|
|
def ses_soft_bounce_callback():
|
|
assert ses_soft_bounce_callback("my-reference") == '{ "Type" : "Notification", "MessageId" : "my-reference", "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing", "Message" : "{\\"notificationType\\":\\"Bounce\\",\\"bounce\\":{\\"bounceType\\":\\"Undetermined\\",\\"bounceSubType\\":\\"General\\"}, \\"mail\\":{\\"messageId\\":\\"%s\\",\\"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\\"}}", "Timestamp" : "2016-03-14T12:35:26.665Z", "SignatureVersion" : "1", "Signature" : "X8d7eTAOZ6wlnrdVVPYanrAlsX0SMPfOzhoTEBnQqYkrNWTqQY91C0f3bxtPdUhUtOowyPAOkTQ4KnZuzphfhVb2p1MyVYMxNKcBFB05/qaCX99+92fjw4x9LeUOwyGwMv5F0Vkfi5qZCcEw69uVrhYLVSTFTrzi/yCtru+yFULMQ6UhbY09GwiP6hjxZMVr8aROQy5lLHglqQzOuSZ4KeD85JjifHdKzlx8jjQ+uj+FLzHXPMAPmPU1JK9kpoHZ1oPshAFgPDpphJe+HwcJ8ezmk+3AEUr3wWli3xF+49y8Z2anASSVp6YI2YP95UT8Rlh3qT3T+V9V8rbSVislxA==", "SigningCertURL" : "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-bb750dd426d95ee9390147a5624348ee.pem", "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"}' # noqa
|