mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 21:48:49 -04:00
test dvla callback update tasks
This commit is contained in:
127
tests/app/celery/test_ftp_update_tasks.py
Normal file
127
tests/app/celery/test_ftp_update_tasks.py
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from freezegun import freeze_time
|
||||||
|
from flask import current_app
|
||||||
|
|
||||||
|
from app.models import (
|
||||||
|
Job,
|
||||||
|
Notification,
|
||||||
|
NOTIFICATION_SENDING,
|
||||||
|
NOTIFICATION_CREATED,
|
||||||
|
NOTIFICATION_TECHNICAL_FAILURE
|
||||||
|
)
|
||||||
|
from app.celery.tasks import (
|
||||||
|
update_job_to_sent_to_dvla,
|
||||||
|
update_dvla_job_to_error,
|
||||||
|
update_letter_notifications_statuses,
|
||||||
|
update_letter_notifications_to_sent_to_dvla,
|
||||||
|
update_letter_notifications_to_error,
|
||||||
|
process_updates_from_file
|
||||||
|
)
|
||||||
|
|
||||||
|
from tests.app.db import create_notification
|
||||||
|
from tests.conftest import set_config
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_job_to_sent_to_dvla(sample_letter_template, sample_letter_job):
|
||||||
|
create_notification(template=sample_letter_template, job=sample_letter_job)
|
||||||
|
create_notification(template=sample_letter_template, job=sample_letter_job)
|
||||||
|
update_job_to_sent_to_dvla(job_id=sample_letter_job.id)
|
||||||
|
|
||||||
|
updated_notifications = Notification.query.all()
|
||||||
|
assert [(n.status == 'sending', n.sent_by == 'dvla') for n in updated_notifications]
|
||||||
|
|
||||||
|
assert Job.query.filter_by(id=sample_letter_job.id).one().job_status == 'sent to dvla'
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_dvla_job_to_error(sample_letter_template, sample_letter_job):
|
||||||
|
create_notification(template=sample_letter_template, job=sample_letter_job)
|
||||||
|
create_notification(template=sample_letter_template, job=sample_letter_job)
|
||||||
|
update_dvla_job_to_error(job_id=sample_letter_job.id)
|
||||||
|
|
||||||
|
updated_notifications = Notification.query.all()
|
||||||
|
for n in updated_notifications:
|
||||||
|
assert n.status == 'created'
|
||||||
|
assert not n.sent_by
|
||||||
|
|
||||||
|
assert Job.query.filter_by(id=sample_letter_job.id).one().job_status == 'error'
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_letter_notifications_statuses_raises_for_invalid_format(notify_api, mocker):
|
||||||
|
invalid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2'
|
||||||
|
mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=invalid_file)
|
||||||
|
|
||||||
|
with pytest.raises(TypeError):
|
||||||
|
update_letter_notifications_statuses(filename='foo.txt')
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_letter_notifications_statuses_calls_with_correct_bucket_location(notify_api, mocker):
|
||||||
|
s3_mock = mocker.patch('app.celery.tasks.s3.get_s3_object')
|
||||||
|
|
||||||
|
with set_config(notify_api, 'NOTIFY_EMAIL_DOMAIN', 'foo.bar'):
|
||||||
|
update_letter_notifications_statuses(filename='foo.txt')
|
||||||
|
s3_mock.assert_called_with('{}-ftp'.format(current_app.config['NOTIFY_EMAIL_DOMAIN']), 'foo.txt')
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_letter_notifications_statuses_builds_updates_from_content(notify_api, mocker):
|
||||||
|
valid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted'
|
||||||
|
mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file)
|
||||||
|
update_mock = mocker.patch('app.celery.tasks.process_updates_from_file')
|
||||||
|
|
||||||
|
update_letter_notifications_statuses(filename='foo.txt')
|
||||||
|
|
||||||
|
update_mock.assert_called_with('ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted')
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_letter_notifications_statuses_builds_updates_list(notify_api, mocker):
|
||||||
|
valid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted'
|
||||||
|
updates = process_updates_from_file(valid_file)
|
||||||
|
|
||||||
|
assert len(updates) == 2
|
||||||
|
|
||||||
|
assert updates[0].reference == 'ref-foo'
|
||||||
|
assert updates[0].status == 'Sent'
|
||||||
|
assert updates[0].page_count == '1'
|
||||||
|
assert updates[0].cost_threshold == 'Unsorted'
|
||||||
|
|
||||||
|
assert updates[1].reference == 'ref-bar'
|
||||||
|
assert updates[1].status == 'Sent'
|
||||||
|
assert updates[1].page_count == '2'
|
||||||
|
assert updates[1].cost_threshold == 'Sorted'
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references(
|
||||||
|
client,
|
||||||
|
sample_letter_template
|
||||||
|
):
|
||||||
|
first = create_notification(sample_letter_template)
|
||||||
|
second = create_notification(sample_letter_template)
|
||||||
|
|
||||||
|
dt = datetime.utcnow()
|
||||||
|
with freeze_time(dt):
|
||||||
|
update_letter_notifications_to_sent_to_dvla([first.reference])
|
||||||
|
|
||||||
|
assert first.status == NOTIFICATION_SENDING
|
||||||
|
assert first.sent_by == 'dvla'
|
||||||
|
assert first.sent_at == dt
|
||||||
|
assert first.updated_at == dt
|
||||||
|
assert second.status == NOTIFICATION_CREATED
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_letter_notifications_to_error_updates_based_on_notification_references(
|
||||||
|
client,
|
||||||
|
sample_letter_template
|
||||||
|
):
|
||||||
|
first = create_notification(sample_letter_template)
|
||||||
|
second = create_notification(sample_letter_template)
|
||||||
|
|
||||||
|
dt = datetime.utcnow()
|
||||||
|
with freeze_time(dt):
|
||||||
|
update_letter_notifications_to_error([first.reference])
|
||||||
|
|
||||||
|
assert first.status == NOTIFICATION_TECHNICAL_FAILURE
|
||||||
|
assert first.sent_by is None
|
||||||
|
assert first.sent_at is None
|
||||||
|
assert first.updated_at == dt
|
||||||
|
assert second.status == NOTIFICATION_CREATED
|
||||||
@@ -741,6 +741,7 @@ def test_run_letter_api_notifications_triggers_ftp_task(client, mocker, sample_l
|
|||||||
queue=QueueNames.PROCESS_FTP
|
queue=QueueNames.PROCESS_FTP
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_run_letter_api_notifications_does_nothing_if_no_created_notifications(
|
def test_run_letter_api_notifications_does_nothing_if_no_created_notifications(
|
||||||
client,
|
client,
|
||||||
mocker,
|
mocker,
|
||||||
|
|||||||
@@ -25,9 +25,6 @@ from app.celery.tasks import (
|
|||||||
send_email,
|
send_email,
|
||||||
persist_letter,
|
persist_letter,
|
||||||
get_template_class,
|
get_template_class,
|
||||||
update_job_to_sent_to_dvla,
|
|
||||||
update_letter_notifications_statuses,
|
|
||||||
process_updates_from_file,
|
|
||||||
send_inbound_sms_to_service)
|
send_inbound_sms_to_service)
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.dao import jobs_dao, services_dao
|
from app.dao import jobs_dao, services_dao
|
||||||
@@ -39,11 +36,9 @@ from app.models import (
|
|||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
EMAIL_TYPE,
|
EMAIL_TYPE,
|
||||||
LETTER_TYPE,
|
LETTER_TYPE,
|
||||||
Job,
|
Job)
|
||||||
JOB_STATUS_ERROR)
|
|
||||||
|
|
||||||
from tests.app import load_example_csv
|
from tests.app import load_example_csv
|
||||||
from tests.conftest import set_config
|
|
||||||
from tests.app.conftest import (
|
from tests.app.conftest import (
|
||||||
sample_service as create_sample_service,
|
sample_service as create_sample_service,
|
||||||
sample_template as create_sample_template,
|
sample_template as create_sample_template,
|
||||||
@@ -642,7 +637,7 @@ def test_should_update_job_to_sent_to_dvla_in_research_mode_for_letter_job(
|
|||||||
A1,A2,A3,A4,A_POST,Alice
|
A1,A2,A3,A4,A_POST,Alice
|
||||||
"""
|
"""
|
||||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=csv)
|
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=csv)
|
||||||
mocker.patch('app.celery.tasks.update_job_to_sent_to_dvla.apply_async')
|
mock_update_job_task = mocker.patch('app.celery.tasks.update_job_to_sent_to_dvla.apply_async')
|
||||||
mocker.patch('app.celery.tasks.persist_letter.apply_async')
|
mocker.patch('app.celery.tasks.persist_letter.apply_async')
|
||||||
mocker.patch('app.celery.tasks.create_uuid', return_value=fake_uuid)
|
mocker.patch('app.celery.tasks.create_uuid', return_value=fake_uuid)
|
||||||
mocker.patch('app.celery.tasks.encryption.encrypt', return_value=test_encrypted_data)
|
mocker.patch('app.celery.tasks.encryption.encrypt', return_value=test_encrypted_data)
|
||||||
@@ -662,7 +657,7 @@ def test_should_update_job_to_sent_to_dvla_in_research_mode_for_letter_job(
|
|||||||
queue=QueueNames.RESEARCH_MODE
|
queue=QueueNames.RESEARCH_MODE
|
||||||
)
|
)
|
||||||
|
|
||||||
update_job_to_sent_to_dvla.apply_async.assert_called_once_with(
|
mock_update_job_task.assert_called_once_with(
|
||||||
[str(job.id)], queue=QueueNames.RESEARCH_MODE)
|
[str(job.id)], queue=QueueNames.RESEARCH_MODE)
|
||||||
|
|
||||||
|
|
||||||
@@ -1114,73 +1109,6 @@ def test_dvla_letter_template(sample_letter_notification):
|
|||||||
assert str(letter) == "140|500|001||random-string|||||||||||||A1||A2|A3|A4|A5|A6|A_POST|||||||||23 March 2017<cr><cr><h1>Template subject<normal><cr><cr>Dear Sir/Madam, Hello. Yours Truly, The Government.<cr><cr>" # noqa
|
assert str(letter) == "140|500|001||random-string|||||||||||||A1||A2|A3|A4|A5|A6|A_POST|||||||||23 March 2017<cr><cr><h1>Template subject<normal><cr><cr>Dear Sir/Madam, Hello. Yours Truly, The Government.<cr><cr>" # noqa
|
||||||
|
|
||||||
|
|
||||||
def test_update_job_to_sent_to_dvla(sample_letter_template, sample_letter_job):
|
|
||||||
create_notification(template=sample_letter_template, job=sample_letter_job)
|
|
||||||
create_notification(template=sample_letter_template, job=sample_letter_job)
|
|
||||||
update_job_to_sent_to_dvla(job_id=sample_letter_job.id)
|
|
||||||
|
|
||||||
updated_notifications = Notification.query.all()
|
|
||||||
assert [(n.status == 'sending', n.sent_by == 'dvla') for n in updated_notifications]
|
|
||||||
|
|
||||||
assert 'sent to dvla' == Job.query.filter_by(id=sample_letter_job.id).one().job_status
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_dvla_job_to_error(sample_letter_template, sample_letter_job):
|
|
||||||
create_notification(template=sample_letter_template, job=sample_letter_job)
|
|
||||||
create_notification(template=sample_letter_template, job=sample_letter_job)
|
|
||||||
update_dvla_job_to_error(job_id=sample_letter_job.id)
|
|
||||||
|
|
||||||
updated_notifications = Notification.query.all()
|
|
||||||
for n in updated_notifications:
|
|
||||||
assert n.status == 'created'
|
|
||||||
assert not n.sent_by
|
|
||||||
|
|
||||||
assert 'error' == Job.query.filter_by(id=sample_letter_job.id).one().job_status
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_letter_notifications_statuses_raises_for_invalid_format(notify_api, mocker):
|
|
||||||
invalid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2'
|
|
||||||
mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=invalid_file)
|
|
||||||
|
|
||||||
with pytest.raises(TypeError):
|
|
||||||
update_letter_notifications_statuses(filename='foo.txt')
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_letter_notifications_statuses_calls_with_correct_bucket_location(notify_api, mocker):
|
|
||||||
s3_mock = mocker.patch('app.celery.tasks.s3.get_s3_object')
|
|
||||||
|
|
||||||
with set_config(notify_api, 'NOTIFY_EMAIL_DOMAIN', 'foo.bar'):
|
|
||||||
update_letter_notifications_statuses(filename='foo.txt')
|
|
||||||
s3_mock.assert_called_with('{}-ftp'.format(current_app.config['NOTIFY_EMAIL_DOMAIN']), 'foo.txt')
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_letter_notifications_statuses_builds_updates_from_content(notify_api, mocker):
|
|
||||||
valid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted'
|
|
||||||
mocker.patch('app.celery.tasks.s3.get_s3_file', return_value=valid_file)
|
|
||||||
update_mock = mocker.patch('app.celery.tasks.process_updates_from_file')
|
|
||||||
|
|
||||||
update_letter_notifications_statuses(filename='foo.txt')
|
|
||||||
|
|
||||||
update_mock.assert_called_with('ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted')
|
|
||||||
|
|
||||||
|
|
||||||
def test_update_letter_notifications_statuses_builds_updates_list(notify_api, mocker):
|
|
||||||
valid_file = 'ref-foo|Sent|1|Unsorted\nref-bar|Sent|2|Sorted'
|
|
||||||
updates = process_updates_from_file(valid_file)
|
|
||||||
|
|
||||||
assert len(updates) == 2
|
|
||||||
|
|
||||||
assert updates[0].reference == 'ref-foo'
|
|
||||||
assert updates[0].status == 'Sent'
|
|
||||||
assert updates[0].page_count == '1'
|
|
||||||
assert updates[0].cost_threshold == 'Unsorted'
|
|
||||||
|
|
||||||
assert updates[1].reference == 'ref-bar'
|
|
||||||
assert updates[1].status == 'Sent'
|
|
||||||
assert updates[1].page_count == '2'
|
|
||||||
assert updates[1].cost_threshold == 'Sorted'
|
|
||||||
|
|
||||||
|
|
||||||
def test_send_inbound_sms_to_service_post_https_request_to_service(notify_api, sample_service):
|
def test_send_inbound_sms_to_service_post_https_request_to_service(notify_api, sample_service):
|
||||||
inbound_api = create_service_inbound_api(service=sample_service, url="https://some.service.gov.uk/",
|
inbound_api = create_service_inbound_api(service=sample_service, url="https://some.service.gov.uk/",
|
||||||
bearer_token="something_unique")
|
bearer_token="something_unique")
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from app import db
|
from app import db, create_random_identifier
|
||||||
from app.dao.jobs_dao import dao_create_job
|
from app.dao.jobs_dao import dao_create_job
|
||||||
from app.dao.service_inbound_api_dao import save_service_inbound_api
|
from app.dao.service_inbound_api_dao import save_service_inbound_api
|
||||||
from app.models import (
|
from app.models import (
|
||||||
@@ -22,6 +22,7 @@ from app.models import (
|
|||||||
InboundNumber,
|
InboundNumber,
|
||||||
Organisation,
|
Organisation,
|
||||||
EMAIL_TYPE,
|
EMAIL_TYPE,
|
||||||
|
LETTER_TYPE,
|
||||||
SMS_TYPE,
|
SMS_TYPE,
|
||||||
INBOUND_SMS_TYPE,
|
INBOUND_SMS_TYPE,
|
||||||
KEY_TYPE_NORMAL,
|
KEY_TYPE_NORMAL,
|
||||||
@@ -142,6 +143,9 @@ def create_notification(
|
|||||||
if not api_key:
|
if not api_key:
|
||||||
api_key = create_api_key(template.service, key_type=key_type)
|
api_key = create_api_key(template.service, key_type=key_type)
|
||||||
|
|
||||||
|
if template.template_type == LETTER_TYPE and reference is None:
|
||||||
|
reference = create_random_identifier()
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'id': uuid.uuid4(),
|
'id': uuid.uuid4(),
|
||||||
'to': to_field,
|
'to': to_field,
|
||||||
|
|||||||
Reference in New Issue
Block a user