Unit testing AuthService
This commit is contained in:
75
tests/persistence/conftest.py
Normal file
75
tests/persistence/conftest.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from uuid import uuid4
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from my_auth.models.token import TokenData
|
||||
from my_auth.models.user import UserCreate
|
||||
from my_auth.persistence.sqlite import SQLiteUserRepository, SQLiteTokenRepository
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_user_data_create():
|
||||
"""Provides valid data for creating a test user."""
|
||||
return UserCreate(
|
||||
email="test.user@example.com",
|
||||
username="TestUser",
|
||||
password="ValidPassword123!", # Meets all strength criteria
|
||||
roles=["member"],
|
||||
user_settings={"theme": "dark"}
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_user_hashed_password():
|
||||
"""Provides a dummy hashed password for persistence (hashing is tested elsewhere)."""
|
||||
return "$2b$12$R.S/XfI2tQYt3Kk.iF1XwOQz0Qe.L0T0mD/O1H8E2V5D4Q6F7G8H9I0"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_token_data():
|
||||
"""Provides valid data for a refresh token."""
|
||||
now = datetime.now()
|
||||
return TokenData(
|
||||
token=f"opaque_refresh_token_{uuid4()}",
|
||||
token_type="refresh",
|
||||
user_id="user_id_for_token_test",
|
||||
expires_at=now + timedelta(days=7),
|
||||
is_revoked=False,
|
||||
created_at=now
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def sqlite_db_path(tmp_path_factory):
|
||||
"""
|
||||
Creates a temporary directory and an SQLite file path for the test session.
|
||||
The directory and its contents are deleted after the session.
|
||||
"""
|
||||
temp_dir = tmp_path_factory.mktemp("sqlite_auth_test")
|
||||
db_file: Path = temp_dir / "auth_test.db"
|
||||
|
||||
yield str(db_file)
|
||||
|
||||
try:
|
||||
if temp_dir.exists():
|
||||
import shutil
|
||||
shutil.rmtree(temp_dir)
|
||||
except OSError as e:
|
||||
# Handle case where directory might be locked, though rare in tests
|
||||
print(f"Error during cleanup of temporary DB directory: {e}")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_repository(sqlite_db_path: str) -> SQLiteUserRepository:
|
||||
"""Provides an instance of SQLiteUserRepository initialized with the in-memory DB."""
|
||||
# Assuming the repository takes the connection object or path (using connection here)
|
||||
return SQLiteUserRepository(sqlite_db_path)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def token_repository(sqlite_db_path: str) -> SQLiteTokenRepository:
|
||||
"""Provides an instance of SQLiteTokenRepository initialized with the in-memory DB."""
|
||||
# Assuming the repository takes the connection object or path (using connection here)
|
||||
return SQLiteTokenRepository(sqlite_db_path)
|
||||
Reference in New Issue
Block a user