0d30c58bf0
Snapshot of the working tree on srv1071960 (branch main), taken during server cleanup. .env files, node_modules, builds, logs and media were excluded.
147 lines
8.7 KiB
Python
147 lines
8.7 KiB
Python
"""Sending the same request twice (a double click, a slow network) never creates or changes anything twice."""
|
|
|
|
from datetime import date
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
|
|
from tests.e2e.test_full_approval_flow import _create_masters
|
|
|
|
API = "/api/v1"
|
|
|
|
|
|
@pytest.fixture
|
|
async def tables(db):
|
|
await db.execute(text('DROP TABLE IF EXISTS "fare_chart"'))
|
|
await db.execute(text(
|
|
'CREATE TABLE "fare_chart" (id UUID PRIMARY KEY, source_row_number INTEGER, name TEXT, headquarter TEXT, '
|
|
"serial_no TEXT, report_date TEXT, from_location TEXT, to_location TEXT, kilometers_km TEXT, owner_user_id UUID)"
|
|
))
|
|
await db.execute(text("DROP TABLE IF EXISTS stock_inventory"))
|
|
await db.execute(text(
|
|
"CREATE TABLE stock_inventory (id SERIAL PRIMARY KEY, serial_no INTEGER UNIQUE, product_name TEXT NOT NULL, "
|
|
"pack TEXT, quantity TEXT, unit TEXT)"
|
|
))
|
|
await db.commit()
|
|
|
|
|
|
async def _audit_count(db, entity_id, action) -> int:
|
|
row = await db.execute(
|
|
text("SELECT count(*) FROM audit_log WHERE entity_id = :e AND action = :a"), {"e": entity_id, "a": action}
|
|
)
|
|
return row.scalar_one()
|
|
|
|
|
|
class TestReports:
|
|
async def _submitted(self, client, auth, org, approved_plan):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=1)
|
|
await approved_plan()
|
|
dcr = (await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})).json()["id"]
|
|
await client.post(
|
|
f"{API}/dcr/{dcr}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
|
|
)
|
|
return dcr, doctors
|
|
|
|
async def test_submitting_twice_is_one_submission(self, client, auth, org, db, approved_plan):
|
|
dcr, _ = await self._submitted(client, auth, org, approved_plan)
|
|
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_ABM"
|
|
assert second.json()["submitted_at"] == first.json()["submitted_at"]
|
|
assert await _audit_count(db, dcr, "SUBMIT") == 1
|
|
|
|
async def test_approving_twice_is_one_approval(self, client, auth, org, db, approved_plan):
|
|
dcr, _ = await self._submitted(client, auth, org, approved_plan)
|
|
await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"])
|
|
first = await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am1"], json={})
|
|
second = await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am1"], json={})
|
|
assert first.status_code == second.status_code == 200
|
|
assert second.json()["approved_at"] == first.json()["approved_at"]
|
|
assert await _audit_count(db, dcr, "APPROVE") == 1
|
|
|
|
async def test_an_area_manager_of_another_hq_still_cannot_approve(self, client, auth, org, approved_plan):
|
|
dcr, _ = await self._submitted(client, auth, org, approved_plan)
|
|
await client.post(f"{API}/dcr/{dcr}/submit", headers=auth["mr1"])
|
|
await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am1"], json={})
|
|
assert (await client.post(f"{API}/dcr/{dcr}/approve", headers=auth["am2"], json={})).status_code == 403
|
|
|
|
async def test_adding_the_same_visit_or_creating_the_same_report_twice_is_refused(self, client, auth, org, approved_plan):
|
|
dcr, doctors = await self._submitted(client, auth, org, approved_plan)
|
|
again = await client.post(
|
|
f"{API}/dcr/{dcr}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "10:30:00", "call_outcome": "MET"},
|
|
)
|
|
assert again.status_code == 409
|
|
assert (await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})).status_code == 409
|
|
report = (await client.get(f"{API}/dcr/{dcr}", headers=auth["mr1"])).json()
|
|
assert len(report["doctor_calls"]) == 1
|
|
|
|
|
|
class TestSetup:
|
|
async def test_the_same_doctor_headquarters_product_and_user_cannot_be_created_twice(self, client, auth, org):
|
|
doctor = {"headquarters_id": str(org["hq1"].id), "name": "Dr Twice", "clinic_address": "1 Main Road"}
|
|
assert (await client.post(f"{API}/doctors", headers=auth["admin"], json=doctor)).status_code == 201
|
|
assert (await client.post(f"{API}/doctors", headers=auth["admin"], json=doctor)).status_code == 409
|
|
|
|
hq = {"code": "NEW1", "name": "New Place"}
|
|
assert (await client.post(f"{API}/headquarters", headers=auth["admin"], json=hq)).status_code == 201
|
|
assert (await client.post(f"{API}/headquarters", headers=auth["admin"], json=hq)).status_code == 409
|
|
|
|
product = {"name": "Twice Tonic"}
|
|
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": "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
|
|
|
|
async def test_assigning_twice_leaves_one_assignment(self, client, auth, org):
|
|
doctors, _ = await _create_masters(client, auth, org, n_doctors=1, n_chemists=0)
|
|
again = await client.post(f"{API}/doctors/{doctors[0]['id']}/assign", headers=auth["admin"], json={"mr_user_id": str(org["mr1"].id)})
|
|
assert again.status_code == 201
|
|
visible = await client.get(f"{API}/doctors", headers=auth["mr1"])
|
|
assert len(visible.json()) == 1
|
|
|
|
async def test_area_manager_assignment_is_repeatable(self, client, auth, org):
|
|
body = {"headquarters_ids": [str(org["hq1"].id)]}
|
|
for _ in range(2):
|
|
resp = await client.put(f"{API}/headquarters/area-managers/{org['am1'].id}", headers=auth["admin"], json=body)
|
|
assert resp.status_code == 200 and len(resp.json()) == 1
|
|
|
|
async def test_the_stock_list_keeps_each_product_once(self, client, auth, tables):
|
|
item = {"product_name": "ZINCOVIT-X", "pack": "10X10 TAB"}
|
|
assert (await client.post(f"{API}/products/stock-inventory", headers=auth["admin"], json=item)).status_code == 201
|
|
for spelling in ("ZINCOVIT-X", " zincovit-x "):
|
|
dup = await client.post(f"{API}/products/stock-inventory", headers=auth["admin"], json={"product_name": spelling})
|
|
assert dup.status_code == 409, spelling
|
|
other = (await client.post(f"{API}/products/stock-inventory", headers=auth["admin"], json={"product_name": "OTHER-Y"})).json()
|
|
clash = await client.patch(f"{API}/products/stock-inventory/{other['id']}", headers=auth["admin"], json={"product_name": "zincovit-x"})
|
|
assert clash.status_code == 409
|
|
keep = await client.patch(f"{API}/products/stock-inventory/{other['id']}", headers=auth["admin"], json={"product_name": "OTHER-Y", "pack": "new"})
|
|
assert keep.status_code == 200 # saving a product under its own name is fine
|
|
|
|
|
|
class TestCompanyForms:
|
|
async def test_an_identical_row_is_a_repeat_but_a_different_one_is_not(self, client, auth, tables):
|
|
row = {"report_date": "2026-09-21", "from_location": "Bhubaneswar", "to_location": "Puri", "kilometers_km": "69"}
|
|
assert (await client.post(f"{API}/csv-data/fare_chart/rows", headers=auth["mr1"], json=row)).status_code == 201
|
|
repeat = await client.post(f"{API}/csv-data/fare_chart/rows", headers=auth["mr1"], json=row)
|
|
assert repeat.status_code == 409, repeat.text
|
|
other = {**row, "to_location": "Cuttack", "kilometers_km": "39"}
|
|
assert (await client.post(f"{API}/csv-data/fare_chart/rows", headers=auth["mr1"], json=other)).status_code == 201
|
|
rows = (await client.get(f"{API}/csv-data/fare_chart/rows", headers=auth["mr1"])).json()
|
|
assert len(rows) == 2
|
|
|
|
async def test_repeated_send_and_approve_are_absorbed(self, client, auth, org, tables):
|
|
await client.post(f"{API}/csv-data/fare_chart/rows", headers=auth["mr1"], json={"from_location": "A", "to_location": "B"})
|
|
statuses = [
|
|
(await client.post(f"{API}/csv-data/fare_chart/approval/request", headers=auth["mr1"])).json()["status"] for _ in range(3)
|
|
]
|
|
assert statuses == ["pending"] * 3
|
|
approved = [
|
|
(await client.post(f"{API}/csv-data/fare_chart/approval/approve?owner_id={org['mr1'].id}", headers=auth["am1"])).json()["status"] for _ in range(3)
|
|
]
|
|
assert approved == ["approved"] * 3
|