fix: reopen clears restored_at, wire settings, docs, startup warning

This commit is contained in:
Atharv1705
2026-11-12 17:34:04 +05:30
parent 5027c0766e
commit e2a6fa601e
17 changed files with 292 additions and 36 deletions
+86 -1
View File
@@ -283,7 +283,7 @@ def test_full_complaint_workflow_and_asset_status_sync(client, db):
assert cls_res.json()["closed_at"] is not None
assert cls_res.json()["asset_status"] == "in_service"
# 7. Reopen -> critical asset back to under_repair
# 7. Reopen -> critical asset back to under_repair and restored_at cleared
reopen_res = client.post(
f"/api/complaints/{cid}/reopen",
headers=bio_headers,
@@ -292,6 +292,8 @@ def test_full_complaint_workflow_and_asset_status_sync(client, db):
assert reopen_res.status_code == 200
assert reopen_res.json()["call_status"] == "in_progress"
assert reopen_res.json()["closed_at"] is None
assert reopen_res.json()["restored_at"] is None
assert reopen_res.json()["downtime_is_ongoing"] is True
assert reopen_res.json()["asset_status"] == "under_repair"
# 8. Correct timestamp: biomedical -> 403, system_admin -> 200
@@ -330,3 +332,86 @@ def test_full_complaint_workflow_and_asset_status_sync(client, db):
assert events[2]["from_value"] == bio1.name
assert events[2]["to_value"] == bio2.name
assert events[8]["note"] == "registered_at: Logged after phone call"
def test_require_closure_remarks_and_wired_settings(client, db):
from datetime import date
from app.services.calculations import compute_sla_status
admin_headers = _auth_headers(client, "admin@poona.demo")
bio_headers = _auth_headers(client, "bio@poona.demo")
asset = db.query(Asset).first()
assert asset is not None
# Create a complaint to test close with require_closure_remarks True vs False
c_res = client.post(
"/api/complaints",
headers=bio_headers,
json={
"asset_id": asset.id,
"description": "Display backlight flicker",
"priority": "non_critical",
},
)
assert c_res.status_code == 201
cid = c_res.json()["id"]
assert c_res.json()["sla_target_hours"] == 24
assert c_res.json()["sla_breached"] is False
# Default require_closure_remarks=True -> blank remarks returns 422 CLOSURE_REMARKS_REQUIRED
fail_close = client.post(
f"/api/complaints/{cid}/close",
headers=bio_headers,
json={"closure_remarks": " "},
)
assert fail_close.status_code == 422
assert fail_close.json()["detail"]["code"] == "CLOSURE_REMARKS_REQUIRED"
try:
# Disable require_closure_remarks -> blank remarks closes cleanly (200)
upd = client.put(
"/api/settings",
headers=admin_headers,
json={"values": {"require_closure_remarks": False}},
)
assert upd.status_code == 200
ok_close = client.post(
f"/api/complaints/{cid}/close",
headers=bio_headers,
json={"closure_remarks": ""},
)
assert ok_close.status_code == 200
assert ok_close.json()["call_status"] == "closed"
# Verify default_pm_mode setting when service_mode is omitted on POST /api/pm/schedules
pm_res = client.post(
"/api/pm/schedules",
headers=bio_headers,
json={
"asset_id": asset.id,
"frequency_months": 6,
"responsible_team": "Biomedical Team",
"next_planned_date": str(date.today()),
},
)
assert pm_res.status_code == 201, pm_res.text
assert pm_res.json()["service_mode"] == "in_house"
finally:
client.put(
"/api/settings",
headers=admin_headers,
json={"values": {"require_closure_remarks": True}},
)
# Pure unit test for compute_sla_status using injected SLA settings
t0 = datetime(2026, 6, 1, 8, 0, tzinfo=timezone.utc)
breached, target = compute_sla_status(
"critical", t0, None, None, critical_sla_hours=4, non_critical_sla_hours=24, now_utc=t0 + timedelta(hours=5)
)
assert target == 4 and breached is True
not_breached, target2 = compute_sla_status(
"non_critical", t0, None, None, critical_sla_hours=4, non_critical_sla_hours=24, now_utc=t0 + timedelta(hours=5)
)
assert target2 == 24 and not_breached is False