Fixed up dates so that we respect mills

This commit is contained in:
Martyn Inglis
2016-03-08 17:45:37 +00:00
parent 67c4bd2263
commit e07d16e8c6
5 changed files with 37 additions and 34 deletions

View File

@@ -13,6 +13,8 @@ from app.clients.sms.firetext import FiretextClient
from app.clients.email.aws_ses import AwsSesClient from app.clients.email.aws_ses import AwsSesClient
from app.encryption import Encryption from app.encryption import Encryption
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f"
db = SQLAlchemy() db = SQLAlchemy()
ma = Marshmallow() ma = Marshmallow()
notify_celery = NotifyCelery() notify_celery = NotifyCelery()

View File

@@ -1,4 +1,4 @@
from app import create_uuid from app import create_uuid, DATETIME_FORMAT
from app import notify_celery, encryption, firetext_client, aws_ses_client from app import notify_celery, encryption, firetext_client, aws_ses_client
from app.clients.email.aws_ses import AwsSesClientException from app.clients.email.aws_ses import AwsSesClientException
from app.clients.sms.firetext import FiretextClientException from app.clients.sms.firetext import FiretextClientException
@@ -38,7 +38,7 @@ def process_job(job_id):
str(job.service_id), str(job.service_id),
str(create_uuid()), str(create_uuid()),
encrypted, encrypted,
str(datetime.utcnow())), datetime.utcnow().strftime(DATETIME_FORMAT)),
queue='bulk-sms' queue='bulk-sms'
) )
@@ -49,7 +49,7 @@ def process_job(job_id):
job.template.subject, job.template.subject,
"{}@{}".format(job.service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']), "{}@{}".format(job.service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
encrypted, encrypted,
str(datetime.utcnow())), datetime.utcnow().strftime(DATETIME_FORMAT)),
queue='bulk-email') queue='bulk-email')
finished = datetime.utcnow() finished = datetime.utcnow()
@@ -88,7 +88,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
service_id=service_id, service_id=service_id,
job_id=notification.get('job', None), job_id=notification.get('job', None),
status=status, status=status,
created_at=created_at, created_at=datetime.strptime(created_at, DATETIME_FORMAT),
sent_at=sent_at, sent_at=sent_at,
sent_by=client.get_name() sent_by=client.get_name()
) )
@@ -158,11 +158,11 @@ def send_email(service_id, notification_id, subject, from_address, encrypted_not
service_id=service_id, service_id=service_id,
job_id=notification.get('job', None), job_id=notification.get('job', None),
status=status, status=status,
created_at=created_at, created_at=datetime.strptime(created_at, DATETIME_FORMAT),
sent_at=sent_at, sent_at=sent_at,
sent_by=client.get_name() sent_by=client.get_name()
) )
dao_create_notification(notification_db_object, TEMPLATE_TYPE_SMS) dao_create_notification(notification_db_object, TEMPLATE_TYPE_EMAIL)
if can_send: if can_send:
try: try:

View File

@@ -10,7 +10,7 @@ from flask import (
from utils.template import Template, NeededByTemplateError, NoPlaceholderForDataError from utils.template import Template, NeededByTemplateError, NoPlaceholderForDataError
from app import api_user, encryption, create_uuid from app import api_user, encryption, create_uuid, DATETIME_FORMAT
from app.authentication.auth import require_admin from app.authentication.auth import require_admin
from app.dao import ( from app.dao import (
templates_dao, templates_dao,
@@ -178,7 +178,7 @@ def send_notification(notification_type):
service_id, service_id,
notification_id, notification_id,
encryption.encrypt(notification), encryption.encrypt(notification),
str(datetime.utcnow()) datetime.utcnow().strftime(DATETIME_FORMAT)
), queue='sms') ), queue='sms')
else: else:
if service.restricted and notification['to'] not in [user.email_address for user in service.users]: if service.restricted and notification['to'] not in [user.email_address for user in service.users]:
@@ -190,6 +190,6 @@ def send_notification(notification_type):
template.subject, template.subject,
"{}@{}".format(service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']), "{}@{}".format(service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
encryption.encrypt(notification), encryption.encrypt(notification),
str(datetime.utcnow()) datetime.utcnow().strftime(DATETIME_FORMAT)
), queue='email') ), queue='email')
return jsonify({'notification_id': notification_id}), 201 return jsonify({'notification_id': notification_id}), 201

View File

@@ -2,7 +2,7 @@ import uuid
import pytest import pytest
from flask import current_app 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.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.email.aws_ses import AwsSesClientException
from app.clients.sms.firetext import FiretextClientException from app.clients.sms.firetext import FiretextClientException
from app.dao import notifications_dao, jobs_dao 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), (str(sample_job.service_id),
"uuid", "uuid",
"something_encrypted", "something_encrypted",
"2016-01-01 11:09:00.061258"), "2016-01-01T11:09:00.061258"),
queue="bulk-sms" queue="bulk-sms"
) )
job = jobs_dao.dao_get_job_by_id(sample_job.id) 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, sample_email_job.template.subject,
"{}@{}".format(sample_email_job.service.email_from, "test.notify.com"), "{}@{}".format(sample_email_job.service.email_from, "test.notify.com"),
"something_encrypted", "something_encrypted",
"2016-01-01 11:09:00.061258"), "2016-01-01T11:09:00.061258"),
queue="bulk-email" queue="bulk-email"
) )
job = jobs_dao.dao_get_job_by_id(sample_email_job.id) 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, sample_template_with_placeholders.service_id,
notification_id, notification_id,
"encrypted-in-reality", "encrypted-in-reality",
now now.strftime(DATETIME_FORMAT)
) )
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: Hello Jo") 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, sample_template.service_id,
notification_id, notification_id,
"encrypted-in-reality", "encrypted-in-reality",
now now.strftime(DATETIME_FORMAT)
) )
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template") 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, service.id,
notification_id, notification_id,
"encrypted-in-reality", "encrypted-in-reality",
now now.strftime(DATETIME_FORMAT)
) )
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template") 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, service.id,
notification_id, notification_id,
"encrypted-in-reality", "encrypted-in-reality",
now now.strftime(DATETIME_FORMAT)
) )
firetext_client.send_sms.assert_not_called() 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', 'subject',
'email_from', 'email_from',
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with( aws_ses_client.send_email.assert_called_once_with(
"email_from", "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, sample_job.service.id,
notification_id, notification_id,
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template") 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) persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id)
assert persisted_notification.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', 'subject',
'email_from', 'email_from',
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with( aws_ses_client.send_email.assert_called_once_with(
"email_from", "email_from",
"my_email@my_email.com", "my_email@my_email.com",
@@ -313,8 +314,8 @@ def test_should_use_email_template_and_persist_without_personalisation(
'subject', 'subject',
'email_from', 'email_from',
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with( aws_ses_client.send_email.assert_called_once_with(
"email_from", "email_from",
"my_email@my_email.com", "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, sample_template.service_id,
notification_id, notification_id,
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_called_once_with("+441234123123", "Sample service: This is a template") 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) persisted_notification = notifications_dao.get_notification(sample_template.service_id, notification_id)
assert persisted_notification.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', 'subject',
'email_from', 'email_from',
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with( aws_ses_client.send_email.assert_called_once_with(
"email_from", "email_from",
"my_email@my_email.com", "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, sample_template.service_id,
notification_id, notification_id,
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
firetext_client.send_sms.assert_not_called() firetext_client.send_sms.assert_not_called()
with pytest.raises(NoResultFound) as e: with pytest.raises(NoResultFound) as e:
notifications_dao.get_notification(sample_template.service_id, notification_id) 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', 'subject',
'email_from', 'email_from',
"encrypted-in-reality", "encrypted-in-reality",
now) now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_not_called() aws_ses_client.send_email.assert_not_called()
with pytest.raises(NoResultFound) as e: with pytest.raises(NoResultFound) as e:
notifications_dao.get_notification(sample_email_template.service_id, notification_id) notifications_dao.get_notification(sample_email_template.service_id, notification_id)

View File

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