mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-29 04:09:36 -04:00
fixed code style
This commit is contained in:
@@ -1,42 +1,18 @@
|
||||
from datetime import datetime, timedelta, date
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, date
|
||||
from functools import partial
|
||||
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
|
||||
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_service_id
|
||||
from app.models import (
|
||||
Job,
|
||||
Notification,
|
||||
NotificationEmailReplyTo,
|
||||
NotificationHistory,
|
||||
NotificationSmsSender,
|
||||
ScheduledNotification,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
NOTIFICATION_STATUS_TYPES,
|
||||
NOTIFICATION_STATUS_TYPES_FAILED,
|
||||
NOTIFICATION_SENT,
|
||||
NOTIFICATION_DELIVERED,
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
JOB_STATUS_IN_PROGRESS
|
||||
)
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
dao_create_notification_email_reply_to_mapping,
|
||||
dao_create_notification_sms_sender_mapping,
|
||||
dao_created_scheduled_notification,
|
||||
dao_delete_notifications_and_history_by_id,
|
||||
dao_get_last_notification_added_for_job_id,
|
||||
dao_get_last_template_usage,
|
||||
dao_get_notification_email_reply_for_notification,
|
||||
dao_get_notifications_by_to_field,
|
||||
dao_get_notification_sms_sender_mapping,
|
||||
dao_get_scheduled_notifications,
|
||||
dao_get_template_usage,
|
||||
dao_timeout_notifications,
|
||||
@@ -56,14 +32,21 @@ from app.dao.notifications_dao import (
|
||||
update_notification_status_by_reference,
|
||||
dao_get_notifications_by_reference
|
||||
)
|
||||
|
||||
from app.dao.services_dao import dao_update_service
|
||||
from tests.app.db import (
|
||||
create_api_key,
|
||||
create_job,
|
||||
create_notification,
|
||||
create_reply_to_email,
|
||||
create_service_sms_sender)
|
||||
from app.models import (
|
||||
Job,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
ScheduledNotification,
|
||||
NOTIFICATION_STATUS_TYPES,
|
||||
NOTIFICATION_STATUS_TYPES_FAILED,
|
||||
NOTIFICATION_SENT,
|
||||
NOTIFICATION_DELIVERED,
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
JOB_STATUS_IN_PROGRESS
|
||||
)
|
||||
from tests.app.conftest import (
|
||||
sample_notification,
|
||||
sample_template,
|
||||
@@ -71,7 +54,13 @@ from tests.app.conftest import (
|
||||
sample_service,
|
||||
sample_job,
|
||||
sample_notification_history as create_notification_history,
|
||||
sample_letter_template)
|
||||
sample_letter_template
|
||||
)
|
||||
from tests.app.db import (
|
||||
create_api_key,
|
||||
create_job,
|
||||
create_notification
|
||||
)
|
||||
|
||||
|
||||
def test_should_have_decorated_notifications_dao_functions():
|
||||
@@ -986,73 +975,6 @@ def test_should_delete_notifications_by_type_after_seven_days(
|
||||
assert notification.created_at.date() >= date(2016, 1, 3)
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_should_delete_notification_to_email_reply_to_after_seven_days(
|
||||
notify_db, notify_db_session, sample_service,
|
||||
):
|
||||
assert len(Notification.query.all()) == 0
|
||||
|
||||
reply_to = create_reply_to_email(sample_service, 'test@example.com')
|
||||
|
||||
email_template = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||
|
||||
# create one notification a day between 1st and 10th from 11:00 to 19:00 of each type
|
||||
for i in range(1, 11):
|
||||
past_date = '2016-01-{0:02d} {0:02d}:00:00.000000'.format(i)
|
||||
with freeze_time(past_date):
|
||||
notification = create_notification(email_template)
|
||||
dao_create_notification_email_reply_to_mapping(notification.id, reply_to.id)
|
||||
|
||||
all_notifications = Notification.query.all()
|
||||
assert len(all_notifications) == 10
|
||||
|
||||
all_notification_email_reply_to = NotificationEmailReplyTo.query.all()
|
||||
assert len(all_notification_email_reply_to) == 10
|
||||
|
||||
# Records before 3rd should be deleted
|
||||
delete_notifications_created_more_than_a_week_ago_by_type(EMAIL_TYPE)
|
||||
remaining_email_notifications = Notification.query.filter_by(notification_type=EMAIL_TYPE).all()
|
||||
remaining_notification_to_email_reply_to = NotificationEmailReplyTo.query.filter_by().all()
|
||||
|
||||
assert len(remaining_email_notifications) == 8
|
||||
assert len(remaining_notification_to_email_reply_to) == 8
|
||||
|
||||
for notification in remaining_email_notifications:
|
||||
assert notification.created_at.date() >= date(2016, 1, 3)
|
||||
|
||||
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_should_delete_notification_to_sms_sender_after_seven_days(
|
||||
sample_template
|
||||
):
|
||||
assert len(Notification.query.all()) == 0
|
||||
|
||||
sms_sender = create_service_sms_sender(service=sample_template.service, sms_sender='123456', is_default=False)
|
||||
|
||||
# create one notification a day between 1st and 10th from 11:00 to 19:00 of each type
|
||||
for i in range(1, 11):
|
||||
past_date = '2016-01-{0:02d} {0:02d}:00:00.000000'.format(i)
|
||||
with freeze_time(past_date):
|
||||
notification = create_notification(template=sample_template, sms_sender_id=sms_sender.id)
|
||||
|
||||
all_notifications = Notification.query.all()
|
||||
assert len(all_notifications) == 10
|
||||
|
||||
all_notification_sms_senders = NotificationSmsSender.query.all()
|
||||
assert len(all_notification_sms_senders) == 10
|
||||
|
||||
# Records before 3rd should be deleted
|
||||
delete_notifications_created_more_than_a_week_ago_by_type(SMS_TYPE)
|
||||
remaining_notifications = Notification.query.filter_by(notification_type=SMS_TYPE).all()
|
||||
remaining_notification_to_sms_sender = NotificationSmsSender.query.filter_by().all()
|
||||
|
||||
assert len(remaining_notifications) == 8
|
||||
assert len(remaining_notification_to_sms_sender) == 8
|
||||
|
||||
for notification in remaining_notifications:
|
||||
assert notification.created_at.date() >= date(2016, 1, 3)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('notification_type', ['sms', 'email', 'letter'])
|
||||
@freeze_time("2016-01-10 12:00:00.000000")
|
||||
def test_should_not_delete_notification_history(notify_db, notify_db_session, sample_service, notification_type):
|
||||
@@ -2007,48 +1929,6 @@ def test_dao_get_notifications_by_to_field_orders_by_created_at_desc(sample_temp
|
||||
assert notifications[1].id == notification_a_minute_ago.id
|
||||
|
||||
|
||||
def test_dao_create_notification_email_reply_to_mapping(sample_service, sample_notification):
|
||||
|
||||
create_reply_to_email(sample_service, "test@test.com")
|
||||
|
||||
reply_to_address = dao_get_reply_to_by_service_id(sample_service.id)
|
||||
|
||||
dao_create_notification_email_reply_to_mapping(sample_notification.id, reply_to_address[0].id)
|
||||
|
||||
email_reply_to = NotificationEmailReplyTo.query.all()
|
||||
|
||||
assert len(email_reply_to) == 1
|
||||
assert email_reply_to[0].notification_id == sample_notification.id
|
||||
assert email_reply_to[0].service_email_reply_to_id == reply_to_address[0].id
|
||||
|
||||
|
||||
def test_dao_create_multiple_notification_email_reply_to_mapping(sample_service, sample_notification):
|
||||
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
||||
|
||||
dao_create_notification_email_reply_to_mapping(sample_notification.id, reply_to_address.id)
|
||||
|
||||
with pytest.raises(IntegrityError) as e:
|
||||
dao_create_notification_email_reply_to_mapping(sample_notification.id, reply_to_address.id)
|
||||
|
||||
assert 'duplicate key value' in str(e.value)
|
||||
|
||||
email_reply_to = NotificationEmailReplyTo.query.all()
|
||||
|
||||
assert len(email_reply_to) == 1
|
||||
assert email_reply_to[0].notification_id == sample_notification.id
|
||||
assert email_reply_to[0].service_email_reply_to_id == reply_to_address.id
|
||||
|
||||
|
||||
def test_dao_get_notification_email_reply_for_notification(sample_service, sample_notification):
|
||||
reply_to_address = create_reply_to_email(sample_service, "test@test.com")
|
||||
dao_create_notification_email_reply_to_mapping(sample_notification.id, reply_to_address.id)
|
||||
assert dao_get_notification_email_reply_for_notification(sample_notification.id) == "test@test.com"
|
||||
|
||||
|
||||
def test_dao_get_notification_email_reply_for_notification_where_no_mapping(notify_db_session, fake_uuid):
|
||||
assert dao_get_notification_email_reply_for_notification(fake_uuid) is None
|
||||
|
||||
|
||||
def test_dao_get_last_notification_added_for_job_id_valid_job_id(sample_template):
|
||||
job = create_job(template=sample_template, notification_count=10,
|
||||
created_at=datetime.utcnow() - timedelta(hours=2),
|
||||
@@ -2121,26 +2001,3 @@ def test_dao_get_notifications_by_reference(sample_template):
|
||||
assert len(notifications) == 2
|
||||
assert notifications[0].id in [notification_1.id, notification_2.id]
|
||||
assert notifications[1].id in [notification_1.id, notification_2.id]
|
||||
|
||||
|
||||
def test_dao_create_notification_sms_sender_mapping(sample_notification):
|
||||
sms_sender = create_service_sms_sender(service=sample_notification.service, sms_sender='123456')
|
||||
dao_create_notification_sms_sender_mapping(notification_id=sample_notification.id,
|
||||
sms_sender_id=sms_sender.id)
|
||||
notification_to_senders = NotificationSmsSender.query.all()
|
||||
assert len(notification_to_senders) == 1
|
||||
assert notification_to_senders[0].notification_id == sample_notification.id
|
||||
assert notification_to_senders[0].service_sms_sender_id == sms_sender.id
|
||||
|
||||
|
||||
def test_dao_get_notification_sms_sender_mapping(sample_notification):
|
||||
sms_sender = create_service_sms_sender(service=sample_notification.service, sms_sender='123456')
|
||||
dao_create_notification_sms_sender_mapping(notification_id=sample_notification.id,
|
||||
sms_sender_id=sms_sender.id)
|
||||
notification_to_sender = dao_get_notification_sms_sender_mapping(sample_notification.id)
|
||||
assert notification_to_sender == '123456'
|
||||
|
||||
|
||||
def test_dao_get_notification_sms_sender_mapping_returns_none(sample_notification):
|
||||
notification_to_sender = dao_get_notification_sms_sender_mapping(sample_notification.id)
|
||||
assert not notification_to_sender
|
||||
|
||||
@@ -13,7 +13,6 @@ from app.models import (
|
||||
Job,
|
||||
MonthlyBilling,
|
||||
Notification,
|
||||
NotificationEmailReplyTo,
|
||||
Organisation,
|
||||
Rate,
|
||||
Service,
|
||||
@@ -34,8 +33,7 @@ from app.models import (
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
dao_created_scheduled_notification,
|
||||
dao_create_notification_sms_sender_mapping
|
||||
dao_created_scheduled_notification
|
||||
)
|
||||
from app.dao.templates_dao import dao_create_template
|
||||
from app.dao.services_dao import dao_create_service
|
||||
@@ -221,9 +219,7 @@ def create_notification(
|
||||
if status != 'created':
|
||||
scheduled_notification.pending = False
|
||||
dao_created_scheduled_notification(scheduled_notification)
|
||||
if sms_sender_id:
|
||||
dao_create_notification_sms_sender_mapping(notification_id=notification.id,
|
||||
sms_sender_id=sms_sender_id)
|
||||
|
||||
return notification
|
||||
|
||||
|
||||
@@ -445,24 +441,6 @@ def create_letter_contact(
|
||||
return letter_content
|
||||
|
||||
|
||||
def create_reply_to_email_for_notification(
|
||||
notification_id,
|
||||
service,
|
||||
email_address,
|
||||
is_default=True
|
||||
):
|
||||
reply_to = create_reply_to_email(service, email_address, is_default)
|
||||
|
||||
notification_email_reply_to = NotificationEmailReplyTo(
|
||||
notification_id=str(notification_id),
|
||||
service_email_reply_to_id=str(reply_to.id)
|
||||
)
|
||||
db.session.add(notification_email_reply_to)
|
||||
db.session.commit()
|
||||
|
||||
return reply_to
|
||||
|
||||
|
||||
def create_annual_billing(
|
||||
service_id, free_sms_fragment_limit, financial_year_start
|
||||
):
|
||||
|
||||
@@ -29,9 +29,7 @@ from tests.app.db import (
|
||||
create_template,
|
||||
create_notification,
|
||||
create_reply_to_email,
|
||||
create_reply_to_email_for_notification,
|
||||
create_service_sms_sender,
|
||||
create_service_with_inbound_number,
|
||||
create_service_with_defined_sms_sender
|
||||
)
|
||||
|
||||
@@ -708,65 +706,13 @@ def test_should_handle_sms_sender_and_prefix_message(
|
||||
)
|
||||
|
||||
|
||||
def test_should_use_inbound_number_as_sender_if_default_sms_sender(
|
||||
notify_db_session,
|
||||
mocker
|
||||
):
|
||||
service = create_service_with_inbound_number(inbound_number='inbound')
|
||||
create_service_sms_sender(service=service, sms_sender="sms_sender", is_default=False)
|
||||
template = create_template(service, content='bar')
|
||||
notification = create_notification(template, reply_to_text='inbound')
|
||||
|
||||
mocker.patch('app.mmg_client.send_sms')
|
||||
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
|
||||
|
||||
send_to_providers.send_sms_to_provider(notification)
|
||||
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
to=ANY,
|
||||
content=ANY,
|
||||
reference=str(notification.id),
|
||||
sender='inbound'
|
||||
)
|
||||
|
||||
|
||||
def test_should_use_default_sms_sender(
|
||||
notify_db_session,
|
||||
mocker
|
||||
):
|
||||
service = create_service_with_defined_sms_sender(sms_sender_value="test sender")
|
||||
template = create_template(service, content='bar')
|
||||
notification = create_notification(template, reply_to_text=service.get_default_sms_sender())
|
||||
|
||||
mocker.patch('app.mmg_client.send_sms')
|
||||
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
|
||||
|
||||
send_to_providers.send_sms_to_provider(notification)
|
||||
|
||||
mmg_client.send_sms.assert_called_once_with(
|
||||
to=ANY,
|
||||
content=ANY,
|
||||
reference=str(notification.id),
|
||||
sender='test sender'
|
||||
)
|
||||
|
||||
|
||||
def test_send_email_to_provider_get_linked_email_reply_to_default_is_false(
|
||||
sample_service,
|
||||
def test_send_email_to_provider_uses_reply_to_from_notification(
|
||||
sample_email_template,
|
||||
mocker):
|
||||
mocker.patch('app.aws_ses_client.send_email', return_value='reference')
|
||||
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
|
||||
|
||||
db_notification = create_notification(template=sample_email_template, reply_to_text="test@test.com")
|
||||
create_reply_to_email(service=sample_service, email_address="test@test.com")
|
||||
|
||||
reply_to = create_reply_to_email_for_notification(
|
||||
db_notification.id,
|
||||
sample_service,
|
||||
"test@test.com",
|
||||
is_default=False
|
||||
)
|
||||
|
||||
send_to_providers.send_email_to_provider(
|
||||
db_notification,
|
||||
@@ -778,45 +724,11 @@ def test_send_email_to_provider_get_linked_email_reply_to_default_is_false(
|
||||
ANY,
|
||||
body=ANY,
|
||||
html_body=ANY,
|
||||
reply_to_address=reply_to.email_address
|
||||
)
|
||||
|
||||
|
||||
def test_send_email_to_provider_get_linked_email_reply_to_create_service_email_after_notification_mapping(
|
||||
sample_service,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
mocker.patch('app.aws_ses_client.send_email', return_value='reference')
|
||||
mocker.patch('app.delivery.send_to_providers.create_initial_notification_statistic_tasks')
|
||||
|
||||
db_notification = create_notification(template=sample_email_template,
|
||||
reply_to_text="test@test.com")
|
||||
|
||||
# notification_to_email_reply_to is being deprecated.
|
||||
reply_to = create_reply_to_email_for_notification(
|
||||
db_notification.id,
|
||||
sample_service,
|
||||
"test@test.com"
|
||||
)
|
||||
|
||||
create_reply_to_email(service=sample_service, email_address='foo@bar.com', is_default=False)
|
||||
|
||||
send_to_providers.send_email_to_provider(
|
||||
db_notification,
|
||||
)
|
||||
|
||||
app.aws_ses_client.send_email.assert_called_once_with(
|
||||
ANY,
|
||||
ANY,
|
||||
ANY,
|
||||
body=ANY,
|
||||
html_body=ANY,
|
||||
reply_to_address=reply_to.email_address
|
||||
reply_to_address="test@test.com"
|
||||
)
|
||||
|
||||
|
||||
def test_send_email_to_provider_should_format_reply_to_email_address(
|
||||
sample_service,
|
||||
sample_email_template,
|
||||
mocker):
|
||||
mocker.patch('app.aws_ses_client.send_email', return_value='reference')
|
||||
@@ -824,12 +736,6 @@ def test_send_email_to_provider_should_format_reply_to_email_address(
|
||||
|
||||
db_notification = create_notification(template=sample_email_template, reply_to_text="test@test.com\t")
|
||||
|
||||
create_reply_to_email_for_notification(
|
||||
db_notification.id,
|
||||
sample_service,
|
||||
"test@test.com"
|
||||
)
|
||||
|
||||
send_to_providers.send_email_to_provider(
|
||||
db_notification,
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ def firetext_post(client, data):
|
||||
data=data,
|
||||
headers=[
|
||||
('Content-Type', 'application/x-www-form-urlencoded'),
|
||||
('X-Forwarded-For', '203.0.113.195, 70.41.3.18, 150.172.238.178') # fake IPs
|
||||
('X-Forwarded-For', '203.0.113.195, 70.41.3.18, 150.172.238.178') # fake IPs
|
||||
])
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ def mmg_post(client, data):
|
||||
data=data,
|
||||
headers=[
|
||||
('Content-Type', 'application/json'),
|
||||
('X-Forwarded-For', '203.0.113.195, 70.41.3.18, 150.172.238.178') # fake IPs
|
||||
('X-Forwarded-For', '203.0.113.195, 70.41.3.18, 150.172.238.178') # fake IPs
|
||||
])
|
||||
|
||||
|
||||
@@ -384,6 +384,7 @@ def test_process_mmg_response_unknown_status_updates_notification_with_failed(
|
||||
assert json_data['result'] == 'success'
|
||||
assert json_data['message'] == 'MMG callback succeeded. reference {} updated'.format(notification.id)
|
||||
assert get_notification_by_id(notification.id).status == 'failed'
|
||||
assert send_mock.called
|
||||
|
||||
|
||||
def test_process_mmg_response_returns_400_for_malformed_data(client):
|
||||
|
||||
@@ -7,21 +7,16 @@ from sqlalchemy.exc import SQLAlchemyError
|
||||
from freezegun import freeze_time
|
||||
from collections import namedtuple
|
||||
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_service_id
|
||||
from app.models import (
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
NotificationEmailReplyTo,
|
||||
NotificationSmsSender,
|
||||
ScheduledNotification,
|
||||
Template
|
||||
)
|
||||
from app.notifications.process_notifications import (
|
||||
create_content_for_notification,
|
||||
persist_notification,
|
||||
persist_email_reply_to_id_for_notification,
|
||||
persist_scheduled_notification,
|
||||
persist_sms_sender_id_for_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient
|
||||
)
|
||||
@@ -29,7 +24,6 @@ from notifications_utils.recipients import validate_and_format_phone_number, val
|
||||
from app.utils import cache_key_for_service_template_counter
|
||||
from app.v2.errors import BadRequestError
|
||||
from tests.app.conftest import sample_api_key as create_api_key
|
||||
from tests.app.db import create_reply_to_email, create_service_sms_sender
|
||||
|
||||
|
||||
def test_create_content_for_notification_passes(sample_email_template):
|
||||
@@ -452,27 +446,3 @@ def test_persist_email_notification_stores_normalised_email(
|
||||
|
||||
assert persisted_notification.to == recipient
|
||||
assert persisted_notification.normalised_to == expected_recipient_normalised
|
||||
|
||||
|
||||
def test_persist_email_reply_to_id_for_notification(sample_service, sample_notification):
|
||||
create_reply_to_email(sample_service, "test@test.com")
|
||||
reply_to_address = dao_get_reply_to_by_service_id(sample_service.id)
|
||||
persist_email_reply_to_id_for_notification(sample_notification.id, reply_to_address[0].id)
|
||||
|
||||
email_reply_to = NotificationEmailReplyTo.query.all()
|
||||
|
||||
assert len(email_reply_to) == 1
|
||||
assert email_reply_to[0].notification_id == sample_notification.id
|
||||
assert email_reply_to[0].service_email_reply_to_id == reply_to_address[0].id
|
||||
|
||||
|
||||
def test_persist_sms_sender_id_for_notification(sample_service, sample_notification):
|
||||
sms_sender = create_service_sms_sender(service=sample_service, sms_sender="123456")
|
||||
persist_sms_sender_id_for_notification(notification_id=sample_notification.id,
|
||||
sms_sender_id=sms_sender.id)
|
||||
|
||||
notification_to_sms_sender = NotificationSmsSender.query.all()
|
||||
|
||||
assert len(notification_to_sms_sender) == 1
|
||||
assert notification_to_sms_sender[0].notification_id == sample_notification.id
|
||||
assert notification_to_sms_sender[0].service_sms_sender_id == sms_sender.id
|
||||
|
||||
@@ -12,15 +12,12 @@ from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
NotificationEmailReplyTo,
|
||||
Notification,
|
||||
NotificationSmsSender
|
||||
Notification
|
||||
)
|
||||
|
||||
from tests.app.db import (
|
||||
create_user,
|
||||
create_reply_to_email,
|
||||
create_service_sms_sender,
|
||||
create_service,
|
||||
create_template
|
||||
)
|
||||
@@ -200,7 +197,7 @@ def test_send_one_off_notification_fails_if_created_by_other_service(sample_temp
|
||||
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):
|
||||
def test_send_one_off_notification_should_add_email_reply_to_text_for_notification(sample_email_template, celery_mock):
|
||||
reply_to_email = create_reply_to_email(sample_email_template.service, 'test@test.com')
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
@@ -216,8 +213,7 @@ def test_send_one_off_notification_should_add_email_reply_to_id_for_email(sample
|
||||
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
|
||||
notification.reply_to_text == reply_to_email.email_address
|
||||
|
||||
|
||||
def test_send_one_off_notification_should_throw_exception_if_reply_to_id_doesnot_exist(
|
||||
@@ -234,26 +230,6 @@ def test_send_one_off_notification_should_throw_exception_if_reply_to_id_doesnot
|
||||
send_one_off_notification(service_id=sample_email_template.service.id, post_data=data)
|
||||
|
||||
|
||||
def test_send_one_off_notification_should_add_sms_sender_mapping_for_sms(sample_template, celery_mock):
|
||||
sms_sender = create_service_sms_sender(service=sample_template.service, sms_sender='123456')
|
||||
data = {
|
||||
'to': '07700 900 001',
|
||||
'template_id': str(sample_template.id),
|
||||
'sender_id': sms_sender.id,
|
||||
'created_by': str(sample_template.service.created_by_id)
|
||||
}
|
||||
|
||||
notification_id = send_one_off_notification(service_id=sample_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 = NotificationSmsSender.query.filter_by(notification_id=notification_id['id']).first()
|
||||
assert mapping_row.service_sms_sender_id == sms_sender.id
|
||||
|
||||
|
||||
def test_send_one_off_notification_should_throw_exception_if_sms_sender_id_doesnot_exist(
|
||||
sample_template
|
||||
):
|
||||
|
||||
@@ -565,6 +565,29 @@ def test_update_set_process_type_on_template(client, sample_template):
|
||||
assert template.process_type == 'priority'
|
||||
|
||||
|
||||
def test_create_a_template_with_reply_to(admin_request, sample_user):
|
||||
service = create_service(service_permissions=['letter'])
|
||||
letter_contact = create_letter_contact(service, "Edinburgh, ED1 1AA")
|
||||
data = {
|
||||
'name': 'my template',
|
||||
'subject': 'subject',
|
||||
'template_type': 'letter',
|
||||
'content': 'template <b>content</b>',
|
||||
'service': str(service.id),
|
||||
'created_by': str(sample_user.id),
|
||||
'reply_to': str(letter_contact.id),
|
||||
}
|
||||
|
||||
json_resp = admin_request.post('template.create_template', service_id=service.id, _data=data, _expected_status=201)
|
||||
|
||||
assert json_resp['data']['template_type'] == 'letter'
|
||||
assert json_resp['data']['reply_to'] == str(letter_contact.id)
|
||||
|
||||
template = Template.query.get(json_resp['data']['id'])
|
||||
from app.schemas import template_schema
|
||||
assert sorted(json_resp['data']) == sorted(template_schema.dump(template).data)
|
||||
|
||||
|
||||
def test_get_template_reply_to(client, sample_letter_template):
|
||||
auth_header = create_authorization_header()
|
||||
letter_contact = create_letter_contact(sample_letter_template.service, "Edinburgh, ED1 1AA")
|
||||
|
||||
@@ -5,12 +5,10 @@ from freezegun import freeze_time
|
||||
|
||||
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
|
||||
SMS_TYPE
|
||||
)
|
||||
from flask import json, current_app
|
||||
|
||||
@@ -18,7 +16,6 @@ from app.models import Notification
|
||||
from app.schema_validation import validate
|
||||
from app.v2.errors import RateLimitError
|
||||
from app.v2.notifications.notification_schemas import post_sms_response, post_email_response
|
||||
from app.v2.notifications.post_notifications import persist_sender_to_notification_mapping
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import (
|
||||
sample_template as create_sample_template,
|
||||
@@ -31,7 +28,7 @@ from tests.app.db import (
|
||||
create_service,
|
||||
create_template,
|
||||
create_reply_to_email,
|
||||
create_service_sms_sender, create_notification,
|
||||
create_service_sms_sender,
|
||||
create_service_with_inbound_number
|
||||
)
|
||||
|
||||
@@ -120,11 +117,7 @@ def test_post_sms_notification_returns_201_with_sms_sender_id(
|
||||
assert response.status_code == 201
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
assert validate(resp_json, post_sms_response) == resp_json
|
||||
notification_to_sms_sender = NotificationSmsSender.query.all()
|
||||
assert len(notification_to_sms_sender) == 1
|
||||
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
|
||||
@@ -626,10 +619,6 @@ def test_post_email_notification_with_valid_reply_to_id_returns_201(client, samp
|
||||
assert resp_json['id'] == str(notification.id)
|
||||
assert mocked.called
|
||||
|
||||
email_reply_to = NotificationEmailReplyTo.query.one()
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -650,35 +639,3 @@ def test_post_email_notification_with_invalid_reply_to_id_returns_400(client, sa
|
||||
assert 'email_reply_to_id {} does not exist in database for service id {}'. \
|
||||
format(fake_uuid, sample_email_template.service_id) in resp_json['errors'][0]['message']
|
||||
assert 'BadRequestError' in resp_json['errors'][0]['error']
|
||||
|
||||
|
||||
def test_persist_sender_to_notification_mapping_for_email(sample_service):
|
||||
template = create_template(service=sample_service, template_type=EMAIL_TYPE)
|
||||
sender = create_reply_to_email(service=sample_service, email_address='reply@test.com', is_default=False)
|
||||
form = {
|
||||
"email_address": "recipient@test.com",
|
||||
"template_id": str(template.id),
|
||||
'email_reply_to_id': str(sender.id)
|
||||
}
|
||||
notification = create_notification(template=template)
|
||||
persist_sender_to_notification_mapping(form=form, notification=notification)
|
||||
notification_to_email_reply_to = NotificationEmailReplyTo.query.all()
|
||||
assert len(notification_to_email_reply_to) == 1
|
||||
assert notification_to_email_reply_to[0].notification_id == notification.id
|
||||
assert notification_to_email_reply_to[0].service_email_reply_to_id == sender.id
|
||||
|
||||
|
||||
def test_persist_sender_to_notification_mapping_for_sms(sample_service):
|
||||
template = create_template(service=sample_service, template_type=SMS_TYPE)
|
||||
sender = create_service_sms_sender(service=sample_service, sms_sender='12345', is_default=False)
|
||||
form = {
|
||||
'phone_number': '+447700900855',
|
||||
'template_id': str(template.id),
|
||||
'sms_sender_id': str(sender.id)
|
||||
}
|
||||
notification = create_notification(template=template)
|
||||
persist_sender_to_notification_mapping(form=form, notification=notification)
|
||||
notification_to_sms_sender = NotificationSmsSender.query.all()
|
||||
assert len(notification_to_sms_sender) == 1
|
||||
assert notification_to_sms_sender[0].notification_id == notification.id
|
||||
assert notification_to_sms_sender[0].service_sms_sender_id == sender.id
|
||||
|
||||
Reference in New Issue
Block a user