mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-08 08:28:24 -04:00
Merge branch 'master' of https://github.com/alphagov/notifications-api into vb-free-sms-limit-history
This commit is contained in:
@@ -12,10 +12,10 @@ from app.models import (
|
||||
Notification,
|
||||
NotificationEmailReplyTo,
|
||||
NotificationHistory,
|
||||
NotificationStatistics,
|
||||
NotificationSmsSender,
|
||||
ScheduledNotification,
|
||||
ServiceEmailReplyTo,
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
NOTIFICATION_STATUS_TYPES,
|
||||
NOTIFICATION_STATUS_TYPES_FAILED,
|
||||
NOTIFICATION_SENT,
|
||||
@@ -23,23 +23,26 @@ from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
JOB_STATUS_IN_PROGRESS)
|
||||
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_statistics_for_service_and_day,
|
||||
dao_get_potential_notification_statistics_for_day,
|
||||
dao_get_notification_sms_sender_mapping,
|
||||
dao_get_scheduled_notifications,
|
||||
dao_get_template_usage,
|
||||
dao_timeout_notifications,
|
||||
dao_update_notification,
|
||||
dao_update_notifications_for_job_to_sent_to_dvla,
|
||||
dao_update_notifications_by_reference,
|
||||
delete_notifications_created_more_than_a_week_ago_by_type,
|
||||
get_notification_by_id,
|
||||
get_notification_for_job,
|
||||
@@ -50,16 +53,16 @@ from app.dao.notifications_dao import (
|
||||
is_delivery_slow_for_provider,
|
||||
set_scheduled_notification_to_processed,
|
||||
update_notification_status_by_id,
|
||||
update_notification_status_by_reference,
|
||||
dao_get_last_notification_added_for_job_id, dao_update_notifications_by_reference)
|
||||
update_notification_status_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_reply_to_email,
|
||||
create_service_sms_sender)
|
||||
from tests.app.conftest import (
|
||||
sample_notification,
|
||||
sample_template,
|
||||
@@ -73,7 +76,6 @@ from tests.app.conftest import (
|
||||
def test_should_have_decorated_notifications_dao_functions():
|
||||
assert dao_get_last_template_usage.__wrapped__.__name__ == 'dao_get_last_template_usage' # noqa
|
||||
assert dao_get_template_usage.__wrapped__.__name__ == 'dao_get_template_usage' # noqa
|
||||
assert dao_get_potential_notification_statistics_for_day.__wrapped__.__name__ == 'dao_get_potential_notification_statistics_for_day' # noqa
|
||||
assert dao_create_notification.__wrapped__.__name__ == 'dao_create_notification' # noqa
|
||||
assert update_notification_status_by_id.__wrapped__.__name__ == 'update_notification_status_by_id' # noqa
|
||||
assert dao_update_notification.__wrapped__.__name__ == 'dao_update_notification' # noqa
|
||||
@@ -575,31 +577,10 @@ def test_should_return_zero_count_if_no_notification_with_reference():
|
||||
assert not update_notification_status_by_reference('something', 'delivered')
|
||||
|
||||
|
||||
def test_should_return_none_if_no_statistics_for_a_service_for_a_day(sample_template, mmg_provider):
|
||||
data = _notification_json(sample_template)
|
||||
|
||||
notification = Notification(**data)
|
||||
dao_create_notification(notification)
|
||||
assert not dao_get_notification_statistics_for_service_and_day(
|
||||
sample_template.service.id, (datetime.utcnow() - timedelta(days=1)).date())
|
||||
|
||||
|
||||
def test_should_be_able_to_get_all_statistics_for_a_service(sample_template, mmg_provider):
|
||||
data = _notification_json(sample_template)
|
||||
|
||||
notification_1 = Notification(**data)
|
||||
notification_2 = Notification(**data)
|
||||
notification_3 = Notification(**data)
|
||||
dao_create_notification(notification_1)
|
||||
dao_create_notification(notification_2)
|
||||
dao_create_notification(notification_3)
|
||||
|
||||
|
||||
def test_create_notification_creates_notification_with_personalisation(notify_db, notify_db_session,
|
||||
sample_template_with_placeholders,
|
||||
sample_job, mmg_provider):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
data = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
|
||||
template=sample_template_with_placeholders,
|
||||
@@ -622,7 +603,6 @@ def test_create_notification_creates_notification_with_personalisation(notify_db
|
||||
|
||||
def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
data = _notification_json(sample_template, job_id=sample_job.id)
|
||||
|
||||
@@ -643,7 +623,6 @@ def test_save_notification_creates_sms(sample_template, sample_job, mmg_provider
|
||||
|
||||
def test_save_notification_and_create_email(sample_email_template, sample_job):
|
||||
assert Notification.query.count() == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
data = _notification_json(sample_email_template, job_id=sample_job.id)
|
||||
|
||||
@@ -767,7 +746,6 @@ def test_not_save_notification_and_not_create_stats_on_commit_error(sample_templ
|
||||
|
||||
assert Notification.query.count() == 0
|
||||
assert Job.query.get(sample_job.id).notifications_sent == 0
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
|
||||
|
||||
def test_save_notification_and_increment_job(sample_template, sample_job, mmg_provider):
|
||||
@@ -1042,6 +1020,38 @@ def test_should_delete_notification_to_email_reply_to_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_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):
|
||||
@@ -2100,3 +2110,26 @@ def test_dao_update_notifications_by_reference_returns_zero_when_no_notification
|
||||
"billable_units": 2}
|
||||
)
|
||||
assert updated_count == 0
|
||||
|
||||
|
||||
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
|
||||
|
||||
@@ -35,7 +35,6 @@ from app.dao.services_dao import (
|
||||
from app.dao.service_permissions_dao import dao_add_service_permission, dao_remove_service_permission
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.models import (
|
||||
NotificationStatistics,
|
||||
ProviderStatistics,
|
||||
VerifyCode,
|
||||
ApiKey,
|
||||
@@ -447,7 +446,6 @@ def test_delete_service_and_associated_objects(notify_db,
|
||||
assert ServicePermission.query.count() == 3
|
||||
|
||||
delete_service_and_all_associated_db_objects(sample_service)
|
||||
assert NotificationStatistics.query.count() == 0
|
||||
assert ProviderStatistics.query.count() == 0
|
||||
assert VerifyCode.query.count() == 0
|
||||
assert ApiKey.query.count() == 0
|
||||
|
||||
Reference in New Issue
Block a user