Buckets:

download
raw
12.3 kB
"""Tests for small polish items: bearer case, require_role without roles field,
bcrypt length limit, SQLModel hashed_password column type, passkey config validation."""
import importlib.util
import pytest
from fastapi import FastAPI, Request
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlmodel import SQLModel
from fastapi_fullauth import FullAuth, FullAuthConfig
from fastapi_fullauth.adapters.sqlmodel import SQLModelAdapter
from fastapi_fullauth.backends.bearer import BearerBackend
from fastapi_fullauth.core.crypto import hash_password
from fastapi_fullauth.exceptions import InvalidPasswordError
from tests.conftest import RefreshToken, User
_has_bcrypt = importlib.util.find_spec("bcrypt") is not None
# ── Bearer header case-insensitivity ────────────────────────────────
@pytest.mark.asyncio
async def test_bearer_accepts_lowercase_scheme():
backend = BearerBackend()
async def _read(auth_header: str | None) -> str | None:
scope = {
"type": "http",
"headers": [(b"authorization", auth_header.encode())] if auth_header else [],
}
request = Request(scope)
return await backend.read_token(request)
assert await _read("Bearer abc") == "abc"
assert await _read("bearer abc") == "abc"
assert await _read("BEARER abc") == "abc"
assert await _read("bEaReR abc") == "abc"
assert await _read("Basic abc") is None
assert await _read(None) is None
# ── require_role tolerates missing roles field ──────────────────────
@pytest.mark.asyncio
async def test_require_role_returns_403_when_user_has_no_roles_field():
"""The default UserSchema has no `roles` field. require_role should return
a clean 403, not AttributeError."""
from fastapi import Depends
from fastapi_fullauth.dependencies import current_user, require_role
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
session_maker = async_sessionmaker(engine, expire_on_commit=False)
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
adapter = SQLModelAdapter(
session_maker=session_maker,
user_model=User,
refresh_token_model=RefreshToken,
)
fullauth = FullAuth(
config=FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
),
adapter=adapter,
)
app = FastAPI()
fullauth.init_app(app)
@app.get("/admin-only", dependencies=[Depends(require_role("admin"))])
async def admin_only(user=Depends(current_user)):
return {"ok": True}
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
await client.post(
"/api/v1/auth/register",
json={"email": "u@test.com", "password": "securepass123"},
)
r = await client.post(
"/api/v1/auth/login",
json={"email": "u@test.com", "password": "securepass123"},
)
token = r.json()["access_token"]
r = await client.get("/admin-only", headers={"Authorization": f"Bearer {token}"})
assert r.status_code == 403
await engine.dispose()
# ── bcrypt 72-byte limit ────────────────────────────────────────────
def test_bcrypt_rejects_passwords_over_72_bytes():
# length check happens before `import bcrypt` so this works even without bcrypt installed
with pytest.raises(InvalidPasswordError, match="bcrypt passwords must be at most"):
hash_password("x" * 73, algorithm="bcrypt")
@pytest.mark.skipif(not _has_bcrypt, reason="bcrypt not installed")
def test_bcrypt_accepts_exactly_72_bytes():
assert hash_password("x" * 72, algorithm="bcrypt").startswith("$2b$")
def test_argon2_accepts_long_passwords():
# argon2id handles arbitrary length
hash_password("x" * 500, algorithm="argon2id")
# ── SQLModel hashed_password column is Text ─────────────────────────
def test_sqlmodel_hashed_password_is_text_column():
from sqlalchemy import Text
from fastapi_fullauth.models.sqlmodel import UserMixin
col = UserMixin.model_fields["hashed_password"].sa_column # type: ignore[attr-defined]
assert isinstance(col.type, Text)
# ── Passkey config validation at construction time ──────────────────
def test_passkey_enabled_without_rp_id_fails_at_config():
with pytest.raises(ValueError, match="PASSKEY_RP_ID is required"):
FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
PASSKEY_ENABLED=True,
)
def test_passkey_enabled_without_origins_fails_at_config():
with pytest.raises(ValueError, match="PASSKEY_ORIGINS is required"):
FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
PASSKEY_ENABLED=True,
PASSKEY_RP_ID="example.com",
)
def test_passkey_rp_id_with_scheme_fails_at_config():
with pytest.raises(ValueError, match="must be a bare domain"):
FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
PASSKEY_ENABLED=True,
PASSKEY_RP_ID="https://example.com",
PASSKEY_ORIGINS=["https://example.com"],
)
def test_passkey_origin_without_scheme_fails_at_config():
with pytest.raises(ValueError, match="must be a full origin"):
FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
PASSKEY_ENABLED=True,
PASSKEY_RP_ID="example.com",
PASSKEY_ORIGINS=["example.com"],
)
def test_passkey_redis_backend_without_url_fails_at_config():
with pytest.raises(ValueError, match="REDIS_URL must be set"):
FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
PASSKEY_ENABLED=True,
PASSKEY_RP_ID="example.com",
PASSKEY_ORIGINS=["https://example.com"],
PASSKEY_CHALLENGE_BACKEND="redis",
)
def test_passkey_config_happy_path():
cfg = FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
PASSKEY_ENABLED=True,
PASSKEY_RP_ID="example.com",
PASSKEY_ORIGINS=["https://example.com", "https://m.example.com"],
)
assert cfg.PASSKEY_ENABLED is True
# ── Adapter init-time model class validation ────────────────────────
def test_adapter_rejects_role_without_user_role():
from tests.conftest import RefreshToken, Role, User
with pytest.raises(ValueError, match="role_model and user_role_model"):
SQLModelAdapter(
session_maker=None, # type: ignore[arg-type]
user_model=User,
refresh_token_model=RefreshToken,
role_model=Role,
)
def test_adapter_rejects_permission_without_role_permission():
from tests.conftest import Permission, RefreshToken, Role, User, UserRole
with pytest.raises(ValueError, match="permission_model and role_permission_model"):
SQLModelAdapter(
session_maker=None, # type: ignore[arg-type]
user_model=User,
refresh_token_model=RefreshToken,
role_model=Role,
user_role_model=UserRole,
permission_model=Permission,
)
def test_adapter_rejects_permission_without_role():
from tests.conftest import Permission, RefreshToken, RolePermission, User
with pytest.raises(ValueError, match="Permissions require role_model"):
SQLModelAdapter(
session_maker=None, # type: ignore[arg-type]
user_model=User,
refresh_token_model=RefreshToken,
permission_model=Permission,
role_permission_model=RolePermission,
)
# ── Redis backends share one client per URL ─────────────────────────
@pytest.mark.asyncio
async def test_redis_backends_share_one_client_per_url():
"""Backends built for the same URL must reuse one client/pool; the client
closes only when the last holder releases it, and double-close is safe."""
from fastapi_fullauth.core._redis import _clients
from fastapi_fullauth.core.blacklist import RedisTokenBlacklist
from fastapi_fullauth.protection.lockout import RedisLockoutManager
url = "redis://shared-client-test:6379/0"
blacklist = RedisTokenBlacklist(url)
lockout = RedisLockoutManager(url)
assert blacklist._redis is lockout._redis
await blacklist.aclose()
assert url in _clients # still held by the lockout manager
await lockout.aclose()
assert url not in _clients
await lockout.aclose() # double-close must not raise or disturb the registry
assert url not in _clients
# ── init_app composes aclose() with a custom lifespan ───────────────
def _memory_fullauth() -> FullAuth:
adapter = SQLModelAdapter(
session_maker=None, # type: ignore[arg-type]
user_model=User,
refresh_token_model=RefreshToken,
)
return FullAuth(
config=FullAuthConfig(SECRET_KEY="test-secret-key-that-is-long-enough-32b"),
adapter=adapter,
)
@pytest.mark.asyncio
async def test_init_app_runs_aclose_after_custom_lifespan_teardown():
"""A custom lifespan must not bypass aclose(): init_app wraps it, and
aclose() runs after the user's lifespan teardown (fullauth is the outer
context)."""
from contextlib import asynccontextmanager
events: list[str] = []
fullauth = _memory_fullauth()
original_aclose = fullauth.aclose
async def tracking_aclose() -> None:
events.append("aclose")
await original_aclose()
fullauth.aclose = tracking_aclose # type: ignore[method-assign]
@asynccontextmanager
async def lifespan(app: FastAPI):
events.append("startup")
yield
events.append("shutdown")
app = FastAPI(lifespan=lifespan)
fullauth.init_app(app)
async with app.router.lifespan_context(app):
events.append("serving")
assert events == ["startup", "serving", "shutdown", "aclose"]
@pytest.mark.asyncio
async def test_init_app_runs_aclose_with_default_lifespan():
"""The wrap also works when the app has no custom lifespan."""
events: list[str] = []
fullauth = _memory_fullauth()
original_aclose = fullauth.aclose
async def tracking_aclose() -> None:
events.append("aclose")
await original_aclose()
fullauth.aclose = tracking_aclose # type: ignore[method-assign]
app = FastAPI()
fullauth.init_app(app)
async with app.router.lifespan_context(app):
pass
assert events == ["aclose"]
@pytest.mark.asyncio
async def test_aclose_isolates_a_failing_resource(caplog):
"""One resource failing to close must not leak the others: every remaining
resource is still closed and the error is logged, not raised."""
import logging
fullauth = _memory_fullauth()
closed: list[str] = []
async def boom() -> None:
raise RuntimeError("redis down")
def spy(name: str):
async def _aclose() -> None:
closed.append(name)
return _aclose
# The first resource closed fails; the others must still close.
fullauth.token_engine.blacklist.aclose = boom # type: ignore[method-assign]
fullauth.auth_rate_limiter.aclose = spy("rate_limiter") # type: ignore[method-assign]
if fullauth.lockout is not None:
fullauth.lockout.aclose = spy("lockout") # type: ignore[method-assign]
with caplog.at_level(logging.WARNING, logger="fastapi_fullauth"):
await fullauth.aclose() # must not raise
assert "rate_limiter" in closed
if fullauth.lockout is not None:
assert "lockout" in closed
assert any("token blacklist" in record.message for record in caplog.records)

Xet Storage Details

Size:
12.3 kB
·
Xet hash:
232e53ba6b3c006baadd2e5def26f12a3b0a3ec280a2c5e40df84cf398967dd3

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.