mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-20 14:29:25 -04:00
Merge branch 'master' into capture-delivery-outcomes
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import uuid
|
||||
import pytest
|
||||
from flask import current_app
|
||||
from utils.recipients import validate_phone_number, format_phone_number
|
||||
|
||||
from app.celery.tasks import (
|
||||
send_sms,
|
||||
send_sms_code,
|
||||
@@ -248,7 +250,7 @@ def test_should_process_all_sms_job(sample_job, sample_job_with_placeholdered_te
|
||||
def test_should_send_template_to_correct_sms_provider_and_persist(sample_template_with_placeholders, mocker):
|
||||
notification = {
|
||||
"template": sample_template_with_placeholders.id,
|
||||
"to": "+441234123123",
|
||||
"to": "+447234123123",
|
||||
"personalisation": {"name": "Jo"}
|
||||
}
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
@@ -265,7 +267,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
||||
)
|
||||
|
||||
firetext_client.send_sms.assert_called_once_with(
|
||||
to="+441234123123",
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: Hello Jo",
|
||||
reference=str(notification_id)
|
||||
)
|
||||
@@ -273,7 +275,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
||||
sample_template_with_placeholders.service_id, notification_id
|
||||
)
|
||||
assert persisted_notification.id == notification_id
|
||||
assert persisted_notification.to == '+441234123123'
|
||||
assert persisted_notification.to == '+447234123123'
|
||||
assert persisted_notification.template_id == sample_template_with_placeholders.id
|
||||
assert persisted_notification.status == 'sent'
|
||||
assert persisted_notification.created_at == now
|
||||
@@ -285,7 +287,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
|
||||
def test_should_send_sms_without_personalisation(sample_template, mocker):
|
||||
notification = {
|
||||
"template": sample_template.id,
|
||||
"to": "+441234123123"
|
||||
"to": "+447234123123"
|
||||
}
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.firetext_client.send_sms')
|
||||
@@ -301,7 +303,7 @@ def test_should_send_sms_without_personalisation(sample_template, mocker):
|
||||
)
|
||||
|
||||
firetext_client.send_sms.assert_called_once_with(
|
||||
to="+441234123123",
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: This is a template",
|
||||
reference=str(notification_id)
|
||||
)
|
||||
@@ -330,7 +332,7 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
|
||||
)
|
||||
|
||||
firetext_client.send_sms.assert_called_once_with(
|
||||
to="+447700900890",
|
||||
to=format_phone_number(validate_phone_number("+447700900890")),
|
||||
content="Sample service: This is a template",
|
||||
reference=str(notification_id)
|
||||
)
|
||||
@@ -396,7 +398,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
|
||||
notification = {
|
||||
"template": sample_job.template.id,
|
||||
"job": sample_job.id,
|
||||
"to": "+441234123123"
|
||||
"to": "+447234123123"
|
||||
}
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.firetext_client.send_sms')
|
||||
@@ -411,13 +413,13 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
|
||||
now.strftime(DATETIME_FORMAT)
|
||||
)
|
||||
firetext_client.send_sms.assert_called_once_with(
|
||||
to="+441234123123",
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: This is a template",
|
||||
reference=str(notification_id)
|
||||
)
|
||||
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
|
||||
assert persisted_notification.id == notification_id
|
||||
assert persisted_notification.to == '+441234123123'
|
||||
assert persisted_notification.to == '+447234123123'
|
||||
assert persisted_notification.job_id == sample_job.id
|
||||
assert persisted_notification.template_id == sample_job.template.id
|
||||
assert persisted_notification.status == 'sent'
|
||||
@@ -520,7 +522,7 @@ def test_should_use_email_template_and_persist_without_personalisation(
|
||||
def test_should_persist_notification_as_failed_if_sms_client_fails(sample_template, mocker):
|
||||
notification = {
|
||||
"template": sample_template.id,
|
||||
"to": "+441234123123"
|
||||
"to": "+447234123123"
|
||||
}
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException(firetext_error()))
|
||||
@@ -536,13 +538,13 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
|
||||
now.strftime(DATETIME_FORMAT)
|
||||
)
|
||||
firetext_client.send_sms.assert_called_once_with(
|
||||
to="+441234123123",
|
||||
to=format_phone_number(validate_phone_number("+447234123123")),
|
||||
content="Sample service: This is a template",
|
||||
reference=str(notification_id)
|
||||
)
|
||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
|
||||
assert persisted_notification.id == notification_id
|
||||
assert persisted_notification.to == '+441234123123'
|
||||
assert persisted_notification.to == '+447234123123'
|
||||
assert persisted_notification.template_id == sample_template.id
|
||||
assert persisted_notification.status == 'failed'
|
||||
assert persisted_notification.created_at == now
|
||||
@@ -590,7 +592,7 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
|
||||
def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
|
||||
notification = {
|
||||
"template": sample_template.id,
|
||||
"to": "+441234123123"
|
||||
"to": "+447234123123"
|
||||
}
|
||||
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||
mocker.patch('app.firetext_client.send_sms')
|
||||
@@ -638,24 +640,28 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
|
||||
|
||||
|
||||
def test_should_send_sms_code(mocker):
|
||||
notification = {'to': '+441234123123',
|
||||
notification = {'to': '+447234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
|
||||
mocker.patch('app.firetext_client.send_sms')
|
||||
send_sms_code(encrypted_notification)
|
||||
firetext_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'], 'send-sms-code')
|
||||
firetext_client.send_sms.assert_called_once_with(format_phone_number(validate_phone_number(notification['to'])),
|
||||
notification['secret_code'],
|
||||
'send-sms-code')
|
||||
|
||||
|
||||
def test_should_throw_firetext_client_exception(mocker):
|
||||
notification = {'to': '+441234123123',
|
||||
notification = {'to': '+447234123123',
|
||||
'secret_code': '12345'}
|
||||
|
||||
encrypted_notification = encryption.encrypt(notification)
|
||||
mocker.patch('app.firetext_client.send_sms', side_effect=FiretextClientException(firetext_error()))
|
||||
send_sms_code(encrypted_notification)
|
||||
firetext_client.send_sms.assert_called_once_with(notification['to'], notification['secret_code'], 'send-sms-code')
|
||||
firetext_client.send_sms.assert_called_once_with(format_phone_number(validate_phone_number(notification['to'])),
|
||||
notification['secret_code'],
|
||||
'send-sms-code')
|
||||
|
||||
|
||||
def test_should_send_email_code(mocker):
|
||||
|
||||
@@ -327,6 +327,11 @@ def mock_celery_send_email_code(mocker):
|
||||
return mocker.patch('app.celery.tasks.send_email_code.apply_async')
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_celery_email_registration_verification(mocker):
|
||||
return mocker.patch('app.celery.tasks.email_registration_verification.apply_async')
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def mock_encryption(mocker):
|
||||
return mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
@@ -70,8 +70,13 @@ def test_should_be_able_to_get_statistics_for_a_service(sample_template):
|
||||
assert len(stats) == 1
|
||||
assert stats[0].emails_requested == 0
|
||||
assert stats[0].sms_requested == 1
|
||||
assert stats[0].sms_delivered == 0
|
||||
assert stats[0].sms_error == 0
|
||||
assert stats[0].day == notification.created_at.strftime(DATE_FORMAT)
|
||||
assert stats[0].service_id == notification.service_id
|
||||
assert stats[0].emails_requested == 0
|
||||
assert stats[0].emails_delivered == 0
|
||||
assert stats[0].emails_error == 0
|
||||
|
||||
|
||||
def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_template):
|
||||
@@ -90,7 +95,11 @@ def test_should_be_able_to_get_statistics_for_a_service_for_a_day(sample_templat
|
||||
sample_template.service.id, now.strftime(DATE_FORMAT)
|
||||
)
|
||||
assert stat.emails_requested == 0
|
||||
assert stat.emails_error == 0
|
||||
assert stat.emails_delivered == 0
|
||||
assert stat.sms_requested == 1
|
||||
assert stat.sms_error == 0
|
||||
assert stat.sms_delivered == 0
|
||||
assert stat.day == notification.created_at.strftime(DATE_FORMAT)
|
||||
assert stat.service_id == notification.service_id
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ def test_create_user(notify_api, notify_db, notify_db_session):
|
||||
assert User.query.count() == 1
|
||||
assert User.query.first().email_address == email
|
||||
assert User.query.first().id == user.id
|
||||
assert not user.platform_admin
|
||||
|
||||
|
||||
def test_get_all_users(notify_api, notify_db, notify_db_session, sample_user):
|
||||
|
||||
@@ -25,7 +25,14 @@ def test_get_notification_by_id(notify_api, sample_notification):
|
||||
|
||||
notification = json.loads(response.get_data(as_text=True))['notification']
|
||||
assert notification['status'] == 'sent'
|
||||
assert notification['template'] == sample_notification.template.id
|
||||
assert notification['template'] == {
|
||||
'id': sample_notification.template.id,
|
||||
'name': sample_notification.template.name,
|
||||
'template_type': sample_notification.template.template_type}
|
||||
assert notification['job'] == {
|
||||
'id': str(sample_notification.job.id),
|
||||
'original_file_name': sample_notification.job.original_file_name
|
||||
}
|
||||
assert notification['to'] == '+447700900855'
|
||||
assert notification['service'] == str(sample_notification.service_id)
|
||||
assert response.status_code == 200
|
||||
@@ -64,7 +71,14 @@ def test_get_all_notifications(notify_api, sample_notification):
|
||||
|
||||
notifications = json.loads(response.get_data(as_text=True))
|
||||
assert notifications['notifications'][0]['status'] == 'sent'
|
||||
assert notifications['notifications'][0]['template'] == sample_notification.template.id
|
||||
assert notifications['notifications'][0]['template'] == {
|
||||
'id': sample_notification.template.id,
|
||||
'name': sample_notification.template.name,
|
||||
'template_type': sample_notification.template.template_type}
|
||||
assert notifications['notifications'][0]['job'] == {
|
||||
'id': str(sample_notification.job.id),
|
||||
'original_file_name': sample_notification.job.original_file_name
|
||||
}
|
||||
assert notifications['notifications'][0]['to'] == '+447700900855'
|
||||
assert notifications['notifications'][0]['service'] == str(sample_notification.service_id)
|
||||
assert response.status_code == 200
|
||||
|
||||
65
tests/app/test_validation.py
Normal file
65
tests/app/test_validation.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from app.models import User, Service
|
||||
from app.validation import allowed_send_to_number, allowed_send_to_email
|
||||
|
||||
|
||||
def test_allowed_send_to_number_returns_true_for_restricted_service_with_same_number():
|
||||
mobile_number = '07524609792'
|
||||
service = _create_service_data(mobile_number)
|
||||
assert allowed_send_to_number(service, mobile_number)
|
||||
|
||||
|
||||
def test_allowed_send_to_number_returns_false_for_restricted_service_with_different_number():
|
||||
mobile_number = '00447524609792'
|
||||
service = _create_service_data(mobile_number)
|
||||
assert not allowed_send_to_number(service, '+447344609793')
|
||||
|
||||
|
||||
def test_allowed_send_to_number_returns_true_for_unrestricted_service_with_different_number():
|
||||
mobile_number = '+447524609792'
|
||||
service = _create_service_data(mobile_number, False)
|
||||
assert allowed_send_to_number(service, '+447344609793')
|
||||
|
||||
|
||||
def test_allowed_send_to_email__returns_true_for_restricted_service_with_same_email():
|
||||
email = 'testing@it.gov.uk'
|
||||
service = _create_service_data(email_address=email)
|
||||
assert allowed_send_to_email(service, email)
|
||||
|
||||
|
||||
def test_allowed_send_to_email__returns_false_for_restricted_service_with_different_email():
|
||||
email = 'testing@it.gov.uk'
|
||||
service = _create_service_data(email_address=email)
|
||||
assert not allowed_send_to_email(service, 'another@it.gov.uk')
|
||||
|
||||
|
||||
def test_allowed_send_to_email__returns_false_for_restricted_service_with_different_email():
|
||||
email = 'testing@it.gov.uk'
|
||||
service = _create_service_data(email_address=email)
|
||||
assert not allowed_send_to_email(service, 'another@it.gov.uk')
|
||||
|
||||
|
||||
def test_allowed_send_to_email__returns_true_for_unrestricted_service_with_different_email():
|
||||
email = 'testing@it.gov.uk'
|
||||
service = _create_service_data(email_address=email, restricted=False)
|
||||
assert allowed_send_to_number(service, 'another@it.gov.uk')
|
||||
|
||||
|
||||
def _create_service_data(mobile_number='+447524609792', restricted=True, email_address='test_user@it.gov.uk'):
|
||||
usr = {
|
||||
'name': 'Test User',
|
||||
'email_address': email_address,
|
||||
'password': 'password',
|
||||
'mobile_number': mobile_number,
|
||||
'state': 'active'
|
||||
}
|
||||
user = User(**usr)
|
||||
data = {
|
||||
'name': 'Test service',
|
||||
'limit': 10,
|
||||
'active': False,
|
||||
'restricted': restricted,
|
||||
'email_from': 'test_service@it.gov.uk'
|
||||
}
|
||||
service = Service(**data)
|
||||
service.users = [user]
|
||||
return service
|
||||
@@ -1,13 +1,25 @@
|
||||
import json
|
||||
import moto
|
||||
from datetime import (datetime, timedelta)
|
||||
|
||||
from datetime import (
|
||||
datetime,
|
||||
timedelta
|
||||
)
|
||||
|
||||
from flask import url_for
|
||||
from app.models import (VerifyCode, User)
|
||||
import app.celery.tasks
|
||||
|
||||
from app.models import (
|
||||
VerifyCode,
|
||||
User
|
||||
)
|
||||
|
||||
from app import db, encryption
|
||||
|
||||
from tests import create_authorization_header
|
||||
from freezegun import freeze_time
|
||||
|
||||
import app.celery.tasks
|
||||
|
||||
|
||||
def test_user_verify_code_sms(notify_api,
|
||||
sample_sms_code):
|
||||
@@ -341,3 +353,23 @@ def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_ap
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert resp.status_code == 404
|
||||
assert json.loads(resp.get_data(as_text=True))['message'] == 'No result found'
|
||||
|
||||
|
||||
def test_send_user_email_verification(notify_api,
|
||||
sample_email_code,
|
||||
mock_celery_email_registration_verification,
|
||||
mock_encryption):
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = json.dumps({})
|
||||
auth_header = create_authorization_header(
|
||||
path=url_for('user.send_user_email_verification', user_id=sample_email_code.user.id),
|
||||
method='POST',
|
||||
request_body=data)
|
||||
resp = client.post(
|
||||
url_for('user.send_user_email_verification', user_id=sample_email_code.user.id),
|
||||
data=data,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert resp.status_code == 204
|
||||
app.celery.tasks.email_registration_verification.apply_async.assert_called_once_with(['something_encrypted'], queue='email-registration-verification') # noqa
|
||||
|
||||
Reference in New Issue
Block a user