Spaces:
Sleeping
Sleeping
| import pytest | |
| from fastapi.testclient import TestClient | |
| from sqlmodel import Session, SQLModel, create_engine | |
| from sqlmodel.pool import StaticPool | |
| from src.main import app | |
| from src.api.dependencies import get_session | |
| # Setup test database | |
| sqlite_url = "sqlite://" | |
| engine = create_engine( | |
| sqlite_url, | |
| connect_args={"check_same_thread": False}, | |
| poolclass=StaticPool, | |
| ) | |
| def session_fixture(): | |
| SQLModel.metadata.create_all(engine) | |
| with Session(engine) as session: | |
| yield session | |
| SQLModel.metadata.drop_all(engine) | |
| def client_fixture(session: Session): | |
| def get_session_override(): | |
| return session | |
| app.dependency_overrides[get_session] = get_session_override | |
| client = TestClient(app) | |
| yield client | |
| app.dependency_overrides.clear() | |
| def test_register_user(client: TestClient): | |
| response = client.post( | |
| "/api/auth/register", | |
| json={"email": "test@example.com", "password": "securepassword123"} | |
| ) | |
| assert response.status_code == 201 | |
| data = response.json() | |
| assert data["email"] == "test@example.com" | |
| assert "id" in data | |
| def test_register_duplicate_email(client: TestClient): | |
| # First registration | |
| client.post( | |
| "/api/auth/register", | |
| json={"email": "test@example.com", "password": "securepassword123"} | |
| ) | |
| # Second registration with same email | |
| response = client.post( | |
| "/api/auth/register", | |
| json={"email": "test@example.com", "password": "securepassword456"} | |
| ) | |
| assert response.status_code == 400 | |
| assert response.json()["detail"] == "A user with this email already exists." | |
| def test_login_success(client: TestClient): | |
| # Register first | |
| client.post( | |
| "/api/auth/register", | |
| json={"email": "test@example.com", "password": "securepassword123"} | |
| ) | |
| # Login | |
| response = client.post( | |
| "/api/auth/login", | |
| json={"email": "test@example.com", "password": "securepassword123"} | |
| ) | |
| assert response.status_code == 200 | |
| data = response.json() | |
| assert "access_token" in data | |
| assert data["token_type"] == "bearer" | |
| def test_login_invalid_credentials(client: TestClient): | |
| # Register first | |
| client.post( | |
| "/api/auth/register", | |
| json={"email": "test@example.com", "password": "securepassword123"} | |
| ) | |
| # Login with wrong password | |
| response = client.post( | |
| "/api/auth/login", | |
| json={"email": "test@example.com", "password": "wrongpassword"} | |
| ) | |
| assert response.status_code == 401 | |
| assert response.json()["detail"] == "Incorrect email or password" | |