mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 10:42:41 -05:00
[WIP]
This commit is contained in:
@@ -978,6 +978,8 @@ class Notification(db.Model):
|
||||
created_by = db.relationship('User')
|
||||
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True)
|
||||
|
||||
reply_to_text = db.Column(db.String, nullable=True)
|
||||
|
||||
__table_args__ = (
|
||||
db.ForeignKeyConstraint(
|
||||
['template_id', 'template_version'],
|
||||
|
||||
@@ -54,7 +54,8 @@ def persist_notification(
|
||||
notification_id=None,
|
||||
simulated=False,
|
||||
created_by_id=None,
|
||||
status=NOTIFICATION_CREATED
|
||||
status=NOTIFICATION_CREATED,
|
||||
reply_to_text=None
|
||||
):
|
||||
notification_created_at = created_at or datetime.utcnow()
|
||||
if not notification_id:
|
||||
@@ -76,7 +77,8 @@ def persist_notification(
|
||||
client_reference=client_reference,
|
||||
reference=reference,
|
||||
created_by_id=created_by_id,
|
||||
status=status
|
||||
status=status,
|
||||
reply_to_text=reply_to_text,
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
|
||||
@@ -92,7 +92,8 @@ def post_notification(notification_type):
|
||||
notification_type=notification_type,
|
||||
api_key=api_user,
|
||||
template=template,
|
||||
service=authenticated_service
|
||||
service=authenticated_service,
|
||||
reply_to_text=sms_sender,
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
@@ -121,7 +122,7 @@ def post_notification(notification_type):
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service):
|
||||
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service, reply_to_text=None):
|
||||
form_send_to = form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number']
|
||||
|
||||
send_to = validate_and_format_recipient(send_to=form_send_to,
|
||||
@@ -142,7 +143,8 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ
|
||||
api_key_id=api_key.id,
|
||||
key_type=api_key.key_type,
|
||||
client_reference=form.get('reference', None),
|
||||
simulated=simulated
|
||||
simulated=simulated,
|
||||
reply_to_text=reply_to_text
|
||||
)
|
||||
|
||||
persist_sender_to_notification_mapping(form, notification)
|
||||
|
||||
@@ -3,14 +3,14 @@ import uuid
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app.dao.service_sms_sender_dao import update_existing_sms_sender_with_inbound_number
|
||||
from app.dao.service_sms_sender_dao import dao_update_service_sms_sender
|
||||
from app.models import (
|
||||
NotificationEmailReplyTo,
|
||||
ScheduledNotification,
|
||||
SCHEDULE_NOTIFICATIONS,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
NotificationSmsSender, ServiceSmsSender
|
||||
NotificationSmsSender
|
||||
)
|
||||
from flask import json, current_app
|
||||
|
||||
@@ -31,7 +31,7 @@ from tests.app.db import (
|
||||
create_service,
|
||||
create_template,
|
||||
create_reply_to_email,
|
||||
create_service_sms_sender, create_notification, create_inbound_number,
|
||||
create_service_sms_sender, create_notification,
|
||||
create_service_with_inbound_number
|
||||
)
|
||||
|
||||
@@ -96,13 +96,13 @@ def test_post_sms_notification_uses_inbound_number_as_sender(client, notify_db_s
|
||||
notification_id = notifications[0].id
|
||||
assert resp_json['id'] == str(notification_id)
|
||||
assert resp_json['content']['from_number'] == '1'
|
||||
assert notifications[0].reply_to_text == '1'
|
||||
mocked.assert_called_once_with([str(notification_id)], queue='send-sms-tasks')
|
||||
|
||||
|
||||
def test_post_sms_notification_returns_201_with_sms_sender_id(
|
||||
client, sample_template_with_placeholders, mocker
|
||||
):
|
||||
|
||||
sms_sender = create_service_sms_sender(service=sample_template_with_placeholders.service, sms_sender='123456')
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
data = {
|
||||
@@ -125,9 +125,40 @@ def test_post_sms_notification_returns_201_with_sms_sender_id(
|
||||
assert str(notification_to_sms_sender[0].notification_id) == resp_json['id']
|
||||
assert resp_json['content']['from_number'] == sms_sender.sms_sender
|
||||
assert notification_to_sms_sender[0].service_sms_sender_id == sms_sender.id
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
assert notifications[0].reply_to_text == sms_sender.sms_sender
|
||||
mocked.assert_called_once_with([resp_json['id']], queue='send-sms-tasks')
|
||||
|
||||
|
||||
def test_notification_reply_to_text_is_original_value_if_sender_is_changed_after_post_notification(
|
||||
client, sample_template, mocker
|
||||
):
|
||||
sms_sender = create_service_sms_sender(service=sample_template.service, sms_sender='123456', is_default=False)
|
||||
mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
|
||||
data = {
|
||||
'phone_number': '+447700900855',
|
||||
'template_id': str(sample_template.id),
|
||||
'sms_sender_id': str(sms_sender.id)
|
||||
}
|
||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
||||
|
||||
response = client.post(
|
||||
path='/v2/notifications/sms',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
dao_update_service_sms_sender(service_id=sample_template.service_id,
|
||||
service_sms_sender_id=sms_sender.id,
|
||||
is_default=sms_sender.is_default,
|
||||
sms_sender='updated')
|
||||
|
||||
assert response.status_code == 201
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
assert notifications[0].reply_to_text == '123456'
|
||||
|
||||
|
||||
@pytest.mark.parametrize("notification_type, key_send_to, send_to",
|
||||
[("sms", "phone_number", "+447700900855"),
|
||||
("email", "email_address", "sample@email.com")])
|
||||
@@ -637,6 +668,7 @@ def test_post_email_notification_with_valid_reply_to_id_returns_201(client, samp
|
||||
|
||||
assert email_reply_to.notification_id == notification.id
|
||||
assert email_reply_to.service_email_reply_to_id == reply_to_email.id
|
||||
assert notification.reply_to_text == reply_to_email.email_address
|
||||
|
||||
|
||||
def test_persist_sender_to_notification_mapping_for_email(notify_db_session):
|
||||
|
||||
Reference in New Issue
Block a user