Files
celtic-life-science/tests/e2e/test_self_report.py
Shubhankar45 0d30c58bf0 Server backup 2026-11-07 of uncommitted work in projects/pharma-web-celtic
Snapshot of the working tree on srv1071960 (branch main), taken during
server cleanup. .env files, node_modules, builds, logs and media were excluded.
2026-11-07 05:43:12 +00:00

142 lines
6.2 KiB
Python

"""An Area Business Manager's own Daily Call Report (Self): no plan, no approval, final on submit."""
from datetime import date
from tests.e2e.test_full_approval_flow import _create_masters
API = "/api/v1"
TODAY = date.today().isoformat()
async def _self_report(client, auth, **extra):
return await client.post(
f"{API}/dcr",
headers=auth["am1"],
json={"report_date": TODAY, "report_type": "SELF", **extra},
)
class TestSelfReport:
async def test_am_files_and_submits_without_a_plan(self, client, auth, org):
doctors, chemists = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1)
created = await _self_report(client, auth)
assert created.status_code == 201, created.text
report = created.json()
assert report["report_type"] == "SELF"
assert report["headquarters_id"] == str(org["hq1"].id) # the one HQ am1 manages
dcr_id = report["id"]
# Any doctor of the headquarters, though it is assigned to an MR, not to the ABM.
visit = await client.post(
f"{API}/dcr/{dcr_id}/calls/doctor",
headers=auth["am1"],
json={
"doctor_id": doctors[0]["id"],
"visit_time": "09:30:00",
"call_outcome": "MET",
"product_promote": ["Calcium", "Zinc"],
"product_sample": ["Iron"],
},
)
assert visit.status_code == 201, visit.text
chem = await client.post(
f"{API}/dcr/{dcr_id}/calls/chemist",
headers=auth["am1"],
json={"chemist_id": chemists[0]["id"], "visit_time": "10:00:00"},
)
assert chem.status_code == 201, chem.text
submitted = await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["am1"])
assert submitted.status_code == 200, submitted.text
assert submitted.json()["status"] == "APPROVED" # final: nobody approves it
assert submitted.json()["is_locked"] is True
locked = await client.patch(
f"{API}/dcr/{dcr_id}", headers=auth["am1"], json={"mr_remarks": "x"}
)
assert locked.status_code == 409
pdf = await client.get(f"{API}/dcr/{dcr_id}/pdf", headers=auth["am1"])
assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-")
async def test_self_list_is_own_and_kept_out_of_the_mr_lists(self, client, auth, org):
dcr_id = (await _self_report(client, auth)).json()["id"]
own = (await client.get(f"{API}/dcr?type=SELF", headers=auth["am1"])).json()
assert [r["id"] for r in own] == [dcr_id]
assert (await client.get(f"{API}/dcr?type=SELF", headers=auth["am2"])).json() == []
everything = (await client.get(f"{API}/dcr", headers=auth["am1"])).json()
assert dcr_id not in {r["id"] for r in everything}
counts = (await client.get(f"{API}/dcr/counts", headers=auth["am1"])).json()
assert counts["DRAFT"] == 0
async def test_one_self_report_per_date(self, client, auth, org):
assert (await _self_report(client, auth)).status_code == 201
assert (await _self_report(client, auth)).status_code == 409
async def test_needs_a_headquarters_the_am_manages(self, client, auth, org):
resp = await _self_report(client, auth, headquarters_id=str(org["hq2"].id))
assert resp.status_code == 403
async def test_only_an_am_files_one_and_an_am_files_no_plan(self, client, auth, org):
mr = await client.post(
f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "SELF"}
)
assert mr.status_code == 403
plan = await client.post(
f"{API}/dcr", headers=auth["am1"], json={"report_date": TODAY, "report_type": "PLAN"}
)
assert plan.status_code == 403
async def test_doctor_from_another_headquarters_is_refused(self, client, auth, org):
other = await client.post(
f"{API}/doctors",
headers=auth["admin"],
json={
"headquarters_id": str(org["hq2"].id),
"name": "Dr Far",
"clinic_address": "Sambalpur",
},
)
dcr_id = (await _self_report(client, auth)).json()["id"]
resp = await client.post(
f"{API}/dcr/{dcr_id}/calls/doctor",
headers=auth["am1"],
json={"doctor_id": other.json()["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
)
assert resp.status_code == 422
async def test_nobody_else_can_touch_it(self, client, auth, org):
dcr_id = (await _self_report(client, auth)).json()["id"]
assert (
await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["mr1"])
).status_code == 403
approve = await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["am1"], json={})
assert approve.status_code == 409 # a draft cannot be approved; submitting finalises it
class TestGeneralManagerOversight:
async def test_gm_sees_every_self_report_read_only(self, client, auth, org):
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=0)
dcr_id = (await _self_report(client, auth)).json()["id"]
await client.post(
f"{API}/dcr/{dcr_id}/calls/doctor",
headers=auth["am1"],
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
)
await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["am1"])
listed = (await client.get(f"{API}/dcr?type=SELF", headers=auth["gm"])).json()
assert [r["id"] for r in listed] == [dcr_id]
assert listed[0]["mr_name"] == org["am1"].full_name
assert (await client.get(f"{API}/dcr/{dcr_id}", headers=auth["gm"])).status_code == 200
pdf = await client.get(f"{API}/dcr/{dcr_id}/pdf", headers=auth["gm"])
assert pdf.status_code == 200
edit = await client.patch(
f"{API}/dcr/{dcr_id}", headers=auth["gm"], json={"mr_remarks": "x"}
)
assert edit.status_code == 403
own = await client.post(
f"{API}/dcr", headers=auth["gm"], json={"report_date": TODAY, "report_type": "SELF"}
)
assert own.status_code == 403