diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 36901aecd..8cad6e116 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -130,9 +130,17 @@ def dao_create_notification(notification): if not notification.status: notification.status = 'created' - notification_history = NotificationHistory.from_notification(notification) db.session.add(notification) - db.session.add(notification_history) + if _should_record_notification_in_history_table(notification): + db.session.add(NotificationHistory.from_notification(notification)) + + +def _should_record_notification_in_history_table(notification): + if notification.api_key_id and notification.key_type == KEY_TYPE_TEST: + return False + if notification.service.research_mode: + return False + return True def _decide_permanent_temporary_failure(current_status, status): @@ -192,6 +200,7 @@ def dao_update_notification(notification): notification_history = NotificationHistory.query.get(notification.id) notification_history.update_from_notification(notification) db.session.add(notification) + db.session.add(notification_history) db.session.commit() diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 674b99e31..99d0b2aba 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -133,7 +133,8 @@ def sample_service(notify_db, active=True, restricted=False, limit=1000, - email_from=None): + email_from=None, + research_mode=False): if user is None: user = sample_user(notify_db, notify_db_session) if email_from is None: @@ -143,6 +144,7 @@ def sample_service(notify_db, 'message_limit': limit, 'active': active, 'restricted': restricted, + 'research_mode': research_mode, 'email_from': email_from, 'created_by': user } diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 8e60f58a3..d86f21bb8 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -148,11 +148,11 @@ def test_should_by_able_to_get_template_count_from_notifications_history(notify_ def test_template_history_should_ignore_test_keys( - notify_db, - notify_db_session, - sample_team_api_key, - sample_test_api_key, - sample_api_key + notify_db, + notify_db_session, + sample_team_api_key, + sample_test_api_key, + sample_api_key ): sms = sample_template(notify_db, notify_db_session) @@ -518,19 +518,43 @@ def test_save_notification(sample_email_template, sample_job, ses_provider): assert Notification.query.count() == 2 -def test_save_notification(sample_template, sample_job, mmg_provider): +def test_save_notification_creates_history(sample_email_template, sample_job): assert Notification.query.count() == 0 - data = _notification_json(sample_template, sample_job.id) + data = _notification_json(sample_email_template, job_id=sample_job.id) notification_1 = Notification(**data) - notification_2 = Notification(**data) dao_create_notification(notification_1) assert Notification.query.count() == 1 + assert NotificationHistory.query.count() == 1 - dao_create_notification(notification_2) - assert Notification.query.count() == 2 +def test_save_notification_with_test_api_key_does_not_create_history(sample_email_template, sample_api_key): + assert Notification.query.count() == 0 + data = _notification_json(sample_email_template) + data['key_type'] = KEY_TYPE_TEST + data['api_key_id'] = sample_api_key.id + + notification_1 = Notification(**data) + dao_create_notification(notification_1) + + assert Notification.query.count() == 1 + assert NotificationHistory.query.count() == 0 + + +def test_save_notification_with_research_mode_service_does_not_create_history( + notify_db, + notify_db_session): + service = sample_service(notify_db, notify_db_session, research_mode=True) + template = sample_template(notify_db, notify_db_session, service=service) + assert Notification.query.count() == 0 + data = _notification_json(template) + + notification_1 = Notification(**data) + dao_create_notification(notification_1) + + assert Notification.query.count() == 1 + assert NotificationHistory.query.count() == 0 def test_not_save_notification_and_not_create_stats_on_commit_error(sample_template, sample_job, mmg_provider): @@ -699,16 +723,15 @@ def test_get_all_notifications_for_job_by_status(notify_db, notify_db_session, s def test_get_notification_billable_unit_count_per_month(notify_db, notify_db_session, sample_service): - for year, month, day in ( - (2017, 1, 15), # ↓ 2016 financial year - (2016, 8, 1), - (2016, 7, 15), - (2016, 4, 15), - (2016, 4, 15), - (2016, 4, 1), # ↓ 2015 financial year - (2016, 3, 31), - (2016, 1, 15) + (2017, 1, 15), # ↓ 2016 financial year + (2016, 8, 1), + (2016, 7, 15), + (2016, 4, 15), + (2016, 4, 15), + (2016, 4, 1), # ↓ 2015 financial year + (2016, 3, 31), + (2016, 1, 15) ): sample_notification( notify_db, notify_db_session, service=sample_service, @@ -718,22 +741,22 @@ def test_get_notification_billable_unit_count_per_month(notify_db, notify_db_ses ) for financial_year, months in ( - ( - 2017, - [] - ), - ( - 2016, - [('April', 2), ('July', 2), ('January', 1)] - ), - ( - 2015, - [('January', 1), ('March', 2)] - ), - ( - 2014, - [] - ) + ( + 2017, + [] + ), + ( + 2016, + [('April', 2), ('July', 2), ('January', 1)] + ), + ( + 2015, + [('January', 1), ('March', 2)] + ), + ( + 2014, + [] + ) ): assert get_notification_billable_unit_count_per_month( sample_service.id, financial_year @@ -841,10 +864,12 @@ def test_updating_notification_updates_notification_history(sample_notification) sample_notification.status = 'sending' dao_update_notification(sample_notification) - - hist = NotificationHistory.query.one() - assert hist.id == sample_notification.id - assert hist.status == 'sending' + notification = Notification.query.one() + hist1 = NotificationHistory.query.one() + assert notification.id == sample_notification.id + assert notification.status == "sending" + assert hist1.id == sample_notification.id + assert hist1.status == 'sending' def test_should_delete_notification_and_notification_history_for_id(notify_db, notify_db_session, sample_template): @@ -1019,7 +1044,6 @@ def test_get_notifications_created_by_api_or_csv_are_returned_correctly_excludin sample_team_api_key, sample_test_api_key ): - sample_notification( notify_db, notify_db_session, created_at=datetime.utcnow(), job=sample_job )