From 6a6db35624e6874e92dda070c89b247f1f0370e7 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Tue, 12 Sep 2023 20:33:10 +0100 Subject: [PATCH] Show an alert when users cancel a job request --- jobserver/views/job_requests.py | 2 ++ .../unit/jobserver/views/test_job_requests.py | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/jobserver/views/job_requests.py b/jobserver/views/job_requests.py index 1361fcebf..c43da9b4e 100644 --- a/jobserver/views/job_requests.py +++ b/jobserver/views/job_requests.py @@ -50,6 +50,8 @@ def post(self, request, *args, **kwargs): job_request.cancelled_actions = actions job_request.save() + messages.success(request, "The requested actions have been cancelled") + return redirect(job_request) diff --git a/tests/unit/jobserver/views/test_job_requests.py b/tests/unit/jobserver/views/test_job_requests.py index 551aca972..eaa524ec6 100644 --- a/tests/unit/jobserver/views/test_job_requests.py +++ b/tests/unit/jobserver/views/test_job_requests.py @@ -66,6 +66,10 @@ def test_jobrequestcancel_success(rf): request = rf.post("/") request.user = user + request.session = "session" + messages = FallbackStorage(request) + request._messages = messages + response = JobRequestCancel.as_view()(request, pk=job_request.pk) assert response.status_code == 302 @@ -76,6 +80,10 @@ def test_jobrequestcancel_success(rf): assert "test2" in job_request.cancelled_actions assert "test3" in job_request.cancelled_actions + messages = list(messages) + assert len(messages) == 1 + assert str(messages[0]) == "The requested actions have been cancelled" + def test_jobrequestcancel_partially_completed(rf): job_request = JobRequestFactory(cancelled_actions=[]) @@ -93,6 +101,10 @@ def test_jobrequestcancel_partially_completed(rf): request = rf.post("/") request.user = user + request.session = "session" + messages = FallbackStorage(request) + request._messages = messages + response = JobRequestCancel.as_view()(request, pk=job_request.pk) assert response.status_code == 302 @@ -101,6 +113,10 @@ def test_jobrequestcancel_partially_completed(rf): job_request.refresh_from_db() assert sorted(job_request.cancelled_actions) == ["test3", "test4"] + messages = list(messages) + assert len(messages) == 1 + assert str(messages[0]) == "The requested actions have been cancelled" + def test_jobrequestcancel_with_job_request_creator(rf): user = UserFactory() @@ -110,6 +126,10 @@ def test_jobrequestcancel_with_job_request_creator(rf): request = rf.post("/") request.user = user + request.session = "session" + messages = FallbackStorage(request) + request._messages = messages + response = JobRequestCancel.as_view()(request, pk=job_request.pk) assert response.status_code == 302 @@ -118,6 +138,10 @@ def test_jobrequestcancel_with_job_request_creator(rf): job_request.refresh_from_db() assert "test1" in job_request.cancelled_actions + messages = list(messages) + assert len(messages) == 1 + assert str(messages[0]) == "The requested actions have been cancelled" + def test_jobrequestcancel_unauthorized(rf): job_request = JobRequestFactory()