phase-1: data model, auth, permissions, masters, settings, seed
This commit is contained in:
@@ -0,0 +1,389 @@
|
||||
from datetime import date, datetime, timezone
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from app.core.time_utils import KOLKATA, effective_today
|
||||
from app.models import (
|
||||
Asset,
|
||||
Attachment,
|
||||
CalibrationPlan,
|
||||
Category,
|
||||
Complaint,
|
||||
Contract,
|
||||
DemoEquipment,
|
||||
Department,
|
||||
DepartmentVisit,
|
||||
InsurancePolicy,
|
||||
Location,
|
||||
PMSchedule,
|
||||
User,
|
||||
Vendor,
|
||||
Warranty,
|
||||
)
|
||||
from app.seed.seed import seed_database
|
||||
from app.services.permissions import RESTRICTED_COMPLAINT_FIELDS, Role, has_capability
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def seeded_test_db(db, tmp_path_factory):
|
||||
upload_dir = tmp_path_factory.mktemp("test_uploads")
|
||||
seed_database(db, upload_dir=upload_dir)
|
||||
return upload_dir
|
||||
|
||||
|
||||
def _auth_headers(client, email: str, password: str = "Demo@123") -> dict[str, str]:
|
||||
res = client.post("/api/auth/login", json={"email": email, "password": password})
|
||||
assert res.status_code == 200, res.text
|
||||
token = res.json()["access_token"]
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def test_login_valid_and_invalid(client, db):
|
||||
res = client.post("/api/auth/login", json={"email": "admin@poona.demo", "password": "Demo@123"})
|
||||
assert res.status_code == 200
|
||||
body = res.json()
|
||||
assert body["token_type"] == "bearer"
|
||||
assert body["access_token"]
|
||||
assert body["user"]["email"] == "admin@poona.demo"
|
||||
assert body["user"]["role"] == "system_admin"
|
||||
assert body["user"]["demo_as_of_date"] is None
|
||||
|
||||
bad_pw = client.post("/api/auth/login", json={"email": "admin@poona.demo", "password": "wrong"})
|
||||
assert bad_pw.status_code == 401
|
||||
|
||||
bad_email = client.post("/api/auth/login", json={"email": "nobody@poona.demo", "password": "Demo@123"})
|
||||
assert bad_email.status_code == 401
|
||||
|
||||
# Inactive user rejected
|
||||
user = db.query(User).filter(User.email == "neha.chavan@poona.demo").first()
|
||||
assert user is not None
|
||||
user.is_active = False
|
||||
db.commit()
|
||||
try:
|
||||
res_inactive = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": "neha.chavan@poona.demo", "password": "Demo@123"},
|
||||
)
|
||||
assert res_inactive.status_code in (401, 403)
|
||||
finally:
|
||||
user.is_active = True
|
||||
db.commit()
|
||||
|
||||
|
||||
def test_auth_me(client):
|
||||
unauth = client.get("/api/auth/me")
|
||||
assert unauth.status_code == 401
|
||||
|
||||
bad_tok = client.get("/api/auth/me", headers={"Authorization": "Bearer invalid.token.here"})
|
||||
assert bad_tok.status_code == 401
|
||||
|
||||
headers = _auth_headers(client, "dept@poona.demo")
|
||||
me = client.get("/api/auth/me", headers=headers)
|
||||
assert me.status_code == 200
|
||||
data = me.json()
|
||||
assert data["email"] == "dept@poona.demo"
|
||||
assert data["role"] == "department_user"
|
||||
assert data["department_name"] == "Intensive Care Unit"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"email",
|
||||
[
|
||||
"bio@poona.demo",
|
||||
"dept@poona.demo",
|
||||
"mgmt@poona.demo",
|
||||
"coord@poona.demo",
|
||||
],
|
||||
)
|
||||
def test_non_admin_forbidden_on_admin_endpoints(client, db, email):
|
||||
headers = _auth_headers(client, email)
|
||||
dept = db.query(Department).first()
|
||||
target_user = db.query(User).filter(User.email == "dept@poona.demo").first()
|
||||
|
||||
# Masters GET is allowed for authenticated users
|
||||
assert client.get("/api/departments", headers=headers).status_code == 200
|
||||
assert client.get("/api/locations", headers=headers).status_code == 200
|
||||
assert client.get("/api/categories", headers=headers).status_code == 200
|
||||
assert client.get("/api/vendors", headers=headers).status_code == 200
|
||||
assert client.get("/api/users/lookup", headers=headers).status_code == 200
|
||||
|
||||
# Masters write -> 403
|
||||
assert (
|
||||
client.post(
|
||||
"/api/departments",
|
||||
headers=headers,
|
||||
json={"name": "Test Dept", "code": "TST"},
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
assert (
|
||||
client.patch(
|
||||
f"/api/departments/{dept.id}",
|
||||
headers=headers,
|
||||
json={"description": "hacked"},
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
assert client.delete(f"/api/departments/{dept.id}", headers=headers).status_code == 403
|
||||
|
||||
# Users admin endpoints -> 403
|
||||
assert client.get("/api/users", headers=headers).status_code == 403
|
||||
assert (
|
||||
client.post(
|
||||
"/api/users",
|
||||
headers=headers,
|
||||
json={
|
||||
"name": "Hack",
|
||||
"email": "hack@poona.demo",
|
||||
"password": "Password1",
|
||||
"role": "biomedical",
|
||||
},
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
assert (
|
||||
client.patch(
|
||||
f"/api/users/{target_user.id}",
|
||||
headers=headers,
|
||||
json={"name": "Hacked"},
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
assert client.delete(f"/api/users/{target_user.id}", headers=headers).status_code == 403
|
||||
|
||||
# Settings endpoints -> 403
|
||||
assert client.get("/api/settings", headers=headers).status_code == 403
|
||||
assert (
|
||||
client.put(
|
||||
"/api/settings",
|
||||
headers=headers,
|
||||
json={"values": {"contract_expiry_alert_days": 45}},
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
assert (
|
||||
client.post(
|
||||
"/api/settings/demo-date",
|
||||
headers=headers,
|
||||
json={"demo_as_of_date": "2026-06-01"},
|
||||
).status_code
|
||||
== 403
|
||||
)
|
||||
|
||||
|
||||
def test_admin_masters_and_users_crud(client):
|
||||
headers = _auth_headers(client, "admin@poona.demo")
|
||||
|
||||
# Department create, patch, soft-delete
|
||||
r = client.post(
|
||||
"/api/departments",
|
||||
headers=headers,
|
||||
json={"name": "Physiotherapy", "code": "PHYS", "description": "Rehab unit"},
|
||||
)
|
||||
assert r.status_code == 201
|
||||
dept_id = r.json()["id"]
|
||||
|
||||
r_patch = client.patch(
|
||||
f"/api/departments/{dept_id}",
|
||||
headers=headers,
|
||||
json={"description": "Physiotherapy & Rehab"},
|
||||
)
|
||||
assert r_patch.status_code == 200
|
||||
assert r_patch.json()["description"] == "Physiotherapy & Rehab"
|
||||
|
||||
r_del = client.delete(f"/api/departments/{dept_id}", headers=headers)
|
||||
assert r_del.status_code == 200
|
||||
assert r_del.json()["is_active"] is False
|
||||
|
||||
# User create, password update, soft-delete
|
||||
u_res = client.post(
|
||||
"/api/users",
|
||||
headers=headers,
|
||||
json={
|
||||
"name": "Temp Staff",
|
||||
"email": "temp.staff@poona.demo",
|
||||
"password": "InitPassword1",
|
||||
"role": "biomedical",
|
||||
},
|
||||
)
|
||||
assert u_res.status_code == 201
|
||||
uid = u_res.json()["id"]
|
||||
|
||||
# Update password
|
||||
u_patch = client.patch(
|
||||
f"/api/users/{uid}",
|
||||
headers=headers,
|
||||
json={"password": "NewPassword2"},
|
||||
)
|
||||
assert u_patch.status_code == 200
|
||||
login_new = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": "temp.staff@poona.demo", "password": "NewPassword2"},
|
||||
)
|
||||
assert login_new.status_code == 200
|
||||
|
||||
# Soft delete user
|
||||
u_del = client.delete(f"/api/users/{uid}", headers=headers)
|
||||
assert u_del.status_code == 200
|
||||
assert u_del.json()["is_active"] is False
|
||||
|
||||
|
||||
def test_permissions_matrix_all_roles():
|
||||
assert "action_taken" in RESTRICTED_COMPLAINT_FIELDS
|
||||
assert "spare_cost_estimate" in RESTRICTED_COMPLAINT_FIELDS
|
||||
|
||||
# system_admin
|
||||
assert has_capability(Role.system_admin, "manage_admin") is True
|
||||
assert has_capability(Role.system_admin, "edit_technical_fields") is True
|
||||
assert has_capability(Role.system_admin, "view_visits") is True
|
||||
|
||||
# biomedical
|
||||
assert has_capability(Role.biomedical, "manage_admin") is False
|
||||
assert has_capability(Role.biomedical, "create_edit_assets") is True
|
||||
assert has_capability(Role.biomedical, "edit_technical_fields") is True
|
||||
assert has_capability(Role.biomedical, "manage_pm") is True
|
||||
assert has_capability(Role.biomedical, "manage_visits") is True
|
||||
assert has_capability(Role.biomedical, "view_all_reports") is True
|
||||
|
||||
# department_user
|
||||
assert has_capability(Role.department_user, "manage_admin") is False
|
||||
assert has_capability(Role.department_user, "view_assets") is True
|
||||
assert has_capability(Role.department_user, "raise_complaint") is True
|
||||
assert has_capability(Role.department_user, "handle_complaint") is False
|
||||
assert has_capability(Role.department_user, "edit_technical_fields") is False
|
||||
assert has_capability(Role.department_user, "view_visits") is False
|
||||
assert has_capability(Role.department_user, "view_dept_reports") is True
|
||||
assert has_capability(Role.department_user, "view_all_reports") is False
|
||||
|
||||
# management
|
||||
assert has_capability(Role.management, "manage_admin") is False
|
||||
assert has_capability(Role.management, "view_assets") is True
|
||||
assert has_capability(Role.management, "view_visits") is True
|
||||
assert has_capability(Role.management, "view_all_reports") is True
|
||||
assert has_capability(Role.management, "create_edit_assets") is False
|
||||
assert has_capability(Role.management, "raise_complaint") is False
|
||||
assert has_capability(Role.management, "edit_technical_fields") is False
|
||||
|
||||
# coordinator
|
||||
assert has_capability(Role.coordinator, "manage_admin") is False
|
||||
assert has_capability(Role.coordinator, "edit_asset_contracts_only") is True
|
||||
assert has_capability(Role.coordinator, "handle_complaint") is True
|
||||
assert has_capability(Role.coordinator, "edit_technical_fields") is False
|
||||
assert has_capability(Role.coordinator, "manage_calibration_contracts") is True
|
||||
assert has_capability(Role.coordinator, "manage_demo_equipment") is True
|
||||
assert has_capability(Role.coordinator, "view_visits") is False
|
||||
|
||||
# invalid / None
|
||||
assert has_capability(None, "view_assets") is False
|
||||
assert has_capability("unknown_role", "view_assets") is False
|
||||
|
||||
|
||||
def test_settings_validation_and_demo_date(client, db):
|
||||
headers = _auth_headers(client, "admin@poona.demo")
|
||||
|
||||
res = client.get("/api/settings", headers=headers)
|
||||
assert res.status_code == 200
|
||||
items = res.json()["items"]
|
||||
assert len(items) == 14
|
||||
|
||||
# Out of range int
|
||||
bad_range = client.put(
|
||||
"/api/settings",
|
||||
headers=headers,
|
||||
json={"values": {"contract_expiry_alert_days": 999}},
|
||||
)
|
||||
assert bad_range.status_code == 422
|
||||
|
||||
# Bool passed to int
|
||||
bad_type = client.put(
|
||||
"/api/settings",
|
||||
headers=headers,
|
||||
json={"values": {"contract_expiry_alert_days": True}},
|
||||
)
|
||||
assert bad_type.status_code == 422
|
||||
|
||||
# Invalid enum
|
||||
bad_enum = client.put(
|
||||
"/api/settings",
|
||||
headers=headers,
|
||||
json={"values": {"default_pm_mode": "third_party"}},
|
||||
)
|
||||
assert bad_enum.status_code == 422
|
||||
|
||||
# Unknown key
|
||||
bad_key = client.put(
|
||||
"/api/settings",
|
||||
headers=headers,
|
||||
json={"values": {"nonexistent_key": 123}},
|
||||
)
|
||||
assert bad_key.status_code == 422
|
||||
|
||||
# Valid update
|
||||
ok_put = client.put(
|
||||
"/api/settings",
|
||||
headers=headers,
|
||||
json={"values": {"contract_expiry_alert_days": 45, "require_closure_remarks": False}},
|
||||
)
|
||||
assert ok_put.status_code == 200
|
||||
updated_map = {item["key"]: item["value"] for item in ok_put.json()["items"]}
|
||||
assert updated_map["contract_expiry_alert_days"] == 45
|
||||
assert updated_map["require_closure_remarks"] is False
|
||||
|
||||
# Demo date set & reset
|
||||
set_demo = client.post(
|
||||
"/api/settings/demo-date",
|
||||
headers=headers,
|
||||
json={"demo_as_of_date": "2026-06-15"},
|
||||
)
|
||||
assert set_demo.status_code == 200
|
||||
assert set_demo.json()["demo_as_of_date"] == "2026-06-15"
|
||||
assert set_demo.json()["effective_today"] == "2026-06-15"
|
||||
assert effective_today(db) == date(2026, 6, 15)
|
||||
|
||||
me_res = client.get("/api/auth/me", headers=headers)
|
||||
assert me_res.json()["demo_as_of_date"] == "2026-06-15"
|
||||
|
||||
# Reset demo date
|
||||
reset_demo = client.post(
|
||||
"/api/settings/demo-date",
|
||||
headers=headers,
|
||||
json={"demo_as_of_date": None},
|
||||
)
|
||||
assert reset_demo.status_code == 200
|
||||
assert reset_demo.json()["demo_as_of_date"] is None
|
||||
assert effective_today(db) == datetime.now(KOLKATA).date()
|
||||
|
||||
|
||||
def test_seed_idempotency_and_counts(db, tmp_path):
|
||||
summary1 = seed_database(db, upload_dir=tmp_path)
|
||||
summary2 = seed_database(db, upload_dir=tmp_path)
|
||||
assert summary1 == summary2
|
||||
|
||||
assert db.query(Department).count() == 8
|
||||
assert db.query(Location).count() == 20
|
||||
assert db.query(Category).count() == 8
|
||||
assert db.query(Vendor).count() == 12
|
||||
assert db.query(User).count() == 11
|
||||
assert db.query(Asset).count() == 60
|
||||
assert db.query(Contract).count() == 35
|
||||
assert db.query(Contract).filter(Contract.is_superseded.is_(True)).count() == 1
|
||||
assert db.query(Warranty).count() == 20
|
||||
assert db.query(InsurancePolicy).count() == 25
|
||||
assert db.query(CalibrationPlan).count() == 20
|
||||
assert db.query(PMSchedule).count() == 45
|
||||
assert db.query(PMSchedule).filter(PMSchedule.service_mode == "in_house").count() == 27
|
||||
assert db.query(PMSchedule).filter(PMSchedule.service_mode == "outsourced").count() == 18
|
||||
assert db.query(Complaint).count() == 30
|
||||
assert db.query(Complaint).filter(Complaint.call_status == "closed").count() == 22
|
||||
assert db.query(Complaint).filter(Complaint.call_status == "in_progress").count() == 4
|
||||
assert db.query(Complaint).filter(Complaint.call_status == "open").count() == 4
|
||||
assert db.query(Complaint).filter(Complaint.spare_required.is_(True)).count() == 6
|
||||
assert db.query(Complaint).filter(Complaint.ce_name.is_not(None)).count() == 5
|
||||
assert db.query(DemoEquipment).count() == 3
|
||||
assert db.query(DepartmentVisit).count() == 12
|
||||
assert db.query(Attachment).count() == 14
|
||||
|
||||
jpegs = list(Path(tmp_path).glob("*.jpg"))
|
||||
pdfs = list(Path(tmp_path).glob("*.pdf"))
|
||||
assert len(jpegs) == 8
|
||||
assert len(pdfs) == 6
|
||||
Reference in New Issue
Block a user