Sqs queues now populated from all create_notification api calls. Marshmallow schemas added for notification.

This commit is contained in:
Nicholas Staples
2016-02-03 13:16:19 +00:00
parent 02a38572bb
commit aba1cd2ed5
7 changed files with 184 additions and 161 deletions

View File

@@ -1,4 +1,3 @@
import boto3
import moto
from tests import create_authorization_header
@@ -85,9 +84,7 @@ def test_create_sms_should_reject_if_no_phone_numbers(
return_value='success'
)
data = {
'notification': {
'template': "my message"
}
'template': "my message"
}
auth_header = create_authorization_header(
service_id=sample_api_key.service_id,
@@ -103,7 +100,7 @@ def test_create_sms_should_reject_if_no_phone_numbers(
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert 'Required data missing' in json_resp['message']['to'][0]
assert 'Missing data for required field.' in json_resp['message']['to'][0]
assert not notify_alpha_client.send_sms.called
@@ -119,10 +116,8 @@ def test_should_reject_bad_phone_numbers(
return_value='success'
)
data = {
'notification': {
'to': 'invalid',
'template': "my message"
}
'to': 'invalid',
'template': "my message"
}
auth_header = create_authorization_header(
request_body=json.dumps(data),
@@ -137,7 +132,7 @@ def test_should_reject_bad_phone_numbers(
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert 'invalid phone number, must be of format +441234123123' in json_resp['message']['to']
assert 'Invalid phone number, must be of format +441234123123' in json_resp['message']['to']
assert not notify_alpha_client.send_sms.called
@@ -153,9 +148,7 @@ def test_should_reject_missing_content(
return_value='success'
)
data = {
'notification': {
'to': '+441234123123'
}
'to': '+441234123123'
}
auth_header = create_authorization_header(
request_body=json.dumps(data),
@@ -170,7 +163,7 @@ def test_should_reject_missing_content(
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert json_resp['result'] == 'error'
assert 'Required content' in json_resp['message']['content']
assert 'Missing data for required field.' in json_resp['message']['content']
assert not notify_alpha_client.send_sms.called
@@ -178,13 +171,13 @@ def test_should_reject_missing_content(
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:
set_up_mock_queue()
mobile = '+447719087678'
msg = 'Message content'
mocker.patch(
@@ -202,10 +195,8 @@ def test_send_template_content(notify_api,
}
)
data = {
'notification': {
'to': mobile,
'template': msg
}
'to': mobile,
'content': msg
}
auth_header = create_authorization_header(
request_body=json.dumps(data),
@@ -247,10 +238,8 @@ def test_send_notification_restrict_mobile(notify_api,
return_value={}
)
data = {
'notification': {
'to': invalid_mob,
'template': sample_template.id
}
'to': invalid_mob,
'template': sample_template.id
}
assert invalid_mob != sample_user.mobile_number
auth_header = create_authorization_header(
@@ -270,14 +259,59 @@ def test_send_notification_restrict_mobile(notify_api,
assert not notify_alpha_client.send_sms.called
def test_send_notification_invalid_template_id(notify_api,
notify_db,
notify_db_session,
sample_api_key,
sample_template,
sample_user,
mocker):
"""
Tests POST endpoint '/sms' with notifications-admin notification with invalid template id
"""
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 = {
'to': invalid_mob,
'template': 9999
}
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')
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 'Template not found' in json_resp['message']['template']
assert not notify_alpha_client.send_sms.called
@moto.mock_sqs
def test_should_allow_valid_message(notify_api, notify_db, notify_db_session, mocker):
def test_should_allow_valid_message(notify_api,
notify_db,
notify_db_session,
sqs_client_conn,
mocker):
"""
Tests POST endpoint '/sms' with notifications-admin notification.
"""
with notify_api.test_request_context():
with notify_api.test_client() as client:
set_up_mock_queue()
mocker.patch(
'app.notify_alpha_client.send_sms',
return_value={
@@ -293,10 +327,8 @@ def test_should_allow_valid_message(notify_api, notify_db, notify_db_session, mo
}
)
data = {
'notification': {
'to': '+441234123123',
'template': 'valid'
}
'to': '+441234123123',
'content': 'valid'
}
auth_header = create_authorization_header(
request_body=json.dumps(data),
@@ -314,11 +346,13 @@ def test_should_allow_valid_message(notify_api, notify_db, notify_db_session, mo
notify_alpha_client.send_sms.assert_called_with(mobile_number='+441234123123', message="valid")
@moto.mock_sqs
def test_send_email_valid_data(notify_api,
notify_db,
notify_db_session,
sample_service,
sample_admin_service_id,
sqs_client_conn,
mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
@@ -343,12 +377,10 @@ def test_send_email_valid_data(notify_api,
}
)
data = {
'notification': {
'to': to_address,
'from': from_address,
'subject': subject,
'message': message
}
'to': to_address,
'from': from_address,
'subject': subject,
'message': message
}
auth_header = create_authorization_header(
request_body=json.dumps(data),
@@ -365,9 +397,3 @@ 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)
def set_up_mock_queue():
# set up mock queue
boto3.setup_default_session(region_name='eu-west-1')
conn = boto3.resource('sqs')

View File

@@ -351,11 +351,9 @@ def test_send_user_code_for_email_uses_optional_to_field(notify_api,
def test_request_verify_code_schema_invalid_code_type(notify_api, notify_db, notify_db_session, sample_user):
import json
from app.schemas import request_verify_code_schema
data = json.dumps({'code_type': 'not_sms'})
code, error = request_verify_code_schema.loads(data)
assert code == {}
assert error == {'code_type': ['Invalid code type']}