-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Create endpoint for retrieving move case audit events
- Loading branch information
Showing
6 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.contrib.contenttypes.models import ContentType | ||
from rest_framework import viewsets | ||
from rest_framework.pagination import LimitOffsetPagination | ||
|
||
|
||
from api.audit_trail.enums import AuditType | ||
from api.audit_trail.models import Audit | ||
from api.cases.models import Case | ||
from api.core.authentication import DataWorkspaceOnlyAuthentication | ||
from api.data_workspace.serializers import AuditMoveCaseSerializer | ||
|
||
|
||
class AuditMoveCaseListView(viewsets.ReadOnlyModelViewSet): | ||
"""Expose 'move case' audit events to data workspace.""" | ||
|
||
authentication_classes = (DataWorkspaceOnlyAuthentication,) | ||
serializer_class = AuditMoveCaseSerializer | ||
pagination_class = LimitOffsetPagination | ||
|
||
def get_queryset(self): | ||
content_type = ContentType.objects.get_for_model(Case) | ||
|
||
# This returns a queryset of audit records for the "move case" audit event. | ||
# For each record, it exposes the nested "queues" JSON property as a top | ||
# level column called "queue" and splits into multiple rows when "queues" | ||
# contains multiple entries. | ||
# It also deals with the fact that the value of "queues" is sometimes an | ||
# array of queue names but sometimes a single string. | ||
return Audit.objects.raw( | ||
""" | ||
with audit_move_case as ( | ||
select *, | ||
case when jsonb_typeof(payload->'queues') = 'array' | ||
then payload->'queues' | ||
else jsonb_build_array(payload->'queues') | ||
end as queues | ||
from audit_trail_audit | ||
where verb = %(verb)s | ||
and (action_object_content_type_id = %(action_type)s | ||
or target_content_type_id = %(target_type)s) | ||
order by created_at | ||
) | ||
select *, value->>0 as "queue" from audit_move_case cross join jsonb_array_elements(queues) | ||
""", | ||
{"verb": AuditType.MOVE_CASE, "action_type": content_type.pk, "target_type": content_type.pk}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from django.urls import reverse | ||
from functools import partial | ||
from rest_framework import status | ||
|
||
from api.audit_trail.enums import AuditType | ||
from api.staticdata.statuses.enums import CaseStatusEnum | ||
from api.staticdata.statuses.libraries.get_case_status import get_case_status_by_status | ||
from test_helpers.clients import DataTestClient | ||
|
||
|
||
class DataWorkspaceAuditMoveCaseTests(DataTestClient): | ||
def setUp(self): | ||
super().setUp() | ||
self.url = reverse("data_workspace:dw-audit-move-case-list") | ||
case = self.create_standard_application_case(self.organisation, "Test Application") | ||
# Audit events are only created for non-draft cases | ||
case.status = get_case_status_by_status(CaseStatusEnum.OPEN) | ||
case.save() | ||
self.create_audit = partial( | ||
super().create_audit, verb=AuditType.MOVE_CASE, actor=self.gov_user, target=case.get_case() | ||
) | ||
|
||
self.create_audit(payload={"queues": "Initial Queue"}) | ||
self.create_audit(verb=AuditType.CREATED_USER_ADVICE) | ||
|
||
def test_audit_move_case(self): | ||
expected_fields = ("created_at", "user", "case", "queue") | ||
|
||
response = self.client.get(self.url) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
results = response.json()["results"] | ||
self.assertEqual(len(results), 1) | ||
self.assertEqual(tuple(results[0].keys()), expected_fields) | ||
self.assertEqual(results[0]["queue"], str(self.queue.pk)) | ||
|
||
response = self.client.options(self.url) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
options = response.json()["actions"]["OPTIONS"] | ||
self.assertEqual(tuple(options.keys()), expected_fields) | ||
|
||
def test_payload_multiple_queues(self): | ||
queue1 = self.create_queue("MOD - DSR Cases to Review", self.team) | ||
queue2 = self.create_queue("MOD - WECA Cases to Review", self.team) | ||
# Create a single audit record with multiple queues in the payload | ||
self.create_audit(payload={"queues": ["MOD - DSR Cases to Review", "MOD - WECA Cases to Review"]}) | ||
|
||
response = self.client.get(self.url) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
results = response.json()["results"] | ||
self.assertEqual(len(results), 3) | ||
self.assertEqual(results[0]["queue"], str(self.queue.pk)) | ||
# The record with multiple queues should have been split into separate entries. | ||
self.assertEqual(results[1]["queue"], str(queue1.pk)) | ||
self.assertEqual(results[2]["queue"], str(queue2.pk)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters