mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 08:51:30 -05:00
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.
This commit is contained in:
@@ -1,17 +1,14 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
import boto3
|
|
||||||
from flask import (
|
from flask import (
|
||||||
Blueprint,
|
Blueprint,
|
||||||
jsonify,
|
jsonify,
|
||||||
request,
|
request
|
||||||
current_app
|
|
||||||
)
|
)
|
||||||
|
|
||||||
from app import (notify_alpha_client, api_user)
|
from app import (notify_alpha_client, api_user)
|
||||||
from app.aws_sqs import add_notification_to_queue
|
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 (
|
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__)
|
notifications = Blueprint('notifications', __name__)
|
||||||
|
|
||||||
@@ -25,21 +22,15 @@ def get_notifications(notification_id):
|
|||||||
def create_sms_notification():
|
def create_sms_notification():
|
||||||
resp_json = request.get_json()
|
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)
|
notification, errors = sms_template_notification_schema.load(resp_json)
|
||||||
if errors:
|
if errors:
|
||||||
return jsonify(result="error", message=errors), 400
|
return jsonify(result="error", message=errors), 400
|
||||||
template_id = notification['template']
|
template_id = notification['template']
|
||||||
message = 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)
|
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(
|
return jsonify(notify_alpha_client.send_sms(
|
||||||
mobile_number=notification['to'], message=message)), 200
|
mobile_number=notification['to'], message=message)), 200
|
||||||
|
|
||||||
|
|||||||
@@ -136,86 +136,6 @@ def test_should_reject_bad_phone_numbers(
|
|||||||
assert not notify_alpha_client.send_sms.called
|
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,
|
def test_send_notification_restrict_mobile(notify_api,
|
||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
@@ -306,6 +226,8 @@ def test_should_allow_valid_message(notify_api,
|
|||||||
notify_db,
|
notify_db,
|
||||||
notify_db_session,
|
notify_db_session,
|
||||||
sqs_client_conn,
|
sqs_client_conn,
|
||||||
|
sample_user,
|
||||||
|
sample_template,
|
||||||
mocker):
|
mocker):
|
||||||
"""
|
"""
|
||||||
Tests POST endpoint '/sms' with notifications-admin notification.
|
Tests POST endpoint '/sms' with notifications-admin notification.
|
||||||
@@ -322,13 +244,13 @@ def test_should_allow_valid_message(notify_api,
|
|||||||
"message": "valid",
|
"message": "valid",
|
||||||
"method": "sms",
|
"method": "sms",
|
||||||
"status": "created",
|
"status": "created",
|
||||||
"to": "+449999999999"
|
"to": sample_user.mobile_number
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
data = {
|
data = {
|
||||||
'to': '+441234123123',
|
'to': '+441234123123',
|
||||||
'content': 'valid'
|
'template': sample_template.id
|
||||||
}
|
}
|
||||||
auth_header = create_authorization_header(
|
auth_header = create_authorization_header(
|
||||||
request_body=json.dumps(data),
|
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))
|
json_resp = json.loads(response.get_data(as_text=True))
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json_resp['notification']['id'] == 100
|
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
|
@moto.mock_sqs
|
||||||
|
|||||||
Reference in New Issue
Block a user