210 lines
4.9 KiB
Python
210 lines
4.9 KiB
Python
"""Pytest configuration and fixtures for all tests."""
|
|
|
|
from collections.abc import Generator
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from app.core.deps import get_db
|
|
from app.database.base import Base
|
|
from app.main import app
|
|
|
|
# Use in-memory SQLite for tests
|
|
SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
|
|
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
|
|
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def db() -> Generator[Session, None, None]:
|
|
"""
|
|
Create a fresh database for each test.
|
|
|
|
Yields:
|
|
Database session
|
|
"""
|
|
# Create all tables
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
# Create session
|
|
session = TestingSessionLocal()
|
|
|
|
try:
|
|
yield session
|
|
finally:
|
|
session.close()
|
|
# Drop all tables after test
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def client(db: Session) -> Generator[TestClient, None, None]:
|
|
"""
|
|
Create a test client with database override.
|
|
|
|
Args:
|
|
db: Test database session
|
|
|
|
Yields:
|
|
FastAPI test client
|
|
"""
|
|
|
|
def override_get_db():
|
|
try:
|
|
yield db
|
|
finally:
|
|
pass
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
@pytest.fixture
|
|
def test_user_data() -> dict:
|
|
"""
|
|
Standard test user data.
|
|
|
|
Returns:
|
|
Dictionary with test user credentials
|
|
"""
|
|
return {"email": "test@example.com", "password": "TestPassword123"}
|
|
|
|
|
|
@pytest.fixture
|
|
def test_user_data_weak_password() -> dict:
|
|
"""
|
|
Test user data with weak password.
|
|
|
|
Returns:
|
|
Dictionary with weak password
|
|
"""
|
|
return {"email": "test@example.com", "password": "weak"}
|
|
|
|
|
|
@pytest.fixture
|
|
def test_user_data_no_uppercase() -> dict:
|
|
"""
|
|
Test user data with no uppercase letter.
|
|
|
|
Returns:
|
|
Dictionary with invalid password
|
|
"""
|
|
return {"email": "test@example.com", "password": "testpassword123"}
|
|
|
|
|
|
@pytest.fixture
|
|
def test_user(client: TestClient, test_user_data: dict):
|
|
"""
|
|
Create and return a test user.
|
|
|
|
Args:
|
|
client: Test client
|
|
test_user_data: User credentials
|
|
|
|
Returns:
|
|
User object
|
|
"""
|
|
from app.database.models.user import User
|
|
|
|
response = client.post("/api/v1/auth/register", json=test_user_data)
|
|
user_id = response.json()["id"]
|
|
|
|
# Get user from database (use same db session)
|
|
from app.core.deps import get_db
|
|
|
|
db_gen = next(app.dependency_overrides[get_db]())
|
|
user = db_gen.query(User).filter(User.id == user_id).first()
|
|
return user
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_headers(client: TestClient, test_user_data: dict) -> dict:
|
|
"""
|
|
Create authenticated headers with JWT token.
|
|
|
|
Args:
|
|
client: Test client
|
|
test_user_data: User credentials
|
|
|
|
Returns:
|
|
Dictionary with Authorization header
|
|
"""
|
|
# Register and login
|
|
client.post("/api/v1/auth/register", json=test_user_data)
|
|
login_response = client.post("/api/v1/auth/login", json=test_user_data)
|
|
token = login_response.json()["access_token"]
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def other_user_data() -> dict:
|
|
"""
|
|
Data for a second test user.
|
|
|
|
Returns:
|
|
Dictionary with test user credentials
|
|
"""
|
|
return {"email": "other@example.com", "password": "OtherPassword123"}
|
|
|
|
|
|
@pytest.fixture
|
|
def other_auth_headers(client: TestClient, other_user_data: dict) -> dict:
|
|
"""
|
|
Create authenticated headers for a second user.
|
|
|
|
Args:
|
|
client: Test client
|
|
other_user_data: Other user credentials
|
|
|
|
Returns:
|
|
Dictionary with Authorization header
|
|
"""
|
|
# Register and login
|
|
client.post("/api/v1/auth/register", json=other_user_data)
|
|
login_response = client.post("/api/v1/auth/login", json=other_user_data)
|
|
token = login_response.json()["access_token"]
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def test_board(client: TestClient, auth_headers: dict):
|
|
"""
|
|
Create a test board.
|
|
|
|
Args:
|
|
client: Test client
|
|
auth_headers: Authentication headers
|
|
|
|
Returns:
|
|
Board object
|
|
"""
|
|
from app.database.models.board import Board
|
|
|
|
response = client.post(
|
|
"/api/v1/boards",
|
|
json={"title": "Test Board", "description": "Test description"},
|
|
headers=auth_headers,
|
|
)
|
|
board_id = response.json()["id"]
|
|
|
|
# Get board from database
|
|
from app.core.deps import get_db
|
|
|
|
db_gen = next(app.dependency_overrides[get_db]())
|
|
board = db_gen.query(Board).filter(Board.id == board_id).first()
|
|
return board
|
|
|