2016-01-27 17:42:05 +00:00
|
|
|
import moto
|
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
from tests import create_authorization_header
|
|
|
|
|
from flask import url_for, json
|
|
|
|
|
from app import notify_alpha_client
|
2016-01-22 14:43:30 +00:00
|
|
|
from app.models import Service
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_notifications(
|
2016-01-28 11:06:24 +00:00
|
|
|
notify_api, notify_db, notify_db_session, sample_api_key, mocker):
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
|
|
|
|
Tests GET endpoint '/' to retrieve entire service list.
|
|
|
|
|
"""
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
mocker.patch(
|
2016-01-19 13:45:57 +00:00
|
|
|
'app.notify_alpha_client.fetch_notification_by_id',
|
2016-01-19 11:23:09 +00:00
|
|
|
return_value={
|
|
|
|
|
'notifications': [
|
|
|
|
|
{
|
|
|
|
|
'id': 'my_id',
|
|
|
|
|
'notification': 'some notify'
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
2016-01-28 11:06:24 +00:00
|
|
|
service_id=sample_api_key.service_id,
|
2016-01-19 13:45:57 +00:00
|
|
|
path=url_for('notifications.get_notifications', notification_id=123),
|
2016-01-19 11:23:09 +00:00
|
|
|
method='GET')
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
2016-01-19 13:45:57 +00:00
|
|
|
url_for('notifications.get_notifications', notification_id=123),
|
2016-01-19 11:23:09 +00:00
|
|
|
headers=[auth_header])
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert len(json_resp['notifications']) == 1
|
|
|
|
|
assert json_resp['notifications'][0]['id'] == 'my_id'
|
|
|
|
|
assert json_resp['notifications'][0]['notification'] == 'some notify'
|
2016-01-19 13:45:57 +00:00
|
|
|
notify_alpha_client.fetch_notification_by_id.assert_called_with("123")
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_notifications_empty_result(
|
2016-01-28 11:06:24 +00:00
|
|
|
notify_api, notify_db, notify_db_session, sample_api_key, mocker):
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
|
|
|
|
Tests GET endpoint '/' to retrieve entire service list.
|
|
|
|
|
"""
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
|
|
|
|
mocker.patch(
|
2016-01-19 13:45:57 +00:00
|
|
|
'app.notify_alpha_client.fetch_notification_by_id',
|
2016-01-19 11:23:09 +00:00
|
|
|
return_value={
|
|
|
|
|
'notifications': [
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(
|
2016-01-28 11:06:24 +00:00
|
|
|
service_id=sample_api_key.service_id,
|
2016-01-19 13:45:57 +00:00
|
|
|
path=url_for('notifications.get_notifications', notification_id=123),
|
2016-01-19 11:23:09 +00:00
|
|
|
method='GET')
|
|
|
|
|
|
|
|
|
|
response = client.get(
|
2016-01-19 13:45:57 +00:00
|
|
|
url_for('notifications.get_notifications', notification_id=123),
|
2016-01-19 11:23:09 +00:00
|
|
|
headers=[auth_header])
|
|
|
|
|
|
|
|
|
|
json_resp = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert len(json_resp['notifications']) == 0
|
2016-01-19 13:45:57 +00:00
|
|
|
notify_alpha_client.fetch_notification_by_id.assert_called_with("123")
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
2016-01-28 11:06:24 +00:00
|
|
|
def test_create_sms_should_reject_if_no_phone_numbers(
|
|
|
|
|
notify_api, notify_db, notify_db_session, sample_api_key, mocker):
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
|
|
|
|
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 = {
|
2016-02-03 13:16:19 +00:00
|
|
|
'template': "my message"
|
2016-01-19 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
auth_header = create_authorization_header(
|
2016-01-28 11:06:24 +00:00
|
|
|
service_id=sample_api_key.service_id,
|
2016-01-19 11:23:09 +00:00
|
|
|
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'
|
2016-02-03 13:16:19 +00:00
|
|
|
assert 'Missing data for required field.' in json_resp['message']['to'][0]
|
2016-01-19 11:23:09 +00:00
|
|
|
assert not notify_alpha_client.send_sms.called
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_should_reject_bad_phone_numbers(
|
2016-01-28 11:06:24 +00:00
|
|
|
notify_api, notify_db, notify_db_session, mocker):
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
|
|
|
|
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 = {
|
2016-02-03 13:16:19 +00:00
|
|
|
'to': 'invalid',
|
|
|
|
|
'template': "my message"
|
2016-01-19 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
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'
|
2016-02-03 13:16:19 +00:00
|
|
|
assert 'Invalid phone number, must be of format +441234123123' in json_resp['message']['to']
|
2016-01-19 11:23:09 +00:00
|
|
|
assert not notify_alpha_client.send_sms.called
|
|
|
|
|
|
|
|
|
|
|
2016-01-22 14:43:30 +00:00
|
|
|
def test_send_notification_restrict_mobile(notify_api,
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_api_key,
|
|
|
|
|
sample_template,
|
|
|
|
|
sample_user,
|
|
|
|
|
mocker):
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
2016-01-22 14:43:30 +00:00
|
|
|
Test POST endpoint '/sms' with service notification with mobile number
|
|
|
|
|
not in restricted list.
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
|
|
|
|
with notify_api.test_request_context():
|
|
|
|
|
with notify_api.test_client() as client:
|
2016-01-28 11:06:24 +00:00
|
|
|
|
2016-01-22 14:43:30 +00:00
|
|
|
Service.query.filter_by(
|
|
|
|
|
id=sample_template.service.id).update({'restricted': True})
|
|
|
|
|
invalid_mob = '+449999999999'
|
2016-01-19 11:23:09 +00:00
|
|
|
mocker.patch(
|
|
|
|
|
'app.notify_alpha_client.send_sms',
|
2016-01-22 14:43:30 +00:00
|
|
|
return_value={}
|
2016-01-19 11:23:09 +00:00
|
|
|
)
|
|
|
|
|
data = {
|
2016-02-03 13:16:19 +00:00
|
|
|
'to': invalid_mob,
|
|
|
|
|
'template': sample_template.id
|
2016-01-19 11:23:09 +00:00
|
|
|
}
|
2016-01-22 14:43:30 +00:00
|
|
|
assert invalid_mob != sample_user.mobile_number
|
2016-01-19 11:23:09 +00:00
|
|
|
auth_header = create_authorization_header(
|
2016-01-22 14:43:30 +00:00
|
|
|
service_id=sample_template.service.id,
|
2016-01-19 11:23:09 +00:00
|
|
|
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
|
2016-01-28 11:06:24 +00:00
|
|
|
assert 'Invalid phone number for restricted service' in json_resp['message']['restricted']
|
|
|
|
|
assert not notify_alpha_client.send_sms.called
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
2016-02-03 13:16:19 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2016-01-27 17:42:05 +00:00
|
|
|
@moto.mock_sqs
|
2016-02-03 13:16:19 +00:00
|
|
|
def test_should_allow_valid_message(notify_api,
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sqs_client_conn,
|
2016-02-04 12:07:26 +00:00
|
|
|
sample_user,
|
|
|
|
|
sample_template,
|
2016-02-03 13:16:19 +00:00
|
|
|
mocker):
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
2016-01-22 14:43:30 +00:00
|
|
|
Tests POST endpoint '/sms' with notifications-admin notification.
|
2016-01-19 11:23:09 +00:00
|
|
|
"""
|
|
|
|
|
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,
|
2016-01-28 11:06:24 +00:00
|
|
|
"message": "valid",
|
2016-01-19 11:23:09 +00:00
|
|
|
"method": "sms",
|
|
|
|
|
"status": "created",
|
2016-02-04 12:07:26 +00:00
|
|
|
"to": sample_user.mobile_number
|
2016-01-19 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
data = {
|
2016-02-03 13:16:19 +00:00
|
|
|
'to': '+441234123123',
|
2016-02-04 12:07:26 +00:00
|
|
|
'template': sample_template.id
|
2016-01-19 11:23:09 +00:00
|
|
|
}
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
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
|
2016-02-04 12:07:26 +00:00
|
|
|
notify_alpha_client.send_sms.assert_called_with(mobile_number='+441234123123',
|
|
|
|
|
message=sample_template.content)
|
2016-01-20 13:14:23 +00:00
|
|
|
|
|
|
|
|
|
2016-02-03 13:16:19 +00:00
|
|
|
@moto.mock_sqs
|
2016-01-20 13:14:23 +00:00
|
|
|
def test_send_email_valid_data(notify_api,
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
sample_service,
|
|
|
|
|
sample_admin_service_id,
|
2016-02-03 13:16:19 +00:00
|
|
|
sqs_client_conn,
|
2016-01-20 13:14:23 +00:00
|
|
|
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 = {
|
2016-02-03 13:16:19 +00:00
|
|
|
'to': to_address,
|
|
|
|
|
'from': from_address,
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
2016-01-20 13:14:23 +00:00
|
|
|
}
|
|
|
|
|
auth_header = create_authorization_header(
|
|
|
|
|
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)
|