import pytest from app.core.security import hash_password from app.models import Asset, Department, User from app.seed.seed import seed_database from app.services.scoping import DepartmentScope, resolve_department_scope @pytest.fixture(scope="module", autouse=True) def seeded_task6_db(db, tmp_path_factory): upload_dir = tmp_path_factory.mktemp("task6_test_uploads") seed_database(db, upload_dir=upload_dir) return upload_dir def _auth_headers(client, email: str) -> dict[str, str]: res = client.post("/api/auth/login", json={"email": email, "password": "Demo@123"}) assert res.status_code == 200, res.text return {"Authorization": f"Bearer {res.json()['access_token']}"} def test_resolve_department_scope_rules(): # Admin is unconstrained admin_user = User(role="system_admin", department_id=None) scope = resolve_department_scope(admin_user, requested_dept_id=5) assert scope == DepartmentScope(is_unconstrained=True, department_id=5, is_empty=False) # Department user with department_id=None is empty unassigned = User(role="department_user", department_id=None) scope_empty = resolve_department_scope(unassigned, requested_dept_id=5) assert scope_empty == DepartmentScope(is_unconstrained=False, department_id=None, is_empty=True) # Department user with assigned department cannot be overridden assigned = User(role="department_user", department_id=1) scope_assigned = resolve_department_scope(assigned, requested_dept_id=99) assert scope_assigned == DepartmentScope(is_unconstrained=False, department_id=1, is_empty=False) def test_unassigned_department_user_sees_zero_records(client, db): # Create a department_user with department_id = None email = "unassigned_dept_user@xyz.demo" user = db.query(User).filter(User.email == email).first() if not user: user = User( email=email, name="Unassigned Dept User", password_hash=hash_password("Demo@123"), role="department_user", department_id=None, is_active=True, ) db.add(user) db.commit() else: user.department_id = None db.commit() headers = _auth_headers(client, email) # 1. Dashboard summary has all-zero counters dash_res = client.get("/api/dashboard/summary", headers=headers) assert dash_res.status_code == 200 dash_data = dash_res.json() kpis = dash_data["kpis"] assert kpis["total_assets"] == 0 assert kpis["assets_under_repair"] == 0 assert kpis["open_complaints"] == 0 assert kpis["pm_due_this_month"] == 0 assert kpis["calibrations_due"] == 0 assert kpis["contracts_expiring"] == 0 assert kpis["equipment_on_demo"] == 0 assert len(dash_data["recent_complaints"]) == 0 assert len(dash_data["needs_attention"]) == 0 # 2. Global search returns 0 assets and 0 complaints search_res = client.get("/api/search?q=card", headers=headers) assert search_res.status_code == 200 search_data = search_res.json() assert search_data["assets"] == [] assert search_data["complaints"] == [] # 3. All accessible report endpoints return 0 rows for this user report_keys = [ "equipment-inventory", "monthly-maintenance", "monthly-department", "breakdown-complaints", "pm-compliance", "calibration-due", "contracts-warranty", "demo-equipment", ] for key in report_keys: rep_res = client.get(f"/api/reports/{key}", headers=headers) assert rep_res.status_code == 200, f"{key} failed: {rep_res.text}" body = rep_res.json() assert len(body["rows"]) == 0, f"Expected 0 rows for {key}, got {len(body['rows'])}" def test_assigned_department_user_scoping_ignores_query_param(client, db): # Get two distinct departments that have assets depts = db.query(Department).filter(Department.is_active.is_(True)).all() assert len(depts) >= 2 dept1, dept2 = depts[0], depts[1] email = "dept1_scoped_user@xyz.demo" user = db.query(User).filter(User.email == email).first() if not user: user = User( email=email, name="Dept1 Scoped User", password_hash=hash_password("Demo@123"), role="department_user", department_id=dept1.id, is_active=True, ) db.add(user) db.commit() else: user.department_id = dept1.id db.commit() headers = _auth_headers(client, email) # Calling with ?department_id=dept2.id MUST be ignored and scoped strictly to dept1 res = client.get( f"/api/reports/equipment-inventory?department_id={dept2.id}", headers=headers, ) assert res.status_code == 200 body = res.json() assert body["filters_applied"]["department_id"] == dept1.id for row in body["rows"]: assert row["department_id"] == dept1.id