From 0580f5ab065d4768fb4764850b65d2b0f329afbb Mon Sep 17 00:00:00 2001 From: Adam Shimali Date: Mon, 8 Feb 2016 14:54:15 +0000 Subject: [PATCH] New endpoint for delivery app to use. Once removal of code that uses existing alpha is done, then duplicated code from /notifications/sms and the new endpoint can be merged. Job id is now avaiable in notificaiton but is not used yet. --- app/authentication/auth.py | 5 ++ app/notifications/rest.py | 32 ++++++++++ app/schemas.py | 1 + config.py | 11 ++-- tests/app/notifications/test_rest.py | 88 ++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 6 deletions(-) diff --git a/app/authentication/auth.py b/app/authentication/auth.py index 8cf9e187c..e80e6f8a7 100644 --- a/app/authentication/auth.py +++ b/app/authentication/auth.py @@ -58,6 +58,11 @@ def fetch_client(client): "client": client, "secret": [current_app.config.get('ADMIN_CLIENT_SECRET')] } + elif client == current_app.config.get('DELIVERY_CLIENT_USER_NAME'): + return { + "client": client, + "secret": [current_app.config.get('DELIVERY_CLIENT_SECRET')] + } else: return { "client": client, diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 7e4889e9f..4781cd10b 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -1,3 +1,5 @@ +import uuid + from flask import ( Blueprint, jsonify, @@ -49,3 +51,33 @@ def create_email_notification(): notification['body'], notification['from_address'], notification['subject'])) + + +@notifications.route('/sms/service/', methods=['POST']) +def create_sms_for_service(service_id): + + resp_json = request.get_json() + + notification, errors = sms_template_notification_schema.load(resp_json) + if errors: + return jsonify(result="error", message=errors), 400 + + template_id = notification['template'] + job_id = notification['job'] + + # TODO: job/job_id is in notification and can used to update job status + + # TODO: remove once beta is reading notifications from the queue + template = templates_dao.get_model_templates(template_id) + + if template.service.id != uuid.UUID(service_id): + message = "Invalid template: id {} for service id: {}".format(template.id, service_id) + return jsonify(result="error", message=message), 400 + + # Actual client is delivery app, but this is sent on behalf of service + add_notification_to_queue(service_id, template_id, 'sms', notification) + + # TODO: remove once beta is reading notifications from the queue + content = template.content + return jsonify(notify_alpha_client.send_sms( + mobile_number=notification['to'], message=content)), 200 diff --git a/app/schemas.py b/app/schemas.py index 981924765..7d4e329df 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -90,6 +90,7 @@ class SmsNotificationSchema(NotificationSchema): class SmsTemplateNotificationSchema(SmsNotificationSchema): template = fields.Int(required=True) + job = fields.String() @validates('template') def validate_template(self, value): diff --git a/config.py b/config.py index 8329e0edf..8f728f1e1 100644 --- a/config.py +++ b/config.py @@ -13,6 +13,8 @@ class Config(object): NOTIFY_DATA_API_AUTH_TOKEN = os.getenv('NOTIFY_API_TOKEN', "dev-token") ADMIN_CLIENT_USER_NAME = None ADMIN_CLIENT_SECRET = None + DELIVERY_CLIENT_USER_NAME = None + DELIVERY_CLIENT_SECRET = None AWS_REGION = 'eu-west-1' NOTIFY_JOB_QUEUE = os.getenv('NOTIFY_JOB_QUEUE', 'notify-jobs-queue') @@ -26,15 +28,12 @@ class Development(Config): DANGEROUS_SALT = 'dangerous-salt' ADMIN_CLIENT_USER_NAME = 'dev-notify-admin' ADMIN_CLIENT_SECRET = 'dev-notify-secret-key' + DELIVERY_CLIENT_USER_NAME = 'dev-notify-delivery' + DELIVERY_CLIENT_SECRET = 'dev-notify-secret-key' -class Test(Config): - DEBUG = True +class Test(Development): SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/test_notification_api' - SECRET_KEY = 'secret-key' - DANGEROUS_SALT = 'dangerous-salt' - ADMIN_CLIENT_USER_NAME = 'dev-notify-admin' - ADMIN_CLIENT_SECRET = 'dev-notify-secret-key' class Live(Config): diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 4595e755f..fb0f2aa8f 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -1,4 +1,5 @@ import moto +import uuid from tests import create_authorization_header from flask import url_for, json @@ -320,3 +321,90 @@ def test_send_email_valid_data(notify_api, assert json_resp['notification']['id'] == 100 notify_alpha_client.send_email.assert_called_with( to_address, message, from_address, subject) + + +@moto.mock_sqs +def test_valid_message_with_service_id(notify_api, + notify_db, + notify_db_session, + sqs_client_conn, + sample_user, + sample_template, + mocker): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + mocker.patch( + 'app.notify_alpha_client.send_sms', + return_value={ + "notification": { + "createdAt": "2015-11-03T09:37:27.414363Z", + "id": 100, + "jobId": 65, + "message": "valid", + "method": "sms", + "status": "created", + "to": sample_user.mobile_number + } + } + ) + job_id = uuid.uuid4() + service_id = sample_template.service.id + url = url_for('notifications.create_sms_for_service', service_id=service_id) + data = { + 'to': '+441234123123', + 'template': sample_template.id, + 'job': job_id + } + auth_header = create_authorization_header( + request_body=json.dumps(data), + path=url, + method='POST') + + response = client.post( + url, + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + + json_resp = json.loads(response.get_data(as_text=True)) + assert response.status_code == 200 + assert json_resp['notification']['id'] == 100 + notify_alpha_client.send_sms.assert_called_with(mobile_number='+441234123123', + message=sample_template.content) + + +@moto.mock_sqs +def test_message_with_incorrect_service_id_should_fail(notify_api, + notify_db, + notify_db_session, + sqs_client_conn, + sample_user, + sample_template, + mocker): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + job_id = uuid.uuid4() + invalid_service_id = uuid.uuid4() + + url = url_for('notifications.create_sms_for_service', service_id=invalid_service_id) + + data = { + 'to': '+441234123123', + 'template': sample_template.id, + 'job': job_id + } + + auth_header = create_authorization_header( + request_body=json.dumps(data), + path=url, + method='POST') + + response = client.post( + url, + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + + json_resp = json.loads(response.get_data(as_text=True)) + assert response.status_code == 400 + expected_error = 'Invalid template: id {} for service id: {}'.format(sample_template.id, + invalid_service_id) + assert json_resp['message'] == expected_error