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):
temp = Template.query.first()
# TODO need better mapping from function params to sql query.
if template_id and service_id:
return Template.query.filter_by(
@@ -30,5 +31,5 @@ def get_model_templates(template_id=None, service_id=None):
elif template_id:
return Template.query.filter_by(id=template_id).one()
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()

View File

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

View File

@@ -13,7 +13,7 @@ itsdangerous==0.24
Flask-Bcrypt==0.6.2
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

View File

@@ -16,7 +16,7 @@ def sample_user(notify_db,
'name': 'Test User',
'email_address': email,
'password': 'password',
'mobile_number': '+44 7700 900986',
'mobile_number': '+447700900986',
'state': 'active'
}
usr = User.query.filter_by(email_address=email).first()
@@ -77,8 +77,10 @@ def sample_service(notify_db,
'limit': 1000,
'active': False,
'restricted': False}
service = Service(**data)
save_model_service(service)
service = Service.query.filter_by(name=service_name).first()
if not service:
service = Service(**data)
save_model_service(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,
email="test@digital.cabinet-office.gov.uk")
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,
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',
'email_address': email,
'password': 'password',
'mobile_number': '+44 7700 900986'
'mobile_number': '+447700900986'
}
user = User(**data)
save_model_user(user)

View File

@@ -1,6 +1,7 @@
from tests import create_authorization_header
from flask import url_for, json
from app import notify_alpha_client
from app.models import Service
def test_get_notifications(
@@ -82,7 +83,7 @@ def test_should_reject_if_no_phone_numbers(
)
data = {
'notification': {
'message': "my message"
'template': "my message"
}
}
auth_header = create_authorization_header(
@@ -97,12 +98,9 @@ def test_should_reject_if_no_phone_numbers(
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
print(json_resp)
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert len(json_resp['message']) == 1
assert len(json_resp['message']['to']) == 1
assert json_resp['message']['to'][0] == 'required'
assert 'Required data missing' in json_resp['message']['to'][0]
assert not notify_alpha_client.send_sms.called
@@ -120,7 +118,7 @@ def test_should_reject_bad_phone_numbers(
data = {
'notification': {
'to': 'invalid',
'message': "my message"
'template': "my message"
}
}
auth_header = create_authorization_header(
@@ -135,16 +133,13 @@ def test_should_reject_bad_phone_numbers(
headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True))
print(json_resp)
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert len(json_resp['message']) == 1
assert len(json_resp['message']['to']) == 1
assert json_resp['message']['to'][0] == 'invalid phone number, must be of format +441234123123'
assert 'invalid phone number, must be of format +441234123123' in json_resp['message']['to']
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):
"""
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))
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] == 'required'
assert 'Required data missing' in json_resp['message']['template']
assert not notify_alpha_client.send_sms.called
def test_should_reject_too_short_message(
notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id, mocker):
def test_send_template_content(notify_api,
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_client() as client:
mocker.patch(
'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 = {
'notification': {
'to': '+441234123123',
'message': ''
'to': sample_user.mobile_number,
'template': sample_template.id
}
}
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),
path=url_for('notifications.create_sms_notification'),
method='POST')
@@ -210,54 +264,13 @@ def test_should_reject_too_short_message(
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_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
assert 'Invalid phone number for restricted service' in json_resp['message']['to']
def test_should_allow_valid_message(
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_client() as client:
@@ -278,11 +291,10 @@ def test_should_allow_valid_message(
data = {
'notification': {
'to': '+441234123123',
'message': 'valid'
'template': 'valid'
}
}
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')
@@ -336,7 +348,6 @@ def test_send_email_valid_data(notify_api,
}
}
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')

View File

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