Files
celtic-life-science/tests/e2e/test_master_approval.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

134 lines
7.8 KiB
Python

"""A doctor, chemist or stockist an MR adds waits for their Area Business Manager; a doctor visit records a gift."""
from datetime import date
API = "/api/v1"
async def _add_doctor(client, auth, org, name="Dr New", who="mr1", hq="hq1"):
resp = await client.post(
f"{API}/doctors", headers=auth[who],
json={"headquarters_id": str(org[hq].id), "name": name, "clinic_address": "Clinic 1, Bhubaneswar"},
)
assert resp.status_code == 201, resp.text
return resp.json()
async def _names(client, headers, path="/doctors"):
return [d["name"] for d in (await client.get(f"{API}{path}", headers=headers)).json()]
class TestAnMrsDoctorWaitsForTheAreaManager:
async def test_it_is_pending_and_unlisted_until_approved(self, client, auth, org):
doctor = await _add_doctor(client, auth, org)
assert doctor["approval_status"] == "PENDING" and doctor["requested_by_name"] == "Ravi Das"
assert "Dr New" not in await _names(client, auth["mr1"])
assert "Dr New" not in await _names(client, auth["am1"])
assert await _names(client, auth["mr1"], "/doctors/requests") == ["Dr New"]
assert await _names(client, auth["am1"], "/doctors/requests") == ["Dr New"]
assert await _names(client, auth["am2"], "/doctors/requests") == [] # another headquarters
approved = await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["am1"])
assert approved.status_code == 200 and approved.json()["approval_status"] == "APPROVED"
assert "Dr New" in await _names(client, auth["mr1"]) # in the list, assigned to the MR who asked
assert await _names(client, auth["mr1"], "/doctors/requests") == []
async def test_only_the_mrs_own_area_manager_or_the_admin_decides(self, client, auth, org):
doctor = await _add_doctor(client, auth, org)
assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["am2"])).status_code == 403
assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["mr1"])).status_code == 403
assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["gm"])).status_code == 403
assert (await client.post(f"{API}/doctors/{doctor['id']}/approve", headers=auth["admin"])).status_code == 200
async def test_a_rejection_tells_the_mr_why_and_adding_it_again_resends_it(self, client, auth, org):
doctor = await _add_doctor(client, auth, org)
rejected = await client.post(f"{API}/doctors/{doctor['id']}/reject", headers=auth["am1"], json={"reason": "Wrong clinic"})
assert rejected.status_code == 200
mine = (await client.get(f"{API}/doctors/requests", headers=auth["mr1"])).json()
assert [(d["approval_status"], d["rejection_reason"]) for d in mine] == [("REJECTED", "Wrong clinic")]
assert await _names(client, auth["am1"], "/doctors/requests") == []
again = await _add_doctor(client, auth, org)
assert again["id"] == doctor["id"] and again["approval_status"] == "PENDING" and again["rejection_reason"] is None
assert await _names(client, auth["am1"], "/doctors/requests") == ["Dr New"]
async def test_a_pending_doctor_cannot_be_visited(self, client, auth, org, approved_plan):
doctor = await _add_doctor(client, auth, org)
await approved_plan()
dcr = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})
assert dcr.status_code == 201, dcr.text
visit = await client.post(
f"{API}/dcr/{dcr.json()['id']}/calls/doctor", headers=auth["mr1"],
json={"doctor_id": doctor["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
)
assert visit.status_code == 422
async def test_the_admins_own_doctor_is_approved_at_once(self, client, auth, org):
doctor = await _add_doctor(client, auth, org, who="admin")
assert doctor["approval_status"] == "APPROVED" and doctor["requested_by_name"] is None
class TestAnMrsChemistWaitsToo:
async def test_it_is_pending_until_approved(self, client, auth, org):
resp = await client.post(
f"{API}/chemists", headers=auth["mr1"],
json={"headquarters_id": str(org["hq1"].id), "name": "New Pharmacy", "shop_address": "Shop 2, Bhubaneswar"},
)
assert resp.status_code == 201 and resp.json()["approval_status"] == "PENDING"
assert "New Pharmacy" not in await _names(client, auth["mr1"], "/chemists")
assert (await client.post(f"{API}/chemists/{resp.json()['id']}/approve", headers=auth["am1"])).status_code == 200
assert "New Pharmacy" in await _names(client, auth["mr1"], "/chemists")
class TestAnMrsStockistWaitsToo:
async def _add(self, client, auth, org, who="mr1"):
resp = await client.post(
f"{API}/stockists", headers=auth[who],
json={"headquarters_id": str(org["hq1"].id), "name": "New Distributors", "address": "Unit 4, Bhubaneswar"},
)
assert resp.status_code == 201, resp.text
return resp.json()
async def test_it_is_pending_until_the_area_manager_approves(self, client, auth, org):
stockist = await self._add(client, auth, org)
assert stockist["approval_status"] == "PENDING" and stockist["requested_by_name"] == "Ravi Das"
assert "New Distributors" not in await _names(client, auth["mr1"], "/stockists")
assert await _names(client, auth["mr1"], "/stockists/requests") == ["New Distributors"]
assert await _names(client, auth["am1"], "/stockists/requests") == ["New Distributors"]
assert await _names(client, auth["am2"], "/stockists/requests") == []
assert (await client.post(f"{API}/stockists/{stockist['id']}/approve", headers=auth["am2"])).status_code == 403
assert (await client.post(f"{API}/stockists/{stockist['id']}/approve", headers=auth["am1"])).status_code == 200
assert "New Distributors" in await _names(client, auth["mr1"], "/stockists")
async def test_a_rejected_one_is_resent_by_adding_it_again(self, client, auth, org):
stockist = await self._add(client, auth, org)
rejected = await client.post(f"{API}/stockists/{stockist['id']}/reject", headers=auth["am1"], json={"reason": "Duplicate"})
assert rejected.status_code == 200 and rejected.json()["rejection_reason"] == "Duplicate"
again = await self._add(client, auth, org)
assert again["id"] == stockist["id"] and again["approval_status"] == "PENDING"
async def test_the_admins_own_stockist_is_approved_at_once(self, client, auth, org):
assert (await self._add(client, auth, org, who="admin"))["approval_status"] == "APPROVED"
class TestGift:
async def test_a_doctor_visit_records_the_gift_and_the_pdf_notes_it(self, client, auth, org, approved_plan):
doctor = await _add_doctor(client, auth, org, who="admin", name="Dr Gifted")
assert (await client.post(f"{API}/doctors/{doctor['id']}/assign", headers=auth["admin"], json={"mr_user_id": str(org["mr1"].id)})).status_code == 201
await approved_plan()
dcr = (await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})).json()
visit = await client.post(
f"{API}/dcr/{dcr['id']}/calls/doctor", headers=auth["mr1"],
json={"doctor_id": doctor["id"], "visit_time": "09:30:00", "call_outcome": "MET", "gift": "Diary"},
)
assert visit.status_code == 201, visit.text
calls = (await client.get(f"{API}/dcr/{dcr['id']}", headers=auth["mr1"])).json()["doctor_calls"]
assert [c["gift"] for c in calls] == ["Diary"]
from types import SimpleNamespace
from app.pdf.forms import _doctor_remarks
call = SimpleNamespace(call_outcome="MET", gift="Diary", remarks="Interested")
assert _doctor_remarks(call) == "Gift: Diary. Interested"