Files
FTL/test/api/conftest.py
T
Dominik e189c8e823 test: add pytest API test files and final BATS validation suite
Add the pytest test files and final BATS suite that were described
in the previous commit but not yet included.

New files:
  test/api/conftest.py     — shared fixtures (api_session, openapi, ftl)
  test/api/pytest.ini      — pytest configuration
  test/api/test_api.py     — HTTP errors, config validation, search,
                              history, lists, queries, Lua pages
  test/api/test_openapi.py — OpenAPI spec validation, teleporter
  test/api/test_z_auth.py  — auth workflow (app password, login,
                              rate limiting, password removal)
  test/test_final.bats     — log validation, config rotation counts,
                              FTL termination

Signed-off-by: Dominik <dl6er@dl6er.de>
2026-03-25 11:28:03 +01:00

76 lines
2.2 KiB
Python

"""
Shared pytest fixtures for FTL API integration tests.
"""
import sys
import os
import pytest
import requests
# Add test/api to the path so libs/ can be imported
sys.path.insert(0, os.path.dirname(__file__))
FTL_URL = "http://127.0.0.1"
@pytest.fixture(scope="session")
def ftl_url():
"""Base URL for the FTL API."""
return FTL_URL
@pytest.fixture(scope="session")
def api_session():
"""Shared authenticated requests.Session for the entire test run.
If no password is set, the session works without authentication.
If a password is set, logs in with "ABC" and stores the SID in
the session headers so all subsequent requests are authenticated.
"""
session = requests.Session()
session.headers["Accept"] = "application/json"
try:
r = session.get(f"{FTL_URL}/api/auth", timeout=5)
if r.status_code not in (200, 401):
r.raise_for_status()
except requests.ConnectionError:
pytest.skip("FTL is not running at " + FTL_URL)
data = r.json()
if not data.get("session", {}).get("valid"):
# Password is set — login with "ABC"
r = session.post(f"{FTL_URL}/api/auth",
json={"password": "ABC"}, timeout=10)
sid = r.json().get("session", {}).get("sid")
if sid:
session.headers["X-FTL-SID"] = sid
return session
@pytest.fixture(scope="session")
def openapi():
"""Parsed OpenAPI specifications (session-scoped, parsed once)."""
from libs.openAPI import openApi
specs = openApi(base_path="src/api/docs/content/specs/", api_root="/api")
assert specs.parse("main.yaml"), "Failed to parse OpenAPI specs"
return specs
@pytest.fixture(scope="session")
def ftl():
"""FTLAPI client with endpoints loaded (session-scoped).
Authenticates with password "ABC" if a password is set, otherwise
connects without authentication.
"""
from libs.FTLAPI import FTLAPI
# Check if authentication is required
r = requests.get(f"{FTL_URL}/api/auth", timeout=5)
data = r.json()
if data.get("session", {}).get("valid"):
client = FTLAPI(FTL_URL)
else:
client = FTLAPI(FTL_URL, "ABC")
client.get_endpoints()
return client