mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Working code and tests.
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user