mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Use the same method to persist a notification.
Refactored the send_sms task to use this method.
This commit is contained in:
@@ -3,7 +3,6 @@ import pytest
|
||||
from datetime import datetime
|
||||
from freezegun import freeze_time
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
from app import (encryption, DATETIME_FORMAT)
|
||||
from app.celery import provider_tasks
|
||||
from app.celery import tasks
|
||||
@@ -189,8 +188,6 @@ def test_should_not_process_email_job_if_would_exceed_send_limits_inc_today(noti
|
||||
|
||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email'))
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")
|
||||
|
||||
process_job(job.id)
|
||||
|
||||
@@ -208,8 +205,6 @@ def test_should_not_process_email_job_if_would_exceed_send_limits(notify_db, not
|
||||
|
||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3')
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")
|
||||
|
||||
process_job(job.id)
|
||||
|
||||
@@ -562,11 +557,12 @@ def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, sample_
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
|
||||
notification_id = uuid.uuid4()
|
||||
now = datetime.utcnow()
|
||||
send_sms(
|
||||
sample_job.service.id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT),
|
||||
now.strftime(DATETIME_FORMAT),
|
||||
api_key_id=str(sample_api_key.id),
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
@@ -581,7 +577,7 @@ def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, sample_
|
||||
assert persisted_notification.template_id == sample_job.template.id
|
||||
assert persisted_notification.status == 'created'
|
||||
assert not persisted_notification.sent_at
|
||||
assert persisted_notification.created_at <= datetime.utcnow()
|
||||
assert persisted_notification.created_at <= now
|
||||
assert not persisted_notification.sent_by
|
||||
assert persisted_notification.job_row_number == 2
|
||||
assert persisted_notification.api_key_id == sample_api_key.id
|
||||
@@ -673,7 +669,7 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
|
||||
assert persisted_notification.to == 'my_email@my_email.com'
|
||||
assert persisted_notification.template_id == sample_email_template_with_placeholders.id
|
||||
assert persisted_notification.template_version == sample_email_template_with_placeholders.version
|
||||
assert persisted_notification.created_at >= now
|
||||
assert persisted_notification.created_at == now
|
||||
assert not persisted_notification.sent_at
|
||||
assert persisted_notification.status == 'created'
|
||||
assert not persisted_notification.sent_by
|
||||
@@ -711,7 +707,7 @@ def test_send_email_should_use_template_version_from_job_not_latest(sample_email
|
||||
assert persisted_notification.to == 'my_email@my_email.com'
|
||||
assert persisted_notification.template_id == sample_email_template.id
|
||||
assert persisted_notification.template_version == version_on_notification
|
||||
assert persisted_notification.created_at >= now
|
||||
assert persisted_notification.created_at == now
|
||||
assert not persisted_notification.sent_at
|
||||
assert persisted_notification.status == 'created'
|
||||
assert not persisted_notification.sent_by
|
||||
@@ -739,7 +735,7 @@ def test_should_use_email_template_subject_placeholders(sample_email_template_wi
|
||||
assert persisted_notification.to == 'my_email@my_email.com'
|
||||
assert persisted_notification.template_id == sample_email_template_with_placeholders.id
|
||||
assert persisted_notification.status == 'created'
|
||||
assert persisted_notification.created_at >= now
|
||||
assert persisted_notification.created_at == now
|
||||
assert not persisted_notification.sent_by
|
||||
assert persisted_notification.personalisation == {"name": "Jo"}
|
||||
assert not persisted_notification.reference
|
||||
@@ -766,7 +762,7 @@ def test_should_use_email_template_and_persist_without_personalisation(sample_em
|
||||
persisted_notification = Notification.query.all()[0]
|
||||
assert persisted_notification.to == 'my_email@my_email.com'
|
||||
assert persisted_notification.template_id == sample_email_template.id
|
||||
assert persisted_notification.created_at >= now
|
||||
assert persisted_notification.created_at == now
|
||||
assert not persisted_notification.sent_at
|
||||
assert persisted_notification.status == 'created'
|
||||
assert not persisted_notification.sent_by
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import datetime
|
||||
|
||||
import pytest
|
||||
from boto3.exceptions import Boto3Error
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
@@ -46,21 +48,44 @@ def test_persist_notification_creates_and_save_to_db(sample_template, sample_api
|
||||
assert NotificationHistory.query.count() == 1
|
||||
|
||||
|
||||
def test_persist_notification_throws_exception_when_missing_template(sample_template, sample_api_key):
|
||||
def test_persist_notification_throws_exception_when_missing_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,
|
||||
recipient='+447111111111',
|
||||
service_id=sample_template.service.id,
|
||||
personalisation=None, notification_type='sms',
|
||||
service_id=sample_api_key.service_id,
|
||||
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
|
||||
|
||||
|
||||
def test_persist_notification_with_job_and_created(sample_job, sample_api_key):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
created_at = datetime.datetime(2016, 11, 11, 16, 8, 18)
|
||||
persist_notification(template_id=sample_job.template.id,
|
||||
template_version=sample_job.template.version,
|
||||
recipient='+447111111111',
|
||||
service_id=sample_job.service.id,
|
||||
personalisation=None, notification_type='sms',
|
||||
api_key_id=sample_api_key.id,
|
||||
key_type=sample_api_key.key_type,
|
||||
created_at=created_at,
|
||||
job_id=sample_job.id,
|
||||
job_row_number=10)
|
||||
assert Notification.query.count() == 1
|
||||
assert NotificationHistory.query.count() == 1
|
||||
persisted_notification = Notification.query.all()[0]
|
||||
assert persisted_notification.job_id == sample_job.id
|
||||
assert persisted_notification.job_row_number == 10
|
||||
assert persisted_notification.created_at == created_at
|
||||
|
||||
|
||||
@pytest.mark.parametrize('research_mode, queue, notification_type, key_type',
|
||||
[(True, 'research-mode', 'sms', 'normal'),
|
||||
(True, 'research-mode', 'email', 'normal'),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
@@ -12,23 +13,20 @@ from app.models import (
|
||||
def test_should_build_notification_from_minimal_set_of_api_derived_params(notify_api):
|
||||
now = datetime.utcnow()
|
||||
|
||||
notification = {
|
||||
'template': 'template',
|
||||
'template_version': '1',
|
||||
'to': 'someone',
|
||||
'personalisation': {}
|
||||
}
|
||||
notification = Notification.from_api_request(
|
||||
created_at=now.strftime(DATETIME_FORMAT),
|
||||
notification=notification,
|
||||
notification_id="notification_id",
|
||||
service_id="service_id",
|
||||
notification = Notification.from_request(
|
||||
template_id='template',
|
||||
template_version='1',
|
||||
recipient='someone',
|
||||
service_id='service_id',
|
||||
notification_type='SMS',
|
||||
created_at=now,
|
||||
api_key_id='api_key_id',
|
||||
key_type='key_type'
|
||||
key_type='key_type',
|
||||
personalisation={},
|
||||
job_id=None,
|
||||
job_row_number=None
|
||||
)
|
||||
assert notification.created_at == now
|
||||
assert notification.id == "notification_id"
|
||||
assert notification.template_id == 'template'
|
||||
assert notification.template_version == '1'
|
||||
assert not notification.job_row_number
|
||||
@@ -44,28 +42,22 @@ def test_should_build_notification_from_minimal_set_of_api_derived_params(notify
|
||||
|
||||
def test_should_build_notification_from_full_set_of_api_derived_params(notify_api):
|
||||
now = datetime.utcnow()
|
||||
|
||||
notification = {
|
||||
'template': 'template',
|
||||
'template_version': '1',
|
||||
'to': 'someone',
|
||||
'personalisation': {'key': 'value'},
|
||||
'job': 'job_id',
|
||||
'row_number': 100
|
||||
}
|
||||
notification = Notification.from_api_request(
|
||||
created_at=now.strftime(DATETIME_FORMAT),
|
||||
notification=notification,
|
||||
notification_id="notification_id",
|
||||
service_id="service_id",
|
||||
notification_type='SMS',
|
||||
api_key_id='api_key_id',
|
||||
key_type='key_type'
|
||||
)
|
||||
notification = Notification.from_request(template_id='template',
|
||||
template_version=1,
|
||||
recipient='someone',
|
||||
service_id='service_id',
|
||||
personalisation={'key': 'value'},
|
||||
notification_type='SMS',
|
||||
api_key_id='api_key_id',
|
||||
key_type='key_type',
|
||||
job_id='job_id',
|
||||
job_row_number=100,
|
||||
created_at=now
|
||||
)
|
||||
assert notification.created_at == now
|
||||
assert notification.id == "notification_id"
|
||||
assert notification.id is None
|
||||
assert notification.template_id == 'template'
|
||||
assert notification.template_version == '1'
|
||||
assert notification.template_version == 1
|
||||
assert notification.job_row_number == 100
|
||||
assert notification.job_id == 'job_id'
|
||||
assert notification.to == 'someone'
|
||||
|
||||
Reference in New Issue
Block a user