Add tests for autoconfirmation

This commit is contained in:
Athanasios Voutsadakis
2017-04-25 17:01:38 +01:00
parent 04b003c152
commit 74433c9335
3 changed files with 52 additions and 2 deletions

View File

@@ -36,6 +36,9 @@ def process_ses_response():
current_app.logger.info("SNS subscription confirmation url: {}".format(ses_request['SubscribeURL']))
subscribed_topic = confirm_subscription(ses_request)
current_app.logger.info("Automatically subscribed to topic: {}".format(subscribed_topic))
return jsonify(
result="success", message="SES callback succeeded"
), 200
errors = validate_callback_data(data=ses_request, fields=['Message'], client_name=client_name)
if errors:

View File

@@ -4,5 +4,5 @@ import requests
def confirm_subscription(confirmation_request):
url = confirmation_request['SubscribeURL']
response = requests.get(url)
if response.code < 400:
return confirmation_request['TopicArn']
response.raise_for_status()
return confirmation_request['TopicArn']

View File

@@ -2,6 +2,8 @@ from datetime import datetime
from flask import json
from freezegun import freeze_time
from requests import HTTPError
import pytest
from app import statsd_client
from app.dao.notifications_dao import get_notification_by_id
@@ -29,6 +31,48 @@ def test_ses_callback_should_fail_if_invalid_json(client):
assert json_resp['message'] == 'SES callback failed: invalid json'
def test_ses_callback_should_auto_confirm_subscriptions(client, rmock):
endpoint = json.loads(ses_confirmation_callback())['SubscribeURL']
rmock.request(
"GET",
endpoint,
json={"status": "success"},
status_code=200)
response = client.post(
path='/notifications/email/ses',
data=ses_confirmation_callback(),
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
)
json_resp = json.loads(response.get_data(as_text=True))
assert rmock.called
assert rmock.request_history[0].url == endpoint
assert response.status_code == 200
assert json_resp['result'] == 'success'
assert json_resp['message'] == 'SES callback succeeded'
def test_ses_callback_autoconfirmat_raises_exception_if_not_200(client, rmock):
endpoint = json.loads(ses_confirmation_callback())['SubscribeURL']
rmock.request(
"GET",
endpoint,
json={"status": "not allowed"},
status_code=405)
with pytest.raises(HTTPError) as exc:
client.post(
path='/notifications/email/ses',
data=ses_confirmation_callback(),
headers=[('Content-Type', 'text/plain; charset=UTF-8')]
)
assert rmock.called
assert rmock.request_history[0].url == endpoint
assert exc.value.response.status_code == 405
def test_ses_callback_should_fail_if_invalid_notification_type(client):
response = client.post(
path='/notifications/email/ses',
@@ -256,3 +300,6 @@ def ses_hard_bounce_callback():
def ses_soft_bounce_callback():
return b'{\n "Type" : "Notification",\n "MessageId" : "ref",\n "TopicArn" : "arn:aws:sns:eu-west-1:123456789012:testing",\n "Message" : "{\\"notificationType\\":\\"Bounce\\",\\"bounce\\":{\\"bounceType\\":\\"Undetermined\\",\\"bounceSubType\\":\\"General\\"}, \\"mail\\":{\\"messageId\\":\\"ref\\",\\"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
def ses_confirmation_callback():
return b'{\n "Type": "SubscriptionConfirmation",\n "MessageId": "165545c9-2a5c-472c-8df2-7ff2be2b3b1b",\n "Token": "2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736",\n "TopicArn": "arn:aws:sns:us-west-2:123456789012:MyTopic",\n "Message": "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:123456789012:MyTopic.\\nTo confirm the subscription, visit the SubscribeURL included in this message.",\n "SubscribeURL": "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:123456789012:MyTopic&Token=2336412f37fb687f5d51e6e241d09c805a5a57b30d712f794cc5f6a988666d92768dd60a747ba6f3beb71854e285d6ad02428b09ceece29417f1f02d609c582afbacc99c583a916b9981dd2728f4ae6fdb82efd087cc3b7849e05798d2d2785c03b0879594eeac82c01f235d0e717736",\n "Timestamp": "2012-04-26T20:45:04.751Z",\n "SignatureVersion": "1",\n "Signature": "EXAMPLEpH+DcEwjAPg8O9mY8dReBSwksfg2S7WKQcikcNKWLQjwu6A4VbeS0QHVCkhRS7fUQvi2egU3N858fiTDN6bkkOxYDVrY0Ad8L10Hs3zH81mtnPk5uvvolIC1CXGu43obcgFxeL3khZl8IKvO61GWB6jI9b5+gLPoBc1Q=",\n "SigningCertURL": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem"\n}' # noqa