update process_letter tests

This commit is contained in:
Leo Hemsted
2017-09-26 11:28:54 +01:00
parent f3db920c71
commit b1928b928c
4 changed files with 34 additions and 75 deletions

View File

@@ -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'
)