store api_key_id and key_type on notification

pass through from POST /notification/<type> to the celery task
also removed a couple of asserts that can fail (based on unfrozen time comparisons)
This commit is contained in:
Leo Hemsted
2016-06-30 17:32:49 +01:00
parent 5ae7ed1acb
commit 01419e7894
4 changed files with 115 additions and 58 deletions

View File

@@ -1,5 +1,5 @@
import uuid
from datetime import datetime
from datetime import datetime, timedelta
import pytest
from freezegun import freeze_time
@@ -21,7 +21,7 @@ from app.celery.tasks import (
from app.clients.email.aws_ses import AwsSesClientException
from app.dao import jobs_dao, provider_details_dao
from app.dao.provider_statistics_dao import get_provider_statistics
from app.models import Notification
from app.models import Notification, KEY_TYPE_TEAM
from tests.app import load_example_csv
from tests.app.conftest import (
sample_service,
@@ -462,7 +462,7 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n
Notification.query.filter_by(id=notification_id).one()
def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, mocker):
def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, sample_api_key, mocker):
notification = _notification_json(
sample_job.template,
to="+447234123123",
@@ -475,7 +475,9 @@ def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, mocker)
sample_job.service.id,
notification_id,
encryption.encrypt(notification),
datetime.utcnow().strftime(DATETIME_FORMAT)
datetime.utcnow().strftime(DATETIME_FORMAT),
api_key_id=str(sample_api_key.id),
key_type=KEY_TYPE_TEAM
)
provider_tasks.send_sms_to_provider.apply_async.assert_called_once_with(
(sample_job.service.id,
@@ -492,9 +494,11 @@ def test_should_send_sms_template_to_and_persist_with_job_id(sample_job, mocker)
assert persisted_notification.created_at <= datetime.utcnow()
assert not persisted_notification.sent_by
assert persisted_notification.job_row_number == 2
assert persisted_notification.api_key_id == sample_api_key.id
assert persisted_notification.key_type == KEY_TYPE_TEAM
def test_should_use_email_template_and_persist(sample_email_template_with_placeholders, mocker):
def test_should_use_email_template_and_persist(sample_email_template_with_placeholders, sample_api_key, mocker):
notification = _notification_json(
sample_email_template_with_placeholders,
"my_email@my_email.com",
@@ -508,20 +512,18 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
notification_id = uuid.uuid4()
freezer = freeze_time("2016-01-01 11:09:00.00000")
freezer.start()
now = datetime.utcnow()
freezer.stop()
with freeze_time("2016-01-01 11:09:00.00000"):
now = datetime.utcnow()
freezer = freeze_time("2016-01-01 11:10:00.00000")
freezer.start()
send_email(
sample_email_template_with_placeholders.service_id,
notification_id,
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT)
)
freezer.stop()
with freeze_time("2016-01-01 11:10:00.00000"):
send_email(
sample_email_template_with_placeholders.service_id,
notification_id,
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT),
api_key_id=str(sample_api_key.id),
key_type=KEY_TYPE_TEAM
)
aws_ses_client.send_email.assert_called_once_with(
'"Sample service" <sample.service@test.notify.com>',
@@ -552,6 +554,8 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
assert persisted_notification.job_row_number == 1
assert persisted_notification.personalisation == {'name': 'Jo'}
assert persisted_notification._personalisation == encryption.encrypt({"name": "Jo"})
assert persisted_notification.api_key_id == sample_api_key.id
assert persisted_notification.key_type == KEY_TYPE_TEAM
def test_send_email_should_use_template_version_from_job_not_latest(sample_email_template, mocker):
@@ -589,8 +593,6 @@ def test_send_email_should_use_template_version_from_job_not_latest(sample_email
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.template_version == version_on_notification
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.status == 'sending'
assert persisted_notification.sent_by == 'ses'
@@ -621,8 +623,6 @@ def test_should_use_email_template_subject_placeholders(sample_email_template_wi
assert persisted_notification.id == notification_id
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template_with_placeholders.id
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.status == 'sending'
assert persisted_notification.sent_by == 'ses'
assert persisted_notification.personalisation == {"name": "Jo"}
@@ -635,13 +635,16 @@ def test_should_use_email_template_and_persist_without_personalisation(sample_em
mocker.patch('app.aws_ses_client.get_name', return_value='ses')
notification_id = uuid.uuid4()
now = datetime.utcnow()
send_email(
sample_email_template.service_id,
notification_id,
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT)
)
with freeze_time("2016-01-01 11:09:00.00000") as freezer:
now = datetime.utcnow()
freezer.tick(delta=timedelta(seconds=60))
send_email(
sample_email_template.service_id,
notification_id,
encryption.encrypt(notification),
now.strftime(DATETIME_FORMAT)
)
aws_ses_client.send_email.assert_called_once_with(
'"Sample service" <sample.service@test.notify.com>',
"my_email@my_email.com",
@@ -655,7 +658,7 @@ def test_should_use_email_template_and_persist_without_personalisation(sample_em
assert persisted_notification.to == 'my_email@my_email.com'
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.created_at == now
assert persisted_notification.sent_at > now
assert persisted_notification.sent_at == now + timedelta(seconds=60)
assert persisted_notification.status == 'sending'
assert persisted_notification.sent_by == 'ses'
assert not persisted_notification.personalisation
@@ -802,8 +805,6 @@ def test_should_call_send_email_response_task_if_research_mode(
assert persisted_notification.to == 'john@smith.com'
assert persisted_notification.template_id == sample_email_template.id
assert persisted_notification.status == 'sending'
assert persisted_notification.sent_at > now
assert persisted_notification.created_at == now
assert persisted_notification.sent_by == 'ses'
assert persisted_notification.reference == str(reference)