mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
- Refactor version 1 of post notificaitons to use the common persist_notificaiton and send_notification_to_queue methods.
- It would be nice to refactor the send_sms and send_email tasks to use these common functions as well, that way I can get rid of the new Notifications.from_v2_api_request method. - Still not happy with the format of the errors. Would like to find a happy place, where the message is descript enough that we do not need external documentation to explain the error. Perhaps we still only need documentation to explain the trial mode concept.
This commit is contained in:
@@ -516,7 +516,7 @@ def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api,
|
||||
def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample_email_template, fake_uuid, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_email_template.service.created_by.email_address,
|
||||
@@ -545,7 +545,7 @@ def test_should_send_sms_to_anyone_with_test_key(
|
||||
):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': '07811111111',
|
||||
@@ -578,7 +578,7 @@ def test_should_send_email_to_anyone_with_test_key(
|
||||
):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': 'anyone123@example.com',
|
||||
@@ -608,7 +608,7 @@ def test_should_send_email_to_anyone_with_test_key(
|
||||
def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_template, fake_uuid, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_template.service.created_by.mobile_number,
|
||||
@@ -638,7 +638,7 @@ def test_should_persist_notification(notify_api, sample_template,
|
||||
fake_uuid, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type))
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
template = sample_template if template_type == 'sms' else sample_email_template
|
||||
to = sample_template.service.created_by.mobile_number if template_type == 'sms' \
|
||||
else sample_email_template.service.created_by.email_address
|
||||
@@ -682,7 +682,7 @@ def test_should_delete_notification_and_return_error_if_sqs_fails(
|
||||
'app.celery.provider_tasks.deliver_{}.apply_async'.format(template_type),
|
||||
side_effect=Exception("failed to talk to SQS")
|
||||
)
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
m1 = mocker.patch('app.dao.notifications_dao.create_uuid', return_value=fake_uuid)
|
||||
template = sample_template if template_type == 'sms' else sample_email_template
|
||||
to = sample_template.service.created_by.mobile_number if template_type == 'sms' \
|
||||
else sample_email_template.service.created_by.email_address
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
from boto3.exceptions import Boto3Error
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.errors import InvalidRequest
|
||||
from app.models import Template, Notification, NotificationHistory
|
||||
from app.notifications.process_notifications import (create_content_for_notification,
|
||||
persist_notification, send_notification_to_queue)
|
||||
@@ -12,7 +12,14 @@ from tests.app.conftest import sample_notification, sample_template, sample_emai
|
||||
def test_create_content_for_notification_passes(sample_email_template):
|
||||
template = Template.query.get(sample_email_template.id)
|
||||
content = create_content_for_notification(template, None)
|
||||
assert content.replaced == template.content
|
||||
|
||||
|
||||
def test_create_content_for_notification_with_placeholders_passes(sample_template_with_placeholders):
|
||||
template = Template.query.get(sample_template_with_placeholders.id)
|
||||
content = create_content_for_notification(template, {'name': 'Bobby'})
|
||||
assert content.content == template.content
|
||||
assert 'Bobby' in content.replaced
|
||||
|
||||
|
||||
def test_create_content_for_notification_fails_with_missing_personalisation(sample_template_with_placeholders):
|
||||
@@ -21,6 +28,12 @@ def test_create_content_for_notification_fails_with_missing_personalisation(samp
|
||||
create_content_for_notification(template, None)
|
||||
|
||||
|
||||
def test_create_content_for_notification_fails_with_additional_personalisation(sample_template_with_placeholders):
|
||||
template = Template.query.get(sample_template_with_placeholders.id)
|
||||
with pytest.raises(BadRequestError):
|
||||
create_content_for_notification(template, {'name': 'Bobbhy', 'Additional': 'Data'})
|
||||
|
||||
|
||||
def test_persist_notification_creates_and_save_to_db(sample_template, sample_api_key):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
@@ -34,6 +47,7 @@ def test_persist_notification_creates_and_save_to_db(sample_template, sample_api
|
||||
|
||||
def test_persist_notification_throws_exception_when_missing_template(sample_template, sample_api_key):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
with pytest.raises(SQLAlchemyError):
|
||||
persist_notification(template_id=None,
|
||||
template_version=None,
|
||||
@@ -42,6 +56,8 @@ def test_persist_notification_throws_exception_when_missing_template(sample_temp
|
||||
personalisation=None, notification_type='sms',
|
||||
api_key_id=sample_api_key.id,
|
||||
key_type=sample_api_key.key_type)
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('research_mode, queue, notification_type, key_type',
|
||||
@@ -62,8 +78,11 @@ def test_send_notification_to_queue(notify_db, notify_db_session,
|
||||
mocked.assert_called_once_with([str(notification.id)], queue=queue)
|
||||
|
||||
|
||||
def test_send_notification_to_queue(sample_notification, mocker):
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async', side_effect=Exception("EXPECTED"))
|
||||
with pytest.raises(InvalidRequest):
|
||||
def test_send_notification_to_queue_throws_exception_deletes_notification(sample_notification, mocker):
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async', side_effect=Boto3Error("EXPECTED"))
|
||||
with pytest.raises(Boto3Error):
|
||||
send_notification_to_queue(sample_notification, False)
|
||||
mocked.assert_called_once_with([(str(sample_notification.id))], queue='send-sms')
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import pytest
|
||||
|
||||
from app.errors import InvalidRequest
|
||||
from app.notifications.validators import check_service_message_limit, check_template_is_for_notification_type, \
|
||||
check_template_is_active, service_can_send_to_recipient, check_sms_content_char_count
|
||||
from app.v2.errors import BadRequestError, TooManyRequestsError
|
||||
@@ -53,7 +52,7 @@ def test_check_template_is_active_passes(sample_template):
|
||||
assert check_template_is_active(sample_template) is None
|
||||
|
||||
|
||||
def test_check_template_is_active_passes(sample_template):
|
||||
def test_check_template_is_active_fails(sample_template):
|
||||
sample_template.archived = True
|
||||
from app.dao.templates_dao import dao_update_template
|
||||
dao_update_template(sample_template)
|
||||
@@ -64,7 +63,6 @@ def test_check_template_is_active_passes(sample_template):
|
||||
assert e.code == '10400'
|
||||
assert e.message == 'Template has been deleted'
|
||||
assert e.link == "link to documentation"
|
||||
assert e.fields[0]["template"] == "has been deleted"
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type',
|
||||
@@ -73,12 +71,10 @@ def test_service_can_send_to_recipient_passes(key_type, notify_db, notify_db_ses
|
||||
trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
|
||||
assert service_can_send_to_recipient(trial_mode_service.users[0].email_address,
|
||||
key_type,
|
||||
trial_mode_service,
|
||||
"email") is None
|
||||
trial_mode_service) is None
|
||||
assert service_can_send_to_recipient(trial_mode_service.users[0].mobile_number,
|
||||
key_type,
|
||||
trial_mode_service,
|
||||
"sms") is None
|
||||
trial_mode_service) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type',
|
||||
@@ -87,12 +83,10 @@ def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(k
|
||||
live_service = create_service(notify_db, notify_db_session, service_name='live', restricted=False)
|
||||
assert service_can_send_to_recipient("some_other_email@test.com",
|
||||
key_type,
|
||||
live_service,
|
||||
"email") is None
|
||||
live_service) is None
|
||||
assert service_can_send_to_recipient('07513332413',
|
||||
key_type,
|
||||
live_service,
|
||||
"sms") is None
|
||||
live_service) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type',
|
||||
@@ -102,13 +96,11 @@ def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(k
|
||||
sample_service_whitelist(notify_db, notify_db_session, email_address="some_other_email@test.com")
|
||||
assert service_can_send_to_recipient("some_other_email@test.com",
|
||||
key_type,
|
||||
sample_service,
|
||||
"email") is None
|
||||
sample_service) is None
|
||||
sample_service_whitelist(notify_db, notify_db_session, mobile_number='07513332413')
|
||||
assert service_can_send_to_recipient('07513332413',
|
||||
key_type,
|
||||
sample_service,
|
||||
"sms") is None
|
||||
sample_service) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type',
|
||||
@@ -118,13 +110,11 @@ def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(key_t
|
||||
with pytest.raises(BadRequestError):
|
||||
assert service_can_send_to_recipient("some_other_email@test.com",
|
||||
key_type,
|
||||
trial_mode_service,
|
||||
"email") is None
|
||||
trial_mode_service) is None
|
||||
with pytest.raises(BadRequestError):
|
||||
assert service_can_send_to_recipient('07513332413',
|
||||
key_type,
|
||||
trial_mode_service,
|
||||
"sms") is None
|
||||
trial_mode_service) is None
|
||||
|
||||
|
||||
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(notify_db, notify_db_session):
|
||||
@@ -132,8 +122,7 @@ def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(n
|
||||
with pytest.raises(BadRequestError):
|
||||
assert service_can_send_to_recipient("0758964221",
|
||||
'team',
|
||||
live_service,
|
||||
"sms") is None
|
||||
live_service) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('char_count', [495, 0, 494, 200])
|
||||
|
||||
Reference in New Issue
Block a user