mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-26 20:41:53 -05:00
Merge pull request #60 from alphagov/delivery-new-endpoint
New endpoint for delivery app to use.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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/<service_id>', 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
|
||||
|
||||
@@ -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):
|
||||
|
||||
11
config.py
11
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):
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user