FluxIP/tests/conftest.py

92 lines
2.5 KiB
Python

from __future__ import annotations
from collections.abc import Iterator
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from app.core.config import AppSettings
from app.main import create_app
@pytest.fixture
def app_settings(tmp_path: Path) -> AppSettings:
return AppSettings(
environment="test",
database_path=tmp_path / "fluxip-test.db",
master_key_file=tmp_path / "fluxip-test.key",
scheduler_enabled=False,
cookie_secure=False,
trusted_hosts="testserver,localhost,127.0.0.1",
allowed_origins="http://testserver",
log_level="WARNING",
)
@pytest.fixture
def client(app_settings: AppSettings) -> Iterator[TestClient]:
application = create_app(app_settings)
with TestClient(
application,
base_url="http://testserver",
client=("127.0.0.1", 50000),
) as test_client:
yield test_client
@pytest.fixture
def authenticated_client(client: TestClient) -> TestClient:
response = client.post(
"/api/v1/auth/setup",
json={"username": "admin", "password": "correct-horse-battery-staple"},
)
assert response.status_code == 201
return client
def csrf_headers(client: TestClient) -> dict[str, str]:
csrf_token = client.cookies.get("fluxip_csrf")
assert csrf_token
return {"X-CSRF-Token": csrf_token}
@pytest.fixture
def integration_payload() -> dict[str, object]:
return {
"aws_access_key_id": "AKIA_TEST_ACCESS_KEY",
"aws_secret_access_key": "aws-secret-value",
"aws_session_token": "aws-session-value",
"use_default_aws_credentials": False,
"cloudflare_api_token": "cloudflare-secret-value",
}
@pytest.fixture
def managed_instance_payload() -> dict[str, object]:
return {
"id": "instance-one",
"display_name": "Proxy One",
"aws_region": "us-east-1",
"lightsail_instance_name": "proxy-node-one",
"cloudflare_zone_name": "example.com",
"cloudflare_zone_id": "zone-test-id",
"cloudflare_record_name": "one.example.com",
"socks_port": 1080,
"proxy_health_check": True,
"health_timeout_seconds": 120,
"release_grace_seconds": 75,
"enabled": True,
}
@pytest.fixture
def group_payload() -> dict[str, object]:
return {
"id": "group-one",
"name": "Primary proxies",
"enabled": True,
"interval_minutes": 15,
"member_ids": ["instance-one"],
}