mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-11 18:38:14 -04:00
Add autoconfirm sns in dvla callback
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
from flask import (
|
from flask import (
|
||||||
Blueprint,
|
Blueprint,
|
||||||
@@ -11,37 +12,52 @@ from flask import (
|
|||||||
from app import statsd_client
|
from app import statsd_client
|
||||||
from app.celery.tasks import update_letter_notifications_statuses
|
from app.celery.tasks import update_letter_notifications_statuses
|
||||||
from app.clients.email.aws_ses import get_aws_responses
|
from app.clients.email.aws_ses import get_aws_responses
|
||||||
from app.dao import (
|
from app.dao import notifications_dao
|
||||||
notifications_dao
|
from app.v2.errors import register_errors
|
||||||
)
|
|
||||||
|
|
||||||
from app.notifications.process_client_response import validate_callback_data
|
from app.notifications.process_client_response import validate_callback_data
|
||||||
|
from app.notifications.utils import autoconfirm_subscription
|
||||||
|
from app.schema_validation import validate
|
||||||
|
|
||||||
|
|
||||||
letter_callback_blueprint = Blueprint('notifications_letter_callback', __name__)
|
letter_callback_blueprint = Blueprint('notifications_letter_callback', __name__)
|
||||||
|
|
||||||
from app.errors import (
|
|
||||||
register_errors,
|
|
||||||
InvalidRequest
|
|
||||||
)
|
|
||||||
|
|
||||||
register_errors(letter_callback_blueprint)
|
register_errors(letter_callback_blueprint)
|
||||||
|
|
||||||
|
|
||||||
|
dvla_sns_callback_schema = {
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"description": "sns callback received on s3 update",
|
||||||
|
"type": "object",
|
||||||
|
"title": "dvla internal sns callback",
|
||||||
|
"properties": {
|
||||||
|
"Type": {"enum": ["Notification", "SubscriptionConfirmation"]},
|
||||||
|
"MessageId": {"type": "string"},
|
||||||
|
"Message": {"type": ["string", "object"]}
|
||||||
|
},
|
||||||
|
"required": ["Type", "MessageId", "Message"]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def validate_schema(schema):
|
||||||
|
def decorator(f):
|
||||||
|
@wraps(f)
|
||||||
|
def wrapper(*args, **kw):
|
||||||
|
validate(request.json, schema)
|
||||||
|
return f(*args, **kw)
|
||||||
|
return wrapper
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
@letter_callback_blueprint.route('/notifications/letter/dvla', methods=['POST'])
|
@letter_callback_blueprint.route('/notifications/letter/dvla', methods=['POST'])
|
||||||
|
@validate_schema(dvla_sns_callback_schema)
|
||||||
def process_letter_response():
|
def process_letter_response():
|
||||||
try:
|
req_json = request.json
|
||||||
req_json = json.loads(request.data)
|
if not autoconfirm_subscription(req_json):
|
||||||
# The callback should have one record for an S3 Put Event.
|
# The callback should have one record for an S3 Put Event.
|
||||||
filename = req_json['Message']['Records'][0]['s3']['object']['key']
|
filename = req_json['Message']['Records'][0]['s3']['object']['key']
|
||||||
|
current_app.logger.info('Received file from DVLA: {}'.format(filename))
|
||||||
except (ValueError, KeyError):
|
|
||||||
error = "DVLA callback failed: Invalid JSON"
|
|
||||||
raise InvalidRequest(error, status_code=400)
|
|
||||||
|
|
||||||
else:
|
|
||||||
current_app.logger.info('DVLA callback: Calling task to update letter notifications')
|
current_app.logger.info('DVLA callback: Calling task to update letter notifications')
|
||||||
update_letter_notifications_statuses.apply_async([filename], queue='notify')
|
update_letter_notifications_statuses.apply_async([filename], queue='notify')
|
||||||
|
|
||||||
return jsonify(
|
return jsonify(
|
||||||
result="success", message="DVLA callback succeeded"
|
result="success", message="DVLA callback succeeded"
|
||||||
), 200
|
), 200
|
||||||
|
|||||||
@@ -16,3 +16,10 @@ def confirm_subscription(confirmation_request):
|
|||||||
raise e
|
raise e
|
||||||
|
|
||||||
return confirmation_request['TopicArn']
|
return confirmation_request['TopicArn']
|
||||||
|
|
||||||
|
|
||||||
|
def autoconfirm_subscription(req_json):
|
||||||
|
if req_json.get('Type') == 'SubscriptionConfirmation':
|
||||||
|
current_app.logger.info("SNS subscription confirmation url: {}".format(req_json['SubscribeURL']))
|
||||||
|
subscribed_topic = confirm_subscription(req_json)
|
||||||
|
return subscribed_topic
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from app.dao.notifications_dao import (
|
|||||||
get_notification_by_id
|
get_notification_by_id
|
||||||
)
|
)
|
||||||
from app.models import NotificationStatistics
|
from app.models import NotificationStatistics
|
||||||
|
from tests.app.notifications.test_notifications_ses_callback import ses_confirmation_callback
|
||||||
from tests.app.conftest import sample_notification as create_sample_notification
|
from tests.app.conftest import sample_notification as create_sample_notification
|
||||||
|
|
||||||
|
|
||||||
@@ -22,24 +23,41 @@ def test_dvla_callback_returns_400_with_invalid_request(client):
|
|||||||
data=data,
|
data=data,
|
||||||
headers=[('Content-Type', 'application/json')]
|
headers=[('Content-Type', 'application/json')]
|
||||||
)
|
)
|
||||||
|
|
||||||
json_resp = json.loads(response.get_data(as_text=True))
|
json_resp = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert json_resp['result'] == 'error'
|
|
||||||
assert json_resp['message'] == 'DVLA callback failed: Invalid JSON'
|
|
||||||
|
|
||||||
|
|
||||||
def test_dvla_callback_returns_200_with_valid_request(client, mocker):
|
def test_dvla_callback_autoconfirms_subscription(client, mocker):
|
||||||
data = _sample_sns_s3_callback()
|
autoconfirm_mock = mocker.patch('app.notifications.notifications_letter_callback.autoconfirm_subscription')
|
||||||
mocker.patch('app.notifications.notifications_letter_callback.update_letter_notifications_statuses.apply_async')
|
|
||||||
|
data = ses_confirmation_callback()
|
||||||
response = client.post(
|
response = client.post(
|
||||||
path='/notifications/letter/dvla',
|
path='/notifications/letter/dvla',
|
||||||
data=data,
|
data=data,
|
||||||
headers=[('Content-Type', 'application/json')]
|
headers=[('Content-Type', 'application/json')]
|
||||||
)
|
)
|
||||||
json_resp = json.loads(response.get_data(as_text=True))
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
assert autoconfirm_mock.called
|
||||||
|
|
||||||
|
|
||||||
|
def test_dvla_callback_autoconfirm_does_not_call_update_letter_notifications_task(client, mocker):
|
||||||
|
autoconfirm_mock = mocker.patch('app.notifications.notifications_letter_callback.autoconfirm_subscription')
|
||||||
|
update_task = \
|
||||||
|
mocker.patch('app.notifications.notifications_letter_callback.update_letter_notifications_statuses.apply_async')
|
||||||
|
|
||||||
|
data = ses_confirmation_callback()
|
||||||
|
response = client.post(
|
||||||
|
path='/notifications/letter/dvla',
|
||||||
|
data=data,
|
||||||
|
headers=[('Content-Type', 'application/json')]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert autoconfirm_mock.called
|
||||||
|
assert not update_task.called
|
||||||
|
|
||||||
|
|
||||||
def test_dvla_callback_calls_update_letter_notifications_task(client, mocker):
|
def test_dvla_callback_calls_update_letter_notifications_task(client, mocker):
|
||||||
@@ -54,7 +72,7 @@ def test_dvla_callback_calls_update_letter_notifications_task(client, mocker):
|
|||||||
json_resp = json.loads(response.get_data(as_text=True))
|
json_resp = json.loads(response.get_data(as_text=True))
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert update_task.called is True
|
assert update_task.called
|
||||||
update_task.assert_called_with(['bar.txt'], queue='notify')
|
update_task.assert_called_with(['bar.txt'], queue='notify')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user