mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Merge branch 'master' into split-sms-and-retry
This commit is contained in:
@@ -20,6 +20,8 @@ def test_make_mmg_callback(notify_api, rmock):
|
||||
send_sms_response("mmg", "1234", "07811111111")
|
||||
|
||||
assert rmock.called
|
||||
assert rmock.request_history[0].url == endpoint
|
||||
assert json.loads(rmock.request_history[0].text)['MSISDN'] == '07811111111'
|
||||
|
||||
|
||||
def test_make_firetext_callback(notify_api, rmock):
|
||||
@@ -32,6 +34,8 @@ def test_make_firetext_callback(notify_api, rmock):
|
||||
send_sms_response("firetext", "1234", "07811111111")
|
||||
|
||||
assert rmock.called
|
||||
assert rmock.request_history[0].url == endpoint
|
||||
assert 'mobile=07811111111' in rmock.request_history[0].text
|
||||
|
||||
|
||||
def test_make_ses_callback(notify_api, rmock):
|
||||
@@ -71,11 +75,21 @@ def test_temp_failure_mmg_callback():
|
||||
|
||||
|
||||
def test_delivered_firetext_callback():
|
||||
assert firetext_callback("1234", "07811111111") == "mobile=07811111111&status=0&time=2016-03-10 14:17:00&reference=1234" # noqa
|
||||
assert firetext_callback('1234', '07811111111') == {
|
||||
'mobile': '07811111111',
|
||||
'status': '0',
|
||||
'time': '2016-03-10 14:17:00',
|
||||
'reference': '1234'
|
||||
}
|
||||
|
||||
|
||||
def test_failure_firetext_callback():
|
||||
assert firetext_callback("1234", "07822222222") == "mobile=07822222222&status=1&time=2016-03-10 14:17:00&reference=1234" # noqa
|
||||
assert firetext_callback('1234', '07822222222') == {
|
||||
'mobile': '07822222222',
|
||||
'status': '1',
|
||||
'time': '2016-03-10 14:17:00',
|
||||
'reference': '1234'
|
||||
}
|
||||
|
||||
|
||||
def test_delivered_ses_callback():
|
||||
|
||||
@@ -7,7 +7,6 @@ from notifications_utils.recipients import validate_phone_number, format_phone_n
|
||||
from app.celery import provider_tasks
|
||||
from app.celery.tasks import (
|
||||
send_sms,
|
||||
send_sms_code,
|
||||
send_email,
|
||||
process_job,
|
||||
email_invited_user,
|
||||
@@ -16,7 +15,8 @@ from app.celery.tasks import (
|
||||
delete_invitations,
|
||||
delete_failed_notifications,
|
||||
delete_successful_notifications,
|
||||
provider_to_use
|
||||
provider_to_use,
|
||||
timeout_notifications
|
||||
)
|
||||
from app.celery.research_mode_tasks import (
|
||||
send_email_response,
|
||||
@@ -807,33 +807,6 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
|
||||
assert 'No row was found for one' in str(e.value)
|
||||
|
||||
|
||||
def test_should_send_sms_code(mocker):
|
||||
notification = {'to': '+447234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
|
||||
mocker.patch('app.mmg_client.send_sms')
|
||||
send_sms_code(encrypted_notification)
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
format_phone_number(validate_phone_number(notification['to'])),
|
||||
"{} is your Notify authentication code".format(notification['secret_code']),
|
||||
'send-sms-code')
|
||||
|
||||
|
||||
def test_should_throw_mmg_client_exception(mocker):
|
||||
notification = {'to': '+447234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
mocker.patch('app.mmg_client.send_sms', side_effect=MMGClientException(mmg_error))
|
||||
send_sms_code(encrypted_notification)
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
format_phone_number(validate_phone_number(notification['to'])),
|
||||
"{} is your Notify authentication code".format(notification['secret_code']),
|
||||
'send-sms-code')
|
||||
|
||||
|
||||
def test_email_invited_user_should_send_email(notify_api, mocker):
|
||||
with notify_api.test_request_context():
|
||||
invitation = {'to': 'new_person@it.gov.uk',
|
||||
@@ -1016,3 +989,41 @@ def _notification_json(template, to, personalisation=None, job_id=None, row_numb
|
||||
if row_number:
|
||||
notification['row_number'] = row_number
|
||||
return notification
|
||||
|
||||
|
||||
def test_update_status_of_notifications_after_timeout(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
sample_template,
|
||||
mmg_provider):
|
||||
with notify_api.test_request_context():
|
||||
not1 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
template=sample_template,
|
||||
status='sending',
|
||||
created_at=datetime.utcnow() - timedelta(
|
||||
seconds=current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD') + 10))
|
||||
timeout_notifications()
|
||||
assert not1.status == 'temporary-failure'
|
||||
|
||||
|
||||
def test_not_update_status_of_notification_before_timeout(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
sample_template,
|
||||
mmg_provider):
|
||||
with notify_api.test_request_context():
|
||||
not1 = sample_notification(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=sample_service,
|
||||
template=sample_template,
|
||||
status='sending',
|
||||
created_at=datetime.utcnow() - timedelta(
|
||||
seconds=current_app.config.get('SENDING_NOTIFICATIONS_TIMEOUT_PERIOD') - 10))
|
||||
timeout_notifications()
|
||||
assert not1.status == 'sending'
|
||||
|
||||
@@ -536,7 +536,7 @@ def mock_firetext_client(mocker, statsd_client=None):
|
||||
statsd_client = statsd_client or mocker.Mock()
|
||||
current_app = mocker.Mock(config={
|
||||
'FIRETEXT_API_KEY': 'foo',
|
||||
'FIRETEXT_NUMBER': 'bar'
|
||||
'FROM_NUMBER': 'bar'
|
||||
})
|
||||
client.init_app(current_app, statsd_client)
|
||||
return client
|
||||
@@ -548,7 +548,7 @@ def mock_mmg_client(mocker, statsd_client=None):
|
||||
statsd_client = statsd_client or mocker.Mock()()
|
||||
current_app = mocker.Mock(config={
|
||||
'MMG_API_KEY': 'foo',
|
||||
'MMG_FROM_NUMBER': 'bar'
|
||||
'FROM_NUMBER': 'bar'
|
||||
})
|
||||
client.init_app(current_app, statsd_client)
|
||||
return client
|
||||
|
||||
@@ -334,3 +334,53 @@ def test_delete_service_and_associated_objects(notify_db,
|
||||
assert InvitedUser.query.count() == 0
|
||||
assert Service.query.count() == 0
|
||||
assert Service.get_history_model().query.count() == 0
|
||||
|
||||
|
||||
def test_add_existing_user_to_another_service_doesnot_change_old_permissions(sample_user):
|
||||
|
||||
service_one = Service(name="service_one",
|
||||
email_from="service_one",
|
||||
message_limit=1000,
|
||||
active=True,
|
||||
restricted=False,
|
||||
created_by=sample_user)
|
||||
|
||||
dao_create_service(service_one, sample_user)
|
||||
assert sample_user.id == service_one.users[0].id
|
||||
test_user_permissions = Permission.query.filter_by(service=service_one, user=sample_user).all()
|
||||
assert len(test_user_permissions) == 8
|
||||
|
||||
other_user = User(
|
||||
name='Other Test User',
|
||||
email_address='other_user@digital.cabinet-office.gov.uk',
|
||||
password='password',
|
||||
mobile_number='+447700900987'
|
||||
)
|
||||
save_model_user(other_user)
|
||||
service_two = Service(name="service_two",
|
||||
email_from="service_two",
|
||||
message_limit=1000,
|
||||
active=True,
|
||||
restricted=False,
|
||||
created_by=other_user)
|
||||
dao_create_service(service_two, other_user)
|
||||
|
||||
assert other_user.id == service_two.users[0].id
|
||||
other_user_permissions = Permission.query.filter_by(service=service_two, user=other_user).all()
|
||||
assert len(other_user_permissions) == 8
|
||||
|
||||
other_user_service_one_permissions = Permission.query.filter_by(service=service_one, user=other_user).all()
|
||||
assert len(other_user_service_one_permissions) == 0
|
||||
|
||||
# adding the other_user to service_one should leave all other_user permissions on service_two intact
|
||||
permissions = []
|
||||
for p in ['send_emails', 'send_texts', 'send_letters']:
|
||||
permissions.append(Permission(permission=p))
|
||||
|
||||
dao_add_user_to_service(service_one, other_user, permissions=permissions)
|
||||
|
||||
other_user_service_one_permissions = Permission.query.filter_by(service=service_one, user=other_user).all()
|
||||
assert len(other_user_service_one_permissions) == 3
|
||||
|
||||
other_user_service_two_permissions = Permission.query.filter_by(service=service_two, user=other_user).all()
|
||||
assert len(other_user_service_two_permissions) == 8
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app import db
|
||||
from app.models import TemplateStatistics
|
||||
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_template as create_sample_template
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
@@ -130,3 +132,102 @@ def test_get_all_template_statistics_with_bad_limit_arg_returns_400(notify_api,
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert json_resp['result'] == 'error'
|
||||
assert json_resp['message'] == {'limit_days': ['blurk is not an integer']}
|
||||
|
||||
|
||||
def test_get_template_statistics_for_template_only_returns_for_provided_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notify_api,
|
||||
sample_service
|
||||
):
|
||||
template_1 = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 1',
|
||||
service=sample_service
|
||||
)
|
||||
template_2 = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 2',
|
||||
service=sample_service
|
||||
)
|
||||
|
||||
template_1_stats_1 = TemplateStatistics(
|
||||
template_id=template_1.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 1)
|
||||
)
|
||||
template_1_stats_2 = TemplateStatistics(
|
||||
template_id=template_1.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 2)
|
||||
)
|
||||
template_2_stats = TemplateStatistics(
|
||||
template_id=template_2.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 1)
|
||||
)
|
||||
|
||||
# separate commit to ensure stats_1 has earlier updated_at time
|
||||
db.session.add(template_1_stats_1)
|
||||
db.session.commit()
|
||||
|
||||
db.session.add_all([template_1_stats_2, template_2_stats])
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/{}'.format(sample_service.id, template_1.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['id'] == str(template_1_stats_2.id)
|
||||
assert json_resp['data'][1]['id'] == str(template_1_stats_1.id)
|
||||
|
||||
|
||||
def test_get_template_statistics_for_template_returns_empty_if_no_statistics(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notify_api,
|
||||
sample_service
|
||||
):
|
||||
template_1 = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 1',
|
||||
service=sample_service
|
||||
)
|
||||
template_2 = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
template_name='Sample Template 2',
|
||||
service=sample_service
|
||||
)
|
||||
|
||||
template_1_stats = TemplateStatistics(
|
||||
template_id=template_1.id,
|
||||
service_id=sample_service.id,
|
||||
day=datetime(2001, 1, 1)
|
||||
)
|
||||
db.session.add(template_1_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics/{}'.format(sample_service.id, template_2.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert json_resp['data'] == []
|
||||
|
||||
10
tests/app/test_schemas.py
Normal file
10
tests/app/test_schemas.py
Normal file
@@ -0,0 +1,10 @@
|
||||
def test_job_schema_doesnt_return_notifications(sample_notification):
|
||||
from app.schemas import job_schema
|
||||
|
||||
job = sample_notification.job
|
||||
assert job.notifications.count() == 1
|
||||
|
||||
data, errors = job_schema.dump(job)
|
||||
|
||||
assert not errors
|
||||
assert 'notifications' not in data
|
||||
Reference in New Issue
Block a user