"""Helpers shared by the server tests.""" from __future__ import annotations import os from pathlib import Path from types import SimpleNamespace from typing import Any import pytest from packetstation.settings import Settings TEST_SESSION_KEY = "test-session-key-0123456789abcdef0123456789" TEST_SEAL_KEY = "test-seal-key-fedcba9876543210fedcba9876543210" ORIGIN_HEADERS = {"Origin": "http://testserver", "X-Requested-With": "PacketStation"} def make_settings(tmp: Path, **over: Any) -> Settings: """Settings for tests. Writes a throwaway session key file unless `session_key_file` is given.""" if "session_key_file" not in over: key_file = tmp / "session_key" if not key_file.exists(): key_file.parent.mkdir(parents=True, exist_ok=True) key_file.write_text(TEST_SESSION_KEY + "\n") over["session_key_file"] = key_file if "seal_key_file" not in over: seal_file = tmp / "seal_key" if not seal_file.exists(): seal_file.parent.mkdir(parents=True, exist_ok=True) seal_file.write_text(TEST_SEAL_KEY + "\n") over["seal_key_file"] = seal_file values: dict[str, Any] = { "_env_file": None, "stack": "ci", "trusted_proxy_host": "", "static_dir": tmp / "no-static", "assets_dir": tmp / "assets", "backup_dir": tmp / "no-backups", } values.update(over) return Settings(**values) def fake_visual(production: bool = False, explicit: bool = False) -> SimpleNamespace: server, database = ("vsql", "PESCOINC") if production else (r"VSQL\DEV", "PESCOINCDEV") return SimpleNamespace( is_production=production, server=server, database=database, model_fields_set={"server", "database"} if explicit else set(), ) PG_URL = os.environ.get("TEST_PG_SUPERUSER_URL") needs_pg = pytest.mark.skipif(not PG_URL, reason="TEST_PG_SUPERUSER_URL not set (no PostgreSQL)") def sign_in(client: Any, login: str, password: str | None = None) -> Any: """Password sign-in through the API (FakeDirectory password by default); returns the response.""" from fakedir import FAKE_PASSWORD return client.post( "/api/auth/signin/password", json={"login": login, "password": FAKE_PASSWORD if password is None else password} ) def app_conn(settings: Settings) -> Any: """An autocommit connection as the app role (for test setup and checks).""" from packetstation import db return db.connect_as("app", settings, autocommit=True) PIN = "246810" def register_device(client: Any, sam: str = "sup.wes", name: str = "Bldg 2 tablet 3") -> dict[str, str]: assert sign_in(client, sam).status_code == 200 r = client.post("/api/account/device", json={"name": name}) assert r.status_code == 200, r.text assert "ps_device=" in r.headers["set-cookie"] and "max-age=31536000" in r.headers["set-cookie"].lower() assert client.post("/api/auth/signout").status_code == 204 device: dict[str, str] = r.json()["device"] return device def set_pin(client: Any, sam: str, pin: str = PIN) -> None: from fakedir import FAKE_PASSWORD assert sign_in(client, sam).status_code == 200 r = client.post("/api/account/pin", json={"password": FAKE_PASSWORD, "pin": pin}) assert r.status_code == 200, r.text assert r.json() == {"pin": {"set": True, "disabled": False}} assert client.post("/api/auth/signout").status_code == 204 def pin_signin(client: Any, emp_no: str, pin: str = PIN) -> Any: return client.post("/api/auth/signin/pin", json={"emp_no": emp_no, "pin": pin})