mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
Merge branch 'master' into noti-stats-cleanup
This commit is contained in:
@@ -16,7 +16,16 @@ from app.clients.sms import SmsClientException
|
||||
from app.dao import notifications_dao, provider_details_dao
|
||||
from app.dao import provider_statistics_dao
|
||||
from app.dao.provider_statistics_dao import get_provider_statistics
|
||||
from app.models import Notification, NotificationStatistics, Job, KEY_TYPE_NORMAL, KEY_TYPE_TEST
|
||||
from app.models import (
|
||||
Notification,
|
||||
NotificationStatistics,
|
||||
Job,
|
||||
Organisation,
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEST,
|
||||
BRANDING_ORG,
|
||||
BRANDING_BOTH
|
||||
)
|
||||
from tests.app.conftest import sample_notification
|
||||
|
||||
|
||||
@@ -188,7 +197,7 @@ def test_send_sms_should_use_template_version_from_notification_not_latest(
|
||||
sender=None
|
||||
)
|
||||
|
||||
persisted_notification = notifications_dao.get_notification(sample_template.service_id, db_notification.id)
|
||||
persisted_notification = notifications_dao.get_notification_by_id(db_notification.id)
|
||||
assert persisted_notification.to == db_notification.to
|
||||
assert persisted_notification.template_id == sample_template.id
|
||||
assert persisted_notification.template_version == version_on_notification
|
||||
@@ -223,7 +232,7 @@ def test_should_call_send_sms_response_task_if_research_mode(notify_db, sample_s
|
||||
('mmg', str(sample_notification.id), sample_notification.to), queue='research-mode'
|
||||
)
|
||||
|
||||
persisted_notification = notifications_dao.get_notification(sample_service.id, sample_notification.id)
|
||||
persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id)
|
||||
assert persisted_notification.to == sample_notification.to
|
||||
assert persisted_notification.template_id == sample_notification.template_id
|
||||
assert persisted_notification.status == 'sending'
|
||||
@@ -499,10 +508,48 @@ def test_should_not_set_billable_units_if_research_mode(notify_db, sample_servic
|
||||
sample_notification.id
|
||||
)
|
||||
|
||||
persisted_notification = notifications_dao.get_notification(sample_service.id, sample_notification.id)
|
||||
persisted_notification = notifications_dao.get_notification_by_id(sample_notification.id)
|
||||
assert persisted_notification.billable_units == 0
|
||||
|
||||
|
||||
def test_get_html_email_renderer_should_return_for_normal_service(sample_service):
|
||||
renderer = provider_tasks.get_html_email_renderer(sample_service)
|
||||
assert renderer.govuk_banner
|
||||
assert renderer.brand_colour is None
|
||||
assert renderer.brand_logo is None
|
||||
assert renderer.brand_name is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('branding_type, govuk_banner', [
|
||||
(BRANDING_ORG, False),
|
||||
(BRANDING_BOTH, True)
|
||||
])
|
||||
def test_get_html_email_renderer_with_branding_details(branding_type, govuk_banner, notify_db, sample_service):
|
||||
sample_service.branding = branding_type
|
||||
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
|
||||
sample_service.organisation = org
|
||||
notify_db.session.add_all([sample_service, org])
|
||||
notify_db.session.commit()
|
||||
|
||||
renderer = provider_tasks.get_html_email_renderer(sample_service)
|
||||
|
||||
assert renderer.govuk_banner == govuk_banner
|
||||
assert renderer.brand_colour == '000000'
|
||||
assert renderer.brand_name == 'Justice League'
|
||||
|
||||
|
||||
def test_get_html_email_renderer_prepends_logo_path(notify_db, sample_service):
|
||||
sample_service.branding = BRANDING_ORG
|
||||
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
|
||||
sample_service.organisation = org
|
||||
notify_db.session.add_all([sample_service, org])
|
||||
notify_db.session.commit()
|
||||
|
||||
renderer = provider_tasks.get_html_email_renderer(sample_service)
|
||||
|
||||
assert renderer.brand_logo == 'http://localhost:6012/static/images/email-template/crests/justice-league.png'
|
||||
|
||||
|
||||
def _get_provider_statistics(service, **kwargs):
|
||||
query = ProviderStatistics.query.filter_by(service=service)
|
||||
if 'providers' in kwargs:
|
||||
|
||||
@@ -23,7 +23,7 @@ from app.models import (
|
||||
from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
dao_update_notification,
|
||||
get_notification,
|
||||
get_notification_with_personalisation,
|
||||
get_notification_for_job,
|
||||
get_notifications_for_job,
|
||||
delete_notifications_created_more_than_a_week_ago,
|
||||
@@ -34,14 +34,18 @@ from app.dao.notifications_dao import (
|
||||
dao_get_template_statistics_for_service,
|
||||
get_notifications_for_service,
|
||||
dao_get_potential_notification_statistics_for_day,
|
||||
dao_get_template_statistics_for_template, get_notification_by_id)
|
||||
dao_get_template_statistics_for_template,
|
||||
get_notification_by_id,
|
||||
dao_get_template_usage
|
||||
)
|
||||
|
||||
from notifications_utils.template import get_sms_fragment_count
|
||||
|
||||
from tests.app.conftest import (sample_notification)
|
||||
from tests.app.conftest import (sample_notification, sample_template, sample_email_template, sample_service)
|
||||
|
||||
|
||||
def test_should_have_decorated_notifications_dao_functions():
|
||||
assert dao_get_template_usage.__wrapped__.__name__ == 'dao_get_template_usage' # noqa
|
||||
assert dao_get_notification_statistics_for_service_and_day.__wrapped__.__name__ == 'dao_get_notification_statistics_for_service_and_day' # noqa
|
||||
assert dao_get_potential_notification_statistics_for_day.__wrapped__.__name__ == 'dao_get_potential_notification_statistics_for_day' # noqa
|
||||
assert dao_get_template_statistics_for_service.__wrapped__.__name__ == 'dao_get_template_statistics_for_service' # noqa
|
||||
@@ -53,12 +57,143 @@ def test_should_have_decorated_notifications_dao_functions():
|
||||
assert update_notification_status_by_reference.__wrapped__.__name__ == 'update_notification_status_by_reference' # noqa
|
||||
assert get_notification_for_job.__wrapped__.__name__ == 'get_notification_for_job' # noqa
|
||||
assert get_notifications_for_job.__wrapped__.__name__ == 'get_notifications_for_job' # noqa
|
||||
assert get_notification.__wrapped__.__name__ == 'get_notification' # noqa
|
||||
assert get_notification_with_personalisation.__wrapped__.__name__ == 'get_notification_with_personalisation' # noqa
|
||||
assert get_notifications_for_service.__wrapped__.__name__ == 'get_notifications_for_service' # noqa
|
||||
assert get_notification_by_id.__wrapped__.__name__ == 'get_notification_by_id' # noqa
|
||||
assert delete_notifications_created_more_than_a_week_ago.__wrapped__.__name__ == 'delete_notifications_created_more_than_a_week_ago' # noqa
|
||||
|
||||
|
||||
def test_should_by_able_to_get_template_count_from_notifications_history(notify_db, notify_db_session, sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
email = sample_email_template(notify_db, notify_db_session)
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, service=sample_service, template=email)
|
||||
|
||||
results = dao_get_template_usage(sample_service.id)
|
||||
assert results[0].name == 'Email Template Name'
|
||||
assert results[0].template_type == 'email'
|
||||
assert results[0].count == 2
|
||||
|
||||
assert results[1].name == 'Template Name'
|
||||
assert results[1].template_type == 'sms'
|
||||
assert results[1].count == 3
|
||||
|
||||
|
||||
def test_should_by_able_to_get_template_count_from_notifications_history_for_service(
|
||||
notify_db,
|
||||
notify_db_session):
|
||||
service_1 = sample_service(notify_db, notify_db_session, service_name="test1", email_from="test1")
|
||||
service_2 = sample_service(notify_db, notify_db_session, service_name="test2", email_from="test2")
|
||||
service_3 = sample_service(notify_db, notify_db_session, service_name="test3", email_from="test3")
|
||||
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, service=service_1, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, service=service_1, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, service=service_2, template=sms)
|
||||
|
||||
assert dao_get_template_usage(service_1.id)[0].count == 2
|
||||
assert dao_get_template_usage(service_2.id)[0].count == 1
|
||||
assert len(dao_get_template_usage(service_3.id)) == 0
|
||||
|
||||
|
||||
def test_should_by_able_to_get_zero_count_from_notifications_history_if_no_rows(sample_service):
|
||||
results = dao_get_template_usage(sample_service.id)
|
||||
assert len(results) == 0
|
||||
|
||||
|
||||
def test_should_by_able_to_get_zero_count_from_notifications_history_if_no_service():
|
||||
results = dao_get_template_usage(str(uuid.uuid4()))
|
||||
assert len(results) == 0
|
||||
|
||||
|
||||
def test_should_by_able_to_get_template_count_from_notifications_history_across_days(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
email = sample_email_template(notify_db, notify_db_session)
|
||||
|
||||
today = datetime.now()
|
||||
yesterday = datetime.now() - timedelta(days=1)
|
||||
one_month_ago = datetime.now() - timedelta(days=30)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=sms)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
|
||||
|
||||
results = dao_get_template_usage(sample_service.id)
|
||||
|
||||
assert len(results) == 2
|
||||
|
||||
assert [(row.name, row.template_type, row.count) for row in results] == [
|
||||
('Email Template Name', 'email', 5),
|
||||
('Template Name', 'sms', 5)
|
||||
]
|
||||
|
||||
|
||||
def test_should_by_able_to_get_template_count_from_notifications_history_with_day_limit(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session)
|
||||
|
||||
email = sample_email_template(notify_db, notify_db_session)
|
||||
|
||||
today = datetime.now()
|
||||
yesterday = datetime.now() - timedelta(days=1)
|
||||
one_month_ago = datetime.now() - timedelta(days=30)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=yesterday, service=sample_service, template=sms)
|
||||
|
||||
sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=one_month_ago, service=sample_service, template=sms)
|
||||
|
||||
results_day_one = dao_get_template_usage(sample_service.id, limit_days=0)
|
||||
assert len(results_day_one) == 2
|
||||
|
||||
results_day_two = dao_get_template_usage(sample_service.id, limit_days=1)
|
||||
assert len(results_day_two) == 2
|
||||
|
||||
results_day_30 = dao_get_template_usage(sample_service.id, limit_days=31)
|
||||
assert len(results_day_30) == 2
|
||||
|
||||
assert [(row.name, row.template_type, row.count) for row in results_day_one] == [
|
||||
('Email Template Name', 'email', 2),
|
||||
('Template Name', 'sms', 1)
|
||||
]
|
||||
|
||||
assert [(row.name, row.template_type, row.count) for row in results_day_two] == [
|
||||
('Email Template Name', 'email', 5),
|
||||
('Template Name', 'sms', 2),
|
||||
]
|
||||
|
||||
assert [(row.name, row.template_type, row.count) for row in results_day_30] == [
|
||||
('Email Template Name', 'email', 5),
|
||||
('Template Name', 'sms', 5),
|
||||
]
|
||||
|
||||
|
||||
def test_should_by_able_to_update_status_by_reference(sample_email_template, ses_provider):
|
||||
data = _notification_json(sample_email_template, status='sending')
|
||||
|
||||
@@ -572,9 +707,11 @@ def test_save_notification_with_no_job(sample_template, mmg_provider):
|
||||
|
||||
|
||||
def test_get_notification(sample_notification):
|
||||
notification_from_db = get_notification(
|
||||
notification_from_db = get_notification_with_personalisation(
|
||||
sample_notification.service.id,
|
||||
sample_notification.id)
|
||||
sample_notification.id,
|
||||
key_type=None
|
||||
)
|
||||
assert sample_notification == notification_from_db
|
||||
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ from app.models import (
|
||||
VerifyCode,
|
||||
ApiKey,
|
||||
Template,
|
||||
TemplateHistory,
|
||||
Job,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
@@ -331,7 +332,7 @@ def test_delete_service_and_associated_objects(notify_db,
|
||||
assert ApiKey.query.count() == 0
|
||||
assert ApiKey.get_history_model().query.count() == 0
|
||||
assert Template.query.count() == 0
|
||||
assert Template.get_history_model().query.count() == 0
|
||||
assert TemplateHistory.query.count() == 0
|
||||
assert Job.query.count() == 0
|
||||
assert Notification.query.count() == 0
|
||||
assert Permission.query.count() == 0
|
||||
|
||||
@@ -6,7 +6,7 @@ from app.dao.templates_dao import (
|
||||
dao_update_template,
|
||||
dao_get_template_versions)
|
||||
from tests.app.conftest import sample_template as create_sample_template
|
||||
from app.models import Template
|
||||
from app.models import Template, TemplateHistory
|
||||
import pytest
|
||||
|
||||
|
||||
@@ -185,7 +185,7 @@ def test_get_template_by_id_and_service_returns_none_if_no_template(sample_servi
|
||||
|
||||
def test_create_template_creates_a_history_record_with_current_data(sample_service, sample_user):
|
||||
assert Template.query.count() == 0
|
||||
assert Template.get_history_model().query.count() == 0
|
||||
assert TemplateHistory.query.count() == 0
|
||||
data = {
|
||||
'name': 'Sample Template',
|
||||
'template_type': "email",
|
||||
@@ -200,7 +200,7 @@ def test_create_template_creates_a_history_record_with_current_data(sample_servi
|
||||
assert Template.query.count() == 1
|
||||
|
||||
template_from_db = Template.query.first()
|
||||
template_history = Template.get_history_model().query.first()
|
||||
template_history = TemplateHistory.query.first()
|
||||
|
||||
assert template_from_db.id == template_history.id
|
||||
assert template_from_db.name == template_history.name
|
||||
@@ -212,7 +212,7 @@ def test_create_template_creates_a_history_record_with_current_data(sample_servi
|
||||
|
||||
def test_update_template_creates_a_history_record_with_current_data(sample_service, sample_user):
|
||||
assert Template.query.count() == 0
|
||||
assert Template.get_history_model().query.count() == 0
|
||||
assert TemplateHistory.query.count() == 0
|
||||
data = {
|
||||
'name': 'Sample Template',
|
||||
'template_type': "email",
|
||||
@@ -228,20 +228,20 @@ def test_update_template_creates_a_history_record_with_current_data(sample_servi
|
||||
assert created.name == 'Sample Template'
|
||||
assert Template.query.count() == 1
|
||||
assert Template.query.first().version == 1
|
||||
assert Template.get_history_model().query.count() == 1
|
||||
assert TemplateHistory.query.count() == 1
|
||||
|
||||
created.name = 'new name'
|
||||
dao_update_template(created)
|
||||
|
||||
assert Template.query.count() == 1
|
||||
assert Template.get_history_model().query.count() == 2
|
||||
assert TemplateHistory.query.count() == 2
|
||||
|
||||
template_from_db = Template.query.first()
|
||||
|
||||
assert template_from_db.version == 2
|
||||
|
||||
assert Template.get_history_model().query.filter_by(name='Sample Template').one().version == 1
|
||||
assert Template.get_history_model().query.filter_by(name='new name').one().version == 2
|
||||
assert TemplateHistory.query.filter_by(name='Sample Template').one().version == 1
|
||||
assert TemplateHistory.query.filter_by(name='new name').one().version == 2
|
||||
|
||||
|
||||
def test_get_template_history_version(sample_user, sample_service, sample_template):
|
||||
@@ -261,12 +261,16 @@ def test_get_template_versions(sample_template):
|
||||
sample_template.content = 'new version'
|
||||
dao_update_template(sample_template)
|
||||
versions = dao_get_template_versions(service_id=sample_template.service_id, template_id=sample_template.id)
|
||||
assert versions.__len__() == 2
|
||||
for x in versions:
|
||||
if x.version == 2:
|
||||
assert x.content == 'new version'
|
||||
else:
|
||||
assert x.content == original_content
|
||||
assert len(versions) == 2
|
||||
versions = sorted(versions, key=lambda x: x.version)
|
||||
assert versions[0].content == original_content
|
||||
assert versions[1].content == 'new version'
|
||||
|
||||
assert versions[0].created_at == versions[1].created_at
|
||||
|
||||
assert versions[0].updated_at is None
|
||||
assert versions[1].updated_at is not None
|
||||
|
||||
from app.schemas import template_history_schema
|
||||
v = template_history_schema.load(versions, many=True)
|
||||
assert v.__len__() == 2
|
||||
assert len(v) == 2
|
||||
|
||||
@@ -8,6 +8,7 @@ from freezegun import freeze_time
|
||||
|
||||
from app.dao.notifications_dao import dao_update_notification
|
||||
from app.dao.api_key_dao import save_model_api_key
|
||||
from app.dao.templates_dao import dao_update_template
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_notification as create_sample_notification
|
||||
@@ -29,7 +30,9 @@ def test_get_sms_notification_by_id(notify_api, sample_notification):
|
||||
assert notification['template'] == {
|
||||
'id': str(sample_notification.template.id),
|
||||
'name': sample_notification.template.name,
|
||||
'template_type': sample_notification.template.template_type}
|
||||
'template_type': sample_notification.template.template_type,
|
||||
'version': 1
|
||||
}
|
||||
assert notification['job'] == {
|
||||
'id': str(sample_notification.job.id),
|
||||
'original_file_name': sample_notification.job.original_file_name
|
||||
@@ -62,7 +65,9 @@ def test_get_email_notification_by_id(notify_api, notify_db, notify_db_session,
|
||||
assert notification['template'] == {
|
||||
'id': str(email_notification.template.id),
|
||||
'name': email_notification.template.name,
|
||||
'template_type': email_notification.template.template_type}
|
||||
'template_type': email_notification.template.template_type,
|
||||
'version': 1
|
||||
}
|
||||
assert notification['job'] == {
|
||||
'id': str(email_notification.job.id),
|
||||
'original_file_name': email_notification.job.original_file_name
|
||||
@@ -166,7 +171,9 @@ def test_get_all_notifications(notify_api, sample_notification):
|
||||
assert notifications['notifications'][0]['template'] == {
|
||||
'id': str(sample_notification.template.id),
|
||||
'name': sample_notification.template.name,
|
||||
'template_type': sample_notification.template.template_type}
|
||||
'template_type': sample_notification.template.template_type,
|
||||
'version': 1
|
||||
}
|
||||
assert notifications['notifications'][0]['job'] == {
|
||||
'id': str(sample_notification.job.id),
|
||||
'original_file_name': sample_notification.job.original_file_name
|
||||
@@ -656,7 +663,6 @@ def test_get_notification_public_api_format_is_not_changed(notify_api, sample_no
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.xfail(strict=True, raises=NeededByTemplateError)
|
||||
def test_get_notification_selects_correct_template_for_personalisation(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
@@ -666,8 +672,9 @@ def test_get_notification_selects_correct_template_for_personalisation(notify_ap
|
||||
notify_db_session,
|
||||
service=sample_template.service,
|
||||
template=sample_template)
|
||||
|
||||
original_content = sample_template.content
|
||||
sample_template.content = '((name))'
|
||||
dao_update_template(sample_template)
|
||||
notify_db.session.commit()
|
||||
|
||||
create_sample_notification(notify_db,
|
||||
@@ -680,14 +687,19 @@ def test_get_notification_selects_correct_template_for_personalisation(notify_ap
|
||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
||||
|
||||
response = client.get(path='/notifications', headers=[auth_header])
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(resp['notifications']) == 2
|
||||
assert resp['notifications'][0]['template_version'] == 1
|
||||
assert resp['notifications'][0]['body'] == 'This is a template'
|
||||
assert resp['notifications'][1]['template_version'] == 2
|
||||
assert resp['notifications'][1]['body'] == 'foo'
|
||||
notis = sorted(resp['notifications'], key=lambda x: x['template_version'])
|
||||
assert len(notis) == 2
|
||||
assert notis[0]['template_version'] == 1
|
||||
assert notis[0]['body'] == original_content
|
||||
assert notis[1]['template_version'] == 2
|
||||
assert notis[1]['body'] == 'foo'
|
||||
|
||||
assert notis[0]['template_version'] == notis[0]['template']['version']
|
||||
assert notis[1]['template_version'] == notis[1]['template']['version']
|
||||
|
||||
|
||||
def _create_auth_header_from_key(api_key):
|
||||
|
||||
@@ -7,7 +7,7 @@ from freezegun import freeze_time
|
||||
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.dao.services_dao import dao_remove_user_from_service
|
||||
from app.models import User
|
||||
from app.models import User, Organisation
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import (
|
||||
sample_service as create_sample_service,
|
||||
@@ -108,6 +108,8 @@ def test_get_service_by_id(notify_api, sample_service):
|
||||
assert json_resp['data']['name'] == sample_service.name
|
||||
assert json_resp['data']['id'] == str(sample_service.id)
|
||||
assert not json_resp['data']['research_mode']
|
||||
assert json_resp['data']['organisation'] is None
|
||||
assert json_resp['data']['branding'] == 'govuk'
|
||||
|
||||
|
||||
def test_get_service_by_id_should_404_if_no_service(notify_api, notify_db):
|
||||
@@ -339,7 +341,11 @@ def test_create_service_should_throw_duplicate_key_constraint_for_existing_email
|
||||
assert "Duplicate service name '{}'".format(service_name) in json_resp['message']['name']
|
||||
|
||||
|
||||
def test_update_service(notify_api, sample_service):
|
||||
def test_update_service(notify_api, notify_db, sample_service):
|
||||
org = Organisation(colour='#000000', logo='justice-league.png', name='Justice League')
|
||||
notify_db.session.add(org)
|
||||
notify_db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
auth_header = create_authorization_header()
|
||||
@@ -354,7 +360,8 @@ def test_update_service(notify_api, sample_service):
|
||||
data = {
|
||||
'name': 'updated service name',
|
||||
'email_from': 'updated.service.name',
|
||||
'created_by': str(sample_service.created_by.id)
|
||||
'created_by': str(sample_service.created_by.id),
|
||||
'organisation': str(org.id)
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
@@ -368,6 +375,7 @@ def test_update_service(notify_api, sample_service):
|
||||
assert resp.status_code == 200
|
||||
assert result['data']['name'] == 'updated service name'
|
||||
assert result['data']['email_from'] == 'updated.service.name'
|
||||
assert result['data']['organisation'] == str(org.id)
|
||||
|
||||
|
||||
def test_update_service_research_mode(notify_api, sample_service):
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
|
||||
from freezegun import freeze_time
|
||||
@@ -6,124 +6,18 @@ from freezegun import freeze_time
|
||||
from app import db
|
||||
from app.models import TemplateStatistics
|
||||
from tests import create_authorization_header
|
||||
from tests.app.conftest import sample_template as create_sample_template
|
||||
from tests.app.conftest import sample_template as create_sample_template, sample_template, sample_notification, \
|
||||
sample_email_template
|
||||
|
||||
|
||||
@freeze_time('2016-04-09')
|
||||
def test_get_template_statistics_for_service_for_last_week(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
def test_get_all_template_statistics_with_bad_arg_returns_400(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 7}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 8
|
||||
assert json_resp['data'][0]['day'] == '2016-04-09'
|
||||
assert json_resp['data'][6]['day'] == '2016-04-03'
|
||||
|
||||
|
||||
@freeze_time('2016-04-30')
|
||||
def test_get_template_statistics_for_service_for_last_week_with_no_data(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
# Date is frozen at 2016-04-30 and no data written since
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 7}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 0
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 30}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 9
|
||||
|
||||
|
||||
def test_get_all_template_statistics_for_service(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 9
|
||||
assert json_resp['data'][0]['day'] == '2016-04-09'
|
||||
assert json_resp['data'][8]['day'] == '2016-04-01'
|
||||
|
||||
|
||||
def test_get_all_template_statistics_with_bad_limit_arg_returns_400(notify_api, sample_template):
|
||||
|
||||
# make 9 stats records from 1st to 9th April
|
||||
for i in range(1, 10):
|
||||
past_date = '2016-04-0{}'.format(i)
|
||||
with freeze_time(past_date):
|
||||
template_stats = TemplateStatistics(template_id=sample_template.id,
|
||||
service_id=sample_template.service_id)
|
||||
db.session.add(template_stats)
|
||||
db.session.commit()
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_template.service_id),
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 'blurk'}
|
||||
)
|
||||
@@ -134,6 +28,135 @@ def test_get_all_template_statistics_with_bad_limit_arg_returns_400(notify_api,
|
||||
assert json_resp['message'] == {'limit_days': ['blurk is not an integer']}
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
def test_get_template_statistics_for_service(notify_db, notify_db_session, notify_api, sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session, service=sample_service)
|
||||
email = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||
today = datetime.now()
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['count'] == 2
|
||||
assert json_resp['data'][0]['template_id'] == str(email.id)
|
||||
assert json_resp['data'][0]['template_name'] == email.name
|
||||
assert json_resp['data'][0]['template_type'] == email.template_type
|
||||
assert json_resp['data'][1]['count'] == 2
|
||||
assert json_resp['data'][1]['template_id'] == str(sms.id)
|
||||
assert json_resp['data'][1]['template_name'] == sms.name
|
||||
assert json_resp['data'][1]['template_type'] == sms.template_type
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
def test_get_template_statistics_for_service_limited_by_day(notify_db, notify_db_session, notify_api, sample_service):
|
||||
sms = sample_template(notify_db, notify_db_session, service=sample_service)
|
||||
email = sample_email_template(notify_db, notify_db_session, service=sample_service)
|
||||
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, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=today, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_week_ago, service=sample_service, template=email)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_month_ago, service=sample_service, template=sms)
|
||||
sample_notification(notify_db, notify_db_session, created_at=a_month_ago, service=sample_service, template=email)
|
||||
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 1}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 2
|
||||
assert json_resp['data'][0]['count'] == 1
|
||||
assert json_resp['data'][0]['template_id'] == str(email.id)
|
||||
assert json_resp['data'][0]['template_name'] == email.name
|
||||
assert json_resp['data'][0]['template_type'] == email.template_type
|
||||
assert json_resp['data'][1]['count'] == 1
|
||||
assert json_resp['data'][1]['template_id'] == str(sms.id)
|
||||
assert json_resp['data'][1]['template_name'] == sms.name
|
||||
assert json_resp['data'][1]['template_type'] == sms.template_type
|
||||
|
||||
response_for_a_week = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 7}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
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'] == 2
|
||||
assert json_resp['data'][0]['template_name'] == 'Email Template Name'
|
||||
assert json_resp['data'][1]['count'] == 2
|
||||
assert json_resp['data'][1]['template_name'] == 'Template Name'
|
||||
|
||||
response_for_a_month = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
query_string={'limit_days': 30}
|
||||
)
|
||||
|
||||
assert response_for_a_month.status_code == 200
|
||||
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'][1]['count'] == 3
|
||||
assert json_resp['data'][1]['template_name'] == 'Template Name'
|
||||
|
||||
response_for_all = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
assert response_for_all.status_code == 200
|
||||
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'][1]['count'] == 3
|
||||
assert json_resp['data'][1]['template_name'] == 'Template Name'
|
||||
|
||||
|
||||
@freeze_time('2016-08-18')
|
||||
def test_returns_empty_list_if_no_templates_used(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
response = client.get(
|
||||
'/service/{}/template-statistics'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 0
|
||||
|
||||
|
||||
def test_get_template_statistics_for_template_only_returns_for_provided_template(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
|
||||
Reference in New Issue
Block a user