Working code and tests.

This commit is contained in:
Nicholas Staples
2016-01-22 14:43:30 +00:00
parent 2c4f2c92b5
commit a9fe6ad469
8 changed files with 143 additions and 117 deletions

View File

@@ -23,6 +23,7 @@ def delete_model_template(template):
def get_model_templates(template_id=None, service_id=None): def get_model_templates(template_id=None, service_id=None):
temp = Template.query.first()
# TODO need better mapping from function params to sql query. # TODO need better mapping from function params to sql query.
if template_id and service_id: if template_id and service_id:
return Template.query.filter_by( return Template.query.filter_by(
@@ -30,5 +31,5 @@ def get_model_templates(template_id=None, service_id=None):
elif template_id: elif template_id:
return Template.query.filter_by(id=template_id).one() return Template.query.filter_by(id=template_id).one()
elif service_id: elif service_id:
return Template.query.filter_by(service=Service.get(service_id)).one() return Template.query.filter_by(service=Service.query.get(service_id)).one()
return Template.query.all() return Template.query.all()

View File

@@ -1,10 +1,13 @@
from flask import ( from flask import (
Blueprint, Blueprint,
jsonify, jsonify,
request request,
current_app
) )
from app import notify_alpha_client from app import notify_alpha_client
from app import api_user
from app.dao import (templates_dao, services_dao)
import re import re
mobile_regex = re.compile("^\\+44[\\d]{10}$") mobile_regex = re.compile("^\\+44[\\d]{10}$")
@@ -21,19 +24,19 @@ def get_notifications(notification_id):
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, to_errors = validate_to(notification, api_user['client'])
message_errors = validate_message(notification) print("create sms")
print(notification)
if to_errors: template, template_errors = validate_template(notification, api_user['client'])
if to_errors['to']:
errors.update(to_errors) errors.update(to_errors)
if message_errors: if template_errors['template']:
errors.update(message_errors) errors.update(template_errors)
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'], mobile_number=to,
message=notification['message'])), 200 message=template)), 200
@notifications.route('/email', methods=['POST']) @notifications.route('/email', methods=['POST'])
@@ -55,36 +58,45 @@ def create_email_notification():
notification['subject'])) notification['subject']))
def validate_to(json_body): def validate_to(json_body, service_id):
errors = [] errors = {"to": []}
mob = json_body.get('to', None)
if 'to' not in json_body: if not mob:
errors.append('required') errors['to'].append('Required data missing')
else: else:
if not mobile_regex.match(json_body['to']): if not mobile_regex.match(mob):
errors.append('invalid phone number, must be of format +441234123123') errors['to'].append('invalid phone number, must be of format +441234123123')
if errors: if service_id != current_app.config.get('ADMIN_CLIENT_USER_NAME'):
return { service = services_dao.get_model_services(service_id=service_id)
"to": errors if service.restricted:
} valid = False
return None for usr in service.users:
if mob == usr.mobile_number:
valid = True
break
if not valid:
errors['to'].append('Invalid phone number for restricted service')
return mob, errors
def validate_message(json_body): def validate_template(json_body, service_id):
errors = [] errors = {"template": []}
template_id = json_body.get('template', None)
if 'message' not in json_body: content = ''
errors.append('required') if not template_id:
errors['template'].append('Required data missing')
else: else:
message_length = len(json_body['message']) if service_id == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
if message_length < 1 or message_length > 160: content = json_body['template']
errors.append('Invalid length. [1 - 160]') else:
try:
if errors: template = templates_dao.get_model_templates(
return { template_id=json_body['template'],
"message": errors service_id=service_id)
} content = template.content
return None except:
errors['template'].append("Unable to load template.")
return content, errors
def validate_required_and_something(json_body, field): def validate_required_and_something(json_body, field):

View File

@@ -13,7 +13,7 @@ itsdangerous==0.24
Flask-Bcrypt==0.6.2 Flask-Bcrypt==0.6.2
credstash==1.8.0 credstash==1.8.0
git+https://github.com/alphagov/notifications-python-client.git@0.2.0#egg=notifications-python-client==0.2.0 git+https://github.com/alphagov/notifications-python-client.git@0.2.1#egg=notifications-python-client==0.2.1
git+https://github.com/alphagov/notifications-utils.git@0.0.3#egg=notifications-utils==0.0.3 git+https://github.com/alphagov/notifications-utils.git@0.0.3#egg=notifications-utils==0.0.3

View File

@@ -16,7 +16,7 @@ def sample_user(notify_db,
'name': 'Test User', 'name': 'Test User',
'email_address': email, 'email_address': email,
'password': 'password', 'password': 'password',
'mobile_number': '+44 7700 900986', 'mobile_number': '+447700900986',
'state': 'active' 'state': 'active'
} }
usr = User.query.filter_by(email_address=email).first() usr = User.query.filter_by(email_address=email).first()
@@ -77,8 +77,10 @@ def sample_service(notify_db,
'limit': 1000, 'limit': 1000,
'active': False, 'active': False,
'restricted': False} 'restricted': False}
service = Service(**data) service = Service.query.filter_by(name=service_name).first()
save_model_service(service) if not service:
service = Service(**data)
save_model_service(service)
return service return service

View File

@@ -52,7 +52,7 @@ def test_get_jobs_for_service(notify_db, notify_db_session, sample_template):
other_user = create_user(notify_db, notify_db_session, other_user = create_user(notify_db, notify_db_session,
email="test@digital.cabinet-office.gov.uk") email="test@digital.cabinet-office.gov.uk")
other_service = create_service(notify_db, notify_db_session, other_service = create_service(notify_db, notify_db_session,
user=other_user) user=other_user, service_name="other service")
other_template = create_template(notify_db, notify_db_session, other_template = create_template(notify_db, notify_db_session,
service=other_service) service=other_service)
other_job = create_job(notify_db, notify_db_session, service=other_service, other_job = create_job(notify_db, notify_db_session, service=other_service,

View File

@@ -12,7 +12,7 @@ def test_create_user(notify_api, notify_db, notify_db_session):
'name': 'Test User', 'name': 'Test User',
'email_address': email, 'email_address': email,
'password': 'password', 'password': 'password',
'mobile_number': '+44 7700 900986' 'mobile_number': '+447700900986'
} }
user = User(**data) user = User(**data)
save_model_user(user) save_model_user(user)

View File

@@ -1,6 +1,7 @@
from tests import create_authorization_header from tests import create_authorization_header
from flask import url_for, json from flask import url_for, json
from app import notify_alpha_client from app import notify_alpha_client
from app.models import Service
def test_get_notifications( def test_get_notifications(
@@ -82,7 +83,7 @@ def test_should_reject_if_no_phone_numbers(
) )
data = { data = {
'notification': { 'notification': {
'message': "my message" 'template': "my message"
} }
} }
auth_header = create_authorization_header( auth_header = create_authorization_header(
@@ -97,12 +98,9 @@ def test_should_reject_if_no_phone_numbers(
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True)) json_resp = json.loads(response.get_data(as_text=True))
print(json_resp)
assert response.status_code == 400 assert response.status_code == 400
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert len(json_resp['message']) == 1 assert 'Required data missing' in json_resp['message']['to'][0]
assert len(json_resp['message']['to']) == 1
assert json_resp['message']['to'][0] == 'required'
assert not notify_alpha_client.send_sms.called assert not notify_alpha_client.send_sms.called
@@ -120,7 +118,7 @@ def test_should_reject_bad_phone_numbers(
data = { data = {
'notification': { 'notification': {
'to': 'invalid', 'to': 'invalid',
'message': "my message" 'template': "my message"
} }
} }
auth_header = create_authorization_header( auth_header = create_authorization_header(
@@ -135,16 +133,13 @@ def test_should_reject_bad_phone_numbers(
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True)) json_resp = json.loads(response.get_data(as_text=True))
print(json_resp)
assert response.status_code == 400 assert response.status_code == 400
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert len(json_resp['message']) == 1 assert 'invalid phone number, must be of format +441234123123' in json_resp['message']['to']
assert len(json_resp['message']['to']) == 1
assert json_resp['message']['to'][0] == 'invalid phone number, must be of format +441234123123'
assert not notify_alpha_client.send_sms.called assert not notify_alpha_client.send_sms.called
def test_should_reject_missing_message( def test_should_reject_missing_template(
notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id, mocker): notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id, mocker):
""" """
Tests GET endpoint '/' to retrieve entire service list. Tests GET endpoint '/' to retrieve entire service list.
@@ -174,31 +169,90 @@ def test_should_reject_missing_message(
json_resp = json.loads(response.get_data(as_text=True)) json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400 assert response.status_code == 400
assert json_resp['result'] == 'error' assert json_resp['result'] == 'error'
assert len(json_resp['message']) == 1 assert 'Required data missing' in json_resp['message']['template']
assert len(json_resp['message']['message']) == 1
assert json_resp['message']['message'][0] == 'required'
assert not notify_alpha_client.send_sms.called assert not notify_alpha_client.send_sms.called
def test_should_reject_too_short_message( def test_send_template_content(notify_api,
notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id, mocker): notify_db,
notify_db_session,
sample_api_key,
sample_template,
sample_user,
mocker):
""" """
Tests GET endpoint '/' to retrieve entire service list. Test POST endpoint '/sms' with service notification.
""" """
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
mocker.patch( mocker.patch(
'app.notify_alpha_client.send_sms', 'app.notify_alpha_client.send_sms',
return_value='success' return_value={
"notification": {
"createdAt": "2015-11-03T09:37:27.414363Z",
"id": 100,
"jobId": 65,
"message": sample_template.content,
"method": "sms",
"status": "created",
"to": sample_user.mobile_number
}
}
) )
data = { data = {
'notification': { 'notification': {
'to': '+441234123123', 'to': sample_user.mobile_number,
'message': '' 'template': sample_template.id
} }
} }
auth_header = create_authorization_header( auth_header = create_authorization_header(
service_id=sample_admin_service_id, service_id=sample_template.service.id,
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=sample_user.mobile_number,
message=sample_template.content)
def test_send_notification_restrict_mobile(notify_api,
notify_db,
notify_db_session,
sample_api_key,
sample_template,
sample_user,
mocker):
"""
Test POST endpoint '/sms' with service notification with mobile number
not in restricted list.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
Service.query.filter_by(
id=sample_template.service.id).update({'restricted': True})
invalid_mob = '+449999999999'
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value={}
)
data = {
'notification': {
'to': invalid_mob,
'template': sample_template.id
}
}
assert invalid_mob != sample_user.mobile_number
auth_header = create_authorization_header(
service_id=sample_template.service.id,
request_body=json.dumps(data), request_body=json.dumps(data),
path=url_for('notifications.create_sms_notification'), path=url_for('notifications.create_sms_notification'),
method='POST') method='POST')
@@ -210,54 +264,13 @@ def test_should_reject_too_short_message(
json_resp = json.loads(response.get_data(as_text=True)) json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400 assert response.status_code == 400
assert json_resp['result'] == 'error' assert 'Invalid phone number for restricted service' in json_resp['message']['to']
assert len(json_resp['message']) == 1
assert len(json_resp['message']['message']) == 1
assert json_resp['message']['message'][0] == 'Invalid length. [1 - 160]'
assert not notify_alpha_client.send_sms.called
def test_should_reject_too_long_message(
notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id, 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 = {
'notification': {
'to': '+441234123123',
'message': '1' * 161
}
}
auth_header = create_authorization_header(
service_id=sample_admin_service_id,
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 len(json_resp['message']) == 1
assert len(json_resp['message']['message']) == 1
assert json_resp['message']['message'][0] == 'Invalid length. [1 - 160]'
assert not notify_alpha_client.send_sms.called
def test_should_allow_valid_message( def test_should_allow_valid_message(
notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id, mocker): notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id, mocker):
""" """
Tests GET endpoint '/' to retrieve entire service list. Tests POST endpoint '/sms' with notifications-admin notification.
""" """
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
@@ -278,11 +291,10 @@ def test_should_allow_valid_message(
data = { data = {
'notification': { 'notification': {
'to': '+441234123123', 'to': '+441234123123',
'message': 'valid' 'template': 'valid'
} }
} }
auth_header = create_authorization_header( auth_header = create_authorization_header(
service_id=sample_admin_service_id,
request_body=json.dumps(data), request_body=json.dumps(data),
path=url_for('notifications.create_sms_notification'), path=url_for('notifications.create_sms_notification'),
method='POST') method='POST')
@@ -336,7 +348,6 @@ def test_send_email_valid_data(notify_api,
} }
} }
auth_header = create_authorization_header( auth_header = create_authorization_header(
service_id=sample_admin_service_id,
request_body=json.dumps(data), request_body=json.dumps(data),
path=url_for('notifications.create_email_notification'), path=url_for('notifications.create_email_notification'),
method='POST') method='POST')

View File

@@ -23,7 +23,7 @@ def test_get_user_list(notify_api, notify_db, notify_db_session, sample_user, sa
"name": "Test User", "name": "Test User",
"email_address": sample_user.email_address, "email_address": sample_user.email_address,
"id": sample_user.id, "id": sample_user.id,
"mobile_number": "+44 7700 900986", "mobile_number": "+447700900986",
"password_changed_at": None, "password_changed_at": None,
"logged_in_at": None, "logged_in_at": None,
"state": "active", "state": "active",
@@ -50,7 +50,7 @@ def test_get_user(notify_api, notify_db, notify_db_session, sample_user, sample_
"name": "Test User", "name": "Test User",
"email_address": sample_user.email_address, "email_address": sample_user.email_address,
"id": sample_user.id, "id": sample_user.id,
"mobile_number": "+44 7700 900986", "mobile_number": "+447700900986",
"password_changed_at": None, "password_changed_at": None,
"logged_in_at": None, "logged_in_at": None,
"state": "active", "state": "active",
@@ -70,7 +70,7 @@ def test_post_user(notify_api, notify_db, notify_db_session, sample_admin_servic
"name": "Test User", "name": "Test User",
"email_address": "user@digital.cabinet-office.gov.uk", "email_address": "user@digital.cabinet-office.gov.uk",
"password": "password", "password": "password",
"mobile_number": "+44 7700 900986", "mobile_number": "+447700900986",
"password_changed_at": None, "password_changed_at": None,
"logged_in_at": None, "logged_in_at": None,
"state": "active", "state": "active",
@@ -103,7 +103,7 @@ def test_post_user_missing_attribute_email(notify_api, notify_db, notify_db_sess
data = { data = {
"name": "Test User", "name": "Test User",
"password": "password", "password": "password",
"mobile_number": "+44 7700 900986", "mobile_number": "+447700900986",
"password_changed_at": None, "password_changed_at": None,
"logged_in_at": None, "logged_in_at": None,
"state": "active", "state": "active",
@@ -134,7 +134,7 @@ def test_post_user_missing_attribute_password(notify_api, notify_db, notify_db_s
data = { data = {
"name": "Test User", "name": "Test User",
"email_address": "user@digital.cabinet-office.gov.uk", "email_address": "user@digital.cabinet-office.gov.uk",
"mobile_number": "+44 7700 900986", "mobile_number": "+447700900986",
"password_changed_at": None, "password_changed_at": None,
"logged_in_at": None, "logged_in_at": None,
"state": "active", "state": "active",
@@ -182,7 +182,7 @@ def test_put_user(notify_api, notify_db, notify_db_session, sample_user, sample_
expected = { expected = {
"name": "Test User", "name": "Test User",
"email_address": new_email, "email_address": new_email,
"mobile_number": "+44 7700 900986", "mobile_number": "+447700900986",
"password_changed_at": None, "password_changed_at": None,
"id": user.id, "id": user.id,
"logged_in_at": None, "logged_in_at": None,
@@ -333,7 +333,7 @@ def test_delete_user(notify_api, notify_db, notify_db_session, sample_user, samp
expected = { expected = {
"name": "Test User", "name": "Test User",
"email_address": sample_user.email_address, "email_address": sample_user.email_address,
"mobile_number": "+44 7700 900986", "mobile_number": "+447700900986",
"password_changed_at": None, "password_changed_at": None,
"id": sample_user.id, "id": sample_user.id,
"logged_in_at": None, "logged_in_at": None,