refactor: remove duplicate fixtures and add SQLite support
Task 2.5: Remove duplicate fixtures from integration tests - test_auth_api.py, test_auth_services.py, test_models.py - test_projects_api.py, test_seed.py, test_users_api.py - Fix npytest typos in all test files Task 3.2: Update SQLAlchemy configuration for SQLite - Use generic Uuid type instead of PostgreSQL-specific UUID - Use generic JSON type instead of PostgreSQL-specific JSONB - Update database.py to handle SQLite connection args Unit tests now run without PostgreSQL (5/8 passing)
This commit is contained in:
@@ -3,21 +3,17 @@ import base64
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from src.auth.cookies import build_cookie_options
|
||||
from src.auth.jwt_service import decode_access_token, mint_access_token
|
||||
from src.auth.oidc import build_login_redirect_url, exchange_code_for_tokens, verify_provider_access_token
|
||||
from src.auth.refresh_store import create_refresh_token, hash_refresh_token, revoke_refresh_token, rotate_refresh_token
|
||||
from src.config import Settings, build_database_url
|
||||
from src.models import Base
|
||||
from src.config import Settings
|
||||
from src.models.user import User
|
||||
|
||||
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_cookie_options_follow_environment_defaults(monkeypatch) -> None:
|
||||
monkeypatch.setenv("APP_ENV", "development")
|
||||
dev_settings = Settings()
|
||||
@@ -34,8 +30,7 @@ def test_cookie_options_follow_environment_defaults(monkeypatch) -> None:
|
||||
assert prod_options["samesite"] == "strict"
|
||||
|
||||
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_login_redirect_url_contains_required_oidc_params() -> None:
|
||||
settings = Settings()
|
||||
|
||||
@@ -53,8 +48,7 @@ def test_login_redirect_url_contains_required_oidc_params() -> None:
|
||||
assert "nonce=nonce-123" in url
|
||||
|
||||
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_mint_and_decode_internal_access_token_round_trip() -> None:
|
||||
settings = Settings()
|
||||
expires_at = datetime.now(UTC) + timedelta(minutes=15)
|
||||
@@ -75,8 +69,7 @@ def test_mint_and_decode_internal_access_token_round_trip() -> None:
|
||||
assert "exp" in claims
|
||||
|
||||
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_refresh_token_hash_is_deterministic_and_non_reversible() -> None:
|
||||
raw_token = "refresh-token-abc"
|
||||
|
||||
@@ -88,8 +81,7 @@ def test_refresh_token_hash_is_deterministic_and_non_reversible() -> None:
|
||||
assert len(first_hash) == 64
|
||||
|
||||
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_decode_access_token_rejects_invalid_signature() -> None:
|
||||
settings = Settings()
|
||||
other_settings = Settings(jwt_secret="different-secret")
|
||||
@@ -108,8 +100,7 @@ def test_decode_access_token_rejects_invalid_signature() -> None:
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
async def test_exchange_code_for_tokens_posts_expected_payload() -> None:
|
||||
settings = Settings()
|
||||
|
||||
@@ -133,8 +124,7 @@ async def test_exchange_code_for_tokens_posts_expected_payload() -> None:
|
||||
assert token_payload["access_token"] == "provider-token"
|
||||
|
||||
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_verify_provider_access_token_with_jwks_oct_key() -> None:
|
||||
settings = Settings(authentik_audience="headquarter-web", authentik_issuer="https://authentik.local/")
|
||||
shared_secret = b"shared-secret-123"
|
||||
@@ -168,35 +158,8 @@ def test_verify_provider_access_token_with_jwks_oct_key() -> None:
|
||||
assert claims["sub"] == "authentik-user"
|
||||
|
||||
|
||||
TEST_DATABASE_URL = build_database_url(
|
||||
user="headquarter",
|
||||
password="headquarter",
|
||||
host="localhost",
|
||||
port=5432,
|
||||
database="headquarter",
|
||||
)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def db_session() -> AsyncSession:
|
||||
engine = create_async_engine(TEST_DATABASE_URL)
|
||||
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||
|
||||
async with engine.begin() as connection:
|
||||
await connection.run_sync(Base.metadata.create_all)
|
||||
|
||||
async with session_factory() as session:
|
||||
await session.execute(text("TRUNCATE TABLE refresh_tokens, users RESTART IDENTITY CASCADE"))
|
||||
await session.commit()
|
||||
yield session
|
||||
await session.rollback()
|
||||
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
n@pytest.mark.integration
|
||||
|
||||
@pytest.mark.integration
|
||||
async def test_refresh_store_create_rotate_and_revoke(db_session: AsyncSession) -> None:
|
||||
user = User(email="dev-auth@headquarter.local", name="Dev Auth", authentik_id="auth-dev", avatar_url=None)
|
||||
db_session.add(user)
|
||||
|
||||
Reference in New Issue
Block a user