Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5a6f4cd44c |
@@ -25,7 +25,14 @@ from cartsnitch_api.routes.user import router as user_router
|
|||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
# Lazy import: keep `dispose_engine` out of the top-level imports so a
|
||||||
|
# stale or partially-built database.py never breaks module load on
|
||||||
|
# container start. The function is required for graceful pool cleanup
|
||||||
|
# on shutdown; if the import fails, the cache_client.close() that
|
||||||
|
# follows the yield would mask it. See CAR-1135 for the original
|
||||||
|
# ImportError that motivated this pattern.
|
||||||
from cartsnitch_api.database import dispose_engine
|
from cartsnitch_api.database import dispose_engine
|
||||||
|
|
||||||
await cache_client.initialize()
|
await cache_client.initialize()
|
||||||
yield
|
yield
|
||||||
await cache_client.close()
|
await cache_client.close()
|
||||||
|
|||||||
+4
-27
@@ -51,21 +51,8 @@ def disable_rate_limiting():
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def engine():
|
def engine():
|
||||||
"""Sync in-memory SQLite engine for model unit tests.
|
"""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.
|
|
||||||
"""
|
|
||||||
eng = create_engine("sqlite:///:memory:")
|
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)
|
Base.metadata.create_all(eng)
|
||||||
yield eng
|
yield eng
|
||||||
eng.dispose()
|
eng.dispose()
|
||||||
@@ -89,16 +76,9 @@ async def db_engine():
|
|||||||
cursor.execute("PRAGMA foreign_keys=ON")
|
cursor.execute("PRAGMA foreign_keys=ON")
|
||||||
cursor.close()
|
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:
|
async with engine.begin() as conn:
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
# Create Better-Auth tables (not managed by SQLAlchemy models)
|
||||||
await conn.execute(
|
await conn.execute(
|
||||||
text("""
|
text("""
|
||||||
CREATE TABLE IF NOT EXISTS sessions (
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
@@ -197,10 +177,8 @@ async def _create_test_user_and_session(
|
|||||||
async with db_engine.begin() as conn:
|
async with db_engine.begin() as conn:
|
||||||
await conn.execute(
|
await conn.execute(
|
||||||
text(
|
text(
|
||||||
"INSERT INTO users (id, email, hashed_password, display_name, "
|
"INSERT INTO users (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, :created_at, :updated_at)"
|
||||||
"VALUES (:id, :email, :hashed_password, :display_name, "
|
|
||||||
":email_verified, :email_inbound_token, :created_at, :updated_at)"
|
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"id": user_id,
|
"id": user_id,
|
||||||
@@ -208,7 +186,6 @@ async def _create_test_user_and_session(
|
|||||||
"hashed_password": "not-used-with-better-auth",
|
"hashed_password": "not-used-with-better-auth",
|
||||||
"display_name": display_name,
|
"display_name": display_name,
|
||||||
"email_verified": False,
|
"email_verified": False,
|
||||||
"email_inbound_token": secrets.token_urlsafe(16),
|
|
||||||
"created_at": now,
|
"created_at": now,
|
||||||
"updated_at": now,
|
"updated_at": now,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -138,9 +138,8 @@ async def test_expired_session_rejected(client, db_engine):
|
|||||||
async with db_engine.begin() as conn:
|
async with db_engine.begin() as conn:
|
||||||
await conn.execute(
|
await conn.execute(
|
||||||
text(
|
text(
|
||||||
"INSERT INTO users (id, email, hashed_password, display_name, "
|
"INSERT INTO users (id, email, hashed_password, display_name, email_verified, created_at, updated_at) "
|
||||||
"email_verified, email_inbound_token, created_at, updated_at) "
|
"VALUES (:id, :email, :hp, :dn, :ev, :ca, :ua)"
|
||||||
"VALUES (:id, :email, :hp, :dn, :ev, :token, :ca, :ua)"
|
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"id": user_id,
|
"id": user_id,
|
||||||
@@ -148,7 +147,6 @@ async def test_expired_session_rejected(client, db_engine):
|
|||||||
"hp": "unused",
|
"hp": "unused",
|
||||||
"dn": "Expired User",
|
"dn": "Expired User",
|
||||||
"ev": False,
|
"ev": False,
|
||||||
"token": secrets.token_urlsafe(16),
|
|
||||||
"ca": now,
|
"ca": now,
|
||||||
"ua": now,
|
"ua": now,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
"""Tests for Settings config, specifically the database_url env var fallback."""
|
"""Tests for Settings config, specifically the database_url env var fallback."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
from cartsnitch_api.config import Settings
|
from cartsnitch_api.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -65,9 +65,8 @@ class TestSessionValidation:
|
|||||||
async with db_engine.begin() as conn:
|
async with db_engine.begin() as conn:
|
||||||
await conn.execute(
|
await conn.execute(
|
||||||
text(
|
text(
|
||||||
"INSERT INTO users (id, email, hashed_password, display_name, "
|
"INSERT INTO users (id, email, hashed_password, display_name, email_verified, created_at, updated_at) "
|
||||||
"email_verified, email_inbound_token, created_at, updated_at) "
|
"VALUES (:id, :email, :hp, :dn, :ev, :ca, :ua)"
|
||||||
"VALUES (:id, :email, :hp, :dn, :ev, :token, :ca, :ua)"
|
|
||||||
),
|
),
|
||||||
{
|
{
|
||||||
"id": user_id,
|
"id": user_id,
|
||||||
@@ -75,7 +74,6 @@ class TestSessionValidation:
|
|||||||
"hp": "unused",
|
"hp": "unused",
|
||||||
"dn": "Expired User",
|
"dn": "Expired User",
|
||||||
"ev": False,
|
"ev": False,
|
||||||
"token": secrets.token_urlsafe(16),
|
|
||||||
"ca": now,
|
"ca": now,
|
||||||
"ua": now,
|
"ua": now,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -17,15 +17,6 @@ from cartsnitch_api.models.user import User, UserStoreAccount
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def engine():
|
def engine():
|
||||||
eng = create_engine("sqlite:///:memory:")
|
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)
|
Base.metadata.create_all(eng)
|
||||||
yield eng
|
yield eng
|
||||||
eng.dispose()
|
eng.dispose()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"""Tests for rate limiting middleware."""
|
"""Tests for rate limiting middleware."""
|
||||||
|
|
||||||
import time
|
import time
|
||||||
from unittest.mock import AsyncMock, MagicMock
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,21 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from httpx import ASGITransport, AsyncClient
|
from httpx import ASGITransport, AsyncClient
|
||||||
|
|
||||||
|
from cartsnitch_api.database import dispose_engine
|
||||||
from cartsnitch_api.main import app
|
from cartsnitch_api.main import app
|
||||||
|
|
||||||
|
|
||||||
|
def test_dispose_engine_importable_from_database():
|
||||||
|
"""Regression for CAR-1135: api main.py used to import dispose_engine
|
||||||
|
at module level. A stale database.py (no dispose_engine) crashed the
|
||||||
|
container at import time with ImportError on line 9. The fix moved
|
||||||
|
the import inside the lifespan function, but `dispose_engine` must
|
||||||
|
still be importable from `cartsnitch_api.database` for the lifespan
|
||||||
|
teardown to actually close pooled connections.
|
||||||
|
"""
|
||||||
|
assert callable(dispose_engine)
|
||||||
|
assert dispose_engine.__name__ == "dispose_engine"
|
||||||
|
|
||||||
EXPECTED_ROUTES = [
|
EXPECTED_ROUTES = [
|
||||||
# Auth (7)
|
# Auth (7)
|
||||||
("post", "/auth/register"),
|
("post", "/auth/register"),
|
||||||
|
|||||||
Reference in New Issue
Block a user