mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-21 16:01:15 -05:00
Merge pull request #24 from alphagov/add_notification_email_endpoint
Added send_email endpoint.
This commit is contained in:
@@ -20,7 +20,6 @@ def get_notifications(notification_id):
|
|||||||
@notifications.route('/sms', methods=['POST'])
|
@notifications.route('/sms', methods=['POST'])
|
||||||
def create_sms_notification():
|
def create_sms_notification():
|
||||||
notification = request.get_json()['notification']
|
notification = request.get_json()['notification']
|
||||||
|
|
||||||
errors = {}
|
errors = {}
|
||||||
to_errors = validate_to(notification)
|
to_errors = validate_to(notification)
|
||||||
message_errors = validate_message(notification)
|
message_errors = validate_message(notification)
|
||||||
@@ -32,13 +31,28 @@ def create_sms_notification():
|
|||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
return jsonify(result="error", message=errors), 400
|
return jsonify(result="error", message=errors), 400
|
||||||
|
return jsonify(notify_alpha_client.send_sms(
|
||||||
return jsonify(notify_alpha_client.send_sms(mobile_number=notification['to'], message=notification['message'])), 200
|
mobile_number=notification['to'],
|
||||||
|
message=notification['message'])), 200
|
||||||
|
|
||||||
|
|
||||||
@notifications.route('/email', methods=['POST'])
|
@notifications.route('/email', methods=['POST'])
|
||||||
def create_email_notification():
|
def create_email_notification():
|
||||||
return jsonify(id=123)
|
notification = request.get_json()['notification']
|
||||||
|
errors = {}
|
||||||
|
for k in ['to', 'from', 'subject', 'message']:
|
||||||
|
k_error = validate_required_and_something(notification, k)
|
||||||
|
if k_error:
|
||||||
|
errors.update(k_error)
|
||||||
|
|
||||||
|
if errors:
|
||||||
|
return jsonify(result="error", message=errors), 400
|
||||||
|
|
||||||
|
return jsonify(notify_alpha_client.send_email(
|
||||||
|
notification['to'],
|
||||||
|
notification['message'],
|
||||||
|
notification['from'],
|
||||||
|
notification['subject']))
|
||||||
|
|
||||||
|
|
||||||
def validate_to(json_body):
|
def validate_to(json_body):
|
||||||
@@ -71,3 +85,10 @@ def validate_message(json_body):
|
|||||||
"message": errors
|
"message": errors
|
||||||
}
|
}
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def validate_required_and_something(json_body, field):
|
||||||
|
errors = []
|
||||||
|
if field not in json_body and json_body[field]:
|
||||||
|
errors.append('Required data for field.')
|
||||||
|
return {field: errors} if errors else None
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ def update_service(service_id):
|
|||||||
|
|
||||||
|
|
||||||
@service.route('/<int:service_id>', methods=['GET'])
|
@service.route('/<int:service_id>', methods=['GET'])
|
||||||
@service.route('/', methods=['GET'])
|
@service.route('', methods=['GET'])
|
||||||
def get_service(service_id=None):
|
def get_service(service_id=None):
|
||||||
try:
|
try:
|
||||||
services = get_model_services(service_id=service_id)
|
services = get_model_services(service_id=service_id)
|
||||||
@@ -114,7 +114,7 @@ def revoke_api_key(service_id):
|
|||||||
return jsonify(), 202
|
return jsonify(), 202
|
||||||
|
|
||||||
|
|
||||||
@service.route('/<int:service_id>/template/', methods=['POST'])
|
@service.route('/<int:service_id>/template', methods=['POST'])
|
||||||
def create_template(service_id):
|
def create_template(service_id):
|
||||||
try:
|
try:
|
||||||
service = get_model_services(service_id=service_id)
|
service = get_model_services(service_id=service_id)
|
||||||
|
|||||||
@@ -296,3 +296,58 @@ def test_should_allow_valid_message(
|
|||||||
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='valid')
|
||||||
|
|
||||||
|
|
||||||
|
def test_send_email_valid_data(notify_api,
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
sample_service,
|
||||||
|
sample_admin_service_id,
|
||||||
|
mocker):
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
|
||||||
|
to_address = "to@notify.com"
|
||||||
|
from_address = "from@notify.com"
|
||||||
|
subject = "This is the subject"
|
||||||
|
message = "This is the message"
|
||||||
|
mocker.patch(
|
||||||
|
'app.notify_alpha_client.send_email',
|
||||||
|
return_value={
|
||||||
|
"notification": {
|
||||||
|
"createdAt": "2015-11-03T09:37:27.414363Z",
|
||||||
|
"id": 100,
|
||||||
|
"jobId": 65,
|
||||||
|
"subject": subject,
|
||||||
|
"message": message,
|
||||||
|
"method": "email",
|
||||||
|
"status": "created",
|
||||||
|
"to": to_address,
|
||||||
|
"from": from_address
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
data = {
|
||||||
|
'notification': {
|
||||||
|
'to': to_address,
|
||||||
|
'from': from_address,
|
||||||
|
'subject': subject,
|
||||||
|
'message': message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auth_header = create_authorization_header(
|
||||||
|
service_id=sample_admin_service_id,
|
||||||
|
request_body=json.dumps(data),
|
||||||
|
path=url_for('notifications.create_email_notification'),
|
||||||
|
method='POST')
|
||||||
|
|
||||||
|
response = client.post(
|
||||||
|
url_for('notifications.create_email_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_email.assert_called_with(
|
||||||
|
to_address, message, from_address, subject)
|
||||||
|
|||||||
Reference in New Issue
Block a user