mirror of
https://github.com/GSA/notifications-api.git
synced 2026-03-02 06:50:08 -05:00
Merge pull request #1309 from alphagov/save-email-reply-to-for-one-off
Add email_reply_to_id for one off notifications
This commit is contained in:
@@ -138,7 +138,7 @@ def validate_template(template_id, personalisation, service, notification_type):
|
||||
def check_service_email_reply_to_id(service_id, reply_to_id):
|
||||
if not (reply_to_id is None):
|
||||
try:
|
||||
reply_to = dao_get_reply_to_by_id(service_id, reply_to_id)
|
||||
dao_get_reply_to_by_id(service_id, reply_to_id)
|
||||
except NoResultFound:
|
||||
message = 'email_reply_to_id {} does not exist in database for service id {}'\
|
||||
.format(reply_to_id, service_id)
|
||||
|
||||
@@ -3,17 +3,17 @@ from app.notifications.validators import (
|
||||
check_service_over_daily_message_limit,
|
||||
validate_and_format_recipient,
|
||||
validate_template,
|
||||
)
|
||||
check_service_email_reply_to_id)
|
||||
from app.notifications.process_notifications import (
|
||||
create_content_for_notification,
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
)
|
||||
persist_email_reply_to_id_for_notification)
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
)
|
||||
EMAIL_TYPE)
|
||||
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.users_dao import get_user_by_id
|
||||
@@ -63,6 +63,10 @@ def send_one_off_notification(service_id, post_data):
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
created_by_id=post_data['created_by']
|
||||
)
|
||||
sender_id = post_data.get('sender_id', None)
|
||||
if sender_id and template.template_type == EMAIL_TYPE:
|
||||
check_service_email_reply_to_id(service_id, sender_id)
|
||||
persist_email_reply_to_id_for_notification(notification.id, sender_id)
|
||||
|
||||
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(
|
||||
|
||||
@@ -7,9 +7,14 @@ from notifications_utils.recipients import InvalidPhoneError
|
||||
from app.v2.errors import BadRequestError, TooManyRequestsError
|
||||
from app.config import QueueNames
|
||||
from app.service.send_notification import send_one_off_notification
|
||||
from app.models import KEY_TYPE_NORMAL, PRIORITY, SMS_TYPE
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
NotificationEmailReplyTo,
|
||||
Notification)
|
||||
|
||||
from tests.app.db import create_user
|
||||
from tests.app.db import create_user, create_reply_to_email
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -178,3 +183,37 @@ def test_send_one_off_notification_fails_if_created_by_other_service(sample_temp
|
||||
send_one_off_notification(sample_template.service_id, post_data)
|
||||
|
||||
assert e.value.message == 'Can’t create notification - Test User is not part of the "Sample service" service'
|
||||
|
||||
|
||||
def test_send_one_off_notification_should_add_email_reply_to_id_for_email(sample_email_template, celery_mock):
|
||||
reply_to_email = create_reply_to_email(sample_email_template.service, 'test@test.com')
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template_id': str(sample_email_template.id),
|
||||
'sender_id': reply_to_email.id,
|
||||
'created_by': str(sample_email_template.service.created_by_id)
|
||||
}
|
||||
|
||||
notification_id = send_one_off_notification(service_id=sample_email_template.service.id, post_data=data)
|
||||
notification = Notification.query.get(notification_id['id'])
|
||||
celery_mock.assert_called_once_with(
|
||||
notification=notification,
|
||||
research_mode=False,
|
||||
queue=None
|
||||
)
|
||||
mapping_row = NotificationEmailReplyTo.query.filter_by(notification_id=notification_id['id']).first()
|
||||
assert mapping_row.service_email_reply_to_id == reply_to_email.id
|
||||
|
||||
|
||||
def test_send_one_off_notification_should_throw_exception_if_reply_to_id_doesnot_exist(
|
||||
sample_email_template, celery_mock
|
||||
):
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template_id': str(sample_email_template.id),
|
||||
'sender_id': str(uuid.uuid4()),
|
||||
'created_by': str(sample_email_template.service.created_by_id)
|
||||
}
|
||||
|
||||
with pytest.raises(expected_exception=BadRequestError):
|
||||
send_one_off_notification(service_id=sample_email_template.service.id, post_data=data)
|
||||
|
||||
Reference in New Issue
Block a user