Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024-12-06 | MAIN --> PROD | DEV (d7b9752) --> STAGING #4520

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions backend/audit/test_verify_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from .models import (
Access,
SingleAuditChecklist,
User,
)
from audit.models.models import STATUS
from audit.views.views import verify_status

from django.core.exceptions import PermissionDenied
from django.test import TestCase
from django.urls import reverse

from model_bakery import baker
from unittest.mock import Mock


class TestVerifyStatus(TestCase):
def _test_verify_status(self, sac_status, required_status, sac_exists):
"""
Helper to test the behavior of a method decorated with verify_status
sac_status - Current status of the sac
required_status - The required status to be provided to the decorator
"""
user = baker.make(User)
self.client.force_login(user)

if sac_exists:
sac = baker.make(SingleAuditChecklist, submission_status=sac_status)
baker.make(Access, user=user, sac=sac, role="editor")

# Set up a fake method to decorate
decorator = verify_status(required_status)
func = Mock()
mock_view = Mock()
mock_request = Mock()
decorated_func = decorator(func)

if sac_exists:
result = decorated_func(mock_view, mock_request, report_id=sac.report_id)

if sac_status == required_status:
# The decorated method should be called as normal if the statuses match
func.assert_called()
else:
# The decorated method should redirect if the statuses don't match
result.client = self.client
self.assertRedirects(
result,
reverse("audit:SubmissionProgress", args=[sac.report_id]),
)
else:
with self.assertRaises(PermissionDenied):
decorated_func(mock_view, mock_request, report_id="bad_report_id")

def test_valid(self):
"""
Current and required statuses matching should pass
"""
self._test_verify_status(
sac_status=STATUS.IN_PROGRESS,
required_status=STATUS.IN_PROGRESS,
sac_exists=True,
)

def test_invalid(self):
"""
Current and required statuses differing should pass
"""
self._test_verify_status(
sac_status=STATUS.IN_PROGRESS,
required_status=STATUS.AUDITEE_CERTIFIED,
sac_exists=True,
)

def test_no_sac(self):
"""
Current and required statuses differing should throw
"""
self._test_verify_status(
sac_status=STATUS.IN_PROGRESS,
required_status=STATUS.AUDITEE_CERTIFIED,
sac_exists=False,
)
60 changes: 44 additions & 16 deletions backend/audit/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ def _friendly_status(status):
return dict(SingleAuditChecklist.STATUS_CHOICES)[status]


def verify_status(status):
"""
Decorator to be applied to view request methods (i.e. get, post) to verify
that the submission is in the correct state before allowing the user to
proceed. An incorrect status usually happens via direct URL access. If the
given status does not match the submission's, it will redirect them back to
the submission progress page.
"""

def decorator_verify_status(request_method):
def verify(view, request, *args, **kwargs):
report_id = kwargs["report_id"]

try:
sac = SingleAuditChecklist.objects.get(report_id=report_id)
except SingleAuditChecklist.DoesNotExist:
raise PermissionDenied("You do not have access to this audit.")

# Return to checklist, the Audit is not in the correct state.
if sac.submission_status != status:
logger.warning(
f"Expected submission status {status} but it's currently {sac.submission_status}"
)
return redirect(f"/audit/submission-progress/{sac.report_id}")
else:
return request_method(view, request, *args, **kwargs)

return verify

return decorator_verify_status


class MySubmissions(LoginRequiredMixin, generic.View):
redirect_field_name = "Home"

Expand Down Expand Up @@ -345,6 +377,7 @@ def post(self, request, *args, **kwargs):


class AuditorCertificationStep1View(CertifyingAuditorRequiredMixin, generic.View):
@verify_status(STATUS.READY_FOR_CERTIFICATION)
def get(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand Down Expand Up @@ -373,6 +406,7 @@ def get(self, request, *args, **kwargs):
except SingleAuditChecklist.DoesNotExist:
raise PermissionDenied("You do not have access to this audit.")

@verify_status(STATUS.READY_FOR_CERTIFICATION)
def post(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand Down Expand Up @@ -410,6 +444,7 @@ def post(self, request, *args, **kwargs):


class AuditorCertificationStep2View(CertifyingAuditorRequiredMixin, generic.View):
@verify_status(STATUS.READY_FOR_CERTIFICATION)
def get(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand Down Expand Up @@ -447,6 +482,7 @@ def get(self, request, *args, **kwargs):
except SingleAuditChecklist.DoesNotExist:
raise PermissionDenied("You do not have access to this audit.")

@verify_status(STATUS.READY_FOR_CERTIFICATION)
def post(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand Down Expand Up @@ -496,6 +532,7 @@ def post(self, request, *args, **kwargs):


class AuditeeCertificationStep1View(CertifyingAuditeeRequiredMixin, generic.View):
@verify_status(STATUS.AUDITOR_CERTIFIED)
def get(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand All @@ -515,15 +552,12 @@ def get(self, request, *args, **kwargs):
"form": form,
}

# Return to checklist, the Audit is not in the correct state.
if sac.submission_status != STATUS.AUDITOR_CERTIFIED:
return redirect(f"/audit/submission-progress/{sac.report_id}")

return render(request, "audit/auditee-certification-step-1.html", context)

except SingleAuditChecklist.DoesNotExist:
raise PermissionDenied("You do not have access to this audit.")

@verify_status(STATUS.AUDITOR_CERTIFIED)
def post(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand All @@ -542,10 +576,6 @@ def post(self, request, *args, **kwargs):
"submission_status": sac.submission_status,
}

# Return to checklist, the Audit is not in the correct state.
if sac.submission_status != STATUS.AUDITOR_CERTIFIED:
return redirect(f"/audit/submission-progress/{sac.report_id}")

if form.is_valid():
# Save to session. Retrieved and saved after step 2.
request.session["AuditeeCertificationStep1Session"] = form.cleaned_data
Expand All @@ -561,6 +591,7 @@ def post(self, request, *args, **kwargs):


class AuditeeCertificationStep2View(CertifyingAuditeeRequiredMixin, generic.View):
@verify_status(STATUS.AUDITOR_CERTIFIED)
def get(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand Down Expand Up @@ -589,15 +620,12 @@ def get(self, request, *args, **kwargs):
"form": form,
}

# Return to checklist, the Audit is not in the correct state.
if sac.submission_status != STATUS.AUDITOR_CERTIFIED:
return redirect(f"/audit/submission-progress/{sac.report_id}")

return render(request, "audit/auditee-certification-step-2.html", context)

except SingleAuditChecklist.DoesNotExist:
raise PermissionDenied("You do not have access to this audit.")

@verify_status(STATUS.AUDITOR_CERTIFIED)
def post(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand All @@ -614,10 +642,6 @@ def post(self, request, *args, **kwargs):
"submission_status": sac.submission_status,
}

# Return to checklist, the Audit is not in the correct state.
if sac.submission_status != STATUS.AUDITOR_CERTIFIED:
return redirect(f"/audit/submission-progress/{sac.report_id}")

if form2.is_valid():
form_cleaned = {
"auditee_certification": form1_cleaned,
Expand Down Expand Up @@ -646,6 +670,7 @@ def post(self, request, *args, **kwargs):


class CertificationView(CertifyingAuditeeRequiredMixin, generic.View):
@verify_status(STATUS.AUDITOR_CERTIFIED)
def get(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand All @@ -661,6 +686,7 @@ def get(self, request, *args, **kwargs):
except SingleAuditChecklist.DoesNotExist:
raise PermissionDenied("You do not have access to this audit.")

@verify_status(STATUS.AUDITOR_CERTIFIED)
def post(self, request, *args, **kwargs):
report_id = kwargs["report_id"]

Expand All @@ -676,6 +702,7 @@ def post(self, request, *args, **kwargs):


class SubmissionView(CertifyingAuditeeRequiredMixin, generic.View):
@verify_status(STATUS.AUDITEE_CERTIFIED)
def get(self, request, *args, **kwargs):
report_id = kwargs["report_id"]
try:
Expand All @@ -690,6 +717,7 @@ def get(self, request, *args, **kwargs):
except SingleAuditChecklist.DoesNotExist:
raise PermissionDenied("You do not have access to this audit.")

@verify_status(STATUS.AUDITEE_CERTIFIED)
def post(self, request, *args, **kwargs):
# RACE HAZARD WARNING
# It is possible for a user to enter the submission multiple times,
Expand Down
2 changes: 1 addition & 1 deletion backend/templates/includes/maintenance_banner.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<section class="usa-alert usa-alert--warning margin-top-0" aria-label="Site alert">
<div class="usa-alert__body">
<div class="usa-alert__body grid-container">
<h4 class="usa-alert__heading">Scheduled system upgrade</h4>

{% comment %} If a message is given, use it. If not, display a generic message with the timeframe. {% endcomment %}
Expand Down
Loading