mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 15:31:15 -05:00
Send notification to active service users with user fields (optional)
This commit is contained in:
@@ -89,6 +89,7 @@ class Config(object):
|
||||
PASSWORD_RESET_TEMPLATE_ID = '474e9242-823b-4f99-813d-ed392e7f1201'
|
||||
ALREADY_REGISTERED_EMAIL_TEMPLATE_ID = '0880fbb1-a0c6-46f0-9a8e-36c986381ceb'
|
||||
CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID = 'eb4d9930-87ab-4aef-9bce-786762687884'
|
||||
SERVICE_NOW_LIVE_TEMPLATE_ID = '618185c6-3636-49cd-b7d2-6f6f5eb3bdde'
|
||||
|
||||
BROKER_URL = 'sqs://'
|
||||
BROKER_TRANSPORT_OPTIONS = {
|
||||
|
||||
33
app/service/sender.py
Normal file
33
app/service/sender.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from flask import current_app
|
||||
|
||||
from app.dao.services_dao import dao_fetch_service_by_id, dao_fetch_active_users_for_service
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL
|
||||
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
||||
|
||||
|
||||
def send_notification_to_service_users(service_id, template_id, personalisation={}, include_user_fields=[]):
|
||||
template = dao_get_template_by_id(template_id)
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
active_users = dao_fetch_active_users_for_service(service.id)
|
||||
notify_service = dao_fetch_service_by_id(current_app.config['NOTIFY_SERVICE_ID'])
|
||||
|
||||
for user in active_users:
|
||||
personalisation = _add_user_fields(user, personalisation, include_user_fields)
|
||||
notification = persist_notification(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=user.email_address if template.template_type == EMAIL_TYPE else user.mobile_number,
|
||||
service=notify_service,
|
||||
personalisation=personalisation,
|
||||
notification_type=template.template_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL
|
||||
)
|
||||
send_notification_to_queue(notification, False, queue='notify')
|
||||
|
||||
|
||||
def _add_user_fields(user, personalisation, fields):
|
||||
for field in fields:
|
||||
personalisation[field] = getattr(user, field)
|
||||
return personalisation
|
||||
110
tests/app/service/test_sender.py
Normal file
110
tests/app/service/test_sender.py
Normal file
@@ -0,0 +1,110 @@
|
||||
import pytest
|
||||
|
||||
from flask import current_app
|
||||
|
||||
from app.dao.services_dao import dao_add_user_to_service
|
||||
from app.models import Notification, EMAIL_TYPE, SMS_TYPE
|
||||
from app.service.sender import send_notification_to_service_users
|
||||
|
||||
from tests.app.conftest import (
|
||||
notify_service as create_notify_service,
|
||||
sample_service as create_sample_service
|
||||
)
|
||||
from tests.app.db import create_template, create_user
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_type', [
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE
|
||||
])
|
||||
def test_send_notification_to_service_users_persists_notifications_correctly(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
notification_type,
|
||||
sample_user,
|
||||
mocker
|
||||
):
|
||||
mocker.patch('app.service.sender.send_notification_to_queue')
|
||||
|
||||
create_notify_service(notify_db, notify_db_session)
|
||||
service = create_sample_service(notify_db, notify_db_session, user=sample_user)
|
||||
template = create_template(service, template_type=notification_type)
|
||||
send_notification_to_service_users(service_id=service.id, template_id=template.id)
|
||||
to = sample_user.email_address if notification_type == EMAIL_TYPE else sample_user.mobile_number
|
||||
|
||||
notification = Notification.query.one()
|
||||
|
||||
assert Notification.query.count() == 1
|
||||
assert notification.to == to
|
||||
assert str(notification.service_id) == current_app.config['NOTIFY_SERVICE_ID']
|
||||
assert notification.template.id == template.id
|
||||
assert notification.template.template_type == notification_type
|
||||
assert notification.notification_type == notification_type
|
||||
|
||||
|
||||
def test_send_notification_to_service_users_sends_to_queue(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_user,
|
||||
mocker
|
||||
):
|
||||
send_mock = mocker.patch('app.service.sender.send_notification_to_queue')
|
||||
|
||||
create_notify_service(notify_db, notify_db_session)
|
||||
service = create_sample_service(notify_db, notify_db_session, user=sample_user)
|
||||
template = create_template(service, template_type=EMAIL_TYPE)
|
||||
send_notification_to_service_users(service_id=service.id, template_id=template.id)
|
||||
|
||||
assert send_mock.called
|
||||
assert send_mock.call_count == 1
|
||||
|
||||
|
||||
def test_send_notification_to_service_users_includes_user_fields_in_personalisation(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_user,
|
||||
mocker
|
||||
):
|
||||
persist_mock = mocker.patch('app.service.sender.persist_notification')
|
||||
mocker.patch('app.service.sender.send_notification_to_queue')
|
||||
|
||||
create_notify_service(notify_db, notify_db_session)
|
||||
service = create_sample_service(notify_db, notify_db_session, user=sample_user)
|
||||
template = create_template(service, template_type=EMAIL_TYPE)
|
||||
send_notification_to_service_users(
|
||||
service_id=service.id,
|
||||
template_id=template.id,
|
||||
include_user_fields=['name', 'email_address', 'state']
|
||||
)
|
||||
|
||||
assert persist_mock.mock_calls[0][2]['personalisation'] == {
|
||||
'name': sample_user.name,
|
||||
'email_address': sample_user.email_address,
|
||||
'state': sample_user.state,
|
||||
}
|
||||
|
||||
|
||||
def test_send_notification_to_service_users_sends_to_active_users_only(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
mocker
|
||||
):
|
||||
mocker.patch('app.service.sender.send_notification_to_queue')
|
||||
|
||||
create_notify_service(notify_db, notify_db_session)
|
||||
|
||||
first_active_user = create_user(email='foo@bar.com', state='active')
|
||||
second_active_user = create_user(email='foo1@bar.com', state='active')
|
||||
pending_user = create_user(email='foo2@bar.com', state='pending')
|
||||
service = create_sample_service(notify_db, notify_db_session, user=first_active_user)
|
||||
dao_add_user_to_service(service, second_active_user)
|
||||
dao_add_user_to_service(service, pending_user)
|
||||
template = create_template(service, template_type=EMAIL_TYPE)
|
||||
|
||||
send_notification_to_service_users(service_id=service.id, template_id=template.id)
|
||||
notifications = Notification.query.all()
|
||||
|
||||
assert Notification.query.count() == 2
|
||||
|
||||
assert notifications[0].to == first_active_user.email_address
|
||||
assert notifications[1].to == second_active_user.email_address
|
||||
Reference in New Issue
Block a user