From f555c7a73b41619a220fa5a1cc9d848e00d2099b Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 25 May 2017 11:41:07 +0100 Subject: [PATCH] Refactor tests to use the create_notication in tests.app.db --- tests/app/celery/test_scheduled_tasks.py | 19 +-- tests/app/db.py | 15 ++- .../notifications/test_get_notifications.py | 110 +++++++++--------- .../notifications/test_post_notifications.py | 64 +++++----- 4 files changed, 103 insertions(+), 105 deletions(-) diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index 538475b6e..194d764fd 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -32,8 +32,7 @@ from tests.app.db import create_notification, create_service from tests.app.conftest import ( sample_job as create_sample_job, sample_notification_history as create_notification_history, - create_custom_template, - sample_notification) + create_custom_template) from tests.conftest import set_config_values 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") -def test_should_send_all_scheduled_notifications_to_deliver_queue(notify_db, - notify_db_session, - sample_template, mocker): +def test_should_send_all_scheduled_notifications_to_deliver_queue(sample_template, mocker): mocked = mocker.patch('app.celery.provider_tasks.deliver_sms') - message_to_deliver = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, - template=sample_template, scheduled_for="2017-05-01 13:15") - sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, - template=sample_template, scheduled_for="2017-05-01 10:15", status='delivered') - 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") + message_to_deliver = create_notification(template=sample_template, scheduled_for="2017-05-01 13:15") + create_notification(template=sample_template, scheduled_for="2017-05-01 10:15", status='delivered') + create_notification(template=sample_template) + create_notification(template=sample_template, scheduled_for="2017-05-01 14:15") scheduled_notifications = dao_get_scheduled_notifications() assert len(scheduled_notifications) == 1 diff --git a/tests/app/db.py b/tests/app/db.py index d418b1c3b..afb35e252 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -3,9 +3,9 @@ import uuid from app.dao.jobs_dao import dao_create_job 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.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.services_dao import dao_create_service from app.dao.service_permissions_dao import dao_add_service_permission @@ -80,7 +80,8 @@ def create_notification( client_reference=None, rate_multiplier=None, international=False, - phone_prefix=None + phone_prefix=None, + scheduled_for=None ): if created_at is None: created_at = datetime.utcnow() @@ -118,6 +119,14 @@ def create_notification( } notification = Notification(**data) 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 diff --git a/tests/app/v2/notifications/test_get_notifications.py b/tests/app/v2/notifications/test_get_notifications.py index bd5cbae6f..ff487275c 100644 --- a/tests/app/v2/notifications/test_get_notifications.py +++ b/tests/app/v2/notifications/test_get_notifications.py @@ -4,10 +4,10 @@ from flask import json from app import DATETIME_FORMAT from tests import create_authorization_header -from tests.app.conftest import ( - sample_notification as create_sample_notification, - sample_template as create_sample_template -) +from tests.app.db import ( + create_notification, + create_template, + create_service) @pytest.mark.parametrize('billable_units, provider', [ @@ -16,17 +16,15 @@ from tests.app.conftest import ( (1, None) ]) 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( - notify_db, notify_db_session, billable_units=billable_units, sent_by=provider, - scheduled_for="2017-05-12 15:15" - ) + sample_notification = create_notification(template=sample_template, billable_units=billable_units, sent_by=provider, + scheduled_for="2017-05-12 15:15" + ) - another = create_sample_notification( - notify_db, notify_db_session, billable_units=billable_units, sent_by=provider, - scheduled_for="2017-06-12 15:15" - ) + another = create_notification(template=sample_template, billable_units=billable_units, sent_by=provider, + scheduled_for="2017-06-12 15:15" + ) auth_header = create_authorization_header(service_id=sample_notification.service_id) response = client.get( 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( - client, notify_db, notify_db_session, sample_email_template_with_placeholders + client, sample_email_template_with_placeholders ): - sample_notification = create_sample_notification( - notify_db, notify_db_session, template=sample_email_template_with_placeholders, personalisation={"name": "Bob"} - ) + sample_notification = create_notification(template=sample_email_template_with_placeholders, + personalisation={"name": "Bob"} + ) auth_header = create_authorization_header(service_id=sample_notification.service_id) response = client.get( @@ -118,9 +116,9 @@ def test_get_notification_by_id_with_placeholders_returns_200( assert json_response == expected_response -def test_get_notification_by_reference_returns_200(client, notify_db, notify_db_session): - sample_notification_with_reference = create_sample_notification( - notify_db, notify_db_session, client_reference='some-client-reference') +def test_get_notification_by_reference_returns_200(client, sample_template): + sample_notification_with_reference = create_notification(template=sample_template, + client_reference='some-client-reference') auth_header = create_authorization_header(service_id=sample_notification_with_reference.service_id) 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" -def test_get_notifications_returns_scheduled_for(client, notify_db, notify_db_session): - sample_notification_with_reference = create_sample_notification( - notify_db, notify_db_session, client_reference='some-client-reference', scheduled_for='2017-05-23 17:15') +def test_get_notifications_returns_scheduled_for(client, sample_template): + sample_notification_with_reference = create_notification(template=sample_template, + 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) 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): - notifications = [create_sample_notification(notify_db, notify_db_session) for _ in range(2)] +def test_get_all_notifications_returns_200(client, sample_template): + notifications = [create_notification(template=sample_template) for _ in range(2)] notification = notifications[-1] 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 -def test_get_all_notifications_filter_by_template_type(client, notify_db, notify_db_session): - email_template = create_sample_template(notify_db, notify_db_session, template_type="email") - sms_template = create_sample_template(notify_db, notify_db_session, template_type="sms") +def test_get_all_notifications_filter_by_template_type(client): + service = create_service() + email_template = create_template(service=service, template_type="email") + sms_template = create_template(service=service, template_type="sms") - notification = create_sample_notification( - notify_db, notify_db_session, template=email_template, to_field="don.draper@scdp.biz") - create_sample_notification(notify_db, notify_db_session, template=sms_template) + notification = create_notification(template=email_template, to_field="don.draper@scdp.biz") + create_notification(template=sms_template) auth_header = create_authorization_header(service_id=notification.service_id) 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]" -def test_get_all_notifications_filter_by_single_status(client, notify_db, notify_db_session): - notification = create_sample_notification(notify_db, notify_db_session, status="pending") - create_sample_notification(notify_db, notify_db_session) +def test_get_all_notifications_filter_by_single_status(client, sample_template): + notification = create_notification(template=sample_template, status="pending") + create_notification(template=sample_template) auth_header = create_authorization_header(service_id=notification.service_id) 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]" -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 = [ - create_sample_notification(notify_db, notify_db_session, status=_status) + create_notification(template=sample_template, status=_status) 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) 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 -def test_get_all_notifications_filter_by_failed_status(client, notify_db, notify_db_session): - created_notification = create_sample_notification(notify_db, notify_db_session, status="created") +def test_get_all_notifications_filter_by_failed_status(client, sample_template): + created_notification = create_notification(template=sample_template, status="created") 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"] ] @@ -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 -def test_get_all_notifications_filter_by_id(client, notify_db, notify_db_session): - older_notification = create_sample_notification(notify_db, notify_db_session) - newer_notification = create_sample_notification(notify_db, notify_db_session) +def test_get_all_notifications_filter_by_id(client, sample_template): + older_notification = create_notification(template=sample_template) + newer_notification = create_notification(template=sample_template) auth_header = create_authorization_header(service_id=newer_notification.service_id) 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" -def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(client, notify_db, notify_db_session): - notification = create_sample_notification(notify_db, notify_db_session) +def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(client, sample_template): + notification = create_notification(template=sample_template) auth_header = create_authorization_header(service_id=notification.service_id) 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 -def test_get_all_notifications_filter_by_id_no_notifications_if_last_notification(client, notify_db, notify_db_session): - notification = create_sample_notification(notify_db, notify_db_session) +def test_get_all_notifications_filter_by_id_no_notifications_if_last_notification(client, sample_template): + notification = create_notification(template=sample_template) auth_header = create_authorization_header(service_id=notification.service_id) 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 -def test_get_all_notifications_filter_multiple_query_parameters(client, notify_db, notify_db_session): - email_template = create_sample_template(notify_db, notify_db_session, template_type="email") - +def test_get_all_notifications_filter_multiple_query_parameters(client, sample_email_template): # this is the notification we are looking for - older_notification = create_sample_notification( - notify_db, notify_db_session, template=email_template, status="pending") + older_notification = create_notification( + template=sample_email_template, status="pending") # 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 - 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 - newer_notification = create_sample_notification(notify_db, notify_db_session) + newer_notification = create_notification(template=sample_email_template) # 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) response = client.get( diff --git a/tests/app/v2/notifications/test_post_notifications.py b/tests/app/v2/notifications/test_post_notifications.py index 3bd191a9b..afce12947 100644 --- a/tests/app/v2/notifications/test_post_notifications.py +++ b/tests/app/v2/notifications/test_post_notifications.py @@ -13,40 +13,38 @@ from tests.app.conftest import sample_template as create_sample_template, sample @pytest.mark.parametrize("reference", [None, "reference_from_client"]) -def test_post_sms_notification_returns_201(notify_api, sample_template_with_placeholders, mocker, reference): - with notify_api.test_request_context(): - with notify_api.test_client() as client: - mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') - data = { - 'phone_number': '+447700900855', - 'template_id': str(sample_template_with_placeholders.id), - 'personalisation': {' Name': 'Jo'} - } - if reference: - data.update({"reference": reference}) - auth_header = create_authorization_header(service_id=sample_template_with_placeholders.service_id) +def test_post_sms_notification_returns_201(client, sample_template_with_placeholders, mocker, reference): + mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') + data = { + 'phone_number': '+447700900855', + 'template_id': str(sample_template_with_placeholders.id), + 'personalisation': {' Name': 'Jo'} + } + if reference: + data.update({"reference": reference}) + auth_header = create_authorization_header(service_id=sample_template_with_placeholders.service_id) - response = client.post( - path='/v2/notifications/sms', - data=json.dumps(data), - headers=[('Content-Type', 'application/json'), auth_header]) - assert response.status_code == 201 - resp_json = json.loads(response.get_data(as_text=True)) - notifications = Notification.query.all() - assert len(notifications) == 1 - notification_id = notifications[0].id - assert resp_json['id'] == str(notification_id) - assert resp_json['reference'] == reference - 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 'v2/notifications/{}'.format(notification_id) in resp_json['uri'] - assert resp_json['template']['id'] == str(sample_template_with_placeholders.id) - assert resp_json['template']['version'] == sample_template_with_placeholders.version - assert 'services/{}/templates/{}'.format(sample_template_with_placeholders.service_id, - sample_template_with_placeholders.id) \ - in resp_json['template']['uri'] - assert not resp_json["scheduled_for"] - assert mocked.called + response = client.post( + path='/v2/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 201 + resp_json = json.loads(response.get_data(as_text=True)) + notifications = Notification.query.all() + assert len(notifications) == 1 + notification_id = notifications[0].id + assert resp_json['id'] == str(notification_id) + assert resp_json['reference'] == reference + 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 'v2/notifications/{}'.format(notification_id) in resp_json['uri'] + assert resp_json['template']['id'] == str(sample_template_with_placeholders.id) + assert resp_json['template']['version'] == sample_template_with_placeholders.version + assert 'services/{}/templates/{}'.format(sample_template_with_placeholders.service_id, + sample_template_with_placeholders.id) \ + in resp_json['template']['uri'] + assert not resp_json["scheduled_for"] + assert mocked.called @pytest.mark.parametrize("notification_type, key_send_to, send_to",