Fix SQLite UUID and server_default incompatibilities in test fixtures
Adds SQLiteCompatibleUUID TypeDecorator and _StringUUID fallback to handle PostgreSQL UUID and Text PK columns when using SQLite test database. - SQLiteCompatibleUUID: converts uuid.UUID to CHAR(32) hex string for bind, returns uuid.UUID on result fetch - _StringUUID: handles Text PK/FK columns that tests bind UUID values into - _adapt_uuid_columns_for_sqlite: replaces PostgresUUID column types - _adapt_text_pk_columns_for_uuid: replaces Text PK types - _adapt_fk_columns_for_uuid: replaces Text FK types - _strip_postgres_server_defaults: removes gen_random_uuid/gen_random_bytes server_defaults that SQLite can't evaluate Updates test_encrypted_json.py fixtures to use shared conftest engine and pass explicit UUID for User records. Fixes CAR-1111. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
+141
-42
@@ -5,20 +5,133 @@ matching the Better-Auth session validation flow.
|
||||
"""
|
||||
|
||||
import secrets
|
||||
import uuid
|
||||
import uuid as uuid_lib
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy import create_engine, event, text
|
||||
from sqlalchemy import create_engine, event, text, TypeDecorator, String
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import UUID as PostgresUUID
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.types import CHAR, TypeEngine
|
||||
|
||||
from cartsnitch_api.config import settings as cartsnitch_settings
|
||||
from cartsnitch_api.database import get_db
|
||||
from cartsnitch_api.main import create_app
|
||||
from cartsnitch_api.models import Base
|
||||
|
||||
cartsnitch_settings = None
|
||||
|
||||
|
||||
class SQLiteCompatibleUUID(TypeDecorator):
|
||||
"""Adapts PostgreSQL UUID for use with SQLite at test runtime.
|
||||
|
||||
Stores as CHAR(32) hex string, converts to Python uuid.UUID on read.
|
||||
"""
|
||||
|
||||
impl = CHAR(32)
|
||||
cache_ok = True
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, uuid_lib.UUID):
|
||||
return value.hex
|
||||
return str(value)
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value is None:
|
||||
return None
|
||||
return uuid_lib.UUID(hex=value)
|
||||
|
||||
|
||||
class _StringUUID(TypeDecorator):
|
||||
"""Fallback TypeDecorator that accepts both UUID objects and strings.
|
||||
|
||||
Used when the model uses Text but tests pass uuid.UUID values.
|
||||
Stores as native string, returns uuid.UUID on read.
|
||||
"""
|
||||
|
||||
impl = String(36)
|
||||
cache_ok = True
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, uuid_lib.UUID):
|
||||
return value.hex
|
||||
return str(value)
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
return uuid_lib.UUID(hex=value)
|
||||
except Exception:
|
||||
return value
|
||||
|
||||
|
||||
def _adapt_uuid_columns_for_sqlite(googol=None):
|
||||
"""Replace PostgreSQL UUID column types with SQLiteCompatibleUUID.
|
||||
|
||||
PostgreSQL UUID columns generate DDL using gen_random_uuid() which SQLite
|
||||
doesn't support. This replaces those column types so SQLite can bind UUIDs
|
||||
as hex strings. Also sets a Python-side default so INSERTs without explicit
|
||||
id succeed. Accepts an optional connection arg from run_sync.
|
||||
"""
|
||||
for table in Base.metadata.tables.values():
|
||||
for column in table.columns.values():
|
||||
if isinstance(column.type, PostgresUUID):
|
||||
column.type = SQLiteCompatibleUUID()
|
||||
if column.server_default is None and column.default is None:
|
||||
column.default = uuid_lib.uuid4
|
||||
|
||||
|
||||
def _adapt_text_pk_columns_for_uuid(googol=None):
|
||||
"""Replace Text primary key columns that tests bind uuid.UUID values into.
|
||||
|
||||
User.id is a Text column but tests pass uuid.UUID objects. SQLite can't
|
||||
bind UUID directly so we swap these to a compatible type. Accepts an
|
||||
optional connection arg from run_sync.
|
||||
"""
|
||||
for table in Base.metadata.tables.values():
|
||||
for column in table.columns.values():
|
||||
if column.primary_key and isinstance(column.type, sa.Text):
|
||||
column.type = _StringUUID()
|
||||
|
||||
|
||||
def _adapt_fk_columns_for_uuid(googol=None):
|
||||
"""Replace FK columns that reference UUID PKs but are typed as Text.
|
||||
|
||||
purchase.user_id, user_store_accounts.user_id/store_id, etc. are typed as str
|
||||
but tests pass uuid.UUID objects for them. Accepts an optional connection arg
|
||||
from run_sync.
|
||||
"""
|
||||
for table in Base.metadata.tables.values():
|
||||
for column in table.columns.values():
|
||||
if column.foreign_keys:
|
||||
for fk in column.foreign_keys:
|
||||
if isinstance(column.type, (sa.Text, sa.String)) and column.type.length in (None, 255):
|
||||
column.type = _StringUUID()
|
||||
|
||||
|
||||
def _strip_postgres_server_defaults(googol=None):
|
||||
"""Remove PostgreSQL-specific server_default expressions for SQLite compatibility.
|
||||
|
||||
PostgreSQL functions like gen_random_uuid() and the base64 encoding expression
|
||||
in email_inbound_token are not valid in SQLite. Accepts an optional connection
|
||||
arg from run_sync.
|
||||
"""
|
||||
for table in Base.metadata.tables.values():
|
||||
for column in table.columns.values():
|
||||
if column.server_default is not None:
|
||||
sd = str(column.server_default.arg)
|
||||
if "gen_random_uuid" in sd or "gen_random_bytes" in sd:
|
||||
column.server_default = None
|
||||
|
||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||
|
||||
TEST_JWT_SECRET = secrets.token_urlsafe(32)
|
||||
TEST_SERVICE_KEY = secrets.token_urlsafe(32)
|
||||
TEST_FERNET_KEY = "7reF42nmTwbdN21PBoubGp7h_FU8qSimstmlaMLoRK8="
|
||||
@@ -26,46 +139,35 @@ TEST_FERNET_KEY = "7reF42nmTwbdN21PBoubGp7h_FU8qSimstmlaMLoRK8="
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_test_settings():
|
||||
original_jwt = cartsnitch_settings.jwt_secret_key
|
||||
original_service = cartsnitch_settings.service_key
|
||||
original_fernet = cartsnitch_settings.fernet_key
|
||||
cartsnitch_settings.jwt_secret_key = TEST_JWT_SECRET
|
||||
cartsnitch_settings.service_key = TEST_SERVICE_KEY
|
||||
cartsnitch_settings.fernet_key = TEST_FERNET_KEY
|
||||
from cartsnitch_api.config import settings as real_settings
|
||||
original_jwt = real_settings.jwt_secret_key
|
||||
original_service = real_settings.service_key
|
||||
original_fernet = real_settings.fernet_key
|
||||
real_settings.jwt_secret_key = TEST_JWT_SECRET
|
||||
real_settings.service_key = TEST_SERVICE_KEY
|
||||
real_settings.fernet_key = TEST_FERNET_KEY
|
||||
yield
|
||||
cartsnitch_settings.jwt_secret_key = original_jwt
|
||||
cartsnitch_settings.service_key = original_service
|
||||
cartsnitch_settings.fernet_key = original_fernet
|
||||
|
||||
|
||||
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
||||
real_settings.jwt_secret_key = original_jwt
|
||||
real_settings.service_key = original_service
|
||||
real_settings.fernet_key = original_fernet
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def disable_rate_limiting():
|
||||
"""Disable rate limiting for all tests to prevent 429 interference."""
|
||||
cartsnitch_settings.rate_limit_enabled = False
|
||||
from cartsnitch_api.config import settings as real_settings
|
||||
real_settings.rate_limit_enabled = False
|
||||
yield
|
||||
cartsnitch_settings.rate_limit_enabled = True
|
||||
real_settings.rate_limit_enabled = True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def engine():
|
||||
"""Sync in-memory SQLite engine for model unit tests.
|
||||
|
||||
Strips PostgreSQL-specific server_default expressions so SQLite can
|
||||
handle all column inserts without missing-function errors.
|
||||
"""
|
||||
"""Sync in-memory SQLite engine for model unit tests."""
|
||||
eng = create_engine("sqlite:///:memory:")
|
||||
|
||||
for table in Base.metadata.tables.values():
|
||||
for col in table.columns.values():
|
||||
sd = col.server_default
|
||||
if sd is not None:
|
||||
expr_str = str(sd.expression).lower()
|
||||
if "gen_random_uuid" in expr_str or "gen_random_bytes" in expr_str:
|
||||
col.server_default = None
|
||||
|
||||
_adapt_uuid_columns_for_sqlite()
|
||||
_adapt_text_pk_columns_for_uuid()
|
||||
_adapt_fk_columns_for_uuid()
|
||||
_strip_postgres_server_defaults()
|
||||
Base.metadata.create_all(eng)
|
||||
yield eng
|
||||
eng.dispose()
|
||||
@@ -89,16 +191,13 @@ async def db_engine():
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
|
||||
for table in Base.metadata.tables.values():
|
||||
for col in table.columns.values():
|
||||
sd = col.server_default
|
||||
if sd is not None:
|
||||
expr_str = str(sd.expression).lower()
|
||||
if "gen_random_uuid" in expr_str or "gen_random_bytes" in expr_str:
|
||||
col.server_default = None
|
||||
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(_adapt_uuid_columns_for_sqlite)
|
||||
await conn.run_sync(_adapt_text_pk_columns_for_uuid)
|
||||
await conn.run_sync(_adapt_fk_columns_for_uuid)
|
||||
await conn.run_sync(_strip_postgres_server_defaults)
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
# Create Better-Auth tables (not managed by SQLAlchemy models)
|
||||
await conn.execute(
|
||||
text("""
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
@@ -186,11 +285,11 @@ async def _create_test_user_and_session(
|
||||
Returns (user_dict, session_token). Better-Auth stores the raw token
|
||||
in the DB, so we insert it as-is.
|
||||
"""
|
||||
user_id = str(uuid.uuid4())
|
||||
user_id = str(uuid_lib.uuid4())
|
||||
email = user_overrides.get("email", "test@example.com")
|
||||
display_name = user_overrides.get("display_name", "Test User")
|
||||
session_token = secrets.token_urlsafe(32)
|
||||
session_id = str(uuid.uuid4())
|
||||
session_id = str(uuid_lib.uuid4())
|
||||
now = datetime.now(UTC).isoformat()
|
||||
expires = (datetime.now(UTC) + timedelta(days=7)).isoformat()
|
||||
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
"""Tests for EncryptedJSON TypeDecorator and session_data encryption."""
|
||||
|
||||
import json
|
||||
import uuid as uuid_lib
|
||||
|
||||
import pytest
|
||||
from cryptography.fernet import Fernet
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy import column, create_engine, table, text
|
||||
from sqlalchemy import column, table, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from cartsnitch_api.config import settings
|
||||
@@ -15,27 +16,13 @@ from cartsnitch_api.models.user import User, UserStoreAccount
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def engine():
|
||||
eng = create_engine("sqlite:///:memory:")
|
||||
|
||||
for table in Base.metadata.tables.values():
|
||||
for col in table.columns.values():
|
||||
sd = col.server_default
|
||||
if sd is not None:
|
||||
expr_str = str(sd.expression).lower()
|
||||
if "gen_random_uuid" in expr_str or "gen_random_bytes" in expr_str:
|
||||
col.server_default = None
|
||||
|
||||
Base.metadata.create_all(eng)
|
||||
yield eng
|
||||
eng.dispose()
|
||||
def engine(engine):
|
||||
yield engine
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def session(engine):
|
||||
factory = sessionmaker(bind=engine)
|
||||
with factory() as sess:
|
||||
yield sess
|
||||
def session(session):
|
||||
yield session
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -49,7 +36,7 @@ def store(session):
|
||||
|
||||
@pytest.fixture
|
||||
def user(session):
|
||||
u = User(email="alice@example.com", hashed_password="fakehash")
|
||||
u = User(id=uuid_lib.uuid4(), email="alice@example.com", hashed_password="fakehash")
|
||||
session.add(u)
|
||||
session.commit()
|
||||
session.refresh(u)
|
||||
|
||||
Reference in New Issue
Block a user