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/jobserver/views/jobs.py b/jobserver/views/jobs.py index b067ec6c0..859c640b1 100644 --- a/jobserver/views/jobs.py +++ b/jobserver/views/jobs.py @@ -1,5 +1,6 @@ import os +from django.contrib import messages from django.core.exceptions import MultipleObjectsReturned from django.http import Http404 from django.shortcuts import get_object_or_404, redirect @@ -27,6 +28,11 @@ def post(self, request, *args, **kwargs): job.job_request.cancelled_actions.append(job.action) job.job_request.save() + + messages.success( + request, f'Your request to cancel "{job.action}" was successful' + ) + return redirect(job) 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() diff --git a/tests/unit/jobserver/views/test_jobs.py b/tests/unit/jobserver/views/test_jobs.py index 55db0c448..8145eb15f 100644 --- a/tests/unit/jobserver/views/test_jobs.py +++ b/tests/unit/jobserver/views/test_jobs.py @@ -1,5 +1,6 @@ import pytest from django.contrib.auth.models import AnonymousUser +from django.contrib.messages.storage.fallback import FallbackStorage from django.http import Http404 from django.utils import timezone @@ -69,6 +70,10 @@ def test_jobcancel_success(rf): request = rf.post("/") request.user = user + request.session = "session" + messages = FallbackStorage(request) + request._messages = messages + response = JobCancel.as_view()(request, identifier=job.identifier) assert response.status_code == 302 @@ -77,6 +82,10 @@ def test_jobcancel_success(rf): job_request.refresh_from_db() assert job_request.cancelled_actions == ["test"] + messages = list(messages) + assert len(messages) == 1 + assert str(messages[0]) == 'Your request to cancel "test" was successful' + def test_jobcancel_with_job_creator(rf): user = UserFactory() @@ -86,6 +95,10 @@ def test_jobcancel_with_job_creator(rf): request = rf.post("/") request.user = user + request.session = "session" + messages = FallbackStorage(request) + request._messages = messages + response = JobCancel.as_view()(request, identifier=job.identifier) assert response.status_code == 302 @@ -94,6 +107,10 @@ def test_jobcancel_with_job_creator(rf): job_request.refresh_from_db() assert job_request.cancelled_actions == ["test"] + messages = list(messages) + assert len(messages) == 1 + assert str(messages[0]) == 'Your request to cancel "test" was successful' + def test_jobcancel_without_permission(rf, user): job = JobFactory(job_request=JobRequestFactory())