Refactor tests to use the create_notication in tests.app.db

This commit is contained in:
Rebecca Law
2017-05-25 11:41:07 +01:00
parent 9f6c037530
commit f555c7a73b
4 changed files with 103 additions and 105 deletions

View File

@@ -32,8 +32,7 @@ from tests.app.db import create_notification, create_service
from tests.app.conftest import ( from tests.app.conftest import (
sample_job as create_sample_job, sample_job as create_sample_job,
sample_notification_history as create_notification_history, sample_notification_history as create_notification_history,
create_custom_template, create_custom_template)
sample_notification)
from tests.conftest import set_config_values from tests.conftest import set_config_values
from unittest.mock import call, patch, PropertyMock from unittest.mock import call, patch, PropertyMock
@@ -419,18 +418,12 @@ def test_switch_providers_on_slow_delivery_does_not_switch_based_on_older_notifi
@freeze_time("2017-05-01 14:00:00") @freeze_time("2017-05-01 14:00:00")
def test_should_send_all_scheduled_notifications_to_deliver_queue(notify_db, def test_should_send_all_scheduled_notifications_to_deliver_queue(sample_template, mocker):
notify_db_session,
sample_template, mocker):
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms') mocked = mocker.patch('app.celery.provider_tasks.deliver_sms')
message_to_deliver = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, message_to_deliver = create_notification(template=sample_template, scheduled_for="2017-05-01 13:15")
template=sample_template, scheduled_for="2017-05-01 13:15") create_notification(template=sample_template, scheduled_for="2017-05-01 10:15", status='delivered')
sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, create_notification(template=sample_template)
template=sample_template, scheduled_for="2017-05-01 10:15", status='delivered') create_notification(template=sample_template, scheduled_for="2017-05-01 14:15")
sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
template=sample_template)
sample_notification(notify_db=notify_db, notify_db_session=notify_db_session,
template=sample_template, scheduled_for="2017-05-01 14:15")
scheduled_notifications = dao_get_scheduled_notifications() scheduled_notifications = dao_get_scheduled_notifications()
assert len(scheduled_notifications) == 1 assert len(scheduled_notifications) == 1

View File

@@ -3,9 +3,9 @@ import uuid
from app.dao.jobs_dao import dao_create_job from app.dao.jobs_dao import dao_create_job
from app.models import (Service, User, Template, Notification, EMAIL_TYPE, LETTER_TYPE, from app.models import (Service, User, Template, Notification, EMAIL_TYPE, LETTER_TYPE,
SMS_TYPE, KEY_TYPE_NORMAL, Job, ServicePermission) SMS_TYPE, KEY_TYPE_NORMAL, Job, ServicePermission, ScheduledNotification)
from app.dao.users_dao import save_model_user from app.dao.users_dao import save_model_user
from app.dao.notifications_dao import dao_create_notification from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification
from app.dao.templates_dao import dao_create_template from app.dao.templates_dao import dao_create_template
from app.dao.services_dao import dao_create_service from app.dao.services_dao import dao_create_service
from app.dao.service_permissions_dao import dao_add_service_permission from app.dao.service_permissions_dao import dao_add_service_permission
@@ -80,7 +80,8 @@ def create_notification(
client_reference=None, client_reference=None,
rate_multiplier=None, rate_multiplier=None,
international=False, international=False,
phone_prefix=None phone_prefix=None,
scheduled_for=None
): ):
if created_at is None: if created_at is None:
created_at = datetime.utcnow() created_at = datetime.utcnow()
@@ -118,6 +119,14 @@ def create_notification(
} }
notification = Notification(**data) notification = Notification(**data)
dao_create_notification(notification) dao_create_notification(notification)
if scheduled_for:
scheduled_notification = ScheduledNotification(id=uuid.uuid4(),
notification_id=notification.id,
scheduled_for=datetime.strptime(scheduled_for,
"%Y-%m-%d %H:%M"))
if status != 'created':
scheduled_notification.pending = False
dao_created_scheduled_notification(scheduled_notification)
return notification return notification

View File

@@ -4,10 +4,10 @@ from flask import json
from app import DATETIME_FORMAT from app import DATETIME_FORMAT
from tests import create_authorization_header from tests import create_authorization_header
from tests.app.conftest import ( from tests.app.db import (
sample_notification as create_sample_notification, create_notification,
sample_template as create_sample_template create_template,
) create_service)
@pytest.mark.parametrize('billable_units, provider', [ @pytest.mark.parametrize('billable_units, provider', [
@@ -16,17 +16,15 @@ from tests.app.conftest import (
(1, None) (1, None)
]) ])
def test_get_notification_by_id_returns_200( def test_get_notification_by_id_returns_200(
client, notify_db, notify_db_session, billable_units, provider client, billable_units, provider, sample_template
): ):
sample_notification = create_sample_notification( sample_notification = create_notification(template=sample_template, billable_units=billable_units, sent_by=provider,
notify_db, notify_db_session, billable_units=billable_units, sent_by=provider, scheduled_for="2017-05-12 15:15"
scheduled_for="2017-05-12 15:15" )
)
another = create_sample_notification( another = create_notification(template=sample_template, billable_units=billable_units, sent_by=provider,
notify_db, notify_db_session, billable_units=billable_units, sent_by=provider, scheduled_for="2017-06-12 15:15"
scheduled_for="2017-06-12 15:15" )
)
auth_header = create_authorization_header(service_id=sample_notification.service_id) auth_header = create_authorization_header(service_id=sample_notification.service_id)
response = client.get( response = client.get(
path='/v2/notifications/{}'.format(sample_notification.id), path='/v2/notifications/{}'.format(sample_notification.id),
@@ -70,11 +68,11 @@ def test_get_notification_by_id_returns_200(
def test_get_notification_by_id_with_placeholders_returns_200( def test_get_notification_by_id_with_placeholders_returns_200(
client, notify_db, notify_db_session, sample_email_template_with_placeholders client, sample_email_template_with_placeholders
): ):
sample_notification = create_sample_notification( sample_notification = create_notification(template=sample_email_template_with_placeholders,
notify_db, notify_db_session, template=sample_email_template_with_placeholders, personalisation={"name": "Bob"} personalisation={"name": "Bob"}
) )
auth_header = create_authorization_header(service_id=sample_notification.service_id) auth_header = create_authorization_header(service_id=sample_notification.service_id)
response = client.get( response = client.get(
@@ -118,9 +116,9 @@ def test_get_notification_by_id_with_placeholders_returns_200(
assert json_response == expected_response assert json_response == expected_response
def test_get_notification_by_reference_returns_200(client, notify_db, notify_db_session): def test_get_notification_by_reference_returns_200(client, sample_template):
sample_notification_with_reference = create_sample_notification( sample_notification_with_reference = create_notification(template=sample_template,
notify_db, notify_db_session, client_reference='some-client-reference') client_reference='some-client-reference')
auth_header = create_authorization_header(service_id=sample_notification_with_reference.service_id) auth_header = create_authorization_header(service_id=sample_notification_with_reference.service_id)
response = client.get( response = client.get(
@@ -137,9 +135,10 @@ def test_get_notification_by_reference_returns_200(client, notify_db, notify_db_
assert json_response['notifications'][0]['reference'] == "some-client-reference" assert json_response['notifications'][0]['reference'] == "some-client-reference"
def test_get_notifications_returns_scheduled_for(client, notify_db, notify_db_session): def test_get_notifications_returns_scheduled_for(client, sample_template):
sample_notification_with_reference = create_sample_notification( sample_notification_with_reference = create_notification(template=sample_template,
notify_db, notify_db_session, client_reference='some-client-reference', scheduled_for='2017-05-23 17:15') client_reference='some-client-reference',
scheduled_for='2017-05-23 17:15')
auth_header = create_authorization_header(service_id=sample_notification_with_reference.service_id) auth_header = create_authorization_header(service_id=sample_notification_with_reference.service_id)
response = client.get( response = client.get(
@@ -208,8 +207,8 @@ def test_get_notification_by_id_invalid_id(client, sample_notification, id):
} }
def test_get_all_notifications_returns_200(client, notify_db, notify_db_session): def test_get_all_notifications_returns_200(client, sample_template):
notifications = [create_sample_notification(notify_db, notify_db_session) for _ in range(2)] notifications = [create_notification(template=sample_template) for _ in range(2)]
notification = notifications[-1] notification = notifications[-1]
auth_header = create_authorization_header(service_id=notification.service_id) auth_header = create_authorization_header(service_id=notification.service_id)
@@ -252,13 +251,13 @@ def test_get_all_notifications_no_notifications_if_no_notifications(client, samp
assert len(json_response['notifications']) == 0 assert len(json_response['notifications']) == 0
def test_get_all_notifications_filter_by_template_type(client, notify_db, notify_db_session): def test_get_all_notifications_filter_by_template_type(client):
email_template = create_sample_template(notify_db, notify_db_session, template_type="email") service = create_service()
sms_template = create_sample_template(notify_db, notify_db_session, template_type="sms") email_template = create_template(service=service, template_type="email")
sms_template = create_template(service=service, template_type="sms")
notification = create_sample_notification( notification = create_notification(template=email_template, to_field="don.draper@scdp.biz")
notify_db, notify_db_session, template=email_template, to_field="don.draper@scdp.biz") create_notification(template=sms_template)
create_sample_notification(notify_db, notify_db_session, template=sms_template)
auth_header = create_authorization_header(service_id=notification.service_id) auth_header = create_authorization_header(service_id=notification.service_id)
response = client.get( response = client.get(
@@ -300,9 +299,9 @@ def test_get_all_notifications_filter_by_template_type_invalid_template_type(cli
assert json_response['errors'][0]['message'] == "template_type orange is not one of [sms, email, letter]" assert json_response['errors'][0]['message'] == "template_type orange is not one of [sms, email, letter]"
def test_get_all_notifications_filter_by_single_status(client, notify_db, notify_db_session): def test_get_all_notifications_filter_by_single_status(client, sample_template):
notification = create_sample_notification(notify_db, notify_db_session, status="pending") notification = create_notification(template=sample_template, status="pending")
create_sample_notification(notify_db, notify_db_session) create_notification(template=sample_template)
auth_header = create_authorization_header(service_id=notification.service_id) auth_header = create_authorization_header(service_id=notification.service_id)
response = client.get( response = client.get(
@@ -338,12 +337,12 @@ def test_get_all_notifications_filter_by_status_invalid_status(client, sample_no
"delivered, pending, failed, technical-failure, temporary-failure, permanent-failure]" "delivered, pending, failed, technical-failure, temporary-failure, permanent-failure]"
def test_get_all_notifications_filter_by_multiple_statuses(client, notify_db, notify_db_session): def test_get_all_notifications_filter_by_multiple_statuses(client, sample_template):
notifications = [ notifications = [
create_sample_notification(notify_db, notify_db_session, status=_status) create_notification(template=sample_template, status=_status)
for _status in ["created", "pending", "sending"] for _status in ["created", "pending", "sending"]
] ]
failed_notification = create_sample_notification(notify_db, notify_db_session, status="permanent-failure") failed_notification = create_notification(template=sample_template, status="permanent-failure")
auth_header = create_authorization_header(service_id=notifications[0].service_id) auth_header = create_authorization_header(service_id=notifications[0].service_id)
response = client.get( response = client.get(
@@ -365,10 +364,10 @@ def test_get_all_notifications_filter_by_multiple_statuses(client, notify_db, no
assert failed_notification.id not in returned_notification_ids assert failed_notification.id not in returned_notification_ids
def test_get_all_notifications_filter_by_failed_status(client, notify_db, notify_db_session): def test_get_all_notifications_filter_by_failed_status(client, sample_template):
created_notification = create_sample_notification(notify_db, notify_db_session, status="created") created_notification = create_notification(template=sample_template, status="created")
failed_notifications = [ failed_notifications = [
create_sample_notification(notify_db, notify_db_session, status=_status) create_notification(template=sample_template, status=_status)
for _status in ["technical-failure", "temporary-failure", "permanent-failure"] for _status in ["technical-failure", "temporary-failure", "permanent-failure"]
] ]
@@ -392,9 +391,9 @@ def test_get_all_notifications_filter_by_failed_status(client, notify_db, notify
assert created_notification.id not in returned_notification_ids assert created_notification.id not in returned_notification_ids
def test_get_all_notifications_filter_by_id(client, notify_db, notify_db_session): def test_get_all_notifications_filter_by_id(client, sample_template):
older_notification = create_sample_notification(notify_db, notify_db_session) older_notification = create_notification(template=sample_template)
newer_notification = create_sample_notification(notify_db, notify_db_session) newer_notification = create_notification(template=sample_template)
auth_header = create_authorization_header(service_id=newer_notification.service_id) auth_header = create_authorization_header(service_id=newer_notification.service_id)
response = client.get( response = client.get(
@@ -425,8 +424,8 @@ def test_get_all_notifications_filter_by_id_invalid_id(client, sample_notificati
assert json_response['errors'][0]['message'] == "older_than is not a valid UUID" assert json_response['errors'][0]['message'] == "older_than is not a valid UUID"
def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(client, notify_db, notify_db_session): def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(client, sample_template):
notification = create_sample_notification(notify_db, notify_db_session) notification = create_notification(template=sample_template)
auth_header = create_authorization_header(service_id=notification.service_id) auth_header = create_authorization_header(service_id=notification.service_id)
response = client.get( response = client.get(
@@ -443,8 +442,8 @@ def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(c
assert len(json_response['notifications']) == 0 assert len(json_response['notifications']) == 0
def test_get_all_notifications_filter_by_id_no_notifications_if_last_notification(client, notify_db, notify_db_session): def test_get_all_notifications_filter_by_id_no_notifications_if_last_notification(client, sample_template):
notification = create_sample_notification(notify_db, notify_db_session) notification = create_notification(template=sample_template)
auth_header = create_authorization_header(service_id=notification.service_id) auth_header = create_authorization_header(service_id=notification.service_id)
response = client.get( response = client.get(
@@ -460,23 +459,22 @@ def test_get_all_notifications_filter_by_id_no_notifications_if_last_notificatio
assert len(json_response['notifications']) == 0 assert len(json_response['notifications']) == 0
def test_get_all_notifications_filter_multiple_query_parameters(client, notify_db, notify_db_session): def test_get_all_notifications_filter_multiple_query_parameters(client, sample_email_template):
email_template = create_sample_template(notify_db, notify_db_session, template_type="email")
# this is the notification we are looking for # this is the notification we are looking for
older_notification = create_sample_notification( older_notification = create_notification(
notify_db, notify_db_session, template=email_template, status="pending") template=sample_email_template, status="pending")
# wrong status # wrong status
create_sample_notification(notify_db, notify_db_session, template=email_template) create_notification(template=sample_email_template)
wrong_template = create_template(sample_email_template.service, template_type='sms')
# wrong template # wrong template
create_sample_notification(notify_db, notify_db_session, status="pending") create_notification(template=wrong_template, status="pending")
# we only want notifications created before this one # we only want notifications created before this one
newer_notification = create_sample_notification(notify_db, notify_db_session) newer_notification = create_notification(template=sample_email_template)
# this notification was created too recently # this notification was created too recently
create_sample_notification(notify_db, notify_db_session, template=email_template, status="pending") create_notification(template=sample_email_template, status="pending")
auth_header = create_authorization_header(service_id=newer_notification.service_id) auth_header = create_authorization_header(service_id=newer_notification.service_id)
response = client.get( response = client.get(

View File

@@ -13,40 +13,38 @@ from tests.app.conftest import sample_template as create_sample_template, sample
@pytest.mark.parametrize("reference", [None, "reference_from_client"]) @pytest.mark.parametrize("reference", [None, "reference_from_client"])
def test_post_sms_notification_returns_201(notify_api, sample_template_with_placeholders, mocker, reference): def test_post_sms_notification_returns_201(client, sample_template_with_placeholders, mocker, reference):
with notify_api.test_request_context(): mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
with notify_api.test_client() as client: data = {
mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') 'phone_number': '+447700900855',
data = { 'template_id': str(sample_template_with_placeholders.id),
'phone_number': '+447700900855', 'personalisation': {' Name': 'Jo'}
'template_id': str(sample_template_with_placeholders.id), }
'personalisation': {' Name': 'Jo'} if reference:
} data.update({"reference": reference})
if reference: auth_header = create_authorization_header(service_id=sample_template_with_placeholders.service_id)
data.update({"reference": reference})
auth_header = create_authorization_header(service_id=sample_template_with_placeholders.service_id)
response = client.post( response = client.post(
path='/v2/notifications/sms', path='/v2/notifications/sms',
data=json.dumps(data), data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 201 assert response.status_code == 201
resp_json = json.loads(response.get_data(as_text=True)) resp_json = json.loads(response.get_data(as_text=True))
notifications = Notification.query.all() notifications = Notification.query.all()
assert len(notifications) == 1 assert len(notifications) == 1
notification_id = notifications[0].id notification_id = notifications[0].id
assert resp_json['id'] == str(notification_id) assert resp_json['id'] == str(notification_id)
assert resp_json['reference'] == reference assert resp_json['reference'] == reference
assert resp_json['content']['body'] == sample_template_with_placeholders.content.replace("(( Name))", "Jo") assert resp_json['content']['body'] == sample_template_with_placeholders.content.replace("(( Name))", "Jo")
assert resp_json['content']['from_number'] == current_app.config['FROM_NUMBER'] assert resp_json['content']['from_number'] == current_app.config['FROM_NUMBER']
assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri'] assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri']
assert resp_json['template']['id'] == str(sample_template_with_placeholders.id) assert resp_json['template']['id'] == str(sample_template_with_placeholders.id)
assert resp_json['template']['version'] == sample_template_with_placeholders.version assert resp_json['template']['version'] == sample_template_with_placeholders.version
assert 'services/{}/templates/{}'.format(sample_template_with_placeholders.service_id, assert 'services/{}/templates/{}'.format(sample_template_with_placeholders.service_id,
sample_template_with_placeholders.id) \ sample_template_with_placeholders.id) \
in resp_json['template']['uri'] in resp_json['template']['uri']
assert not resp_json["scheduled_for"] assert not resp_json["scheduled_for"]
assert mocked.called assert mocked.called
@pytest.mark.parametrize("notification_type, key_send_to, send_to", @pytest.mark.parametrize("notification_type, key_send_to, send_to",