Merge pull request #2201 from alphagov/one-off-letters-from-admin-app

Allow the admin app to send one-off letters
This commit is contained in:
Chris Hill-Scott
2018-11-01 11:40:48 +00:00
committed by GitHub
4 changed files with 65 additions and 21 deletions

View File

@@ -12,6 +12,7 @@ from notifications_utils.recipients import (
from app import redis_store from app import redis_store
from app.celery import provider_tasks from app.celery import provider_tasks
from app.celery.letters_pdf_tasks import create_letters_pdf
from app.config import QueueNames from app.config import QueueNames
from app.models import ( from app.models import (
@@ -149,6 +150,10 @@ def send_notification_to_queue(notification, research_mode, queue=None):
if not queue: if not queue:
queue = QueueNames.SEND_EMAIL queue = QueueNames.SEND_EMAIL
deliver_task = provider_tasks.deliver_email deliver_task = provider_tasks.deliver_email
if notification.notification_type == LETTER_TYPE:
if not queue:
queue = QueueNames.CREATE_LETTERS_PDF
deliver_task = create_letters_pdf
try: try:
deliver_task.apply_async([str(notification.id)], queue=queue) deliver_task.apply_async([str(notification.id)], queue=queue)

View File

@@ -1,6 +1,7 @@
from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import NoResultFound
from app.config import QueueNames from app.config import QueueNames
from app.dao.notifications_dao import _update_notification_status
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.notifications.validators import ( from app.notifications.validators import (
@@ -17,6 +18,8 @@ from app.models import (
PRIORITY, PRIORITY,
SMS_TYPE, SMS_TYPE,
EMAIL_TYPE, EMAIL_TYPE,
LETTER_TYPE,
NOTIFICATION_DELIVERED,
) )
from app.dao.services_dao import dao_fetch_service_by_id from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id_and_service_id from app.dao.templates_dao import dao_get_template_by_id_and_service_id
@@ -78,11 +81,18 @@ def send_one_off_notification(service_id, post_data):
) )
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
send_notification_to_queue(
notification=notification, if template.template_type == LETTER_TYPE and service.research_mode:
research_mode=service.research_mode, _update_notification_status(
queue=queue_name notification,
) NOTIFICATION_DELIVERED,
)
else:
send_notification_to_queue(
notification=notification,
research_mode=service.research_mode,
queue=queue_name,
)
return {'id': str(notification.id)} return {'id': str(notification.id)}

View File

@@ -257,22 +257,35 @@ def test_persist_notification_increments_cache_if_key_exists(sample_template, sa
] ]
@pytest.mark.parametrize('research_mode, requested_queue, expected_queue, notification_type, key_type', @pytest.mark.parametrize((
[(True, None, 'research-mode-tasks', 'sms', 'normal'), 'research_mode, requested_queue, notification_type, key_type, expected_queue, expected_task'
(True, None, 'research-mode-tasks', 'email', 'normal'), ), [
(True, None, 'research-mode-tasks', 'email', 'team'), (True, None, 'sms', 'normal', 'research-mode-tasks', 'provider_tasks.deliver_sms'),
(False, None, 'send-sms-tasks', 'sms', 'normal'), (True, None, 'email', 'normal', 'research-mode-tasks', 'provider_tasks.deliver_email'),
(False, None, 'send-email-tasks', 'email', 'normal'), (True, None, 'email', 'team', 'research-mode-tasks', 'provider_tasks.deliver_email'),
(False, None, 'send-sms-tasks', 'sms', 'team'), (True, None, 'letter', 'normal', 'research-mode-tasks', 'letters_pdf_tasks.create_letters_pdf'),
(False, None, 'research-mode-tasks', 'sms', 'test'), (False, None, 'sms', 'normal', 'send-sms-tasks', 'provider_tasks.deliver_sms'),
(True, 'notify-internal-tasks', 'research-mode-tasks', 'email', 'normal'), (False, None, 'email', 'normal', 'send-email-tasks', 'provider_tasks.deliver_email'),
(False, 'notify-internal-tasks', 'notify-internal-tasks', 'sms', 'normal'), (False, None, 'sms', 'team', 'send-sms-tasks', 'provider_tasks.deliver_sms'),
(False, 'notify-internal-tasks', 'notify-internal-tasks', 'email', 'normal'), (False, None, 'letter', 'normal', 'create-letters-pdf-tasks', 'letters_pdf_tasks.create_letters_pdf'),
(False, 'notify-internal-tasks', 'research-mode-tasks', 'sms', 'test')]) (False, None, 'sms', 'test', 'research-mode-tasks', 'provider_tasks.deliver_sms'),
def test_send_notification_to_queue(notify_db, notify_db_session, (True, 'notify-internal-tasks', 'email', 'normal', 'research-mode-tasks', 'provider_tasks.deliver_email'),
research_mode, requested_queue, expected_queue, (False, 'notify-internal-tasks', 'sms', 'normal', 'notify-internal-tasks', 'provider_tasks.deliver_sms'),
notification_type, key_type, mocker): (False, 'notify-internal-tasks', 'email', 'normal', 'notify-internal-tasks', 'provider_tasks.deliver_email'),
mocked = mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type)) (False, 'notify-internal-tasks', 'sms', 'test', 'research-mode-tasks', 'provider_tasks.deliver_sms'),
])
def test_send_notification_to_queue(
notify_db,
notify_db_session,
research_mode,
requested_queue,
notification_type,
key_type,
expected_queue,
expected_task,
mocker,
):
mocked = mocker.patch('app.celery.{}.apply_async'.format(expected_task))
Notification = namedtuple('Notification', ['id', 'key_type', 'notification_type', 'created_at']) Notification = namedtuple('Notification', ['id', 'key_type', 'notification_type', 'created_at'])
notification = Notification( notification = Notification(
id=uuid.uuid4(), id=uuid.uuid4(),

View File

@@ -258,6 +258,22 @@ def test_send_one_off_letter_notification_should_use_template_reply_to_text(samp
assert notification.reply_to_text == "Edinburgh, ED1 1AA" assert notification.reply_to_text == "Edinburgh, ED1 1AA"
def test_send_one_off_letter_should_not_make_pdf_in_research_mode(sample_letter_template):
sample_letter_template.service.research_mode = True
data = {
'to': 'A. Name',
'template_id': str(sample_letter_template.id),
'created_by': str(sample_letter_template.service.created_by_id)
}
notification = send_one_off_notification(service_id=sample_letter_template.service.id, post_data=data)
notification = Notification.query.get(notification['id'])
assert notification.status == "delivered"
def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text(sample_service, celery_mock): def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text(sample_service, celery_mock):
template = create_template(service=sample_service, template_type=SMS_TYPE) template = create_template(service=sample_service, template_type=SMS_TYPE)
sms_sender = create_service_sms_sender( sms_sender = create_service_sms_sender(