From b1928b928cc6a5e4897bf008c3f260e72d2dbcc0 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 26 Sep 2017 11:28:54 +0100 Subject: [PATCH] update process_letter tests --- app/dao/notifications_dao.py | 2 +- app/v2/notifications/post_notifications.py | 1 - .../test_process_letter_notifications.py | 58 +++---------------- .../test_post_letter_notifications.py | 48 +++++++-------- 4 files changed, 34 insertions(+), 75 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 38f48e265..f7b15b4dc 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -592,7 +592,7 @@ def dao_set_created_live_letter_api_notifications_to_pending(): Notification.notification_type == LETTER_TYPE, Notification.status == NOTIFICATION_CREATED, Notification.key_type == KEY_TYPE_NORMAL, - Notification.api_key != None + Notification.api_key != None # noqa ).with_for_update( ).all() diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index b449b64ec..b3cf1c0f8 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -43,7 +43,6 @@ from app.v2.notifications.create_response import ( create_post_email_response_from_notification, create_post_letter_response_from_notification ) -from app.variables import LETTER_TEST_API_FILENAME @v2_notification_blueprint.route('/', methods=['POST']) diff --git a/tests/app/notifications/test_process_letter_notifications.py b/tests/app/notifications/test_process_letter_notifications.py index aaf43c4db..24f37c06f 100644 --- a/tests/app/notifications/test_process_letter_notifications.py +++ b/tests/app/notifications/test_process_letter_notifications.py @@ -1,52 +1,10 @@ -import pytest - -from app.dao.services_dao import dao_archive_service -from app.models import Job -from app.models import JOB_STATUS_READY_TO_SEND from app.models import LETTER_TYPE from app.models import Notification +from app.models import NOTIFICATION_CREATED from app.notifications.process_letter_notifications import create_letter_notification -from app.v2.errors import InvalidRequest -from app.variables import LETTER_API_FILENAME - -from tests.app.db import create_service -from tests.app.db import create_template -def test_create_job_rejects_inactive_service(notify_db_session): - service = create_service() - template = create_template(service, template_type=LETTER_TYPE) - dao_archive_service(service.id) - - with pytest.raises(InvalidRequest) as exc_info: - create_letter_api_job(template) - - assert exc_info.value.message == 'Service {} is inactive'.format(service.id) - - -def test_create_job_rejects_archived_template(sample_letter_template): - sample_letter_template.archived = True - - with pytest.raises(InvalidRequest) as exc_info: - create_letter_api_job(sample_letter_template) - - assert exc_info.value.message == 'Template {} is deleted'.format(sample_letter_template.id) - - -def test_create_job_creates_job(sample_letter_template): - job = create_letter_api_job(sample_letter_template) - - assert job == Job.query.one() - assert job.original_file_name == LETTER_API_FILENAME - assert job.service == sample_letter_template.service - assert job.template_id == sample_letter_template.id - assert job.template_version == sample_letter_template.version - assert job.notification_count == 1 - assert job.job_status == JOB_STATUS_READY_TO_SEND - assert job.created_by is None - - -def test_create_letter_notification_creates_notification(sample_letter_job, sample_api_key): +def test_create_letter_notification_creates_notification(sample_letter_template, sample_api_key): data = { 'personalisation': { 'address_line_1': 'The Queen', @@ -55,20 +13,20 @@ def test_create_letter_notification_creates_notification(sample_letter_job, samp } } - notification = create_letter_notification(data, sample_letter_job, sample_api_key) + notification = create_letter_notification(data, sample_letter_template, sample_api_key, NOTIFICATION_CREATED) assert notification == Notification.query.one() - assert notification.job == sample_letter_job - assert notification.template == sample_letter_job.template + assert notification.job is None + assert notification.status == NOTIFICATION_CREATED + assert notification.template == sample_letter_template assert notification.api_key == sample_api_key assert notification.notification_type == LETTER_TYPE assert notification.key_type == sample_api_key.key_type - assert notification.job_row_number == 0 assert notification.reference is not None assert notification.client_reference is None -def test_create_letter_notification_sets_reference(sample_letter_job, sample_api_key): +def test_create_letter_notification_sets_reference(sample_letter_template, sample_api_key): data = { 'personalisation': { 'address_line_1': 'The Queen', @@ -78,6 +36,6 @@ def test_create_letter_notification_sets_reference(sample_letter_job, sample_api 'reference': 'foo' } - notification = create_letter_notification(data, sample_letter_job, sample_api_key) + notification = create_letter_notification(data, sample_letter_template, sample_api_key, NOTIFICATION_CREATED) assert notification.client_reference == 'foo' diff --git a/tests/app/v2/notifications/test_post_letter_notifications.py b/tests/app/v2/notifications/test_post_letter_notifications.py index b46f093ca..ccfa39b2f 100644 --- a/tests/app/v2/notifications/test_post_letter_notifications.py +++ b/tests/app/v2/notifications/test_post_letter_notifications.py @@ -11,12 +11,11 @@ from app.models import KEY_TYPE_TEAM from app.models import KEY_TYPE_TEST from app.models import LETTER_TYPE from app.models import Notification +from app.models import NOTIFICATION_SENDING from app.models import SMS_TYPE from app.schema_validation import validate from app.v2.errors import RateLimitError from app.v2.notifications.notification_schemas import post_letter_response -from app.variables import LETTER_TEST_API_FILENAME -from app.variables import LETTER_API_FILENAME from tests import create_authorization_header from tests.app.db import create_service, create_template @@ -45,7 +44,6 @@ def letter_request(client, data, service_id, key_type=KEY_TYPE_NORMAL, _expected @pytest.mark.parametrize('reference', [None, 'reference_from_client']) def test_post_letter_notification_returns_201(client, sample_letter_template, mocker, reference): - mocked = mocker.patch('app.celery.tasks.build_dvla_file.apply_async') data = { 'template_id': str(sample_letter_template.id), 'personalisation': { @@ -63,8 +61,7 @@ def test_post_letter_notification_returns_201(client, sample_letter_template, mo resp_json = letter_request(client, data, service_id=sample_letter_template.service_id) assert validate(resp_json, post_letter_response) == resp_json - job = Job.query.one() - assert job.original_file_name == LETTER_API_FILENAME + assert Job.query.count() == 0 notification = Notification.query.one() notification_id = notification.id assert resp_json['id'] == str(notification_id) @@ -82,8 +79,6 @@ def test_post_letter_notification_returns_201(client, sample_letter_template, mo ) assert not resp_json['scheduled_for'] - mocked.assert_called_once_with([str(job.id)], queue='job-tasks') - def test_post_letter_notification_returns_400_and_missing_template( client, @@ -220,15 +215,14 @@ def test_post_letter_notification_returns_403_if_not_allowed_to_send_notificatio (True, KEY_TYPE_NORMAL), (False, KEY_TYPE_TEST) ]) -def test_post_letter_notification_doesnt_queue_task( +def test_post_letter_notification_queues_success( client, notify_db_session, mocker, research_mode, key_type ): - real_task = mocker.patch('app.celery.tasks.build_dvla_file.apply_async') - fake_task = mocker.patch('app.celery.tasks.update_job_to_sent_to_dvla.apply_async') + fake_task = mocker.patch('app.celery.tasks.update_letter_notifications_to_sent_to_dvla.apply_async') service = create_service(research_mode=research_mode, service_permissions=[LETTER_TYPE]) template = create_template(service, template_type=LETTER_TYPE) @@ -240,10 +234,12 @@ def test_post_letter_notification_doesnt_queue_task( letter_request(client, data, service_id=service.id, key_type=key_type) - job = Job.query.one() - assert job.original_file_name == LETTER_TEST_API_FILENAME - assert not real_task.called - fake_task.assert_called_once_with([str(job.id)], queue='research-mode-tasks') + notification = Notification.query.one() + assert notification.status == NOTIFICATION_SENDING + fake_task.assert_called_once_with( + kwargs={'notification_references': [notification.reference]}, + queue='research-mode-tasks' + ) def test_post_letter_notification_doesnt_accept_team_key(client, sample_letter_template): @@ -282,17 +278,23 @@ def test_post_letter_notification_doesnt_send_in_trial(client, sample_trial_lett {'error': 'BadRequestError', 'message': 'Cannot send letters when service is in trial mode'}] -def test_post_letter_notification_calls_update_job_sent_to_dvla_when_service_is_in_trial_mode_but_using_test_key( - client, sample_trial_letter_template, mocker): - build_dvla_task = mocker.patch('app.celery.tasks.build_dvla_file.apply_async') - update_job_task = mocker.patch('app.celery.tasks.update_job_to_sent_to_dvla.apply_async') +def test_post_letter_notification_fakes_dvla_when_service_is_in_trial_mode_but_using_test_key( + client, + sample_trial_letter_template, + mocker +): + update_task = mocker.patch('app.celery.tasks.update_letter_notifications_to_sent_to_dvla.apply_async') data = { "template_id": sample_trial_letter_template.id, "personalisation": {'address_line_1': 'Foo', 'address_line_2': 'Bar', 'postcode': 'Baz'} } - letter_request(client, data=data, service_id=sample_trial_letter_template.service_id, - key_type=KEY_TYPE_TEST) - job = Job.query.one() - update_job_task.assert_called_once_with([str(job.id)], queue='research-mode-tasks') - assert not build_dvla_task.called + + letter_request(client, data=data, service_id=sample_trial_letter_template.service_id, key_type=KEY_TYPE_TEST) + + notification = Notification.query.one() + assert notification.status == NOTIFICATION_SENDING + update_task.assert_called_once_with( + kwargs={'notification_references': [notification.reference]}, + queue='research-mode-tasks' + )