74 lines
1.9 KiB
Python
74 lines
1.9 KiB
Python
import asyncio
|
|
from contextlib import contextmanager
|
|
import os
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
from src.db.base import SessionLocal
|
|
from src.db.session import get_db
|
|
from src.main import app
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
def client():
|
|
loop = asyncio.get_event_loop()
|
|
|
|
db = SessionLocal()
|
|
try:
|
|
async def _get_db():
|
|
try:
|
|
yield db
|
|
await db.flush()
|
|
finally:
|
|
await db.close()
|
|
app.dependency_overrides[get_db] = _get_db
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
app.dependency_overrides.clear()
|
|
finally:
|
|
loop.run_until_complete(db.rollback())
|
|
loop.run_until_complete(db.close())
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_password() -> str:
|
|
# Пароль админа берём из окружения, чтобы не хардкодить локальные отличия.
|
|
return os.getenv("OPENBAO__TEST_ADMIN_PASSWORD", "admin123")
|
|
|
|
|
|
@pytest.fixture
|
|
def isp_password() -> str:
|
|
# Пароль админа берём из окружения, чтобы не хардкодить локальные отличия.
|
|
return os.getenv("OPENBAO__TEST_EXECUTOR_PASSWORD", "admin123")
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_tokens(client, admin_password: str):
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username": "admin", "password": admin_password},
|
|
)
|
|
assert response.status_code == 200
|
|
return response.json()
|
|
|
|
|
|
@pytest.fixture
|
|
def isp_tokens(client, isp_password: str):
|
|
response = client.post(
|
|
"/api/v1/auth/login",
|
|
json={"username": "isp1", "password": isp_password},
|
|
)
|
|
assert response.status_code == 200
|
|
return response.json()
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_headers():
|
|
def _build(tokens: dict) -> dict:
|
|
return {"Authorization": f"Bearer {tokens['access_token']}"}
|
|
|
|
return _build
|