mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user