import io import uuid from datetime import date from fastapi.testclient import TestClient from app.main import app from app.auth.security import get_current_user def _csv_file(name: str, text: str): return ("files", (name, io.BytesIO(text.encode("utf-8")), "text/csv")) def _seed_file(client: TestClient, file_no: str, owner_id: str = None) -> None: owner_id = owner_id or f"R{uuid.uuid4().hex[:6]}" rolodex_csv = f"Id,Last\n{owner_id},Alpha\n" files_csv = ( "File_No,Id,File_Type,Regarding,Opened,Empl_Num,Status,Rate_Per_Hour\n" f"{file_no},{owner_id},CIVIL,Test,{date.today():%Y-%m-%d},E01,ACTIVE,100\n" ) client.post("/api/import/batch-upload", files=[ _csv_file("ROLODEX.csv", rolodex_csv), _csv_file("FILES.csv", files_csv), ]) def _auth(): app.dependency_overrides[get_current_user] = lambda: { "id": 1, "username": "tester", "is_admin": True, "is_active": True, } def test_schedule_tokenized_search_version_and_frequency(): _auth() client = TestClient(app) file_no = f"PF-SS-{uuid.uuid4().hex[:8]}" _seed_file(client, file_no) client.post("/api/pensions/schedules", json={"file_no": file_no, "version": "A1", "frequency": "Monthly", "vests_on": "2024-01-01"}) client.post("/api/pensions/schedules", json={"file_no": file_no, "version": "B2", "frequency": "Quarterly", "vests_on": "2024-02-01"}) # Both tokens must be present across allowed columns r = client.get("/api/pensions/schedules", params={"file_no": file_no, "search": "B2 Month"}) assert r.status_code == 200 items = r.json() # No schedule has both 'B2' and 'Month' assert items == [] # Single token search r = client.get("/api/pensions/schedules", params={"file_no": file_no, "search": "Monthly"}) assert r.status_code == 200 items = r.json() assert len(items) == 1 and items[0]["frequency"] == "Monthly" app.dependency_overrides.pop(get_current_user, None) def test_marriages_tokenized_search_spouse_and_notes(): _auth() client = TestClient(app) file_no = f"PF-MS-{uuid.uuid4().hex[:8]}" _seed_file(client, file_no) client.post("/api/pensions/marriages", json={"file_no": file_no, "version": "01", "spouse_name": "Jane Doe", "notes": "Alpha beta"}) client.post("/api/pensions/marriages", json={"file_no": file_no, "version": "02", "spouse_name": "John Smith", "notes": "Gamma delta"}) # Both tokens required across fields r = client.get("/api/pensions/marriages", params={"file_no": file_no, "search": "Jane delta"}) assert r.status_code == 200 items = r.json() assert items == [] # Single token r = client.get("/api/pensions/marriages", params={"file_no": file_no, "search": "Gamma"}) assert r.status_code == 200 items = r.json() assert len(items) == 1 and items[0]["spouse_name"] == "John Smith" app.dependency_overrides.pop(get_current_user, None) def test_death_tokenized_search_beneficiary_and_type(): _auth() client = TestClient(app) file_no = f"PF-DS-{uuid.uuid4().hex[:8]}" _seed_file(client, file_no) client.post("/api/pensions/death-benefits", json={"file_no": file_no, "version": "01", "beneficiary_name": "Alice", "benefit_type": "Lump Sum", "notes": "Alpha"}) client.post("/api/pensions/death-benefits", json={"file_no": file_no, "version": "02", "beneficiary_name": "Bob", "benefit_type": "Annuity", "notes": "Beta"}) # Both tokens required r = client.get("/api/pensions/death-benefits", params={"file_no": file_no, "search": "Alice Annuity"}) assert r.status_code == 200 items = r.json() assert items == [] # Single token r = client.get("/api/pensions/death-benefits", params={"file_no": file_no, "search": "Annuity"}) assert r.status_code == 200 items = r.json() assert len(items) == 1 and items[0]["benefit_type"] == "Annuity" app.dependency_overrides.pop(get_current_user, None) def test_separations_tokenized_search_terms_and_notes(): _auth() client = TestClient(app) file_no = f"PF-SSep-{uuid.uuid4().hex[:8]}" _seed_file(client, file_no) client.post("/api/pensions/separations", json={"file_no": file_no, "version": "01", "agreement_date": "2024-01-01", "terms": "Alpha Clause", "notes": "First"}) client.post("/api/pensions/separations", json={"file_no": file_no, "version": "02", "agreement_date": "2024-02-01", "terms": "Beta Clause", "notes": "Second"}) # Both tokens required r = client.get("/api/pensions/separations", params={"file_no": file_no, "search": "Alpha Second"}) assert r.status_code == 200 items = r.json() assert items == [] # Single token r = client.get("/api/pensions/separations", params={"file_no": file_no, "search": "Clause"}) assert r.status_code == 200 items = r.json() assert len(items) == 2 app.dependency_overrides.pop(get_current_user, None)