194 lines
10 KiB
Python
194 lines
10 KiB
Python
"""Product promoted/sample, POB, stockist visit and order: stored, returned, validated, printed."""
|
|
|
|
from datetime import date
|
|
|
|
from tests.e2e.test_full_approval_flow import _create_masters
|
|
|
|
API = "/api/v1"
|
|
|
|
|
|
async def _draft(client, auth, org):
|
|
doctors, chemists = await _create_masters(client, auth, org, n_doctors=2, n_chemists=1)
|
|
resp = await client.post(f"{API}/dcr", headers=auth["mr1"], json={"report_date": date.today().isoformat()})
|
|
assert resp.status_code == 201, resp.text
|
|
return resp.json()["id"], doctors, chemists
|
|
|
|
|
|
class TestFormFields:
|
|
async def test_fields_round_trip_through_the_api(self, client, auth, org):
|
|
dcr_id, doctors, chemists = await _draft(client, auth, org)
|
|
d = await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET",
|
|
"product_promote": " Calcium ", "product_sample": "Vit D", "remarks": "ok"},
|
|
)
|
|
assert d.status_code == 201, d.text
|
|
c = await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/chemist", headers=auth["mr1"],
|
|
json={"chemist_id": chemists[0]["id"], "visit_time": "10:00:00", "pob": "12,500"},
|
|
)
|
|
assert c.status_code == 201, c.text
|
|
patched = await client.patch(
|
|
f"{API}/dcr/{dcr_id}", headers=auth["mr1"],
|
|
json={"stockist_visit": "Ganesh Distributors", "order_value": "45,000"},
|
|
)
|
|
assert patched.status_code == 200, patched.text
|
|
|
|
report = (await client.get(f"{API}/dcr/{dcr_id}", headers=auth["mr1"])).json()
|
|
assert report["doctor_calls"][0]["product_promote"] == "Calcium" # trimmed
|
|
assert report["doctor_calls"][0]["product_sample"] == "Vit D"
|
|
assert report["chemist_calls"][0]["pob"] == "12,500"
|
|
assert report["stockist_visit"] == "Ganesh Distributors"
|
|
assert report["order_value"] == "45,000"
|
|
|
|
async def test_blank_values_are_stored_as_null_and_can_be_cleared(self, client, auth, org):
|
|
dcr_id, _, _ = await _draft(client, auth, org)
|
|
await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "X"})
|
|
cleared = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": " "})
|
|
assert cleared.json()["stockist_visit"] is None
|
|
|
|
async def test_patching_one_field_leaves_the_others_alone(self, client, auth, org):
|
|
dcr_id, _, _ = await _draft(client, auth, org)
|
|
await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"mr_remarks": "Good day", "order_value": "10"})
|
|
again = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "S"})
|
|
body = again.json()
|
|
assert (body["mr_remarks"], body["order_value"], body["stockist_visit"]) == ("Good day", "10", "S")
|
|
|
|
async def test_too_long_values_are_rejected(self, client, auth, org):
|
|
dcr_id, doctors, _ = await _draft(client, auth, org)
|
|
too_long = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"stockist_visit": "x" * 201})
|
|
assert too_long.status_code == 422
|
|
bad_call = await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET", "product_promote": "x" * 201},
|
|
)
|
|
assert bad_call.status_code == 422
|
|
|
|
async def test_only_the_owner_can_edit_them_and_only_while_editable(self, client, auth, org):
|
|
dcr_id, doctors, _ = await _draft(client, auth, org)
|
|
assert (await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr2"], json={"order_value": "1"})).status_code in (403, 404)
|
|
assert (await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["am1"], json={"order_value": "1"})).status_code == 403
|
|
await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
|
|
)
|
|
assert (await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["mr1"])).status_code == 200
|
|
locked = await client.patch(f"{API}/dcr/{dcr_id}", headers=auth["mr1"], json={"order_value": "1"})
|
|
assert locked.status_code == 409 # submitted reports are locked
|
|
|
|
async def test_approved_pdf_is_generated_with_the_new_layout(self, client, auth, org):
|
|
dcr_id, doctors, chemists = await _draft(client, auth, org)
|
|
await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET", "product_promote": "Calcium"},
|
|
)
|
|
await client.post(f"{API}/dcr/{dcr_id}/submit", headers=auth["mr1"])
|
|
assert (await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["am1"], json={})).status_code == 200
|
|
pdf = await client.get(f"{API}/dcr/{dcr_id}/pdf", headers=auth["mr1"])
|
|
assert pdf.status_code == 200 and pdf.content.startswith(b"%PDF-")
|
|
|
|
|
|
class TestResponsesReflectTheChange:
|
|
"""Regression: the response to an add/remove used to list the visits as they were BEFORE it."""
|
|
|
|
async def test_adding_a_visit_returns_it_in_the_same_response(self, client, auth, org):
|
|
dcr_id, doctors, chemists = await _draft(client, auth, org)
|
|
added = await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctors[0]["id"], "visit_time": "09:30:00", "call_outcome": "MET"},
|
|
)
|
|
assert [c["doctor_id"] for c in added.json()["doctor_calls"]] == [doctors[0]["id"]]
|
|
second = await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/chemist", headers=auth["mr1"],
|
|
json={"chemist_id": chemists[0]["id"], "visit_time": "10:00:00"},
|
|
)
|
|
body = second.json()
|
|
assert len(body["doctor_calls"]) == 1 and len(body["chemist_calls"]) == 1
|
|
|
|
async def test_removing_a_visit_returns_the_report_without_it(self, client, auth, org):
|
|
dcr_id, doctors, _ = await _draft(client, auth, org)
|
|
for i, doctor in enumerate(doctors):
|
|
r = await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
json={"doctor_id": doctor["id"], "visit_time": f"09:{30 + i}:00", "call_outcome": "MET"},
|
|
)
|
|
call_id = r.json()["doctor_calls"][0]["id"]
|
|
removed = await client.delete(f"{API}/dcr/{dcr_id}/calls/{call_id}", headers=auth["mr1"])
|
|
assert removed.status_code == 200, removed.text
|
|
assert call_id not in [c["id"] for c in removed.json()["doctor_calls"]]
|
|
assert len(removed.json()["doctor_calls"]) == len(doctors) - 1
|
|
# ...and a fresh read agrees.
|
|
again = (await client.get(f"{API}/dcr/{dcr_id}", headers=auth["mr1"])).json()
|
|
assert len(again["doctor_calls"]) == len(doctors) - 1
|
|
|
|
|
|
class TestDownloadUsesTheCurrentLayout:
|
|
async def test_a_report_approved_under_an_older_layout_still_downloads_in_the_new_one(self, client, auth, org, db):
|
|
from pathlib import Path
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.reports.models import DailyCallReport
|
|
|
|
dcr_id, doctors, _ = await _draft(client, auth, org)
|
|
await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
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["mr1"])
|
|
assert (await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["am1"], json={})).status_code == 200
|
|
|
|
# Simulate the file stored back when an older design was in use.
|
|
stored = (await db.execute(select(DailyCallReport.pdf_path).where(DailyCallReport.id == dcr_id))).scalar_one()
|
|
Path(stored).write_bytes(b"%PDF-1.4 OLD DESIGN")
|
|
|
|
pdf = await client.get(f"{API}/dcr/{dcr_id}/pdf", headers=auth["mr1"])
|
|
assert pdf.status_code == 200
|
|
assert b"OLD DESIGN" not in pdf.content and pdf.content.startswith(b"%PDF-") and len(pdf.content) > 5000
|
|
# ...and still only for approved reports.
|
|
draft_id, _, _ = await _draft_second(client, auth, org)
|
|
assert (await client.get(f"{API}/dcr/{draft_id}/pdf", headers=auth["mr1"])).status_code == 422
|
|
|
|
|
|
async def _draft_second(client, auth, org):
|
|
"""A second, unapproved report (yesterday's) for the same MR."""
|
|
from datetime import timedelta
|
|
|
|
resp = await client.post(
|
|
f"{API}/dcr", headers=auth["mr1"], json={"report_date": (date.today() - timedelta(days=1)).isoformat()}
|
|
)
|
|
assert resp.status_code == 201, resp.text
|
|
return resp.json()["id"], None, None
|
|
|
|
|
|
class TestApprovedPdfIsSigned:
|
|
async def test_the_pdf_names_the_approving_area_manager_from_the_audit_trail(self, client, auth, org, db):
|
|
from sqlalchemy import select
|
|
|
|
from app.pdf.service import PDFService
|
|
from app.reports.models import DailyCallReport
|
|
from app.reports.repository import DCRRepository
|
|
|
|
dcr_id, doctors, _ = await _draft(client, auth, org)
|
|
await client.post(
|
|
f"{API}/dcr/{dcr_id}/calls/doctor", headers=auth["mr1"],
|
|
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["mr1"])
|
|
assert (await client.post(f"{API}/dcr/{dcr_id}/approve", headers=auth["am1"], json={})).status_code == 200
|
|
|
|
report = await DCRRepository(db).get_full(
|
|
(await db.execute(select(DailyCallReport.id).where(DailyCallReport.id == dcr_id))).scalar_one()
|
|
)
|
|
context = await PDFService(db)._context(report)
|
|
assert context["approval"]["name"] == "Suresh Behera"
|
|
assert len(context["approval"]["at"]) == 16 and context["mr_signed"] == "Ravi Das (MR001)"
|
|
|
|
async def test_a_report_that_is_not_approved_has_no_approval_signature(self, client, auth, org, db):
|
|
from app.pdf.service import PDFService
|
|
from app.reports.repository import DCRRepository
|
|
|
|
dcr_id, _, _ = await _draft(client, auth, org)
|
|
report = await DCRRepository(db).get_full(dcr_id if not isinstance(dcr_id, str) else __import__("uuid").UUID(dcr_id))
|
|
assert (await PDFService(db)._context(report))["approval"] is None
|