talos-access / backend /tests /storage /test_supabase.py
Greg
refactor(ta-01): swap supabase-py for boto3 S3 adapter
959bccd
Raw History Blame Contribute Delete
2.49 kB
import pytest
def _stub_boto3(monkeypatch):
"""Replace boto3 with a no-op stub so the SupabaseStorage constructor
can build a 'client' without hitting the network or requiring real AWS
creds during tests."""
import sys
import types
fake_boto3 = types.ModuleType("boto3")
fake_boto3.client = lambda *a, **kw: object() # type: ignore[attr-defined]
monkeypatch.setitem(sys.modules, "boto3", fake_boto3)
fake_botocore = types.ModuleType("botocore")
fake_client_mod = types.ModuleType("botocore.client")
fake_client_mod.Config = lambda **kw: object() # type: ignore[attr-defined]
fake_botocore.client = fake_client_mod # type: ignore[attr-defined]
monkeypatch.setitem(sys.modules, "botocore", fake_botocore)
monkeypatch.setitem(sys.modules, "botocore.client", fake_client_mod)
def test_constructor_rejects_missing_credentials():
from app.storage.supabase import SupabaseStorage
with pytest.raises(RuntimeError, match="SUPABASE_ACCESS_KEY_ID"):
SupabaseStorage(
access_key="",
secret_key="",
endpoint="",
region="us-west-2",
bucket="",
)
def test_constructor_with_credentials_stores_config(monkeypatch):
_stub_boto3(monkeypatch)
from app.storage.supabase import SupabaseStorage
s = SupabaseStorage(
access_key="ak",
secret_key="sk",
endpoint="https://abc.storage.supabase.co/storage/v1/s3",
region="us-west-2",
bucket="user-photos",
)
assert s.bucket == "user-photos"
assert s.signed_url_ttl == 600
def test_strip_url_prefix_passes_through_relative_paths():
from app.storage.supabase import SupabaseStorage
assert SupabaseStorage._strip_url_prefix("1/2/abc.webp") == "1/2/abc.webp"
def test_strip_url_prefix_extracts_key_from_full_url():
from app.storage.supabase import SupabaseStorage
full = "https://abc.storage.supabase.co/user-photos/1/2/abc.webp?X-Amz-Signature=xyz"
assert SupabaseStorage._strip_url_prefix(full) == "1/2/abc.webp"
def test_get_url_passes_http_urls_through(monkeypatch):
_stub_boto3(monkeypatch)
from app.storage.supabase import SupabaseStorage
s = SupabaseStorage(
access_key="ak", secret_key="sk",
endpoint="https://abc.storage.supabase.co/storage/v1/s3",
region="us-west-2", bucket="user-photos",
)
full = "https://signed.example.com/file.webp?sig=x"
assert s.get_url(full) == full