From 0825177f2d3c911cc6e94d2877203fa5e348a353 Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Fri, 10 Nov 2017 11:33:42 +0000 Subject: [PATCH] Add tests for notification.template returning the right version Adds test for: * checking the template version foreign key constraint * checking that template changes don't affect existing notifications * notification statistics aren't affected by different template versions * notification stats always return the current template name --- tests/app/service/test_rest.py | 18 +++++++++++++ tests/app/template_statistics/test_rest.py | 30 ++++++++++++++-------- tests/app/test_model.py | 19 ++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 59a55da66..bd365b739 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -1451,6 +1451,24 @@ def test_get_notification_for_service_includes_created_by(admin_request, sample_ } +def test_get_notification_for_service_returns_old_template_version(admin_request, sample_template): + sample_notification = create_notification(sample_template) + sample_notification.reference = 'modified-inplace' + sample_template.version = 2 + sample_template.content = 'New template content' + + resp = admin_request.get( + 'service.get_notification_for_service', + service_id=sample_notification.service_id, + notification_id=sample_notification.id + ) + + assert resp['reference'] == 'modified-inplace' + assert resp['template']['version'] == 1 + assert resp['template']['content'] == sample_notification.template.content + assert resp['template']['content'] != sample_template.content + + @pytest.mark.parametrize( 'include_from_test_key, expected_count_of_notifications', [ diff --git a/tests/app/template_statistics/test_rest.py b/tests/app/template_statistics/test_rest.py index f71bea3af..3aa6f9463 100644 --- a/tests/app/template_statistics/test_rest.py +++ b/tests/app/template_statistics/test_rest.py @@ -4,6 +4,8 @@ import json import pytest from freezegun import freeze_time +from app.dao.templates_dao import dao_update_template + from tests import create_authorization_header from tests.app.conftest import ( sample_template as create_sample_template, @@ -108,9 +110,9 @@ def test_get_template_statistics_for_service_limit_7_days(notify_db, notify_db_s json_resp = json.loads(response_for_a_week.get_data(as_text=True)) assert len(json_resp['data']) == 2 assert json_resp['data'][0]['count'] == 3 - assert json_resp['data'][0]['template_name'] == 'Email Template Name' + assert json_resp['data'][0]['template_name'] == 'New Email Template Name' assert json_resp['data'][1]['count'] == 3 - assert json_resp['data'][1]['template_name'] == 'Template Name' + assert json_resp['data'][1]['template_name'] == 'New SMS Template Name' mocked_redis_get.assert_called_once_with("{}-template-counter-limit-7-days".format(email.service_id)) if cache_values: @@ -138,9 +140,9 @@ def test_get_template_statistics_for_service_limit_30_days(notify_db, notify_db_ json_resp = json.loads(response_for_a_month.get_data(as_text=True)) assert len(json_resp['data']) == 2 assert json_resp['data'][0]['count'] == 3 - assert json_resp['data'][0]['template_name'] == 'Email Template Name' + assert json_resp['data'][0]['template_name'] == 'New Email Template Name' assert json_resp['data'][1]['count'] == 3 - assert json_resp['data'][1]['template_name'] == 'Template Name' + assert json_resp['data'][1]['template_name'] == 'New SMS Template Name' mock_redis.assert_not_called() @@ -159,9 +161,9 @@ def test_get_template_statistics_for_service_no_limit(notify_db, notify_db_sessi json_resp = json.loads(response_for_all.get_data(as_text=True)) assert len(json_resp['data']) == 2 assert json_resp['data'][0]['count'] == 3 - assert json_resp['data'][0]['template_name'] == 'Email Template Name' + assert json_resp['data'][0]['template_name'] == 'New Email Template Name' assert json_resp['data'][1]['count'] == 3 - assert json_resp['data'][1]['template_name'] == 'Template Name' + assert json_resp['data'][1]['template_name'] == 'New SMS Template Name' mock_redis.assert_not_called() @@ -172,12 +174,20 @@ def set_up_notifications(notify_db, notify_db_session): today = datetime.now() a_week_ago = datetime.now() - timedelta(days=7) a_month_ago = datetime.now() - timedelta(days=30) - sample_notification(notify_db, notify_db_session, created_at=today, template=sms) - sample_notification(notify_db, notify_db_session, created_at=today, template=email) - sample_notification(notify_db, notify_db_session, created_at=a_week_ago, template=sms) - sample_notification(notify_db, notify_db_session, created_at=a_week_ago, template=email) sample_notification(notify_db, notify_db_session, created_at=a_month_ago, template=sms) sample_notification(notify_db, notify_db_session, created_at=a_month_ago, template=email) + email.name = 'Updated Email Template Name' + dao_update_template(email) + sms.name = 'Updated SMS Template Name' + dao_update_template(sms) + sample_notification(notify_db, notify_db_session, created_at=a_week_ago, template=sms) + sample_notification(notify_db, notify_db_session, created_at=a_week_ago, template=email) + email.name = 'New Email Template Name' + dao_update_template(email) + sms.name = 'New SMS Template Name' + dao_update_template(sms) + sample_notification(notify_db, notify_db_session, created_at=today, template=sms) + sample_notification(notify_db, notify_db_session, created_at=today, template=email) return email, sms diff --git a/tests/app/test_model.py b/tests/app/test_model.py index 09f1325f8..fe30b3006 100644 --- a/tests/app/test_model.py +++ b/tests/app/test_model.py @@ -1,6 +1,7 @@ import pytest from freezegun import freeze_time +from sqlalchemy.exc import IntegrityError from app import encryption from app.models import ( @@ -239,6 +240,24 @@ def test_letter_notification_serializes_with_subject(client, sample_letter_templ assert res['subject'] == 'Template subject' +def test_notification_references_template_history(client, sample_template): + noti = create_notification(sample_template) + sample_template.version = 3 + sample_template.content = 'New template content' + + res = noti.serialize() + assert res['template']['version'] == 1 + + assert res['body'] == noti.template.content + assert noti.template.content != sample_template.content + + +def test_notification_requires_a_valid_template_version(client, sample_template): + sample_template.version = 2 + with pytest.raises(IntegrityError): + create_notification(sample_template) + + def test_inbound_number_serializes_with_service(client, notify_db_session): service = create_service() inbound_number = create_inbound_number(number='1', service_id=service.id)