Add comprehensive test coverage for email service, password reset endpoints, and soft delete functionality

- Introduced unit tests for `EmailService` covering `ConsoleEmailBackend` and `SMTPEmailBackend`.
- Added tests for password reset request and confirmation endpoints, including edge cases and error handling.
- Implemented soft delete CRUD tests to validate `deleted_at` behavior and data exclusion in queries.
- Enhanced API tests for email functionality and user management workflows.
This commit is contained in:
Felipe Cardoso
2025-10-30 17:18:25 +01:00
parent 182b12b2d5
commit defa33975f
7 changed files with 2063 additions and 2 deletions

View File

@@ -3,8 +3,12 @@ import uuid
from datetime import datetime, timezone
import pytest
from fastapi.testclient import TestClient
from app.main import app
from app.core.database import get_db
from app.models.user import User
from app.core.auth import get_password_hash
from app.utils.test_utils import setup_test_db, teardown_test_db, setup_async_test_db, teardown_async_test_db
@@ -63,4 +67,90 @@ def mock_user(db_session):
)
db_session.add(mock_user)
db_session.commit()
return mock_user
return mock_user
@pytest.fixture(scope="function")
def test_db():
"""
Creates a test database for integration tests.
This creates a fresh database for each test to ensure isolation.
"""
test_engine, TestingSessionLocal = setup_test_db()
# Create a session
with TestingSessionLocal() as session:
yield session
# Clean up
teardown_test_db(test_engine)
@pytest.fixture(scope="function")
def client(test_db):
"""
Create a FastAPI test client with a test database.
This overrides the get_db dependency to use the test database.
"""
def override_get_db():
try:
yield test_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(test_db):
"""
Create a test user in the database.
Password: TestPassword123
"""
user = User(
id=uuid.uuid4(),
email="testuser@example.com",
password_hash=get_password_hash("TestPassword123"),
first_name="Test",
last_name="User",
phone_number="+1234567890",
is_active=True,
is_superuser=False,
preferences=None,
)
test_db.add(user)
test_db.commit()
test_db.refresh(user)
return user
@pytest.fixture
def test_superuser(test_db):
"""
Create a test superuser in the database.
Password: SuperPassword123
"""
user = User(
id=uuid.uuid4(),
email="superuser@example.com",
password_hash=get_password_hash("SuperPassword123"),
first_name="Super",
last_name="User",
phone_number="+9876543210",
is_active=True,
is_superuser=True,
preferences=None,
)
test_db.add(user)
test_db.commit()
test_db.refresh(user)
return user