Skip to content

Commit

Permalink
Show an alert when users cancel an action
Browse files Browse the repository at this point in the history
  • Loading branch information
ghickman committed Sep 12, 2023
1 parent 6a6db35 commit 9a82ded
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jobserver/views/jobs.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)


Expand Down
17 changes: 17 additions & 0 deletions tests/unit/jobserver/views/test_jobs.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 9a82ded

Please sign in to comment.