2026-02-05 15:13:06 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-03-09 18:03:04 +08:00
|
|
|
import pytest
|
|
|
|
|
from pydantic import ValidationError
|
2026-02-05 15:13:06 +08:00
|
|
|
from pytest import MonkeyPatch
|
|
|
|
|
|
2026-03-09 18:03:04 +08:00
|
|
|
from core.config.settings import Settings, SupabaseSettings
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_social_prefixed_supabase_env_populates_settings(
|
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
|
) -> None:
|
2026-03-09 18:03:04 +08:00
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__PUBLIC_URL", "https://public.example:8443")
|
2026-02-05 15:13:06 +08:00
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__ANON_KEY", "anon-key")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__SERVICE_ROLE_KEY", "service-key")
|
2026-03-07 14:55:00 +08:00
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__SITE_URL", "https://app.example.com")
|
|
|
|
|
monkeypatch.setenv(
|
|
|
|
|
"SOCIAL_SUPABASE__ADDITIONAL_REDIRECT_URLS",
|
|
|
|
|
'["https://a.example.com", "https://b.example.com/path"]',
|
|
|
|
|
)
|
2026-02-05 15:13:06 +08:00
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__HOST", "db")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__PORT", "5432")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__NAME", "app")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__USER", "user")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__PASSWORD", "pass")
|
|
|
|
|
|
|
|
|
|
settings = Settings()
|
|
|
|
|
|
2026-03-09 18:03:04 +08:00
|
|
|
assert str(settings.supabase.public_url) == "https://public.example:8443/"
|
2026-02-05 15:13:06 +08:00
|
|
|
assert settings.supabase.anon_key == "anon-key"
|
|
|
|
|
assert settings.supabase.service_role_key == "service-key"
|
2026-03-07 14:55:00 +08:00
|
|
|
assert settings.supabase.site_url == "https://app.example.com"
|
|
|
|
|
assert settings.supabase.additional_redirect_urls == [
|
|
|
|
|
"https://a.example.com",
|
|
|
|
|
"https://b.example.com/path",
|
|
|
|
|
]
|
2026-02-05 15:13:06 +08:00
|
|
|
|
|
|
|
|
supabase_settings = settings.model_dump()["supabase"]
|
2026-03-09 18:03:04 +08:00
|
|
|
assert str(supabase_settings["public_url"]) == "https://public.example:8443/"
|
2026-02-05 15:13:06 +08:00
|
|
|
assert supabase_settings["anon_key"] == "anon-key"
|
|
|
|
|
assert supabase_settings["service_role_key"] == "service-key"
|
2026-03-07 14:55:00 +08:00
|
|
|
assert supabase_settings["site_url"] == "https://app.example.com"
|
2026-03-09 18:03:04 +08:00
|
|
|
assert "jwt_secret" not in supabase_settings
|
|
|
|
|
assert "public_scheme" not in supabase_settings
|
|
|
|
|
assert "public_host" not in supabase_settings
|
|
|
|
|
assert "kong_http_port" not in supabase_settings
|
2026-02-05 15:13:06 +08:00
|
|
|
assert settings.database_url == "postgresql+asyncpg://user:pass@db:5432/app"
|
2026-03-09 18:03:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cloud_supabase_env_populates_settings(monkeypatch: MonkeyPatch) -> None:
|
|
|
|
|
monkeypatch.setenv(
|
|
|
|
|
"SOCIAL_SUPABASE__PUBLIC_URL", "https://project.example.supabase.co"
|
|
|
|
|
)
|
|
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__ANON_KEY", "anon-key")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__SERVICE_ROLE_KEY", "service-key")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__JWT_AUDIENCE", "authenticated")
|
|
|
|
|
|
|
|
|
|
settings = Settings()
|
|
|
|
|
|
|
|
|
|
assert str(settings.supabase.public_url) == "https://project.example.supabase.co/"
|
|
|
|
|
assert settings.supabase.jwt_audience == "authenticated"
|
|
|
|
|
assert settings.supabase.jwt_issuer == "https://project.example.supabase.co/auth/v1"
|
|
|
|
|
assert (
|
|
|
|
|
settings.supabase.jwks_url
|
|
|
|
|
== "https://project.example.supabase.co/auth/v1/.well-known/jwks.json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
supabase_settings = settings.model_dump()["supabase"]
|
|
|
|
|
assert "jwt_secret" not in supabase_settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_missing_public_url_raises_validation_error() -> None:
|
|
|
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
|
|
|
SupabaseSettings()
|
|
|
|
|
|
|
|
|
|
assert "public_url" in str(exc_info.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_public_url_with_trailing_slash_normalizes_correctly(
|
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
|
) -> None:
|
|
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__PUBLIC_URL", "https://example.supabase.co/")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__ANON_KEY", "anon-key")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_SUPABASE__SERVICE_ROLE_KEY", "service-key")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__HOST", "db")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__PORT", "5432")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__NAME", "app")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__USER", "user")
|
|
|
|
|
monkeypatch.setenv("SOCIAL_DATABASE__PASSWORD", "pass")
|
|
|
|
|
|
|
|
|
|
settings = Settings()
|
|
|
|
|
|
|
|
|
|
assert settings.supabase.jwt_issuer == "https://example.supabase.co/auth/v1"
|
|
|
|
|
assert (
|
|
|
|
|
settings.supabase.jwks_url
|
|
|
|
|
== "https://example.supabase.co/auth/v1/.well-known/jwks.json"
|
|
|
|
|
)
|
|
|
|
|
assert settings.supabase.url == "https://example.supabase.co/"
|