mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-26 01:03:41 -04:00
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
This commit is contained in:
@@ -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(
|
@pytest.mark.parametrize(
|
||||||
'include_from_test_key, expected_count_of_notifications',
|
'include_from_test_key, expected_count_of_notifications',
|
||||||
[
|
[
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import json
|
|||||||
import pytest
|
import pytest
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
|
||||||
|
from app.dao.templates_dao import dao_update_template
|
||||||
|
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
from tests.app.conftest import (
|
from tests.app.conftest import (
|
||||||
sample_template as create_sample_template,
|
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))
|
json_resp = json.loads(response_for_a_week.get_data(as_text=True))
|
||||||
assert len(json_resp['data']) == 2
|
assert len(json_resp['data']) == 2
|
||||||
assert json_resp['data'][0]['count'] == 3
|
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]['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))
|
mocked_redis_get.assert_called_once_with("{}-template-counter-limit-7-days".format(email.service_id))
|
||||||
if cache_values:
|
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))
|
json_resp = json.loads(response_for_a_month.get_data(as_text=True))
|
||||||
assert len(json_resp['data']) == 2
|
assert len(json_resp['data']) == 2
|
||||||
assert json_resp['data'][0]['count'] == 3
|
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]['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()
|
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))
|
json_resp = json.loads(response_for_all.get_data(as_text=True))
|
||||||
assert len(json_resp['data']) == 2
|
assert len(json_resp['data']) == 2
|
||||||
assert json_resp['data'][0]['count'] == 3
|
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]['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()
|
mock_redis.assert_not_called()
|
||||||
|
|
||||||
@@ -172,12 +174,20 @@ def set_up_notifications(notify_db, notify_db_session):
|
|||||||
today = datetime.now()
|
today = datetime.now()
|
||||||
a_week_ago = datetime.now() - timedelta(days=7)
|
a_week_ago = datetime.now() - timedelta(days=7)
|
||||||
a_month_ago = datetime.now() - timedelta(days=30)
|
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=sms)
|
||||||
sample_notification(notify_db, notify_db_session, created_at=a_month_ago, template=email)
|
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
|
return email, sms
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
from app import encryption
|
from app import encryption
|
||||||
from app.models import (
|
from app.models import (
|
||||||
@@ -239,6 +240,24 @@ def test_letter_notification_serializes_with_subject(client, sample_letter_templ
|
|||||||
assert res['subject'] == 'Template subject'
|
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):
|
def test_inbound_number_serializes_with_service(client, notify_db_session):
|
||||||
service = create_service()
|
service = create_service()
|
||||||
inbound_number = create_inbound_number(number='1', service_id=service.id)
|
inbound_number = create_inbound_number(number='1', service_id=service.id)
|
||||||
|
|||||||
Reference in New Issue
Block a user