mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-26 08:08:17 -04:00
Merge pull request #666 from alphagov/write-to-postgres-not-sqs
Write to postgres not sqs
This commit is contained in:
@@ -134,8 +134,11 @@ def send_sms(self,
|
||||
return
|
||||
|
||||
try:
|
||||
_save_notification(created_at, notification, notification_id, service_id, SMS_TYPE, api_key_id, key_type)
|
||||
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, SMS_TYPE, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
send_sms_to_provider.apply_async((service_id, notification_id), queue='send-sms')
|
||||
|
||||
current_app.logger.info(
|
||||
@@ -169,7 +172,11 @@ def send_email(self, service_id,
|
||||
return
|
||||
|
||||
try:
|
||||
_save_notification(created_at, notification, notification_id, service_id, EMAIL_TYPE, api_key_id, key_type)
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, EMAIL_TYPE, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
|
||||
send_email_to_provider.apply_async((service_id, notification_id), queue='send-email')
|
||||
|
||||
@@ -185,25 +192,6 @@ def send_email(self, service_id,
|
||||
)
|
||||
|
||||
|
||||
def _save_notification(created_at, notification, notification_id, service_id, notification_type, api_key_id, key_type):
|
||||
notification_db_object = Notification(
|
||||
id=notification_id,
|
||||
template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
status='created',
|
||||
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type
|
||||
)
|
||||
dao_create_notification(notification_db_object)
|
||||
|
||||
|
||||
def service_allowed_to_send_to(recipient, service, key_type):
|
||||
if not service.restricted or key_type == KEY_TYPE_TEST:
|
||||
return True
|
||||
|
||||
@@ -106,7 +106,7 @@ def dao_get_template_usage(service_id, limit_days=None):
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_last_template_usage(template_id):
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id)\
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id) \
|
||||
.join(Template) \
|
||||
.order_by(desc(NotificationHistory.created_at)) \
|
||||
.first()
|
||||
@@ -275,3 +275,14 @@ def delete_notifications_created_more_than_a_week_ago(status):
|
||||
).delete(synchronize_session='fetch')
|
||||
db.session.commit()
|
||||
return deleted
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def dao_delete_notifications_and_history_by_id(notification_id):
|
||||
db.session.query(Notification).filter(
|
||||
Notification.id == notification_id
|
||||
).delete(synchronize_session='fetch')
|
||||
db.session.query(NotificationHistory).filter(
|
||||
NotificationHistory.id == notification_id
|
||||
).delete(synchronize_session='fetch')
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import uuid
|
||||
import datetime
|
||||
|
||||
from sqlalchemy.dialects.postgresql import (
|
||||
UUID,
|
||||
JSON
|
||||
@@ -15,8 +14,8 @@ from app.encryption import (
|
||||
from app.authentication.utils import get_secret
|
||||
from app import (
|
||||
db,
|
||||
encryption
|
||||
)
|
||||
encryption,
|
||||
DATETIME_FORMAT)
|
||||
|
||||
from app.history_meta import Versioned
|
||||
|
||||
@@ -74,7 +73,6 @@ user_to_service = db.Table(
|
||||
UniqueConstraint('user_id', 'service_id', name='uix_user_to_service')
|
||||
)
|
||||
|
||||
|
||||
BRANDING_GOVUK = 'govuk'
|
||||
BRANDING_ORG = 'org'
|
||||
BRANDING_BOTH = 'both'
|
||||
@@ -395,6 +393,7 @@ class VerifyCode(db.Model):
|
||||
def check_code(self, cde):
|
||||
return check_hash(cde, self._code)
|
||||
|
||||
|
||||
NOTIFICATION_CREATED = 'created'
|
||||
NOTIFICATION_SENDING = 'sending'
|
||||
NOTIFICATION_DELIVERED = 'delivered'
|
||||
@@ -427,7 +426,6 @@ NOTIFICATION_STATUS_TYPES_ENUM = db.Enum(*NOTIFICATION_STATUS_TYPES, name='notif
|
||||
|
||||
|
||||
class Notification(db.Model):
|
||||
|
||||
__tablename__ = 'notifications'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
@@ -482,6 +480,32 @@ class Notification(db.Model):
|
||||
if personalisation:
|
||||
self._personalisation = encryption.encrypt(personalisation)
|
||||
|
||||
@classmethod
|
||||
def from_api_request(
|
||||
cls,
|
||||
created_at,
|
||||
notification,
|
||||
notification_id,
|
||||
service_id,
|
||||
notification_type,
|
||||
api_key_id,
|
||||
key_type):
|
||||
return cls(
|
||||
id=notification_id,
|
||||
template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
status='created',
|
||||
created_at=datetime.datetime.strptime(created_at, DATETIME_FORMAT),
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type
|
||||
)
|
||||
|
||||
|
||||
class NotificationHistory(db.Model):
|
||||
__tablename__ = 'notification_history'
|
||||
@@ -522,7 +546,6 @@ INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']
|
||||
|
||||
|
||||
class InvitedUser(db.Model):
|
||||
|
||||
__tablename__ = 'invited_users'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
@@ -598,7 +621,6 @@ class Permission(db.Model):
|
||||
|
||||
|
||||
class TemplateStatistics(db.Model):
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False)
|
||||
service = db.relationship('Service', backref=db.backref('template_statistics', lazy='dynamic'))
|
||||
@@ -615,7 +637,6 @@ class TemplateStatistics(db.Model):
|
||||
|
||||
|
||||
class Event(db.Model):
|
||||
|
||||
__tablename__ = 'events'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
|
||||
@@ -13,9 +13,10 @@ from notifications_utils.recipients import allowed_to_send_to, first_column_head
|
||||
from notifications_utils.template import Template
|
||||
from notifications_utils.renderers import PassThrough
|
||||
from app.clients.email.aws_ses import get_aws_responses
|
||||
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT, statsd_client
|
||||
from app import api_user, create_uuid, DATETIME_FORMAT, statsd_client
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
||||
from app.dao.services_dao import dao_fetch_todays_stats_for_service
|
||||
from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST
|
||||
from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST, Notification, KEY_TYPE_NORMAL, EMAIL_TYPE
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
services_dao,
|
||||
@@ -35,7 +36,6 @@ from app.schemas import (
|
||||
day_schema,
|
||||
unarchived_template_schema
|
||||
)
|
||||
from app.celery.tasks import send_sms, send_email
|
||||
from app.utils import pagination_links
|
||||
|
||||
notifications = Blueprint('notifications', __name__)
|
||||
@@ -46,6 +46,7 @@ from app.errors import (
|
||||
)
|
||||
|
||||
register_errors(notifications)
|
||||
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
|
||||
|
||||
|
||||
@notifications.route('/notifications/email/ses', methods=['POST'])
|
||||
@@ -239,14 +240,14 @@ def send_notification(notification_type):
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
if all((
|
||||
api_user.key_type != KEY_TYPE_TEST,
|
||||
service.restricted or api_user.key_type == KEY_TYPE_TEAM,
|
||||
not allowed_to_send_to(
|
||||
notification['to'],
|
||||
itertools.chain.from_iterable(
|
||||
[user.mobile_number, user.email_address] for user in service.users
|
||||
api_user.key_type != KEY_TYPE_TEST,
|
||||
service.restricted or api_user.key_type == KEY_TYPE_TEAM,
|
||||
not allowed_to_send_to(
|
||||
notification['to'],
|
||||
itertools.chain.from_iterable(
|
||||
[user.mobile_number, user.email_address] for user in service.users
|
||||
)
|
||||
)
|
||||
)
|
||||
)):
|
||||
if (api_user.key_type == KEY_TYPE_TEAM):
|
||||
message = 'Can’t send to this recipient using a team-only API key'
|
||||
@@ -262,34 +263,15 @@ def send_notification(notification_type):
|
||||
|
||||
notification_id = create_uuid()
|
||||
notification.update({"template_version": template.version})
|
||||
if notification_type == SMS_TYPE:
|
||||
send_sms.apply_async(
|
||||
(
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
),
|
||||
kwargs={
|
||||
'api_key_id': str(api_user.id),
|
||||
'key_type': api_user.key_type
|
||||
},
|
||||
queue='db-sms'
|
||||
)
|
||||
else:
|
||||
send_email.apply_async(
|
||||
(
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
),
|
||||
kwargs={
|
||||
'api_key_id': str(api_user.id),
|
||||
'key_type': api_user.key_type
|
||||
},
|
||||
queue='db-email'
|
||||
)
|
||||
persist_notification(
|
||||
service,
|
||||
notification_id,
|
||||
notification,
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT),
|
||||
notification_type,
|
||||
str(api_user.id),
|
||||
api_user.key_type
|
||||
)
|
||||
|
||||
return jsonify(
|
||||
data=get_notification_return_data(
|
||||
@@ -320,3 +302,33 @@ def get_notification_statistics_for_day():
|
||||
)
|
||||
data, errors = notifications_statistics_schema.dump(statistics, many=True)
|
||||
return jsonify(data=data), 200
|
||||
|
||||
|
||||
def persist_notification(
|
||||
service,
|
||||
notification_id,
|
||||
notification,
|
||||
created_at,
|
||||
notification_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
):
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, notification_type, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
if notification_type == SMS_TYPE:
|
||||
send_sms_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-sms')
|
||||
if notification_type == EMAIL_TYPE:
|
||||
send_email_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-email')
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Failed to send to SQS exception", e)
|
||||
dao_delete_notifications_and_history_by_id(notification_id)
|
||||
raise InvalidRequest(message="Internal server error", status_code=500)
|
||||
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(notification_type, notification_id, created_at)
|
||||
)
|
||||
|
||||
@@ -33,7 +33,8 @@ from app.dao.notifications_dao import (
|
||||
get_notifications_for_job,
|
||||
get_notifications_for_service,
|
||||
update_notification_status_by_id,
|
||||
update_notification_status_by_reference
|
||||
update_notification_status_by_reference,
|
||||
dao_delete_notifications_and_history_by_id
|
||||
)
|
||||
|
||||
from notifications_utils.template import get_sms_fragment_count
|
||||
@@ -55,6 +56,7 @@ def test_should_have_decorated_notifications_dao_functions():
|
||||
assert get_notifications_for_service.__wrapped__.__name__ == 'get_notifications_for_service' # noqa
|
||||
assert get_notification_by_id.__wrapped__.__name__ == 'get_notification_by_id' # noqa
|
||||
assert delete_notifications_created_more_than_a_week_ago.__wrapped__.__name__ == 'delete_notifications_created_more_than_a_week_ago' # noqa
|
||||
assert dao_delete_notifications_and_history_by_id.__wrapped__.__name__ == 'dao_delete_notifications_and_history_by_id' # noqa
|
||||
|
||||
|
||||
def test_should_be_able_to_get_template_usage_history(notify_db, notify_db_session, sample_service):
|
||||
@@ -73,7 +75,6 @@ def test_should_be_able_to_get_all_template_usage_history_order_by_notification_
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
@@ -88,7 +89,6 @@ def test_should_be_able_to_get_all_template_usage_history_order_by_notification_
|
||||
def test_should_be_able_to_get_no_template_usage_history_if_no_notifications_using_template(
|
||||
notify_db,
|
||||
notify_db_session):
|
||||
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
|
||||
results = dao_get_last_template_usage(sms.id)
|
||||
@@ -729,6 +729,67 @@ def test_updating_notification_updates_notification_history(sample_notification)
|
||||
assert hist.status == 'sending'
|
||||
|
||||
|
||||
def test_should_delete_notification_and_notification_history_for_id(notify_db, notify_db_session, sample_template):
|
||||
data = _notification_json(sample_template)
|
||||
notification = Notification(**data)
|
||||
|
||||
dao_create_notification(notification)
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
assert NotificationHistory.query.count() == 1
|
||||
|
||||
dao_delete_notifications_and_history_by_id(notification.id)
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationHistory.query.count() == 0
|
||||
|
||||
|
||||
def test_should_delete_only_notification_and_notification_history_with_id(notify_db, notify_db_session,
|
||||
sample_template):
|
||||
id_1 = uuid.uuid4()
|
||||
id_2 = uuid.uuid4()
|
||||
data_1 = _notification_json(sample_template, id=id_1)
|
||||
data_2 = _notification_json(sample_template, id=id_2)
|
||||
|
||||
notification_1 = Notification(**data_1)
|
||||
notification_2 = Notification(**data_2)
|
||||
|
||||
dao_create_notification(notification_1)
|
||||
dao_create_notification(notification_2)
|
||||
|
||||
assert Notification.query.count() == 2
|
||||
assert NotificationHistory.query.count() == 2
|
||||
|
||||
dao_delete_notifications_and_history_by_id(notification_1.id)
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
assert NotificationHistory.query.count() == 1
|
||||
assert Notification.query.first().id == notification_2.id
|
||||
assert NotificationHistory.query.first().id == notification_2.id
|
||||
|
||||
|
||||
def test_should_delete_no_notifications_or_notification_historys_if_no_matching_ids(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_template
|
||||
):
|
||||
id_1 = uuid.uuid4()
|
||||
id_2 = uuid.uuid4()
|
||||
data_1 = _notification_json(sample_template, id=id_1)
|
||||
|
||||
notification_1 = Notification(**data_1)
|
||||
|
||||
dao_create_notification(notification_1)
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
assert NotificationHistory.query.count() == 1
|
||||
|
||||
dao_delete_notifications_and_history_by_id(id_2)
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
assert NotificationHistory.query.count() == 1
|
||||
|
||||
|
||||
def _notification_json(sample_template, job_id=None, id=None, status=None):
|
||||
data = {
|
||||
'to': '+44709123456',
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
import random
|
||||
import string
|
||||
@@ -10,7 +11,8 @@ from notifications_python_client.authentication import create_jwt_token
|
||||
|
||||
import app
|
||||
from app import encryption
|
||||
from app.models import ApiKey, KEY_TYPE_TEAM, KEY_TYPE_TEST
|
||||
from app.dao import notifications_dao
|
||||
from app.models import ApiKey, KEY_TYPE_TEAM, KEY_TYPE_TEST, Notification, NotificationHistory
|
||||
from app.dao.templates_dao import dao_get_all_templates_for_service, dao_update_template
|
||||
from app.dao.services_dao import dao_update_service
|
||||
from app.dao.api_key_dao import save_model_api_key
|
||||
@@ -26,7 +28,7 @@ from tests.app.conftest import (
|
||||
def test_create_sms_should_reject_if_missing_required_fields(notify_api, sample_api_key, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
data = {}
|
||||
auth_header = create_authorization_header(service_id=sample_api_key.service_id)
|
||||
@@ -46,7 +48,7 @@ def test_create_sms_should_reject_if_missing_required_fields(notify_api, sample_
|
||||
def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
data = {
|
||||
'to': 'invalid',
|
||||
@@ -60,7 +62,7 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||
assert json_resp['result'] == 'error'
|
||||
assert len(json_resp['message'].keys()) == 1
|
||||
assert 'Invalid phone number: Must not contain letters or symbols' in json_resp['message']['to']
|
||||
@@ -70,7 +72,7 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker):
|
||||
def test_send_notification_invalid_template_id(notify_api, sample_template, mocker, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
data = {
|
||||
'to': '+447700900855',
|
||||
@@ -84,7 +86,7 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 404
|
||||
test_string = 'No result found'
|
||||
@@ -95,7 +97,7 @@ def test_send_notification_invalid_template_id(notify_api, sample_template, mock
|
||||
def test_send_notification_with_placeholders_replaced(notify_api, sample_email_template_with_placeholders, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.tasks.send_email_to_provider.apply_async')
|
||||
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
@@ -115,16 +117,12 @@ def test_send_notification_with_placeholders_replaced(notify_api, sample_email_t
|
||||
notification_id = response_data['notification']['id']
|
||||
data.update({"template_version": sample_email_template_with_placeholders.version})
|
||||
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template_with_placeholders.service.id),
|
||||
notification_id,
|
||||
ANY,
|
||||
"2016-01-01T11:09:00.061258"),
|
||||
kwargs=ANY,
|
||||
queue="db-email"
|
||||
str(notification_id)),
|
||||
queue="send-email"
|
||||
)
|
||||
assert response.status_code == 201
|
||||
assert encryption.decrypt(app.celery.tasks.send_email.apply_async.call_args[0][0][2]) == data
|
||||
assert response_data['body'] == 'Hello Jo\nThis is an email from GOV.UK'
|
||||
assert response_data['subject'] == 'Jo'
|
||||
|
||||
@@ -152,7 +150,7 @@ def test_should_not_send_notification_for_archived_template(notify_api, sample_t
|
||||
def test_send_notification_with_missing_personalisation(notify_api, sample_template_with_placeholders, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
data = {
|
||||
'to': '+447700900855',
|
||||
@@ -169,7 +167,7 @@ def test_send_notification_with_missing_personalisation(notify_api, sample_templ
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert 'Missing personalisation: name' in json_resp['message']['template']
|
||||
@@ -180,7 +178,7 @@ def test_send_notification_with_too_much_personalisation_data(
|
||||
):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
data = {
|
||||
'to': '+447700900855',
|
||||
@@ -197,7 +195,7 @@ def test_send_notification_with_too_much_personalisation_data(
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert 'Personalisation not needed for template: foo' in json_resp['message']['template']
|
||||
@@ -206,7 +204,7 @@ def test_send_notification_with_too_much_personalisation_data(
|
||||
def test_should_not_send_sms_if_restricted_and_not_a_service_user(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
sample_template.service.restricted = True
|
||||
dao_update_service(sample_template.service)
|
||||
@@ -224,7 +222,7 @@ def test_should_not_send_sms_if_restricted_and_not_a_service_user(notify_api, sa
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert [(
|
||||
@@ -236,7 +234,7 @@ def test_should_not_send_sms_if_restricted_and_not_a_service_user(notify_api, sa
|
||||
def test_should_send_sms_if_restricted_and_a_service_user(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
sample_template.service.restricted = True
|
||||
dao_update_service(sample_template.service)
|
||||
@@ -252,14 +250,14 @@ def test_should_send_sms_if_restricted_and_a_service_user(notify_api, sample_tem
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert app.celery.tasks.send_sms.apply_async.called
|
||||
assert app.celery.provider_tasks.send_sms_to_provider.apply_async.called
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
def test_should_send_email_if_restricted_and_a_service_user(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
|
||||
sample_email_template.service.restricted = True
|
||||
dao_update_service(sample_email_template.service)
|
||||
@@ -275,14 +273,14 @@ def test_should_send_email_if_restricted_and_a_service_user(notify_api, sample_e
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert app.celery.tasks.send_email.apply_async.called
|
||||
assert app.celery.provider_tasks.send_email_to_provider.apply_async.called
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
def test_should_not_allow_template_from_another_service(notify_api, service_factory, sample_user, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
service_1 = service_factory.get('service 1', user=sample_user, email_from='service.1')
|
||||
service_2 = service_factory.get('service 2', user=sample_user, email_from='service.2')
|
||||
@@ -301,7 +299,7 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 404
|
||||
test_string = 'No result found'
|
||||
@@ -325,7 +323,7 @@ def test_should_not_allow_template_content_too_large(
|
||||
):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
template = create_sample_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -362,7 +360,7 @@ def test_should_not_allow_template_content_too_large(
|
||||
def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
@@ -379,18 +377,9 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
|
||||
|
||||
response_data = json.loads(response.data)['data']
|
||||
notification_id = response_data['notification']['id']
|
||||
assert app.encryption.encrypt.call_args[0][0]['to'] == '+447700900855'
|
||||
assert app.encryption.encrypt.call_args[0][0]['template'] == str(sample_template.id)
|
||||
assert app.encryption.encrypt.call_args[0][0]['template_version'] == sample_template.version
|
||||
|
||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||
(str(sample_template.service_id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
"2016-01-01T11:09:00.061258"),
|
||||
kwargs=ANY,
|
||||
queue="db-sms"
|
||||
)
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_template.service_id), notification_id), queue='send-sms')
|
||||
assert response.status_code == 201
|
||||
assert notification_id
|
||||
assert 'subject' not in response_data
|
||||
@@ -401,7 +390,7 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
|
||||
def test_create_email_should_reject_if_missing_required_fields(notify_api, sample_api_key, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
|
||||
data = {}
|
||||
auth_header = create_authorization_header(service_id=sample_api_key.service_id)
|
||||
@@ -412,7 +401,7 @@ def test_create_email_should_reject_if_missing_required_fields(notify_api, sampl
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
|
||||
assert json_resp['result'] == 'error'
|
||||
assert 'Missing data for required field.' in json_resp['message']['to'][0]
|
||||
assert 'Missing data for required field.' in json_resp['message']['template'][0]
|
||||
@@ -422,7 +411,7 @@ def test_create_email_should_reject_if_missing_required_fields(notify_api, sampl
|
||||
def test_should_reject_email_notification_with_bad_email(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
to_address = "bad-email"
|
||||
data = {
|
||||
'to': to_address,
|
||||
@@ -436,7 +425,7 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
|
||||
assert response.status_code == 400
|
||||
assert data['result'] == 'error'
|
||||
assert data['message']['to'][0] == 'Not a valid email address'
|
||||
@@ -446,7 +435,7 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
|
||||
notify_api, sample_email_template, mocker, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': fake_uuid
|
||||
@@ -459,7 +448,7 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
|
||||
assert response.status_code == 404
|
||||
assert data['result'] == 'error'
|
||||
test_string = 'No result found'
|
||||
@@ -469,7 +458,7 @@ def test_should_reject_email_notification_with_template_id_that_cant_be_found(
|
||||
def test_should_not_allow_email_template_from_another_service(notify_api, service_factory, sample_user, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
|
||||
service_1 = service_factory.get('service 1', template_type='email', user=sample_user,
|
||||
email_from='service.1')
|
||||
@@ -491,7 +480,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 404
|
||||
test_string = 'No result found'
|
||||
@@ -501,7 +490,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic
|
||||
def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
|
||||
sample_email_template.service.restricted = True
|
||||
dao_update_service(sample_email_template.service)
|
||||
@@ -519,7 +508,7 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert [(
|
||||
@@ -532,7 +521,7 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api,
|
||||
def test_should_allow_valid_email_notification(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
@@ -549,16 +538,9 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
|
||||
assert response.status_code == 201
|
||||
response_data = json.loads(response.get_data(as_text=True))['data']
|
||||
notification_id = response_data['notification']['id']
|
||||
assert app.encryption.encrypt.call_args[0][0]['to'] == 'ok@ok.com'
|
||||
assert app.encryption.encrypt.call_args[0][0]['template'] == str(sample_email_template.id)
|
||||
assert app.encryption.encrypt.call_args[0][0]['template_version'] == sample_email_template.version
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service_id),
|
||||
notification_id,
|
||||
"something_encrypted",
|
||||
"2016-01-01T11:09:00.061258"),
|
||||
kwargs=ANY,
|
||||
queue="db-email"
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service_id), notification_id),
|
||||
queue="send-email"
|
||||
)
|
||||
|
||||
assert response.status_code == 201
|
||||
@@ -576,7 +558,7 @@ def test_should_not_block_api_call_if_over_day_limit_for_live_service(
|
||||
mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=False)
|
||||
@@ -609,7 +591,7 @@ def test_should_block_api_call_if_over_day_limit_for_restricted_service(
|
||||
mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=True)
|
||||
@@ -645,7 +627,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
|
||||
restricted):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=restricted)
|
||||
@@ -671,7 +653,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
|
||||
def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
email_template = create_sample_email_template(notify_db, notify_db.session, content='hello\nthere')
|
||||
|
||||
data = {
|
||||
@@ -691,7 +673,7 @@ def test_should_not_return_html_in_body(notify_api, notify_db, notify_db_session
|
||||
|
||||
def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
|
||||
data = {
|
||||
'to': "not-someone-we-trust@email-address.com",
|
||||
@@ -709,7 +691,7 @@ def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert [
|
||||
@@ -719,7 +701,7 @@ def test_should_not_send_email_if_team_api_key_and_not_a_service_user(notify_api
|
||||
|
||||
def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api, sample_template, mocker):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
|
||||
data = {
|
||||
'to': '07123123123',
|
||||
@@ -734,7 +716,7 @@ def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api,
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert [
|
||||
@@ -742,9 +724,10 @@ def test_should_not_send_sms_if_team_api_key_and_not_a_service_user(notify_api,
|
||||
] == json_resp['message']['to']
|
||||
|
||||
|
||||
def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample_email_template, mocker):
|
||||
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.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_email_template.service.created_by.email_address,
|
||||
@@ -762,23 +745,20 @@ def test_should_send_email_if_team_api_key_and_a_service_user(notify_api, sample
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
ANY,
|
||||
kwargs={
|
||||
'api_key_id': str(api_key.id),
|
||||
'key_type': api_key.key_type
|
||||
},
|
||||
queue='db-email')
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service.id), fake_uuid),
|
||||
queue='send-email')
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
@pytest.mark.parametrize('restricted', [True, False])
|
||||
@pytest.mark.parametrize('limit', [0, 1])
|
||||
def test_should_send_email_to_anyone_with_test_key(
|
||||
notify_api, sample_email_template, mocker, restricted, limit
|
||||
notify_api, sample_email_template, mocker, restricted, limit, fake_uuid
|
||||
):
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': 'anyone123@example.com',
|
||||
@@ -801,20 +781,15 @@ def test_should_send_email_to_anyone_with_test_key(
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))]
|
||||
)
|
||||
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
ANY,
|
||||
kwargs={
|
||||
'api_key_id': str(api_key.id),
|
||||
'key_type': api_key.key_type
|
||||
},
|
||||
queue='db-email'
|
||||
)
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service.id), fake_uuid), queue='send-email')
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_template, mocker):
|
||||
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.tasks.send_sms.apply_async')
|
||||
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_template.service.created_by.mobile_number,
|
||||
@@ -832,11 +807,140 @@ def test_should_send_sms_if_team_api_key_and_a_service_user(notify_api, sample_t
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
app.celery.tasks.send_sms.apply_async.assert_called_once_with(
|
||||
ANY,
|
||||
kwargs={
|
||||
'api_key_id': str(api_key.id),
|
||||
'key_type': api_key.key_type
|
||||
},
|
||||
queue='db-sms')
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_template.service.id), fake_uuid), queue='send-sms')
|
||||
assert response.status_code == 201
|
||||
|
||||
|
||||
def test_should_persist_sms_notification(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.send_sms_to_provider.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_template.service.created_by.mobile_number,
|
||||
'template': sample_template.id
|
||||
}
|
||||
api_key = ApiKey(
|
||||
service=sample_template.service,
|
||||
name='team_key',
|
||||
created_by=sample_template.created_by,
|
||||
key_type=KEY_TYPE_TEAM)
|
||||
save_model_api_key(api_key)
|
||||
auth_header = create_jwt_token(secret=api_key.unsigned_secret, client_id=str(api_key.service_id))
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_template.service.id), fake_uuid), queue='send-sms')
|
||||
assert response.status_code == 201
|
||||
|
||||
notification = notifications_dao.get_notification_by_id(fake_uuid)
|
||||
assert notification.to == sample_template.service.created_by.mobile_number
|
||||
assert notification.template_id == sample_template.id
|
||||
assert notification.notification_type == 'sms'
|
||||
|
||||
|
||||
def test_should_persist_email_notification(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.send_email_to_provider.apply_async')
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_email_template.service.created_by.email_address,
|
||||
'template': sample_email_template.id
|
||||
}
|
||||
api_key = ApiKey(
|
||||
service=sample_email_template.service,
|
||||
name='team_key',
|
||||
created_by=sample_email_template.created_by,
|
||||
key_type=KEY_TYPE_TEAM)
|
||||
save_model_api_key(api_key)
|
||||
auth_header = create_jwt_token(secret=api_key.unsigned_secret, client_id=str(api_key.service_id))
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service.id), fake_uuid), queue='send-email')
|
||||
assert response.status_code == 201
|
||||
|
||||
notification = notifications_dao.get_notification_by_id(fake_uuid)
|
||||
assert notification.to == sample_email_template.service.created_by.email_address
|
||||
assert notification.template_id == sample_email_template.id
|
||||
assert notification.notification_type == 'email'
|
||||
|
||||
|
||||
def test_should_delete_email_notification_and_return_error_if_sqs_fails(
|
||||
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.send_email_to_provider.apply_async',
|
||||
side_effect=Exception("failed to talk to SQS")
|
||||
)
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_email_template.service.created_by.email_address,
|
||||
'template': sample_email_template.id
|
||||
}
|
||||
api_key = ApiKey(
|
||||
service=sample_email_template.service,
|
||||
name='team_key',
|
||||
created_by=sample_email_template.created_by,
|
||||
key_type=KEY_TYPE_TEAM)
|
||||
save_model_api_key(api_key)
|
||||
auth_header = create_jwt_token(secret=api_key.unsigned_secret, client_id=str(api_key.service_id))
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
app.celery.provider_tasks.send_email_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service.id), fake_uuid), queue='send-email')
|
||||
|
||||
assert response.status_code == 500
|
||||
assert not notifications_dao.get_notification_by_id(fake_uuid)
|
||||
assert not NotificationHistory.query.get(fake_uuid)
|
||||
|
||||
|
||||
def test_should_delete_sms_notification_and_return_error_if_sqs_fails(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.send_sms_to_provider.apply_async',
|
||||
side_effect=Exception("failed to talk to SQS")
|
||||
)
|
||||
mocker.patch('app.notifications.rest.create_uuid', return_value=fake_uuid)
|
||||
|
||||
data = {
|
||||
'to': sample_template.service.created_by.mobile_number,
|
||||
'template': sample_template.id
|
||||
}
|
||||
api_key = ApiKey(
|
||||
service=sample_template.service,
|
||||
name='team_key',
|
||||
created_by=sample_template.created_by,
|
||||
key_type=KEY_TYPE_TEAM)
|
||||
save_model_api_key(api_key)
|
||||
auth_header = create_jwt_token(secret=api_key.unsigned_secret, client_id=str(api_key.service_id))
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), ('Authorization', 'Bearer {}'.format(auth_header))])
|
||||
|
||||
app.celery.provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
|
||||
(str(sample_template.service.id), fake_uuid), queue='send-sms')
|
||||
|
||||
assert response.status_code == 500
|
||||
assert not notifications_dao.get_notification_by_id(fake_uuid)
|
||||
assert not NotificationHistory.query.get(fake_uuid)
|
||||
|
||||
72
tests/app/test_model.py
Normal file
72
tests/app/test_model.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from datetime import datetime
|
||||
|
||||
from app import DATETIME_FORMAT
|
||||
from app.models import Notification
|
||||
|
||||
|
||||
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_type='SMS',
|
||||
api_key_id='api_key_id',
|
||||
key_type='key_type'
|
||||
)
|
||||
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
|
||||
assert not notification.job_id
|
||||
assert notification.to == 'someone'
|
||||
assert notification.service_id == 'service_id'
|
||||
assert notification.status == 'created'
|
||||
assert not notification.personalisation
|
||||
assert notification.notification_type == 'SMS'
|
||||
assert notification.api_key_id == 'api_key_id'
|
||||
assert notification.key_type == 'key_type'
|
||||
|
||||
|
||||
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'
|
||||
)
|
||||
assert notification.created_at == now
|
||||
assert notification.id == "notification_id"
|
||||
assert notification.template_id == 'template'
|
||||
assert notification.template_version == '1'
|
||||
assert notification.job_row_number == 100
|
||||
assert notification.job_id == 'job_id'
|
||||
assert notification.to == 'someone'
|
||||
assert notification.service_id == 'service_id'
|
||||
assert notification.status == 'created'
|
||||
assert notification.personalisation == {'key': 'value'}
|
||||
assert notification.notification_type == 'SMS'
|
||||
assert notification.api_key_id == 'api_key_id'
|
||||
assert notification.key_type == 'key_type'
|
||||
Reference in New Issue
Block a user