SurfSense/surfsense_backend/tests/conftest.py
Anish Sarkar 4ff712578d refactor: Enhance test utilities for document upload by integrating search space handling
- Updated test fixtures to include search space ID retrieval for improved document upload tests.
- Refactored authentication and document upload functions to accept search space ID as a parameter.
- Removed hardcoded search space ID references to streamline test configurations.
2026-02-25 17:29:09 +05:30

77 lines
2 KiB
Python

"""Root conftest — shared fixtures available to all test modules."""
from __future__ import annotations
import contextlib
from collections.abc import AsyncGenerator
import httpx
import pytest
from tests.utils.helpers import (
BACKEND_URL,
auth_headers,
delete_document,
get_auth_token,
get_search_space_id,
)
@pytest.fixture(scope="session")
def backend_url() -> str:
return BACKEND_URL
@pytest.fixture(scope="session")
async def auth_token(backend_url: str) -> str:
"""Authenticate once per session, registering the user if needed."""
async with httpx.AsyncClient(
base_url=backend_url, timeout=30.0
) as client:
return await get_auth_token(client)
@pytest.fixture(scope="session")
async def search_space_id(backend_url: str, auth_token: str) -> int:
"""Discover the first search space belonging to the test user."""
async with httpx.AsyncClient(
base_url=backend_url, timeout=30.0
) as client:
return await get_search_space_id(client, auth_token)
@pytest.fixture(scope="session")
def headers(auth_token: str) -> dict[str, str]:
"""Authorization headers reused across all tests in the session."""
return auth_headers(auth_token)
@pytest.fixture
async def client(backend_url: str) -> AsyncGenerator[httpx.AsyncClient]:
"""Per-test async HTTP client pointing at the running backend."""
async with httpx.AsyncClient(
base_url=backend_url, timeout=180.0
) as c:
yield c
@pytest.fixture
def cleanup_doc_ids() -> list[int]:
"""Accumulator for document IDs that should be deleted after the test."""
return []
@pytest.fixture(autouse=True)
async def _cleanup_documents(
client: httpx.AsyncClient,
headers: dict[str, str],
cleanup_doc_ids: list[int],
):
"""
Runs after every test. Deletes any document IDs that were appended to
the ``cleanup_doc_ids`` list during the test body.
"""
yield
for doc_id in cleanup_doc_ids:
with contextlib.suppress(Exception):
await delete_document(client, headers, doc_id)