Add the scoped PostgreSQL/pgvector schema, independent ingestion settings and ports, embedding profile checks, and trusted administrator bootstrap. Include repository contracts and guidance, organized provider-free and PostgreSQL tests, deployment setup, and byte-preserving migration attributes. Validation: 163 tests passed; all 48 PostgreSQL cases passed with pgvector Python 0.5.0 and 0.3.6. No production schema or historical metadata was changed.
78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
"""Database URL validation and exception redaction."""
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
from app.modules.documents.settings import IngestionSettings
|
|
|
|
|
|
def assert_secret_free_exception(exc, secret):
|
|
pending = [exc]
|
|
seen = set()
|
|
while pending:
|
|
error = pending.pop()
|
|
if id(error) in seen:
|
|
continue
|
|
seen.add(id(error))
|
|
outputs = [str(error), repr(error)]
|
|
if isinstance(error, ValidationError):
|
|
outputs += [error.json(), repr(error.errors())]
|
|
for item in error.errors():
|
|
contextual = item.get("ctx", {}).get("error")
|
|
if isinstance(contextual, BaseException):
|
|
pending.append(contextual)
|
|
assert all(secret not in output for output in outputs)
|
|
pending.extend(item for item in (error.__context__, error.__cause__)
|
|
if item is not None)
|
|
|
|
|
|
@pytest.mark.parametrize("url", [
|
|
"postgresql+psycopg://dummy-user:dummy-password@host:dummy-url-secret/db",
|
|
"postgresql+psycopg://dummy-url-secret:dummy-password@host:invalid-port/db",
|
|
"postgresql+psycopg://dummy-user:dummy-url-secret@host:invalid-port/db",
|
|
"dummy-url-secret-not-a-url",
|
|
"postgresql+psycopg:/dummy-url-secret",
|
|
"://dummy-url-secret",
|
|
"postgresql+psycopg://u:dummy-url-secret@host:99999/db",
|
|
"postgresql+psycopg://u:dummy-url-secret@host:-1/db",
|
|
])
|
|
@pytest.mark.parametrize("source", ["direct", "environment"])
|
|
def test_malformed_database_urls_are_generic_secret_free_validation_errors(
|
|
settings, monkeypatch, url, source
|
|
):
|
|
values = settings.model_dump()
|
|
if source == "environment":
|
|
values.pop("database_url")
|
|
monkeypatch.setenv("INGEST_DATABASE_URL", url)
|
|
else:
|
|
values["database_url"] = url
|
|
with pytest.raises(ValidationError) as caught:
|
|
IngestionSettings(_env_file=None, **values)
|
|
error = caught.value
|
|
assert "Database URL must be a valid postgresql+psycopg URL" in str(error)
|
|
assert_secret_free_exception(error, "dummy-url-secret")
|
|
assert_secret_free_exception(error, "dummy-password")
|
|
assert error.__context__ is None
|
|
assert error.__cause__ is None
|
|
for item in error.errors():
|
|
contextual = item.get("ctx", {}).get("error")
|
|
if isinstance(contextual, BaseException):
|
|
assert contextual.__context__ is None
|
|
assert contextual.__cause__ is None
|
|
|
|
|
|
@pytest.mark.parametrize("url", [
|
|
"postgresql+psycopg://user:dummy-password@localhost:5432/database",
|
|
"postgresql+psycopg://user:p%40ss@localhost:6543/database?sslmode=require",
|
|
"postgresql+psycopg:///database?host=/var/run/postgresql",
|
|
"postgresql+psycopg://user@localhost/database",
|
|
])
|
|
@pytest.mark.parametrize("source", ["direct", "environment"])
|
|
def test_valid_psycopg_urls_preserve_identity(settings, monkeypatch, url, source):
|
|
values = settings.model_dump()
|
|
if source == "environment":
|
|
values.pop("database_url")
|
|
monkeypatch.setenv("INGEST_DATABASE_URL", url)
|
|
else:
|
|
values["database_url"] = url
|
|
configured = IngestionSettings(_env_file=None, **values)
|
|
assert configured.database_url.get_secret_value() == url
|