Merge pull request #141 from alphagov/capture-aggregate-data

Capture aggregate data
This commit is contained in:
NIcholas Staples
2016-03-08 17:54:39 +00:00
14 changed files with 550 additions and 81 deletions

View File

@@ -2,7 +2,7 @@ import uuid
import pytest
from flask import current_app
from app.celery.tasks import (send_sms, send_sms_code, send_email_code, send_email, process_job, email_invited_user)
from app import (firetext_client, aws_ses_client, encryption)
from app import (firetext_client, aws_ses_client, encryption, DATETIME_FORMAT)
from app.clients.email.aws_ses import AwsSesClientException
from app.clients.sms.firetext import FiretextClientException
from app.dao import notifications_dao, jobs_dao
@@ -34,7 +34,7 @@ def test_should_process_sms_job(sample_job, mocker):
(str(sample_job.service_id),
"uuid",
"something_encrypted",
"2016-01-01 11:09:00.061258"),
"2016-01-01T11:09:00.061258"),
queue="bulk-sms"
)
job = jobs_dao.dao_get_job_by_id(sample_job.id)
@@ -69,7 +69,7 @@ def test_should_process_email_job(sample_email_job, mocker):
sample_email_job.template.subject,
"{}@{}".format(sample_email_job.service.email_from, "test.notify.com"),
"something_encrypted",
"2016-01-01 11:09:00.061258"),
"2016-01-01T11:09:00.061258"),
queue="bulk-email"
)
job = jobs_dao.dao_get_job_by_id(sample_email_job.id)
@@ -106,7 +106,7 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat
sample_template_with_placeholders.service_id,
notification_id,
"encrypted-in-reality",
now
now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: Hello Jo")
@@ -138,7 +138,7 @@ def test_should_send_sms_without_personalisation(sample_template, mocker):
sample_template.service_id,
notification_id,
"encrypted-in-reality",
now
now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
@@ -164,7 +164,7 @@ def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notif
service.id,
notification_id,
"encrypted-in-reality",
now
now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
@@ -190,7 +190,7 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db,
service.id,
notification_id,
"encrypted-in-reality",
now
now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_not_called()
@@ -217,7 +217,8 @@ def test_should_send_email_if_restricted_service_and_valid_email(notify_db, noti
'subject',
'email_from',
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with(
"email_from",
@@ -243,8 +244,8 @@ def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sa
sample_job.service.id,
notification_id,
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
assert persisted_notification.id == notification_id
@@ -275,8 +276,8 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
'subject',
'email_from',
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with(
"email_from",
"my_email@my_email.com",
@@ -313,8 +314,8 @@ def test_should_use_email_template_and_persist_without_personalisation(
'subject',
'email_from',
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with(
"email_from",
"my_email@my_email.com",
@@ -339,8 +340,8 @@ def test_should_persist_notification_as_failed_if_sms_client_fails(sample_templa
sample_template.service_id,
notification_id,
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template")
persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
assert persisted_notification.id == notification_id
@@ -371,8 +372,8 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
'subject',
'email_from',
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with(
"email_from",
"my_email@my_email.com",
@@ -405,8 +406,8 @@ def test_should_not_send_sms_if_db_peristance_failed(sample_template, mocker):
sample_template.service_id,
notification_id,
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_not_called()
with pytest.raises(NoResultFound) as e:
notifications_dao.get_notification(sample_template.service_id, notification_id)
@@ -431,8 +432,8 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
'subject',
'email_from',
"encrypted-in-reality",
now)
now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_not_called()
with pytest.raises(NoResultFound) as e:
notifications_dao.get_notification(sample_email_template.service_id, notification_id)

View File

@@ -283,12 +283,13 @@ def sample_notification(notify_db,
'id': notification_id,
'to': to,
'job': job,
'service_id': service.id,
'service': service,
'template': template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification)
dao_create_notification(notification, template.template_type)
return notification

View File

@@ -1,28 +1,119 @@
from app.models import Notification, Job
from datetime import datetime
import pytest
import uuid
from freezegun import freeze_time
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
from app.models import Notification, Job, NotificationStatistics
from datetime import datetime, timedelta
from app.dao.notifications_dao import (
dao_create_notification,
dao_update_notification,
get_notification,
get_notification_for_job,
get_notifications_for_job
get_notifications_for_job,
dao_get_notification_statistics_for_service
)
from tests.app.conftest import sample_job
def test_save_notification_and_increment_job(sample_template, sample_job):
assert Notification.query.count() == 0
def test_should_be_able_to_get_statistics_for_a_service(sample_template):
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification)
dao_create_notification(notification, sample_template.template_type)
stats = dao_get_notification_statistics_for_service(sample_template.service.id)
assert len(stats) == 1
assert stats[0].emails_requested == 0
assert stats[0].sms_requested == 1
assert stats[0].day == notification.created_at.strftime('%Y-%m-%d')
assert stats[0].service_id == notification.service_id
def test_should_be_able_to_get_all_statistics_for_a_service(sample_template):
data = {
'to': '+44709123456',
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification_1 = Notification(**data)
notification_2 = Notification(**data)
notification_3 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type)
dao_create_notification(notification_2, sample_template.template_type)
dao_create_notification(notification_3, sample_template.template_type)
stats = dao_get_notification_statistics_for_service(sample_template.service.id)
assert len(stats) == 1
assert stats[0].emails_requested == 0
assert stats[0].sms_requested == 3
def test_should_be_able_to_get_all_statistics_for_a_service_for_several_days(sample_template):
data = {
'to': '+44709123456',
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template
}
today = datetime.utcnow()
yesterday = datetime.utcnow() - timedelta(days=1)
two_days_ago = datetime.utcnow() - timedelta(days=2)
data.update({
'created_at': today
})
notification_1 = Notification(**data)
data.update({
'created_at': yesterday
})
notification_2 = Notification(**data)
data.update({
'created_at': two_days_ago
})
notification_3 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type)
dao_create_notification(notification_2, sample_template.template_type)
dao_create_notification(notification_3, sample_template.template_type)
stats = dao_get_notification_statistics_for_service(sample_template.service.id)
assert len(stats) == 3
assert stats[0].emails_requested == 0
assert stats[0].sms_requested == 1
assert stats[0].day == today.strftime('%Y-%m-%d')
assert stats[1].emails_requested == 0
assert stats[1].sms_requested == 1
assert stats[1].day == yesterday.strftime('%Y-%m-%d')
assert stats[2].emails_requested == 0
assert stats[2].sms_requested == 1
assert stats[2].day == two_days_ago.strftime('%Y-%m-%d')
def test_should_be_empty_list_if_no_statistics_for_a_service(sample_service):
assert len(dao_get_notification_statistics_for_service(sample_service.id)) == 0
def test_save_notification_and_create_sms_stats(sample_template, sample_job):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
@@ -35,9 +126,251 @@ def test_save_notification_and_increment_job(sample_template, sample_job):
assert 'sent' == notification_from_db.status
assert Job.query.get(sample_job.id).notifications_sent == 1
stats = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_template.service.id
).first()
assert stats.emails_requested == 0
assert stats.sms_requested == 1
def test_save_notification_and_create_email_stats(sample_email_template, sample_job):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_email_template.service,
'service_id': sample_email_template.service.id,
'template': sample_email_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification, sample_email_template.template_type)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
assert notification_from_db.id
assert data['to'] == notification_from_db.to
assert data['job_id'] == notification_from_db.job_id
assert data['service'] == notification_from_db.service
assert data['template'] == notification_from_db.template
assert data['created_at'] == notification_from_db.created_at
assert 'sent' == notification_from_db.status
assert Job.query.get(sample_job.id).notifications_sent == 1
stats = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_email_template.service.id
).first()
assert stats.emails_requested == 1
assert stats.sms_requested == 0
@freeze_time("2016-01-01 00:00:00.000000")
def test_save_notification_handles_midnight_properly(sample_template, sample_job):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type)
assert Notification.query.count() == 1
stats = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_template.service.id
).first()
assert stats.day == '2016-01-01'
@freeze_time("2016-01-01 23:59:59.999999")
def test_save_notification_handles_just_before_midnight_properly(sample_template, sample_job):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type)
assert Notification.query.count() == 1
stats = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_template.service.id
).first()
assert stats.day == '2016-01-01'
def test_save_notification_and_increment_email_stats(sample_email_template, sample_job):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_email_template.service,
'service_id': sample_email_template.service.id,
'template': sample_email_template,
'created_at': datetime.utcnow()
}
notification_1 = Notification(**data)
notification_2 = Notification(**data)
dao_create_notification(notification_1, sample_email_template.template_type)
assert Notification.query.count() == 1
stats1 = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_email_template.service.id
).first()
assert stats1.emails_requested == 1
assert stats1.sms_requested == 0
dao_create_notification(notification_2, sample_email_template)
assert Notification.query.count() == 2
stats2 = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_email_template.service.id
).first()
assert stats2.emails_requested == 2
assert stats2.sms_requested == 0
def test_save_notification_and_increment_sms_stats(sample_template, sample_job):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification_1 = Notification(**data)
notification_2 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type)
assert Notification.query.count() == 1
stats1 = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_template.service.id
).first()
assert stats1.emails_requested == 0
assert stats1.sms_requested == 1
dao_create_notification(notification_2, sample_template.template_type)
assert Notification.query.count() == 2
stats2 = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_template.service.id
).first()
assert stats2.emails_requested == 0
assert stats2.sms_requested == 2
def test_not_save_notification_and_not_create_stats_on_commit_error(sample_template, sample_job):
random_id = str(uuid.uuid4())
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': random_id,
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
with pytest.raises(SQLAlchemyError):
dao_create_notification(notification, sample_template.template_type)
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):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'job_id': sample_job.id,
'service': sample_template.service,
'service_id': sample_template.service.id,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification, sample_template.template_type)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
assert notification_from_db.id
assert data['to'] == notification_from_db.to
assert data['job_id'] == notification_from_db.job_id
assert data['service'] == notification_from_db.service
assert data['template'] == notification_from_db.template
assert data['created_at'] == notification_from_db.created_at
assert 'sent' == notification_from_db.status
assert Job.query.get(sample_job.id).notifications_sent == 1
notification_2 = Notification(**data)
dao_create_notification(notification_2, sample_template)
assert Notification.query.count() == 2
assert Job.query.get(sample_job.id).notifications_sent == 2
def test_should_not_increment_job_if_notification_fails_to_persist(sample_template, sample_job):
random_id = str(uuid.uuid4())
assert Notification.query.count() == 0
data = {
'id': random_id,
'to': '+44709123456',
'job_id': sample_job.id,
'service_id': sample_template.service.id,
'service': sample_template.service,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification_1 = Notification(**data)
dao_create_notification(notification_1, sample_template.template_type)
assert Notification.query.count() == 1
assert Job.query.get(sample_job.id).notifications_sent == 1
job_last_updated_at = Job.query.get(sample_job.id).updated_at
notification_2 = Notification(**data)
with pytest.raises(SQLAlchemyError):
dao_create_notification(notification_2, sample_template.template_type)
assert Notification.query.count() == 1
assert Job.query.get(sample_job.id).notifications_sent == 1
assert Job.query.get(sample_job.id).updated_at == job_last_updated_at
def test_save_notification_and_increment_correct_job(notify_db, notify_db_session, sample_template):
job_1 = sample_job(notify_db, notify_db_session, sample_template.service)
job_2 = sample_job(notify_db, notify_db_session, sample_template.service)
@@ -45,13 +378,14 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
data = {
'to': '+44709123456',
'job_id': job_1.id,
'service_id': sample_template.service.id,
'service': sample_template.service,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification)
dao_create_notification(notification, sample_template.template_type)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
@@ -67,17 +401,17 @@ def test_save_notification_and_increment_correct_job(notify_db, notify_db_sessio
def test_save_notification_with_no_job(sample_template):
assert Notification.query.count() == 0
data = {
'to': '+44709123456',
'service_id': sample_template.service.id,
'service': sample_template.service,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification)
dao_create_notification(notification, sample_template.template_type)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
@@ -97,18 +431,18 @@ def test_get_notification(sample_notification):
def test_save_notification_no_job_id(sample_template):
assert Notification.query.count() == 0
to = '+44709123456'
data = {
'to': to,
'service_id': sample_template.service.id,
'service': sample_template.service,
'template': sample_template,
'created_at': datetime.utcnow()
}
notification = Notification(**data)
dao_create_notification(notification)
dao_create_notification(notification, sample_template.template_type)
assert Notification.query.count() == 1
notification_from_db = Notification.query.all()[0]
@@ -128,20 +462,28 @@ def test_get_notification_for_job(sample_notification):
def test_get_all_notifications_for_job(notify_db, notify_db_session, sample_job):
from tests.app.conftest import sample_notification
for i in range(0, 5):
sample_notification(notify_db,
notify_db_session,
service=sample_job.service,
template=sample_job.template,
job=sample_job)
try:
sample_notification(notify_db,
notify_db_session,
service=sample_job.service,
template=sample_job.template,
job=sample_job)
except IntegrityError:
pass
notifcations_from_db = get_notifications_for_job(sample_job.service.id, sample_job.id).items
assert len(notifcations_from_db) == 5
stats = NotificationStatistics.query.filter(
NotificationStatistics.service_id == sample_job.service.id
).first()
assert stats.emails_requested == 0
assert stats.sms_requested == 5
def test_update_notification(sample_notification):
def test_update_notification(sample_notification, sample_template):
assert sample_notification.status == 'sent'
sample_notification.status = 'failed'
dao_update_notification(sample_notification)

View File

@@ -359,7 +359,7 @@ def test_send_notification_with_placeholders_replaced(notify_api, sample_templat
(str(sample_template_with_placeholders.service.id),
notification_id,
"something_encrypted",
"2016-01-01 11:09:00.061258"),
"2016-01-01T11:09:00.061258"),
queue="sms"
)
assert response.status_code == 201
@@ -526,7 +526,7 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
(str(sample_template.service_id),
notification_id,
"something_encrypted",
"2016-01-01 11:09:00.061258"),
"2016-01-01T11:09:00.061258"),
queue="sms"
)
assert response.status_code == 201
@@ -748,7 +748,7 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
"Email Subject",
"sample.service@test.notify.com",
"something_encrypted",
"2016-01-01 11:09:00.061258"),
"2016-01-01T11:09:00.061258"),
queue="email"
)
assert response.status_code == 201

View File

@@ -348,7 +348,7 @@ def test_should_get_only_templates_for_that_servcie(notify_api, service_factory)
method='POST',
request_body=data
)
client.post(
resp = client.post(
'/service/{}/template'.format(service_1.id),
headers=[('Content-Type', 'application/json'), create_auth_header],
data=data