Skip to content

Commit

Permalink
Merge pull request #1810 from uktrade/LTD-4641_hotfix_govuser_cannot_…
Browse files Browse the repository at this point in the history
…close_query_with_blank_response

LTD-4641 [hotfix] Update view so govuser cannot close query with blank response
  • Loading branch information
hnryjmes authored Feb 7, 2024
2 parents 8fbd5be + 402991e commit c4e5eb9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
15 changes: 5 additions & 10 deletions api/cases/tests/test_case_ecju_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,25 +522,20 @@ def test_close_query_has_optional_response_exporter(self):
self.assertIsNone(response_ecju_query["response"])
self.assertIsNotNone(response_ecju_query["responded_at"])

def test_close_query_has_optional_response_govuser(self):
@parameterized.expand(["", None])
def test_close_query_has_required_response_govuser(self, response_value):
case = self.create_standard_application_case(self.organisation)
ecju_query = self.create_ecju_query(case, question="provide details please", gov_user=self.gov_user)

self.assertIsNone(ecju_query.responded_at)

query_response_url = reverse("cases:case_ecju_query", kwargs={"pk": case.id, "ecju_pk": ecju_query.id})

data = {"response": ""}
data = {"response": response_value}

response = self.client.put(query_response_url, data, **self.gov_headers)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

response_ecju_query = response.json()["ecju_query"]
self.assertIsNone(response_ecju_query["response"])
self.assertIsNotNone(response_ecju_query["responded_at"])

response_get = self.client.get(query_response_url, **self.gov_headers)
self.assertEqual(True, response_get.json()["ecju_query"]["is_manually_closed"])
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.json()["errors"], "Enter a reason why you are closing the query")

def test_caseworker_manually_closes_query_exporter_responds_raises_error(self):
case = self.create_standard_application_case(self.organisation)
Expand Down
10 changes: 9 additions & 1 deletion api/cases/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,15 @@ def put(self, request, pk, ecju_pk):
status=status.HTTP_400_BAD_REQUEST,
)

is_govuser = hasattr(request.user, "govuser")
is_blank_response = not bool(request.data.get("response"))

# response is required only when a govuser closes a query
if is_govuser and is_blank_response:
return JsonResponse(
data={"errors": "Enter a reason why you are closing the query"}, status=status.HTTP_400_BAD_REQUEST
)

data = {"responded_by_user": str(request.user.pk)}

if request.data.get("response"):
Expand All @@ -632,7 +641,6 @@ def put(self, request, pk, ecju_pk):
if serializer.is_valid():
if "validate_only" not in request.data or not request.data["validate_only"]:
serializer.save()
is_govuser = hasattr(request.user, "govuser")
# If the user is a Govuser query is manually being closed by a caseworker
query_verb = AuditType.ECJU_QUERY_MANUALLY_CLOSED if is_govuser else AuditType.ECJU_QUERY_RESPONSE
audit_trail_service.create(
Expand Down

0 comments on commit c4e5eb9

Please sign in to comment.