HH/apps/complaints/tests_routing_rejection.py
2026-07-05 14:49:45 +03:00

261 lines
10 KiB
Python

"""
Tests for the champion "reject wrong-department routing" feature.
Covers:
- Token-based (no-login) rejection via complaint_explanation_form
- Login-based rejection via involved_department_reject_routing
- Primary department rejection clears complaint.department
- Guards: can't reject twice, can't reject after response submitted
- Re-send to a rejected department resets the rejection
"""
import secrets
from django.contrib.auth.models import Group
from django.test import TestCase
from django.urls import reverse
from apps.accounts.models import User
from apps.complaints.models import (
Complaint,
ComplaintExplanation,
ComplaintInvolvedDepartment,
ComplaintUpdate,
)
from apps.complaints.services.complaint_service import (
RoutingRejectionError,
reject_department_routing,
)
from apps.organizations.models import Department, Hospital, Staff
class RoutingRejectionBase(TestCase):
"""Shared fixtures: hospital, two departments, PX admin, champion, complaint."""
@classmethod
def _make_staff(cls, first_name, last_name, emp_id, hospital, department):
return Staff.objects.create(
first_name=first_name,
last_name=last_name,
staff_type="admin",
job_title="Champion",
employee_id=emp_id,
hospital=hospital,
department=department,
status="active",
)
def setUp(self):
self.hospital = Hospital.objects.create(name="Test Hosp", code="TH001", status="active")
self.dept_a = Department.objects.create(
hospital=self.hospital, name="Dept A", name_en="Dept A", code="DA", status="active",
)
self.dept_b = Department.objects.create(
hospital=self.hospital, name="Dept B", name_en="Dept B", code="DB", status="active",
)
# PX admin
self.px_admin = User.objects.create_user(
email="px@test.com", password="pass123", hospital=self.hospital,
)
px_group, _ = Group.objects.get_or_create(name="PX Admin")
self.px_admin.groups.add(px_group)
# Champion of dept_a
self.champ_user = User.objects.create_user(
email="champ@test.com", password="pass123",
hospital=self.hospital, department=self.dept_a,
)
self.champ_staff = self._make_staff(
"Champ", "One", "EMP-CHAMP-1", self.hospital, self.dept_a,
)
self.champ_staff.user = self.champ_user
self.champ_staff.save()
self.dept_a.champion = self.champ_staff
self.dept_a.save(update_fields=["champion"])
# Complaint routed to dept_a (primary, sent)
self.complaint = Complaint.objects.create(
hospital=self.hospital,
department=self.dept_a,
title="Wrong routing test",
description="Sent to the wrong department.",
created_by=self.px_admin,
status="in_progress",
)
self.involved = ComplaintInvolvedDepartment.objects.create(
complaint=self.complaint,
department=self.dept_a,
role="primary",
is_primary=True,
sent=True,
)
class ModelHelperTests(RoutingRejectionBase):
def test_can_reject_routing_initially_true(self):
self.assertTrue(self.involved.can_reject_routing)
def test_cannot_reject_after_response_submitted(self):
self.involved.response_submitted = True
self.involved.save()
self.assertFalse(self.involved.can_reject_routing)
def test_cannot_reject_twice(self):
reject_department_routing(self.involved, staff=self.champ_staff, reason="wrong dept")
self.involved.refresh_from_db()
self.assertFalse(self.involved.can_reject_routing)
with self.assertRaises(RoutingRejectionError):
reject_department_routing(self.involved, staff=self.champ_staff, reason="again")
class ServiceRejectTests(RoutingRejectionBase):
def test_service_reject_marks_fields(self):
reject_department_routing(
self.involved,
staff=self.champ_staff,
reason="not ours",
suggested_department=self.dept_b,
)
self.involved.refresh_from_db()
self.assertEqual(self.involved.routing_status, "rejected")
self.assertIsNotNone(self.involved.rejected_at)
self.assertEqual(self.involved.rejected_by_staff_id, self.champ_staff.id)
self.assertEqual(self.involved.rejection_reason, "not ours")
self.assertEqual(self.involved.suggested_department_id, self.dept_b.id)
def test_service_reject_primary_clears_complaint_department(self):
reject_department_routing(self.involved, staff=self.champ_staff, reason="wrong")
self.complaint.refresh_from_db()
self.assertIsNone(self.complaint.department_id)
self.assertIsNone(self.complaint.section_id)
def test_service_reject_secondary_keeps_other_department(self):
# Make dept_b a secondary involved department
inv_b = ComplaintInvolvedDepartment.objects.create(
complaint=self.complaint, department=self.dept_b, role="secondary", sent=True,
)
reject_department_routing(inv_b, staff=self.champ_staff, reason="wrong")
# Primary dept_a must remain untouched
self.complaint.refresh_from_db()
self.assertEqual(self.complaint.department_id, self.dept_a.id)
def test_service_reject_creates_update_and_audit(self):
reject_department_routing(self.involved, staff=self.champ_staff, reason="wrong dept")
self.assertTrue(
ComplaintUpdate.objects.filter(
complaint=self.complaint, update_type="note",
).exists()
)
class TokenViewRejectTests(RoutingRejectionBase):
def setUp(self):
super().setUp()
self.explanation = ComplaintExplanation.objects.create(
complaint=self.complaint,
staff=self.champ_staff,
token=secrets.token_urlsafe(32),
explanation="",
is_used=False,
)
self.url = reverse(
"complaints:complaint_explanation_form",
kwargs={"complaint_id": self.complaint.id, "token": self.explanation.token},
)
def test_token_reject_success(self):
resp = self.client.post(self.url, {
"action": "reject_routing",
"rejection_reason": "Belongs to another dept",
})
self.assertEqual(resp.status_code, 200)
self.assertTemplateUsed(resp, "complaints/explanation_routing_rejected.html")
self.involved.refresh_from_db()
self.assertEqual(self.involved.routing_status, "rejected")
self.assertEqual(self.involved.rejection_reason, "Belongs to another dept")
# Token consumed
self.explanation.refresh_from_db()
self.assertTrue(self.explanation.is_used)
def test_token_reject_requires_reason(self):
resp = self.client.post(self.url, {"action": "reject_routing", "rejection_reason": ""})
self.assertEqual(resp.status_code, 200)
self.assertTemplateUsed(resp, "complaints/explanation_form.html")
self.involved.refresh_from_db()
self.assertEqual(self.involved.routing_status, "sent")
def test_token_reject_already_used_token_blocked(self):
self.explanation.is_used = True
self.explanation.save()
resp = self.client.post(self.url, {
"action": "reject_routing", "rejection_reason": "x",
})
# Already-submitted template is rendered when token is used
self.assertTemplateUsed(resp, "complaints/explanation_already_submitted.html")
class LoginViewRejectTests(RoutingRejectionBase):
def test_login_reject_by_champion(self):
self.client.login(username="champ@test.com", password="pass123")
url = reverse(
"complaints:involved_department_reject_routing", kwargs={"pk": self.involved.pk},
)
resp = self.client.post(url, {"rejection_reason": "Wrong department"})
self.assertEqual(resp.status_code, 302)
self.involved.refresh_from_db()
self.assertEqual(self.involved.routing_status, "rejected")
def test_login_reject_requires_permission(self):
# A random user with no relation to the department
User.objects.create_user(
email="other@test.com", password="pass123", hospital=self.hospital,
)
self.client.login(username="other@test.com", password="pass123")
url = reverse(
"complaints:involved_department_reject_routing", kwargs={"pk": self.involved.pk},
)
resp = self.client.post(url, {"rejection_reason": "Wrong department"})
self.assertEqual(resp.status_code, 302) # redirected, but not rejected
self.involved.refresh_from_db()
self.assertEqual(self.involved.routing_status, "sent")
def test_login_reject_requires_reason(self):
self.client.login(username="champ@test.com", password="pass123")
url = reverse(
"complaints:involved_department_reject_routing", kwargs={"pk": self.involved.pk},
)
resp = self.client.post(url, {"rejection_reason": ""})
self.assertEqual(resp.status_code, 302)
self.involved.refresh_from_db()
self.assertEqual(self.involved.routing_status, "sent")
class ResendResetTests(RoutingRejectionBase):
def test_resend_to_rejected_department_resets_routing(self):
reject_department_routing(self.involved, staff=self.champ_staff, reason="wrong")
self.involved.refresh_from_db()
self.assertEqual(self.involved.routing_status, "rejected")
# Simulate re-send via the service path
from django.utils import timezone
inv, created = ComplaintInvolvedDepartment.objects.get_or_create(
complaint=self.complaint, department=self.dept_a,
defaults={"role": "primary", "sent": True, "sent_at": timezone.now()},
)
self.assertFalse(created)
inv.routing_status = ComplaintInvolvedDepartment.RoutingStatus.SENT
inv.rejected_at = None
inv.rejected_by_staff = None
inv.rejection_reason = ""
inv.suggested_department = None
inv.sent = True
inv.sent_at = timezone.now()
inv.save()
inv.refresh_from_db()
self.assertEqual(inv.routing_status, "sent")
self.assertEqual(inv.rejection_reason, "")