Compare commits

...

9 Commits

Author SHA1 Message Date
Flea Flicker 8c78fde48d Fix SQLite UUID and server_default incompatibilities in test fixtures
CI / lint (pull_request) Failing after 7s
CI / typecheck (pull_request) Failing after 48s
CI / test (pull_request) Failing after 1m35s
CI / build-and-push (pull_request) Has been skipped
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>
2026-06-01 11:37:05 +00:00
Barcode Betty 84c143c4e7 Remove deploy-dev/deploy-uat CI jobs (CAR-1069) (#37)
CI / lint (push) Failing after 3s
CI / typecheck (push) Failing after 19s
CI / lint (pull_request) Failing after 4s
CI / test (push) Failing after 30s
CI / build-and-push (push) Has been skipped
CI / typecheck (pull_request) Failing after 18s
CI / test (pull_request) Failing after 29s
CI / build-and-push (pull_request) Has been skipped
Co-authored-by: Barcode Betty <32+cs_betty@noreply.git.farh.net>
Co-committed-by: Barcode Betty <32+cs_betty@noreply.git.farh.net>
2026-05-27 01:56:53 +00:00
Savannah Savings 1c42e4b0af Merge pull request 'Fix: strip PostgreSQL server_defaults from SQLite test fixtures' (#32) from betty/fix-email-inbound-token-tests into dev
CI / lint (push) Failing after 7s
CI / typecheck (push) Failing after 17s
CI / test (push) Failing after 18s
CI / build-and-push (push) Has been skipped
CI / deploy-uat (push) Has been skipped
CI / deploy-dev (push) Failing after 25s
Merge PR #32: Fix SQLite server_default stripping for test fixtures
2026-05-23 23:46:59 +00:00
Barcode Betty 6755ca8c27 Fix: strip PostgreSQL server_default from UUID + gen_random_bytes columns for SQLite tests
CI / lint (pull_request) Failing after 3s
CI / typecheck (pull_request) Failing after 19s
CI / test (pull_request) Failing after 16s
CI / build-and-push (pull_request) Has been skipped
CI / deploy-dev (pull_request) Has been skipped
CI / deploy-uat (pull_request) Has been skipped
The sync engine fixture (engine) and async engine fixture (db_engine) now
iterate all Base.metadata tables and null server_default on any column
whose SQL text contains 'gen_random_uuid' or 'gen_random_bytes'. This
covers all UUIDPrimaryKeyMixin columns (Purchase, PurchaseItem, Store,
StoreLocation, Coupon, NormalizedProduct, PriceHistory,
ShrinkflationEvent, UserStoreAccount) as well as the
email_inbound_token gen_random_bytes expression in User.

Without this, SQLite raises 'type UUID is not supported' when the ORM
tries to bind Python UUID objects, and NOT NULL constraint failures when
server_default expressions reference non-existent PostgreSQL functions.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-05-23 23:36:08 +00:00
Savannah Savings 280882f515 Merge pull request 'Fix test failures: email_inbound_token server_default for SQLite' (#29) from betty/fix-email-inbound-token-tests into dev
CI / test (push) Failing after 1m19s
CI / build-and-push (push) Has been skipped
CI / lint (push) Successful in 5s
CI / typecheck (push) Failing after 29s
CI / deploy-uat (push) Has been skipped
CI / deploy-dev (push) Failing after 28s
CI / lint (pull_request) Successful in 6s
CI / typecheck (pull_request) Failing after 31s
CI / test (pull_request) Failing after 43s
CI / build-and-push (pull_request) Has been skipped
CI / deploy-dev (pull_request) Has been skipped
CI / deploy-uat (pull_request) Has been skipped
Fix test failures: email_inbound_token server_default for SQLite (#29)

Strip PostgreSQL-only server_default from email_inbound_token before SQLite create_all(). Add email_inbound_token to test user INSERT statements.

Reviewed-by: Savannah Savings (CTO)
Approved-by: Checkout Charlie (QA)
2026-05-23 23:25:03 +00:00
Savannah Savings ec4eaa1f03 Merge pull request 'Fix ruff lint errors across codebase' (#30) from barcode-betty/car-1004-fix-ruff-lint into dev
CI / build-and-push (push) Has been skipped
CI / deploy-uat (push) Has been skipped
CI / build-and-push (pull_request) Has been skipped
CI / test (push) Failing after 1s
CI / deploy-dev (push) Failing after 1s
CI / typecheck (push) Failing after 0s
CI / typecheck (pull_request) Failing after 17s
CI / lint (push) Failing after 1s
CI / lint (pull_request) Failing after 3s
CI / test (pull_request) Failing after 49s
CI / deploy-dev (pull_request) Has been skipped
CI / deploy-uat (pull_request) Has been skipped
Merge PR #30: Fix ruff lint errors across codebase

Fixes 56 ruff lint errors (E501, F401, I001) in cartsnitch/api.
QA: cs_charlie APPROVED
CTO: cs_savannah APPROVED
2026-05-23 23:11:54 +00:00
Barcode Betty 0e3c9fb52e Fix: strip PostgreSQL server_default from email_inbound_token for SQLite
CI / lint (pull_request) Failing after 5s
CI / typecheck (pull_request) Failing after 32s
CI / test (pull_request) Failing after 1m23s
CI / build-and-push (pull_request) Has been skipped
CI / deploy-dev (pull_request) Has been skipped
CI / deploy-uat (pull_request) Has been skipped
The email_inbound_token column uses a PostgreSQL-only server_default
(gen_random_bytes/encode/trim) that SQLite cannot parse.
Strip the server_default before metadata.create_all() in both the
sync engine and async db_engine fixtures so tests run against SQLite.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-05-23 23:07:39 +00:00
Barcode Betty cc6ca5982c fix: resolve email_inbound_token conflict in test fixtures
CI / lint (pull_request) Failing after 7s
CI / typecheck (pull_request) Failing after 31s
CI / test (pull_request) Failing after 51s
CI / build-and-push (pull_request) Has been skipped
CI / deploy-dev (pull_request) Has been skipped
CI / deploy-uat (pull_request) Has been skipped
Rebase on latest dev and wrap SQL INSERT lines to honor ruff line-length=100.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-05-23 23:00:02 +00:00
Barcode Betty c9fd066c31 fix: resolve email_inbound_token conflict in test fixtures
CI / lint (pull_request) Failing after 7s
CI / typecheck (pull_request) Failing after 31s
CI / test (pull_request) Failing after 49s
CI / build-and-push (pull_request) Has been skipped
CI / deploy-dev (pull_request) Has been skipped
CI / deploy-uat (pull_request) Has been skipped
2026-05-23 22:57:16 +00:00
8 changed files with 2294 additions and 126 deletions
-87
View File
@@ -175,90 +175,3 @@ jobs:
git tag "v${{ steps.calver.outputs.version }}"
git push origin "v${{ steps.calver.outputs.version }}"
deploy-dev:
runs-on: ubuntu-latest
needs: [build-and-push]
if: always() && !cancelled() && github.event_name == 'push' && (github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/main')
steps:
- name: Checkout infra repo
uses: actions/checkout@v4
with:
repository: cartsnitch/infra
token: ${{ secrets.GITEA_TOKEN }}
ref: main
path: infra
- name: Install kubectl
uses: azure/setup-kubectl@v4
- name: Install kustomize
uses: imranismail/setup-kustomize@v2
- name: Determine image tag
id: api_tag
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "tag=${{ needs.build-and-push.outputs.calver_tag }}" >> "$GITHUB_OUTPUT"
else
echo "tag=${{ needs.build-and-push.outputs.sha_tag }}" >> "$GITHUB_OUTPUT"
fi
- name: Update api image tag
if: needs.build-and-push.result == 'success'
run: |
cd infra/apps/overlays/dev
kustomize edit set image ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.api_tag.outputs.tag }}
- name: Commit and push to infra
run: |
cd infra
git config user.name "cartsnitch-ci[bot]"
git config user.email "cartsnitch-ci[bot]@users.noreply.github.com"
git add apps/overlays/dev/kustomization.yaml
git commit -m "ci(dev): update api image"
git pull --rebase origin main
git push origin main
deploy-uat:
runs-on: ubuntu-latest
needs: [build-and-push]
if: always() && !cancelled() && github.event_name == 'push' && (github.ref == 'refs/heads/uat' || github.ref == 'refs/heads/main')
steps:
- name: Checkout infra repo
uses: actions/checkout@v4
with:
repository: cartsnitch/infra
token: ${{ secrets.GITEA_TOKEN }}
ref: main
path: infra
- name: Install kubectl
uses: azure/setup-kubectl@v4
- name: Install kustomize
uses: imranismail/setup-kustomize@v2
- name: Determine image tag
id: api_tag
run: |
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
echo "tag=${{ needs.build-and-push.outputs.calver_tag }}" >> "$GITHUB_OUTPUT"
else
echo "tag=${{ needs.build-and-push.outputs.sha_tag }}" >> "$GITHUB_OUTPUT"
fi
- name: Update api image tag
if: needs.build-and-push.result == 'success'
run: |
cd infra/apps/overlays/uat
kustomize edit set image ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.api_tag.outputs.tag }}
- name: Commit and push to infra
run: |
cd infra
git config user.name "cartsnitch-ci[bot]"
git config user.email "cartsnitch-ci[bot]@users.noreply.github.com"
git add apps/overlays/uat/kustomization.yaml
git commit -m "ci(uat): update api image"
git pull --rebase origin main
git push origin main
+11
View File
@@ -0,0 +1,11 @@
{
"mcpServers": {
"gitea": {
"type": "http",
"url": "https://git-mcp.farh.net/mcp",
"headers": {
"Authorization": "Bearer ${GITEA_TOKEN}"
}
}
}
}
+6 -1
View File
@@ -15,12 +15,17 @@ dependencies = [
"sqlalchemy[asyncio]>=2.0.35",
"asyncpg>=0.30.0",
"alembic>=1.13,<2.0",
"psycopg2>=2.9,<3.0",
"psycopg2-binary>=2.9,<3.0",
"python-jose[cryptography]>=3.3.0",
"passlib[bcrypt]>=1.7.4",
"httpx>=0.27.0",
"redis[hiredis]>=5.2.0",
"cryptography>=43.0.0",
"pytest[dev]>=9.0.3",
"pytest-asyncio[dev]>=1.3.0",
"aiosqlite[dev]>=0.22.1",
"ruff[dev]>=0.15.14",
"mypy[dev]>=1.19.0",
]
[project.optional-dependencies]
+143 -23
View File
@@ -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,33 +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."""
eng = create_engine("sqlite:///:memory:")
_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()
@@ -77,6 +192,10 @@ async def db_engine():
cursor.close()
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(
@@ -166,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()
@@ -178,9 +297,9 @@ async def _create_test_user_and_session(
await conn.execute(
text(
"INSERT INTO users (id, email, hashed_password, display_name, "
"email_verified, created_at, updated_at) "
"VALUES (:id, :email, :hashed_password, :display_name, :email_verified, "
":created_at, :updated_at)"
"email_verified, email_inbound_token, created_at, updated_at) "
"VALUES (:id, :email, :hashed_password, :display_name, "
":email_verified, :email_inbound_token, :created_at, :updated_at)"
),
{
"id": user_id,
@@ -188,6 +307,7 @@ async def _create_test_user_and_session(
"hashed_password": "not-used-with-better-auth",
"display_name": display_name,
"email_verified": False,
"email_inbound_token": secrets.token_urlsafe(16),
"created_at": now,
"updated_at": now,
},
+3 -2
View File
@@ -139,8 +139,8 @@ async def test_expired_session_rejected(client, db_engine):
await conn.execute(
text(
"INSERT INTO users (id, email, hashed_password, display_name, "
"email_verified, created_at, updated_at) "
"VALUES (:id, :email, :hp, :dn, :ev, :ca, :ua)"
"email_verified, email_inbound_token, created_at, updated_at) "
"VALUES (:id, :email, :hp, :dn, :ev, :token, :ca, :ua)"
),
{
"id": user_id,
@@ -148,6 +148,7 @@ async def test_expired_session_rejected(client, db_engine):
"hp": "unused",
"dn": "Expired User",
"ev": False,
"token": secrets.token_urlsafe(16),
"ca": now,
"ua": now,
},
+3 -2
View File
@@ -66,8 +66,8 @@ class TestSessionValidation:
await conn.execute(
text(
"INSERT INTO users (id, email, hashed_password, display_name, "
"email_verified, created_at, updated_at) "
"VALUES (:id, :email, :hp, :dn, :ev, :ca, :ua)"
"email_verified, email_inbound_token, created_at, updated_at) "
"VALUES (:id, :email, :hp, :dn, :ev, :token, :ca, :ua)"
),
{
"id": user_id,
@@ -75,6 +75,7 @@ class TestSessionValidation:
"hp": "unused",
"dn": "Expired User",
"ev": False,
"token": secrets.token_urlsafe(16),
"ca": now,
"ua": now,
},
+7 -11
View File
@@ -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,18 +16,13 @@ from cartsnitch_api.models.user import User, UserStoreAccount
@pytest.fixture
def engine():
eng = create_engine("sqlite:///:memory:")
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
@@ -40,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)
Generated
+2121
View File
File diff suppressed because it is too large Load Diff