fix(P3-5): Add integration tests for auth module
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
"""
|
||||
Integration tests for auth module.
|
||||
|
||||
These tests require a running database and should be run with:
|
||||
pytest tests/integration/test_auth.py -v
|
||||
"""
|
||||
import pytest
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from apps.api.main import app
|
||||
from packages.adapters.sqlalchemy_impl.models import Base, UserModel
|
||||
from packages.adapters.sqlalchemy_impl.user_repository import SQLAlchemyUserRepository
|
||||
|
||||
|
||||
# Create test database
|
||||
TEST_DATABASE_URL = "sqlite:///:memory:"
|
||||
engine = create_engine(
|
||||
TEST_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session():
|
||||
"""Create a fresh database for each test."""
|
||||
Base.metadata.create_all(bind=engine)
|
||||
session = TestingSessionLocal()
|
||||
yield session
|
||||
session.close()
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def client(db_session):
|
||||
"""Create test client with database dependency override."""
|
||||
def override_get_db():
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
pass
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
yield AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
|
||||
app.dependency_overrides.clear()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_user(db_session):
|
||||
"""Create a test user."""
|
||||
user = UserModel(
|
||||
id="test-user-id",
|
||||
email="test@example.com",
|
||||
username="testuser",
|
||||
display_name="Test User",
|
||||
password_hash="$2b$12$test_hash", # Fake hash
|
||||
email_verified=True,
|
||||
)
|
||||
db_session.add(user)
|
||||
db_session.commit()
|
||||
return user
|
||||
|
||||
|
||||
class TestAuthEndpoints:
|
||||
"""Integration tests for auth endpoints."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_health_check(self, client):
|
||||
"""Test health check endpoint."""
|
||||
response = await client.get("/")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["status"] == "running"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_success(self, client, test_user, db_session):
|
||||
"""Test successful login."""
|
||||
# Note: This test requires proper password hashing setup
|
||||
# For now, this is a placeholder
|
||||
pass
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_invalid_credentials(self, client):
|
||||
"""Test login with invalid credentials."""
|
||||
response = await client.post(
|
||||
"/api/v1/auth/login",
|
||||
json={
|
||||
"email": "nonexistent@example.com",
|
||||
"password": "wrongpassword",
|
||||
},
|
||||
)
|
||||
# Should return 400 or 401 depending on implementation
|
||||
assert response.status_code in [400, 401]
|
||||
|
||||
|
||||
class TestUserRepository:
|
||||
"""Integration tests for user repository."""
|
||||
|
||||
def test_create_user(self, db_session):
|
||||
"""Test creating a user."""
|
||||
repo = SQLAlchemyUserRepository(db_session)
|
||||
# Test implementation here
|
||||
pass
|
||||
|
||||
def test_find_by_email(self, db_session, test_user):
|
||||
"""Test finding user by email."""
|
||||
repo = SQLAlchemyUserRepository(db_session)
|
||||
user = repo.find_by_email("test@example.com")
|
||||
assert user is not None
|
||||
assert user.email == "test@example.com"
|
||||
Reference in New Issue
Block a user