From 1d4d03dbe817a73c7d0baca9d96b0c0c99e761a9 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 4 Feb 2016 12:07:26 +0000 Subject: [PATCH] Update to create_sms_notification Removed the logic to check the api_user is the admin client user name. There is another controller method to handle sending the verification codes. --- app/notifications/rest.py | 31 ++++------ tests/app/notifications/test_rest.py | 89 ++-------------------------- 2 files changed, 17 insertions(+), 103 deletions(-) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index db7a6d22e..7e4889e9f 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -1,17 +1,14 @@ -import json - -import boto3 from flask import ( Blueprint, jsonify, - request, - current_app + request ) + from app import (notify_alpha_client, api_user) from app.aws_sqs import add_notification_to_queue -from app.dao import (templates_dao, services_dao) +from app.dao import (templates_dao) from app.schemas import ( - email_notification_schema, sms_admin_notification_schema, sms_template_notification_schema) + email_notification_schema, sms_template_notification_schema) notifications = Blueprint('notifications', __name__) @@ -25,21 +22,15 @@ def get_notifications(notification_id): def create_sms_notification(): resp_json = request.get_json() - # TODO: should create a different endpoint for the admin client to send verify codes. - if api_user['client'] == current_app.config.get('ADMIN_CLIENT_USER_NAME'): - notification, errors = sms_admin_notification_schema.load(resp_json) - if errors: - return jsonify(result="error", message=errors), 400 - template_id = 'admin' - message = notification['content'] - else: - notification, errors = sms_template_notification_schema.load(resp_json) - if errors: - return jsonify(result="error", message=errors), 400 - template_id = notification['template'] - message = notification['template'] + notification, errors = sms_template_notification_schema.load(resp_json) + if errors: + return jsonify(result="error", message=errors), 400 + template_id = notification['template'] + # TODO: remove once beta is reading notifications from the queue + message = templates_dao.get_model_templates(template_id).content add_notification_to_queue(api_user['client'], template_id, 'sms', notification) + # TODO: remove once beta is reading notifications from the queue return jsonify(notify_alpha_client.send_sms( mobile_number=notification['to'], message=message)), 200 diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index 27d598f37..4595e755f 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -136,86 +136,6 @@ def test_should_reject_bad_phone_numbers( assert not notify_alpha_client.send_sms.called -def test_should_reject_missing_content( - notify_api, notify_db, notify_db_session, mocker): - """ - Tests GET endpoint '/' to retrieve entire service list. - """ - with notify_api.test_request_context(): - with notify_api.test_client() as client: - mocker.patch( - 'app.notify_alpha_client.send_sms', - return_value='success' - ) - data = { - 'to': '+441234123123' - } - auth_header = create_authorization_header( - request_body=json.dumps(data), - path=url_for('notifications.create_sms_notification'), - method='POST') - - response = client.post( - url_for('notifications.create_sms_notification'), - 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 - assert json_resp['result'] == 'error' - assert 'Missing data for required field.' in json_resp['message']['content'] - assert not notify_alpha_client.send_sms.called - - -@moto.mock_sqs -def test_send_template_content(notify_api, - notify_db, - notify_db_session, - sqs_client_conn, - mocker): - """ - Test POST endpoint '/sms' with service notification. - """ - with notify_api.test_request_context(): - with notify_api.test_client() as client: - mobile = '+447719087678' - msg = 'Message content' - mocker.patch( - 'app.notify_alpha_client.send_sms', - return_value={ - "notification": { - "createdAt": "2015-11-03T09:37:27.414363Z", - "id": 100, - "jobId": 65, - "message": msg, - "method": "sms", - "status": "created", - "to": mobile - } - } - ) - data = { - 'to': mobile, - 'content': msg - } - auth_header = create_authorization_header( - request_body=json.dumps(data), - path=url_for('notifications.create_sms_notification'), - method='POST') - - response = client.post( - url_for('notifications.create_sms_notification'), - 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=mobile, - message=msg) - - def test_send_notification_restrict_mobile(notify_api, notify_db, notify_db_session, @@ -306,6 +226,8 @@ def test_should_allow_valid_message(notify_api, notify_db, notify_db_session, sqs_client_conn, + sample_user, + sample_template, mocker): """ Tests POST endpoint '/sms' with notifications-admin notification. @@ -322,13 +244,13 @@ def test_should_allow_valid_message(notify_api, "message": "valid", "method": "sms", "status": "created", - "to": "+449999999999" + "to": sample_user.mobile_number } } ) data = { 'to': '+441234123123', - 'content': 'valid' + 'template': sample_template.id } auth_header = create_authorization_header( request_body=json.dumps(data), @@ -343,7 +265,8 @@ def test_should_allow_valid_message(notify_api, 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="valid") + notify_alpha_client.send_sms.assert_called_with(mobile_number='+441234123123', + message=sample_template.content) @moto.mock_sqs