forked from cartsnitch/cartsnitch
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ec9deb515b | |||
| cfed9b0482 | |||
| 25edd8d5e3 | |||
| bd3cb3b9ab | |||
| 3bedc651c6 | |||
| 138033be9b | |||
| 8ddefe82e4 | |||
| def921f115 | |||
| 43ee1c3531 | |||
| f03d7a33c8 | |||
| 7bf0165fe4 | |||
| ef63c47b7c | |||
| 67e60c9ae1 | |||
| a25b673dd6 | |||
| 4e003ba3d0 | |||
| 4996ff7432 |
+10
-3
@@ -18,7 +18,7 @@ if not db_url:
|
|||||||
"CARTSNITCH_DATABASE_URL_SYNC must be set. "
|
"CARTSNITCH_DATABASE_URL_SYNC must be set. "
|
||||||
"Example: postgresql://user:pass@localhost:5432/cartsnitch"
|
"Example: postgresql://user:pass@localhost:5432/cartsnitch"
|
||||||
)
|
)
|
||||||
config.set_main_option("sqlalchemy.url", db_url)
|
config.set_main_option("sqlalchemy.url", db_url.replace("%", "%%"))
|
||||||
|
|
||||||
target_metadata = Base.metadata
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
@@ -31,6 +31,7 @@ def run_migrations_offline() -> None:
|
|||||||
target_metadata=target_metadata,
|
target_metadata=target_metadata,
|
||||||
literal_binds=True,
|
literal_binds=True,
|
||||||
dialect_opts={"paramstyle": "named"},
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
version_table_column_width=128,
|
||||||
)
|
)
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
@@ -44,13 +45,19 @@ def run_migrations_online() -> None:
|
|||||||
poolclass=pool.NullPool,
|
poolclass=pool.NullPool,
|
||||||
)
|
)
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
context.configure(connection=connection, target_metadata=target_metadata)
|
context.configure(connection=connection, target_metadata=target_metadata, version_table_column_width=128)
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
# Create any tables defined in models but not yet created by migrations.
|
# Create any tables defined in models but not yet created by migrations.
|
||||||
# This bootstraps fresh databases that have no legacy schema.
|
# This bootstraps fresh databases that have no legacy schema.
|
||||||
# checkfirst=True ensures this is a no-op on existing databases.
|
# checkfirst=True ensures this is a no-op on existing databases.
|
||||||
Base.metadata.create_all(bind=connection, checkfirst=True)
|
try:
|
||||||
|
Base.metadata.create_all(bind=connection, checkfirst=True)
|
||||||
|
except Exception as exc:
|
||||||
|
import logging
|
||||||
|
logging.getLogger("alembic.env").warning(
|
||||||
|
"create_all failed (non-fatal, migrations should handle table creation): %s", exc
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if context.is_offline_mode():
|
if context.is_offline_mode():
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
"""Bootstrap users table on fresh databases.
|
||||||
|
|
||||||
|
On fresh databases, migrations 001-006 skip users-table operations because
|
||||||
|
the table does not exist yet. Base.metadata.create_all() in env.py is meant
|
||||||
|
to handle this, but if it fails (import errors, etc.) the table is never
|
||||||
|
created. This migration creates the users table with raw SQL as a safety net.
|
||||||
|
|
||||||
|
Revision ID: 007_bootstrap_users_table
|
||||||
|
Revises: 006_email_inbound_token_server_default
|
||||||
|
Create Date: 2026-04-04
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
revision = "007_bootstrap_users_table"
|
||||||
|
down_revision = "006_email_inbound_token_server_default"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
conn = op.get_bind()
|
||||||
|
inspector = sa.inspect(conn)
|
||||||
|
if inspector.has_table("users"):
|
||||||
|
return # Table already exists (non-fresh DB or create_all already ran)
|
||||||
|
|
||||||
|
conn.execute(text("""
|
||||||
|
CREATE TABLE users (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
email VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
hashed_password VARCHAR(255),
|
||||||
|
display_name VARCHAR(100),
|
||||||
|
email_verified BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
image TEXT,
|
||||||
|
email_inbound_token VARCHAR(22) NOT NULL UNIQUE
|
||||||
|
DEFAULT replace(replace(trim(trailing '=' from encode(gen_random_bytes(16), 'base64')), '+', '-'), '/', '_'),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||||
|
)
|
||||||
|
"""))
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
op.execute(text("DROP TABLE IF EXISTS users"))
|
||||||
@@ -19,12 +19,15 @@ bearer_scheme = HTTPBearer(auto_error=False)
|
|||||||
|
|
||||||
# Better-Auth session cookie name
|
# Better-Auth session cookie name
|
||||||
SESSION_COOKIE_NAME = "better-auth.session_token"
|
SESSION_COOKIE_NAME = "better-auth.session_token"
|
||||||
|
# Secure prefix used by better-auth on HTTPS deployments
|
||||||
|
SECURE_SESSION_COOKIE_NAME = "__Secure-better-auth.session_token"
|
||||||
|
|
||||||
|
|
||||||
async def _validate_session_token(token: str, db: AsyncSession) -> str:
|
async def _validate_session_token(token: str, db: AsyncSession) -> str:
|
||||||
"""Validate a Better-Auth session token against the sessions table.
|
"""Validate a Better-Auth session token against the sessions table.
|
||||||
|
|
||||||
Returns the user_id (as str) if the session is valid and not expired.
|
Better-Auth stores the raw token in the DB. The cookie/Bearer header
|
||||||
|
carries the same raw token, so we compare directly.
|
||||||
"""
|
"""
|
||||||
result = await db.execute(
|
result = await db.execute(
|
||||||
text("SELECT user_id, expires_at FROM sessions WHERE token = :token"),
|
text("SELECT user_id, expires_at FROM sessions WHERE token = :token"),
|
||||||
@@ -65,8 +68,8 @@ async def get_current_user(
|
|||||||
"""
|
"""
|
||||||
token: str | None = None
|
token: str | None = None
|
||||||
|
|
||||||
# 1. Check session cookie
|
# 1. Check session cookie — prefer __Secure- variant (HTTPS) over plain (HTTP dev)
|
||||||
cookie_token = request.cookies.get(SESSION_COOKIE_NAME)
|
cookie_token = request.cookies.get(SECURE_SESSION_COOKIE_NAME) or request.cookies.get(SESSION_COOKIE_NAME)
|
||||||
if cookie_token:
|
if cookie_token:
|
||||||
token = cookie_token
|
token = cookie_token
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
import base64
|
import base64
|
||||||
|
|
||||||
from pydantic import model_validator
|
from pydantic import AliasChoices, Field, model_validator
|
||||||
from pydantic_settings import BaseSettings
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
|
|
||||||
class Settings(BaseSettings):
|
class Settings(BaseSettings):
|
||||||
model_config = {"env_prefix": "CARTSNITCH_"}
|
model_config = {"env_prefix": "CARTSNITCH_"}
|
||||||
|
|
||||||
database_url: str = "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
database_url: str = Field(
|
||||||
|
default="postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
||||||
|
validation_alias=AliasChoices("CARTSNITCH_DATABASE_URL", "DATABASE_URL"),
|
||||||
|
)
|
||||||
redis_url: str = "redis://localhost:6379/0"
|
redis_url: str = "redis://localhost:6379/0"
|
||||||
|
|
||||||
jwt_secret_key: str = "change-me-in-production"
|
jwt_secret_key: str = "change-me-in-production"
|
||||||
@@ -49,5 +52,12 @@ class Settings(BaseSettings):
|
|||||||
) from None
|
) from None
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def normalize_database_url(self):
|
||||||
|
"""Normalize postgresql:// → postgresql+asyncpg:// for the asyncpg driver."""
|
||||||
|
if self.database_url.startswith("postgresql://"):
|
||||||
|
self.database_url = self.database_url.replace("postgresql://", "postgresql+asyncpg://", 1)
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|||||||
@@ -136,7 +136,8 @@ async def client(db_engine):
|
|||||||
async def _create_test_user_and_session(client: AsyncClient, db_engine, **user_overrides) -> tuple[dict, str]:
|
async def _create_test_user_and_session(client: AsyncClient, db_engine, **user_overrides) -> tuple[dict, str]:
|
||||||
"""Create a test user and a valid session directly in the DB.
|
"""Create a test user and a valid session directly in the DB.
|
||||||
|
|
||||||
Returns (user_dict, session_token).
|
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.uuid4())
|
||||||
email = user_overrides.get("email", "test@example.com")
|
email = user_overrides.get("email", "test@example.com")
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
"""Tests for Settings config, specifically the database_url env var fallback."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from cartsnitch_api.config import Settings
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_prefers_cartsnitch_prefix():
|
||||||
|
"""CARTSNITCH_DATABASE_URL takes precedence over DATABASE_URL."""
|
||||||
|
env = {
|
||||||
|
"CARTSNITCH_DATABASE_URL": "postgresql+asyncpg://user1:pass1@host1:5432/db1",
|
||||||
|
"DATABASE_URL": "postgresql://user2:pass2@host2:5432/db2",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://user1:pass1@host1:5432/db1"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_falls_back_to_database_url():
|
||||||
|
"""When CARTSNITCH_DATABASE_URL is absent, DATABASE_URL is accepted."""
|
||||||
|
env = {
|
||||||
|
"DATABASE_URL": "postgresql://user:pass@dbhost:5432/mydb",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://user:pass@dbhost:5432/mydb"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_normalizes_plain_postgresql_prefix():
|
||||||
|
"""DATABASE_URL with plain postgresql:// is normalized to postgresql+asyncpg://."""
|
||||||
|
env = {
|
||||||
|
"DATABASE_URL": "postgresql://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_preserves_asyncpg_prefix():
|
||||||
|
"""CARTSNITCH_DATABASE_URL with postgresql+asyncpg:// is left unchanged."""
|
||||||
|
env = {
|
||||||
|
"CARTSNITCH_DATABASE_URL": "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch",
|
||||||
|
}
|
||||||
|
settings = Settings(**env)
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
||||||
|
|
||||||
|
|
||||||
|
def test_database_url_default():
|
||||||
|
"""When neither env var is set, the hardcoded default is used."""
|
||||||
|
settings = Settings()
|
||||||
|
assert settings.database_url == "postgresql+asyncpg://cartsnitch:cartsnitch@localhost:5432/cartsnitch"
|
||||||
@@ -14,7 +14,7 @@ if config.config_file_name is not None:
|
|||||||
|
|
||||||
db_url = os.environ.get("CARTSNITCH_DATABASE_URL_SYNC")
|
db_url = os.environ.get("CARTSNITCH_DATABASE_URL_SYNC")
|
||||||
if db_url:
|
if db_url:
|
||||||
config.set_main_option("sqlalchemy.url", db_url)
|
config.set_main_option("sqlalchemy.url", db_url.replace("%", "%%"))
|
||||||
|
|
||||||
target_metadata = Base.metadata
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user