diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index ef35206f5..d26b5a647 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -12,16 +12,18 @@ from sqlalchemy import ( from app import db from app.dao.dao_utils import transactional +from app.dao.templates_dao import dao_get_template_by_id from app.utils import midnight_n_days_ago + from app.models import ( Job, + JOB_STATUS_FINISHED, JOB_STATUS_PENDING, JOB_STATUS_SCHEDULED, LETTER_TYPE, Notification, Template, ServiceDataRetention, - NOTIFICATION_SENDING, NOTIFICATION_CREATED, NOTIFICATION_CANCELLED, JOB_STATUS_CANCELLED @@ -169,8 +171,11 @@ def dao_cancel_letter_job(job): def can_cancel_letter_job(job): - # assert is a letter job - # assert job status == finished??? + template = dao_get_template_by_id(job.template_id) + if template.template_type != LETTER_TYPE: + return False + if job.job_status != JOB_STATUS_FINISHED: + return False # Notifications are not in pending-virus-check count_notifications = Notification.query.filter( Notification.job_id == job.id, @@ -179,6 +184,3 @@ def can_cancel_letter_job(job): if count_notifications != job.notification_count: return False return letter_can_be_cancelled(NOTIFICATION_CREATED, job.created_at) - - - diff --git a/tests/app/dao/test_jobs_dao.py b/tests/app/dao/test_jobs_dao.py index 731ae959a..96e23d238 100644 --- a/tests/app/dao/test_jobs_dao.py +++ b/tests/app/dao/test_jobs_dao.py @@ -311,24 +311,55 @@ def assert_job_stat(job, result, sent, delivered, failed): assert result.failed == failed +@freeze_time('2019-06-13 13:00') +def test_dao_cancel_letter_job_updates_notifications_and_job_to_cancelled(sample_letter_template): + job = create_job(template=sample_letter_template, notification_count=1, job_status='finished') + notification = create_notification(template=job.template, job=job, status='created') + assert dao_cancel_letter_job(job) == 1 + assert notification.status == 'cancelled' + assert job.job_status == 'cancelled' + + +@freeze_time('2019-06-13 13:00') def test_dao_cancel_letter_job_does_not_allow_cancel_if_notification_in_sending(sample_letter_template): - job = create_job(template=sample_letter_template, notification_count=2) - create_notification(template=job.template, job=job, status='sending') + job = create_job(template=sample_letter_template, notification_count=2, job_status='finished') + letter_1 = create_notification(template=job.template, job=job, status='sending') + letter_2 = create_notification(template=job.template, job=job, status='created') + assert not dao_cancel_letter_job(job) + assert letter_1.status == 'sending' + assert letter_2.status == 'created' + assert job.job_status == 'finished' + + +def test_dao_cancel_letter_job_does_not_allow_cancel_if_letters_already_sent_to_dvla(sample_letter_template): + with freeze_time('2019-06-13 13:00'): + job = create_job(template=sample_letter_template, notification_count=1, job_status='finished') + letter = create_notification(template=job.template, job=job, status='created') + + with freeze_time('2019-06-13 17:32'): + assert not dao_cancel_letter_job(job) + assert letter.status == 'created' + assert job.job_status == 'finished' + + +@freeze_time('2019-06-13 13:00') +def test_dao_cancel_letter_job_does_not_allow_cancel_if_not_a_letter_job(sample_template): + job = create_job(template=sample_template, notification_count=1, job_status='finished') + notification = create_notification(template=job.template, job=job, status='created') + assert not dao_cancel_letter_job(job) + assert notification.status == 'created' + assert job.job_status == 'finished' + + +@freeze_time('2019-06-13 13:00') +def test_dao_cancel_letter_job_does_not_allow_cancel_if_job_not_finished(sample_letter_template): + job = create_job(template=sample_letter_template, notification_count=1, job_status="finished") create_notification(template=job.template, job=job, status='created') assert not dao_cancel_letter_job(job) -def test_dao_cancel_letter_job_updates_notifications_and_job_to_cancelled(sample_job): - notification = create_notification(template=sample_job.template, job=sample_job, status='created') - assert dao_cancel_letter_job(sample_job) == 1 - assert notification.status == 'cancelled' - assert sample_job.job_status == 'cancelled' - - -def test_dao_cancel_letter_job_returns_false_if_too_late(): - pass - - -def test_dao_cancel_letter_returns_false_if_not_a_letter_job(): - pass - +@freeze_time('2019-06-13 13:00') +def test_dao_cancel_letter_job_does_not_allow_cancel_if_notifications_not_in_db_yet(sample_letter_template): + job = create_job(template=sample_letter_template, notification_count=2, job_status='finished') + create_notification(template=job.template, job=job, status='created') + assert not dao_cancel_letter_job(job)