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.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""An Area Manager can manage any number of headquarters; only an Admin can change that."""
|
||||
"""An Area Business Manager can manage any number of headquarters; only an Admin can change that."""
|
||||
|
||||
import uuid
|
||||
|
||||
@@ -104,7 +104,7 @@ class TestAreaManagerHeadquarters:
|
||||
for who in ("gm", "am1", "mr1"):
|
||||
put = await client.put(f"{API}/area-managers/{org['am1'].id}", headers=auth[who], json=body)
|
||||
assert put.status_code == 403, (who, put.text)
|
||||
# GM may read the mappings (to see who manages what), AM and MR may not.
|
||||
# AGM may read the mappings (to see who manages what), ABM and MR may not.
|
||||
assert (await client.get(f"{API}/area-managers", headers=auth["gm"])).status_code == 200
|
||||
for who in ("am1", "mr1"):
|
||||
assert (await client.get(f"{API}/area-managers", headers=auth[who])).status_code == 403
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Morning day plan → Area Manager approval → end-of-day report against it (rules P1-P5)."""
|
||||
"""Morning day plan → Area Business Manager approval → end-of-day report against it (rules P1-P5)."""
|
||||
|
||||
from datetime import date
|
||||
|
||||
@@ -99,7 +99,7 @@ class TestEndOfDayNeedsAnApprovedPlan:
|
||||
await _plan(client, auth, doctors, [], approve=False)
|
||||
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": TODAY, "report_type": "ACTUAL"})
|
||||
assert resp.status_code == 422
|
||||
assert "approved by the Area Manager" in resp.json()["error"]["message"]
|
||||
assert "approved by the Area Business Manager" in resp.json()["error"]["message"]
|
||||
|
||||
async def test_once_approved_it_is_linked_to_the_plan(self, client, auth, org):
|
||||
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
||||
|
||||
@@ -48,7 +48,7 @@ class TestReports:
|
||||
first = await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"])
|
||||
second = await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"])
|
||||
assert first.status_code == second.status_code == 200
|
||||
assert second.json()["status"] == "PENDING_AM"
|
||||
assert second.json()["status"] == "PENDING_ABM"
|
||||
assert second.json()["submitted_at"] == first.json()["submitted_at"]
|
||||
assert await _audit_count(db, dcr, "SUBMIT") == 1
|
||||
|
||||
@@ -93,7 +93,7 @@ class TestSetup:
|
||||
assert (await client.post(f"{API}/products", headers=auth["admin"], json=product)).status_code == 201
|
||||
assert (await client.post(f"{API}/products", headers=auth["admin"], json=product)).status_code == 409
|
||||
|
||||
user = {"email": "dup@acmepharma.in", "password": "Str0ngPass9", "full_name": "Dup User", "employee_code": "D1", "role": "GM"}
|
||||
user = {"email": "dup@acmepharma.in", "password": "Str0ngPass9", "full_name": "Dup User", "employee_code": "D1", "role": "AGM"}
|
||||
assert (await client.post(f"{API}/users", headers=auth["admin"], json=user)).status_code == 201
|
||||
assert (await client.post(f"{API}/users", headers=auth["admin"], json=user)).status_code == 409
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ def _by_table(resp):
|
||||
|
||||
class TestFormApproval:
|
||||
async def test_every_approval_form_starts_as_draft_for_everyone(self, client, auth):
|
||||
# An MR sees all three forms as drafts; an Area Manager or admin has nothing to act on yet.
|
||||
# An MR sees all three forms as drafts; an Area Business Manager or admin has nothing to act on yet.
|
||||
forms = _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"]))
|
||||
assert set(forms) == {"tour_plan", "standard_tour_plan", "fare_chart", "expense_statement"}
|
||||
assert {f["status"] for f in forms.values()} == {"draft"}
|
||||
@@ -47,7 +47,7 @@ class TestFormApproval:
|
||||
done = await client.post(f"{API}/fare_chart/approval/approve?owner_id={org['mr1'].id}", headers=auth["am1"])
|
||||
assert done.status_code == 200, done.text
|
||||
assert done.json()["status"] == "approved" and done.json()["approved_by_name"] == "Suresh Behera"
|
||||
# The MR's very next read shows the AM's approval — no shared browser needed.
|
||||
# The MR's very next read shows the ABM's approval — no shared browser needed.
|
||||
seen = _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"]))["fare_chart"]
|
||||
assert (seen["status"], seen["approved_by_name"]) == ("approved", "Suresh Behera")
|
||||
|
||||
@@ -61,7 +61,7 @@ class TestFormApproval:
|
||||
early = await client.post(f"{API}/fare_chart/approval/approve?owner_id={org['mr1'].id}", headers=auth["am1"])
|
||||
assert early.status_code == 409, early.text
|
||||
assert _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"]))["fare_chart"]["status"] == "draft"
|
||||
assert (await client.get(f"{API}/approvals", headers=auth["am1"])).json() == [] # a draft is nothing for the AM to act on
|
||||
assert (await client.get(f"{API}/approvals", headers=auth["am1"])).json() == [] # a draft is nothing for the ABM to act on
|
||||
|
||||
async def test_double_clicks_are_harmless(self, client, auth, org, fare_chart):
|
||||
await _add_row(client, auth)
|
||||
@@ -93,11 +93,11 @@ class TestFormApproval:
|
||||
assert (await change()).status_code in (200, 201)
|
||||
state = _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"]))["fare_chart"]
|
||||
assert state["status"] == "draft" and state["approved_by_name"] is None
|
||||
# A pending (not yet approved) form is withdrawn too, so the AM never signs stale data.
|
||||
# A pending (not yet approved) form is withdrawn too, so the ABM never signs stale data.
|
||||
# An MR may remove a row they entered by mistake; that too withdraws the request.
|
||||
await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])
|
||||
assert (await client.delete(f"{API}/fare_chart/rows/{row_id}", headers=auth["mr1"])).status_code == 204
|
||||
assert (await client.get(f"{API}/approvals", headers=auth["am1"])).json() == [] # withdrawn: nothing left for the AM
|
||||
assert (await client.get(f"{API}/approvals", headers=auth["am1"])).json() == [] # withdrawn: nothing left for the ABM
|
||||
|
||||
async def test_the_pdf_signature_comes_from_the_server_not_the_browser(self, client, auth, org, db, fare_chart):
|
||||
# Not approved: a browser claiming otherwise changes nothing.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Company forms: listed in the company's order, and readable (never writable) by the GM."""
|
||||
"""Company forms: listed in the company's order, and readable (never writable) by the AGM."""
|
||||
|
||||
from tests.e2e.test_form_ownership import forms # noqa: F401 - creates the form tables
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Company forms belong to one MR; only that MR's own Area Manager receives and signs the request."""
|
||||
"""Company forms belong to one MR; only that MR's own Area Business Manager receives and signs the request."""
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
@@ -110,7 +110,7 @@ class TestOnlyTheAssignedAreaManagerGetsTheRequest:
|
||||
await db.execute(text("UPDATE am_headquarters SET effective_to = current_date WHERE headquarters_id = :h"), {"h": org["hq1"].id})
|
||||
await db.commit()
|
||||
resp = await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])
|
||||
assert resp.status_code == 422 and "Area Manager" in resp.text
|
||||
assert resp.status_code == 422 and "Area Business Manager" in resp.text
|
||||
|
||||
async def test_the_pdf_is_signed_by_the_approver_of_that_mr_only(self, client, auth, org, db, forms):
|
||||
from app.csv_data.router import _signature
|
||||
|
||||
@@ -77,7 +77,7 @@ async def _create_masters(client, auth, org, n_doctors=15, n_chemists=5):
|
||||
class TestStep1And2Setup:
|
||||
async def test_admin_creates_user_and_org_mapping(self, client, auth, org):
|
||||
"""Step 1-2: users exist with correct roles, and the MR's profile resolves
|
||||
both its headquarters and its DERIVED reporting Area Manager."""
|
||||
both its headquarters and its DERIVED reporting Area Business Manager."""
|
||||
resp = await client.get(f"{API}/users/me/profile", headers=auth["mr1"])
|
||||
assert resp.status_code == 200, resp.text
|
||||
profile = resp.json()
|
||||
@@ -120,6 +120,15 @@ class TestStep1And2Setup:
|
||||
|
||||
|
||||
class TestStep3And5Masters:
|
||||
async def test_admin_and_agm_see_doctor_mr_assignment(self, client, auth, org):
|
||||
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=0)
|
||||
|
||||
for who in ("admin", "gm"):
|
||||
resp = await client.get(f"{API}/doctors", headers=auth[who])
|
||||
assert resp.status_code == 200, resp.text
|
||||
row = next(item for item in resp.json() if item["id"] == doctors[0]["id"])
|
||||
assert row["assigned_mr_name"] == org["mr1"].full_name
|
||||
|
||||
async def test_mr_sees_exactly_the_assigned_doctors(self, client, auth, org):
|
||||
"""Step 3 + Step 5 (S5): 15 doctors created and assigned → MR sees 15."""
|
||||
await _create_masters(client, auth, org)
|
||||
@@ -288,11 +297,11 @@ class TestFullApprovalFlow:
|
||||
# --- Step 8: MR submits ---------------------------------------
|
||||
resp = await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["mr1"])
|
||||
assert resp.status_code == 200, resp.text
|
||||
assert resp.json()["status"] == DCRStatus.PENDING_AM.value
|
||||
assert resp.json()["status"] == DCRStatus.PENDING_ABM.value
|
||||
assert resp.json()["submitted_at"] is not None
|
||||
|
||||
# --- Step 9a: AM returns it -----------------------------------
|
||||
queue = await client.get(f"{API}/dcr?status=PENDING_AM", headers=auth["am1"])
|
||||
# --- Step 9a: ABM returns it -----------------------------------
|
||||
queue = await client.get(f"{API}/dcr?status=PENDING_ABM", headers=auth["am1"])
|
||||
assert [r["id"] for r in queue.json()] == [dcr_id]
|
||||
|
||||
# A reason is mandatory (V8).
|
||||
@@ -309,7 +318,7 @@ class TestFullApprovalFlow:
|
||||
assert resp.status_code == 200, resp.text
|
||||
returned = resp.json()
|
||||
assert returned["status"] == DCRStatus.RETURNED.value
|
||||
assert returned["returned_by_role"] == "AM"
|
||||
assert returned["returned_by_role"] == "ABM"
|
||||
assert "chemist visits" in returned["return_reason"]
|
||||
|
||||
# The MR can edit again once returned.
|
||||
@@ -322,12 +331,12 @@ class TestFullApprovalFlow:
|
||||
|
||||
# --- Step 9b: MR resubmits ------------------------------------
|
||||
resp = await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["mr1"])
|
||||
assert resp.json()["status"] == DCRStatus.PENDING_AM.value
|
||||
assert resp.json()["status"] == DCRStatus.PENDING_ABM.value
|
||||
# Resubmission clears the previous return.
|
||||
assert resp.json()["return_reason"] is None
|
||||
|
||||
# --- Step 10: the Area Manager gives the final approval -------
|
||||
# There is no General Manager stage, and the GM cannot approve.
|
||||
# --- Step 10: the Area Business Manager gives the final approval -------
|
||||
# There is no AGM stage, and the AGM cannot approve.
|
||||
gm_try = await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["gm"], json={})
|
||||
assert gm_try.status_code == 403, gm_try.text
|
||||
gone = await client.post(f"{API}/dcr/{dcr_id}/forward", headers=auth["am1"], json={})
|
||||
@@ -371,7 +380,7 @@ class TestFullApprovalFlow:
|
||||
for role in ("gm", "admin"):
|
||||
again = await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth[role], json={})
|
||||
assert again.status_code in (403, 409)
|
||||
# The approving AM repeating the click changes nothing (idempotent), and is not an error.
|
||||
# The approving ABM repeating the click changes nothing (idempotent), and is not an error.
|
||||
repeat = await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["am1"], json={})
|
||||
assert repeat.status_code == 200 and repeat.json()["approved_at"] == approved["approved_at"]
|
||||
# ...but an approved report can still not be returned or reopened by anybody.
|
||||
@@ -379,7 +388,7 @@ class TestFullApprovalFlow:
|
||||
back = await client.post(f"{API}/dcr/{dcr_id}/return", headers=auth[role], json={"reason": "reopen it please"})
|
||||
assert back.status_code in (403, 409), role
|
||||
|
||||
# --- Step 12: MR, AM and GM each download the PDF -------------
|
||||
# --- Step 12: MR, ABM and AGM each download the PDF -------------
|
||||
for role in ("mr1", "am1", "gm"):
|
||||
pdf = await client.get(f"{API}/dcr/{dcr_id}/pdf", headers=auth[role])
|
||||
assert pdf.status_code == 200, f"{role}: {pdf.text}"
|
||||
@@ -399,11 +408,11 @@ class TestFullApprovalFlow:
|
||||
|
||||
entries = history.json()
|
||||
assert entries[2]["actor_name"] == "Suresh Behera"
|
||||
assert entries[2]["actor_role"] == "AM"
|
||||
assert entries[2]["actor_role"] == "ABM"
|
||||
assert "chemist visits" in entries[2]["comment"]
|
||||
# The final entry is the Area Manager's approval.
|
||||
# The final entry is the Area Business Manager's approval.
|
||||
assert entries[-1]["actor_name"] == "Suresh Behera"
|
||||
assert entries[-1]["actor_role"] == "AM"
|
||||
assert entries[-1]["actor_role"] == "ABM"
|
||||
assert entries[-1]["comment"] == "Approved for the month."
|
||||
assert all(e["at"] for e in entries)
|
||||
|
||||
@@ -456,7 +465,7 @@ class TestNegativeScope:
|
||||
await client.post(f"{API}/dcr/{dcr_id}/{action}", headers=auth["am2"], json=body)
|
||||
).status_code == 403
|
||||
|
||||
listing = await client.get(f"{API}/dcr?status=PENDING_AM", headers=auth["am2"])
|
||||
listing = await client.get(f"{API}/dcr?status=PENDING_ABM", headers=auth["am2"])
|
||||
assert listing.json() == []
|
||||
|
||||
async def test_x3_admin_cannot_approve(self, client, auth, org, approved_plan):
|
||||
@@ -470,6 +479,9 @@ class TestNegativeScope:
|
||||
assert (
|
||||
await client.get(f"{API}/dcr/{dcr_id}", headers=auth["admin"])
|
||||
).status_code == 200
|
||||
listing = await client.get(f"{API}/dcr", headers=auth["admin"])
|
||||
assert listing.status_code == 200, listing.text
|
||||
assert dcr_id in {report["id"] for report in listing.json()}
|
||||
|
||||
async def test_admin_cannot_create_a_report(self, client, auth):
|
||||
resp = await client.post(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""A doctor or chemist an MR adds waits for their Area Manager; a doctor visit records a gift."""
|
||||
"""A doctor, chemist or stockist an MR adds waits for their Area Business Manager; a doctor visit records a gift."""
|
||||
|
||||
from datetime import date
|
||||
|
||||
@@ -80,6 +80,37 @@ class TestAnMrsChemistWaitsToo:
|
||||
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")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""An Area Manager's own Daily Call Report (Self): no plan, no approval, final on submit."""
|
||||
"""An Area Business Manager's own Daily Call Report (Self): no plan, no approval, final on submit."""
|
||||
|
||||
from datetime import date
|
||||
|
||||
@@ -26,7 +26,7 @@ class TestSelfReport:
|
||||
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 AM.
|
||||
# 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"],
|
||||
|
||||
@@ -23,9 +23,9 @@ class TestAdminChangesRole:
|
||||
)
|
||||
assert doctor.status_code == 201
|
||||
|
||||
resp = await _patch(client, auth, org["mr1"], {"role": "AM"})
|
||||
resp = await _patch(client, auth, org["mr1"], {"role": "ABM"})
|
||||
assert resp.status_code == 200, resp.text
|
||||
assert resp.json()["role"] == "AM"
|
||||
assert resp.json()["role"] == "ABM"
|
||||
assert resp.json()["headquarters_id"] is None
|
||||
|
||||
# The doctor stays in the headquarters but is no longer assigned to anyone.
|
||||
@@ -48,16 +48,16 @@ class TestAdminChangesRole:
|
||||
assert str(org["am1"].id) not in {m["area_manager_user_id"] for m in mappings.json()}
|
||||
|
||||
async def test_gm_becomes_am(self, client, auth, org):
|
||||
resp = await _patch(client, auth, org["gm"], {"role": "AM"})
|
||||
resp = await _patch(client, auth, org["gm"], {"role": "ABM"})
|
||||
assert resp.status_code == 200, resp.text
|
||||
assert resp.json()["role"] == "AM"
|
||||
assert resp.json()["role"] == "ABM"
|
||||
|
||||
async def test_role_change_signs_the_user_out(self, client, auth, org):
|
||||
login = await client.post(
|
||||
f"{API}/auth/login",
|
||||
json={"email": org["gm"].email, "password": DEFAULT_PASSWORD},
|
||||
)
|
||||
await _patch(client, auth, org["gm"], {"role": "AM"})
|
||||
await _patch(client, auth, org["gm"], {"role": "ABM"})
|
||||
again = await client.post(
|
||||
f"{API}/auth/refresh", json={"refresh_token": login.json()["refresh_token"]}
|
||||
)
|
||||
@@ -68,9 +68,9 @@ class TestAdminChangesRole:
|
||||
assert resp.status_code == 422
|
||||
|
||||
async def test_admin_cannot_change_own_role(self, client, auth, org):
|
||||
resp = await _patch(client, auth, org["admin"], {"role": "GM"})
|
||||
resp = await _patch(client, auth, org["admin"], {"role": "AGM"})
|
||||
assert resp.status_code == 422
|
||||
|
||||
async def test_only_admin_can_change_a_role(self, client, auth, org):
|
||||
resp = await _patch(client, auth, org["mr1"], {"role": "AM"}, as_="gm")
|
||||
resp = await _patch(client, auth, org["mr1"], {"role": "ABM"}, as_="gm")
|
||||
assert resp.status_code == 403
|
||||
|
||||
Reference in New Issue
Block a user