116 lines
7.1 KiB
Python
116 lines
7.1 KiB
Python
"""Company-form approvals live on the server, so everyone sees the same state at once."""
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
|
|
from app.csv_data.router import _signature
|
|
|
|
API = "/api/v1/csv-data"
|
|
|
|
|
|
@pytest.fixture
|
|
async def fare_chart(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.commit()
|
|
|
|
|
|
async def _add_row(client, auth) -> str:
|
|
resp = await client.post(f"{API}/fare_chart/rows", headers=auth["mr1"], json={"from_location": "A", "to_location": "B"})
|
|
assert resp.status_code == 201, resp.text
|
|
return resp.json()["id"]
|
|
|
|
|
|
def _by_table(resp):
|
|
return {a["table_name"]: a for a in resp.json()}
|
|
|
|
|
|
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.
|
|
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"}
|
|
for who in ("am1", "admin"):
|
|
resp = await client.get(f"{API}/approvals", headers=auth[who])
|
|
assert resp.status_code == 200 and resp.json() == [], (who, resp.text)
|
|
|
|
async def test_mr_requests_and_am_approves_and_both_see_it_immediately(self, client, auth, org, fare_chart):
|
|
await _add_row(client, auth)
|
|
sent = await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])
|
|
assert sent.status_code == 200 and sent.json()["status"] == "pending"
|
|
assert _by_table(await client.get(f"{API}/approvals", headers=auth["am1"]))["fare_chart"]["status"] == "pending"
|
|
|
|
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.
|
|
seen = _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"]))["fare_chart"]
|
|
assert (seen["status"], seen["approved_by_name"]) == ("approved", "Suresh Behera")
|
|
|
|
async def test_an_empty_form_cannot_be_sent_for_approval(self, client, auth, fare_chart):
|
|
resp = await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])
|
|
assert resp.status_code == 422, resp.text
|
|
assert _by_table(await client.get(f"{API}/approvals", headers=auth["mr1"]))["fare_chart"]["status"] == "draft"
|
|
|
|
async def test_nothing_can_be_approved_until_it_has_been_sent(self, client, auth, org, fare_chart):
|
|
await _add_row(client, auth)
|
|
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
|
|
|
|
async def test_double_clicks_are_harmless(self, client, auth, org, fare_chart):
|
|
await _add_row(client, auth)
|
|
for _ in range(2):
|
|
assert (await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])).json()["status"] == "pending"
|
|
for _ in range(2):
|
|
assert (await client.post(f"{API}/fare_chart/approval/approve?owner_id={org['mr1'].id}", headers=auth["am1"])).json()["status"] == "approved"
|
|
# Once approved, sending again is refused until something changes.
|
|
assert (await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])).status_code == 409
|
|
|
|
async def test_only_the_right_role_can_act(self, client, auth, org):
|
|
for who in ("am1", "admin", "gm"):
|
|
assert (await client.post(f"{API}/fare_chart/approval/request", headers=auth[who])).status_code == 403, who
|
|
for who in ("mr1", "admin", "gm"):
|
|
assert (await client.post(f"{API}/fare_chart/approval/approve?owner_id={org['mr1'].id}", headers=auth[who])).status_code == 403, who
|
|
|
|
async def test_forms_without_an_approval_step_are_not_found(self, client, auth, org):
|
|
for table in ("weekly_report", "nonsense"):
|
|
assert (await client.post(f"{API}/{table}/approval/approve?owner_id={org['mr1'].id}", headers=auth["am1"])).status_code == 404
|
|
|
|
async def test_changing_the_rows_withdraws_a_pending_or_approved_form(self, client, auth, org, fare_chart):
|
|
row_id = await _add_row(client, auth)
|
|
for change in (
|
|
lambda: client.patch(f"{API}/fare_chart/rows/{row_id}", headers=auth["mr1"], json={"to_location": "C"}),
|
|
lambda: client.post(f"{API}/fare_chart/rows", headers=auth["mr1"], json={"from_location": "X", "to_location": "Y"}),
|
|
):
|
|
await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])
|
|
await client.post(f"{API}/fare_chart/approval/approve?owner_id={org['mr1'].id}", headers=auth["am1"])
|
|
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.
|
|
# 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
|
|
|
|
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.
|
|
mr = org["mr1"].id
|
|
assert await _signature("fare_chart", mr, db) == ("pending", None, None)
|
|
spoof = await client.get(f"{API}/fare_chart/pdf?approval_status=approved&am_name=Somebody", headers=auth["mr1"])
|
|
assert spoof.status_code == 200
|
|
await _add_row(client, auth)
|
|
await client.post(f"{API}/fare_chart/approval/request", headers=auth["mr1"])
|
|
await client.post(f"{API}/fare_chart/approval/approve?owner_id={org['mr1'].id}", headers=auth["am1"])
|
|
await db.rollback() # see the other session's commit
|
|
status, name, when = await _signature("fare_chart", mr, db)
|
|
assert (status, name) == ("approved", "Suresh Behera")
|
|
assert when and len(when) == 16 and when[2] == "/" and when[13] == ":" # dd/mm/yyyy HH:MM
|
|
assert await _signature("weekly_report", mr, db) == (None, None, None)
|