mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-24 01:11:38 -05:00
Add an endpoint to cancel a job
If you schedule a job you might change your mind or circumstances might change. So you need to be able to cancel it. This commit adds a `POST` endpoint for individual jobs which sets their status to `cancelled`. This also means adding a new status of `cancelled`, so there’s a migration…
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import requests_mock
|
||||
import pytest
|
||||
import uuid
|
||||
from datetime import (datetime, date)
|
||||
from datetime import (datetime, date, timedelta)
|
||||
|
||||
from flask import current_app
|
||||
|
||||
@@ -288,6 +288,22 @@ def sample_job_with_placeholdered_template(
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_scheduled_job(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=None
|
||||
):
|
||||
return sample_job(
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
service=service,
|
||||
template=sample_template_with_placeholders(notify_db, notify_db_session),
|
||||
scheduled_for=(datetime.utcnow() + timedelta(minutes=60)).isoformat(),
|
||||
job_status='scheduled'
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def sample_email_job(notify_db,
|
||||
notify_db_session,
|
||||
|
||||
@@ -7,6 +7,7 @@ from app.dao.jobs_dao import (
|
||||
dao_update_job,
|
||||
dao_get_jobs_by_service_id,
|
||||
dao_get_scheduled_jobs,
|
||||
dao_get_future_scheduled_job_by_id_and_service_id,
|
||||
dao_get_notification_outcomes_for_job
|
||||
)
|
||||
|
||||
@@ -251,3 +252,10 @@ def test_get_scheduled_jobs_gets_ignores_jobs_scheduled_in_the_future(notify_db,
|
||||
sample_job(notify_db, notify_db_session, scheduled_for=one_minute_in_the_future, job_status='scheduled')
|
||||
jobs = dao_get_scheduled_jobs()
|
||||
assert len(jobs) == 0
|
||||
|
||||
|
||||
def test_get_future_scheduled_job_gets_a_job_yet_to_send(notify_db, notify_db_session):
|
||||
one_hour_from_now = datetime.utcnow() + timedelta(minutes=60)
|
||||
job = sample_job(notify_db, notify_db_session, scheduled_for=one_hour_from_now, job_status='scheduled')
|
||||
result = dao_get_future_scheduled_job_by_id_and_service_id(job.id, job.service_id)
|
||||
assert result.id == job.id
|
||||
|
||||
@@ -109,6 +109,31 @@ def test_get_job_by_id(notify_api, sample_job):
|
||||
assert resp_json['data']['created_by']['name'] == 'Test User'
|
||||
|
||||
|
||||
def test_cancel_job(notify_api, sample_scheduled_job):
|
||||
job_id = str(sample_scheduled_job.id)
|
||||
service_id = sample_scheduled_job.service.id
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
path = '/service/{}/job/{}/cancel'.format(service_id, job_id)
|
||||
auth_header = create_authorization_header(service_id=service_id)
|
||||
response = client.post(path, headers=[auth_header])
|
||||
assert response.status_code == 200
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
assert resp_json['data']['id'] == job_id
|
||||
assert resp_json['data']['job_status'] == 'cancelled'
|
||||
|
||||
|
||||
def test_cant_cancel_normal_job(notify_api, sample_job, mocker):
|
||||
job_id = str(sample_job.id)
|
||||
service_id = sample_job.service.id
|
||||
with notify_api.test_request_context(), notify_api.test_client() as client:
|
||||
mock_update = mocker.patch('app.dao.jobs_dao.dao_update_job')
|
||||
path = '/service/{}/job/{}/cancel'.format(service_id, job_id)
|
||||
auth_header = create_authorization_header(service_id=service_id)
|
||||
response = client.post(path, headers=[auth_header])
|
||||
assert response.status_code == 404
|
||||
assert mock_update.call_count == 0
|
||||
|
||||
|
||||
def test_create_unscheduled_job(notify_api, sample_template, mocker, fake_uuid):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
Reference in New Issue
Block a user