More checks and tests for cancelling a letter job

This commit is contained in:
Pea Tyczynska
2019-06-13 13:41:31 +01:00
parent 663b260777
commit cc966b1cf0
2 changed files with 55 additions and 22 deletions

View File

@@ -12,16 +12,18 @@ from sqlalchemy import (
from app import db from app import db
from app.dao.dao_utils import transactional 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.utils import midnight_n_days_ago
from app.models import ( from app.models import (
Job, Job,
JOB_STATUS_FINISHED,
JOB_STATUS_PENDING, JOB_STATUS_PENDING,
JOB_STATUS_SCHEDULED, JOB_STATUS_SCHEDULED,
LETTER_TYPE, LETTER_TYPE,
Notification, Notification,
Template, Template,
ServiceDataRetention, ServiceDataRetention,
NOTIFICATION_SENDING,
NOTIFICATION_CREATED, NOTIFICATION_CREATED,
NOTIFICATION_CANCELLED, NOTIFICATION_CANCELLED,
JOB_STATUS_CANCELLED JOB_STATUS_CANCELLED
@@ -169,8 +171,11 @@ def dao_cancel_letter_job(job):
def can_cancel_letter_job(job): def can_cancel_letter_job(job):
# assert is a letter job template = dao_get_template_by_id(job.template_id)
# assert job status == finished??? if template.template_type != LETTER_TYPE:
return False
if job.job_status != JOB_STATUS_FINISHED:
return False
# Notifications are not in pending-virus-check # Notifications are not in pending-virus-check
count_notifications = Notification.query.filter( count_notifications = Notification.query.filter(
Notification.job_id == job.id, Notification.job_id == job.id,
@@ -179,6 +184,3 @@ def can_cancel_letter_job(job):
if count_notifications != job.notification_count: if count_notifications != job.notification_count:
return False return False
return letter_can_be_cancelled(NOTIFICATION_CREATED, job.created_at) return letter_can_be_cancelled(NOTIFICATION_CREATED, job.created_at)

View File

@@ -311,24 +311,55 @@ def assert_job_stat(job, result, sent, delivered, failed):
assert result.failed == 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): 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) job = create_job(template=sample_letter_template, notification_count=2, job_status='finished')
create_notification(template=job.template, job=job, status='sending') 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') create_notification(template=job.template, job=job, status='created')
assert not dao_cancel_letter_job(job) assert not dao_cancel_letter_job(job)
def test_dao_cancel_letter_job_updates_notifications_and_job_to_cancelled(sample_job): @freeze_time('2019-06-13 13:00')
notification = create_notification(template=sample_job.template, job=sample_job, status='created') def test_dao_cancel_letter_job_does_not_allow_cancel_if_notifications_not_in_db_yet(sample_letter_template):
assert dao_cancel_letter_job(sample_job) == 1 job = create_job(template=sample_letter_template, notification_count=2, job_status='finished')
assert notification.status == 'cancelled' create_notification(template=job.template, job=job, status='created')
assert sample_job.job_status == 'cancelled' assert not dao_cancel_letter_job(job)
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