Buckets:

download
raw
40.1 kB
"""Tests for authentication flows: register, login, logout, refresh tokens, email verification, password reset, token expiry, and auth rate limiting.""" import pytest from fastapi import Depends, FastAPI from httpx import ASGITransport, AsyncClient from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from sqlmodel import SQLModel from fastapi_fullauth import AuthRateLimits, FullAuth, FullAuthConfig from fastapi_fullauth.core.crypto import hash_refresh_token from fastapi_fullauth.dependencies import current_token_payload, current_user from tests.conftest import make_test_adapter async def _make_db(): 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) return engine, session_maker async def _make_app(adapter=None, **fullauth_kwargs): """Helper to build a test app with given FullAuth config.""" engine, session_maker = await _make_db() adapter = adapter or make_test_adapter(session_maker) fullauth = FullAuth( config=FullAuthConfig( SECRET_KEY="test-secret-key-that-is-long-enough-32b", ), adapter=adapter, **fullauth_kwargs, ) app = FastAPI() fullauth.init_app(app) return app, adapter, fullauth, engine # Basic auth flows (register, login, logout, /me) @pytest.mark.asyncio async def test_register(client): r = await client.post( "/api/v1/auth/register", json={"email": "new@test.com", "password": "securepass123"}, ) assert r.status_code == 201 data = r.json() assert data["email"] == "new@test.com" assert data["is_active"] is True assert data["is_verified"] is False @pytest.mark.asyncio async def test_register_duplicate(client, registered_user): r = await client.post( "/api/v1/auth/register", json={"email": "user@test.com", "password": "securepass123"}, ) assert r.status_code == 409 @pytest.mark.asyncio async def test_refresh_tokens_are_hashed_at_rest(adapter, login_tokens): """The database stores only the sha256 digest of a refresh token, so a leaked database cannot hand out live sessions. Lookups hash the presented token; the raw token must not match a stored row.""" raw = login_tokens["refresh_token"] assert await adapter.get_refresh_token(raw) is None stored = await adapter.get_refresh_token(hash_refresh_token(raw)) assert stored is not None assert stored.token != raw @pytest.mark.asyncio async def test_register_anti_enumeration_same_response_for_new_and_existing(): """With PREVENT_REGISTRATION_ENUMERATION (the default): new and duplicate emails produce identical 202 responses so the endpoint can't be used to probe whether an email is registered.""" engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) fullauth = FullAuth( config=FullAuthConfig( SECRET_KEY="test-secret-key-that-is-long-enough-32b", PREVENT_REGISTRATION_ENUMERATION=True, ), adapter=adapter, ) app = FastAPI() fullauth.init_app(app) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: first = await client.post( "/api/v1/auth/register", json={"email": "new@test.com", "password": "securepass123"}, ) second = await client.post( "/api/v1/auth/register", json={"email": "new@test.com", "password": "securepass123"}, ) assert first.status_code == 202 assert second.status_code == 202 assert first.json() == second.json() await engine.dispose() @pytest.mark.asyncio async def test_register_weak_password(client): r = await client.post( "/api/v1/auth/register", json={"email": "weak@test.com", "password": "short"}, ) assert r.status_code == 422 @pytest.mark.asyncio async def test_login_success(client, registered_user): r = await client.post( "/api/v1/auth/login", json={"email": "user@test.com", "password": "securepass123"}, ) assert r.status_code == 200 data = r.json() assert "access_token" in data assert "refresh_token" in data assert data["token_type"] == "bearer" @pytest.mark.asyncio async def test_login_wrong_password(client, registered_user): r = await client.post( "/api/v1/auth/login", json={"email": "user@test.com", "password": "wrongpassword"}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_login_nonexistent_user(client): r = await client.post( "/api/v1/auth/login", json={"email": "nobody@test.com", "password": "whatever123"}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_current_token_payload_exposes_claims(): async def add_claims(user): return {"tenant_id": "acme", "plan": "pro"} app, adapter, fullauth, engine = await _make_app(on_create_token_claims=add_claims) @app.get("/whoami") async def whoami(payload=Depends(current_token_payload)): return {"sub": payload.sub, "extra": payload.extra} transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "claims@test.com", "password": "securepass123"}, ) login = await client.post( "/api/v1/auth/login", json={"email": "claims@test.com", "password": "securepass123"}, ) token = login.json()["access_token"] # no token -> 401 anon = await client.get("/whoami") assert anon.status_code == 401 # valid token -> payload carries the custom claims r = await client.get("/whoami", headers={"Authorization": f"Bearer {token}"}) assert r.status_code == 200 body = r.json() assert body["sub"] assert body["extra"]["tenant_id"] == "acme" assert body["extra"]["plan"] == "pro" await engine.dispose() @pytest.mark.asyncio async def test_login_locked_account_returns_generic_credentials_error(): engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) fullauth = FullAuth( config=FullAuthConfig( SECRET_KEY="test-secret-key-that-is-long-enough-32b", MAX_LOGIN_ATTEMPTS=2, AUTH_RATE_LIMIT_ENABLED=False, ), adapter=adapter, ) app = FastAPI() fullauth.init_app(app) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "locked@test.com", "password": "securepass123"}, ) for _ in range(2): await client.post( "/api/v1/auth/login", json={"email": "locked@test.com", "password": "wrong"}, ) # account is now locked = even the correct password must look identical # to "wrong password" (401 + generic detail), not 423 or a locked-specific body r = await client.post( "/api/v1/auth/login", json={"email": "locked@test.com", "password": "securepass123"}, ) assert r.status_code == 401 assert r.json()["detail"] == "Could not validate credentials" await engine.dispose() @pytest.mark.asyncio async def test_me_authenticated(client, auth_headers): r = await client.get("/me", headers=auth_headers) assert r.status_code == 200 assert r.json()["email"] == "user@test.com" @pytest.mark.asyncio async def test_me_no_token(client): r = await client.get("/me") assert r.status_code == 401 @pytest.mark.asyncio async def test_me_invalid_token(client): r = await client.get("/me", headers={"Authorization": "Bearer garbage"}) assert r.status_code == 401 @pytest.mark.asyncio async def test_logout_rejects_purpose_scoped_token(client, fullauth, registered_user): # A password-reset / email-verify token is access-typed but purpose-scoped; # it must not be accepted as a session credential at /logout. token = fullauth.token_engine.create_access_token( user_id=registered_user["id"], extra={"purpose": "password_reset"}, ) r = await client.post( "/api/v1/auth/logout", headers={"Authorization": f"Bearer {token}"}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_custom_claims_reject_reserved_purpose_key(adapter): from uuid import uuid4 from fastapi_fullauth import FullAuth, FullAuthConfig, UserSchema async def add_claims(user): return {"purpose": "password_reset"} fullauth = FullAuth( config=FullAuthConfig(SECRET_KEY="test-secret-key-that-is-long-enough-32b"), adapter=adapter, on_create_token_claims=add_claims, ) with pytest.raises(ValueError, match="reserved"): await fullauth.get_custom_claims(UserSchema(id=uuid4(), email="x@test.com")) @pytest.mark.asyncio async def test_logout(client, auth_headers, login_tokens): r = await client.post("/api/v1/auth/logout", headers=auth_headers) assert r.status_code == 204 # token should be blacklisted now r = await client.get("/me", headers=auth_headers) assert r.status_code == 401 @pytest.mark.asyncio async def test_refresh(client, login_tokens): r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": login_tokens["refresh_token"]}, ) assert r.status_code == 200 data = r.json() assert "access_token" in data assert data["access_token"] != login_tokens["access_token"] @pytest.mark.asyncio async def test_refresh_reuse_blocked(client, login_tokens): # first refresh works r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": login_tokens["refresh_token"]}, ) assert r.status_code == 200 # second use of same refresh token should fail (blacklisted) r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": login_tokens["refresh_token"]}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_refresh_rotation_is_atomic_on_store_failure(client, login_tokens): """If persisting the new refresh token fails mid-rotation, the whole rotation must roll back so the old token isn't left revoked-with-no-successor (a silently orphaned session). The old token must remain usable.""" import importlib from unittest.mock import patch async def _boom(*args, **kwargs): raise RuntimeError("simulated store failure") old_refresh = login_tokens["refresh_token"] # Patch via the module: the re-exported `refresh` function shadows its module name. refresh_flow = importlib.import_module("fastapi_fullauth.flows.refresh") with ( patch.object(refresh_flow, "issue_token_pair", _boom), pytest.raises(RuntimeError), ): await client.post( "/api/v1/auth/refresh", json={"refresh_token": old_refresh}, ) # The failed rotation rolled back: the old refresh token still works. r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": old_refresh}, ) assert r.status_code == 200 @pytest.mark.asyncio async def test_refresh_with_access_token_fails(client, login_tokens): r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": login_tokens["access_token"]}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_password_reset_flow(client, registered_user, fullauth): # request reset r = await client.post( "/api/v1/auth/password-reset/request", json={"email": "user@test.com"}, ) assert r.status_code == 202 # generate token manually for testing (normally sent via email) from fastapi_fullauth.flows.password_reset import request_password_reset token = await request_password_reset(fullauth.adapter, fullauth.token_engine, "user@test.com") assert token is not None # confirm reset r = await client.post( "/api/v1/auth/password-reset/confirm", json={"token": token, "new_password": "newpassword123"}, ) assert r.status_code == 200 # login with new password r = await client.post( "/api/v1/auth/login", json={"email": "user@test.com", "password": "newpassword123"}, ) assert r.status_code == 200 # old password should fail r = await client.post( "/api/v1/auth/login", json={"email": "user@test.com", "password": "securepass123"}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_password_reset_nonexistent_user(client): r = await client.post( "/api/v1/auth/password-reset/request", json={"email": "nobody@test.com"}, ) # should still return 202 to prevent enumeration assert r.status_code == 202 # Refresh token persistence, reuse detection, and Redis blacklist @pytest.mark.asyncio async def test_login_persists_refresh_token(): app, adapter, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) refresh_token = r.json()["refresh_token"] # refresh token should be stored in adapter (as its digest) stored = await adapter.get_refresh_token(hash_refresh_token(refresh_token)) assert stored is not None assert stored.user_id is not None assert stored.family_id is not None assert stored.revoked is False await engine.dispose() @pytest.mark.asyncio async def test_refresh_persists_new_token_and_revokes_old(): app, adapter, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) old_refresh = r.json()["refresh_token"] r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": old_refresh}, ) assert r.status_code == 200 new_refresh = r.json()["refresh_token"] assert new_refresh != old_refresh # old token should be revoked old_stored = await adapter.get_refresh_token(hash_refresh_token(old_refresh)) assert old_stored is not None assert old_stored.revoked is True # new token should be stored new_stored = await adapter.get_refresh_token(hash_refresh_token(new_refresh)) assert new_stored is not None assert new_stored.revoked is False # same family assert old_stored.family_id == new_stored.family_id await engine.dispose() @pytest.mark.asyncio async def test_refresh_reuse_blocked_by_blacklist(): """Replaying an already-used refresh token is blocked (JTI blacklisted).""" app, adapter, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) old_refresh = r.json()["refresh_token"] # first refresh = should succeed r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": old_refresh}, ) assert r.status_code == 200 # replay the OLD refresh token = blocked by blacklist r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": old_refresh}, ) assert r.status_code == 401 await engine.dispose() @pytest.mark.asyncio async def test_refresh_reuse_revokes_family_when_blacklist_lost(): """If the blacklist lost the JTI (e.g. restart), DB reuse detection kicks in.""" app, adapter, fullauth, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) old_refresh = r.json()["refresh_token"] # first refresh = succeeds, old token revoked in DB r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": old_refresh}, ) assert r.status_code == 200 new_refresh = r.json()["refresh_token"] # simulate blacklist loss (e.g., server restart with InMemory) fullauth.token_engine.blacklist = type(fullauth.token_engine.blacklist)() # replay the old token = blacklist doesn't catch it, # but DB reuse detection revokes the family r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": old_refresh}, ) assert r.status_code == 401 # the new token's family should also be revoked new_stored = await adapter.get_refresh_token(hash_refresh_token(new_refresh)) assert new_stored is not None assert new_stored.revoked is True await engine.dispose() @pytest.mark.asyncio async def test_refresh_reuse_caught_by_explicit_blacklist_check(): """Even if decode_token doesn't catch it (e.g. race window), the explicit is_blacklisted guard before issuing new tokens rejects the second call.""" from unittest.mock import patch app, adapter, fullauth, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) refresh_token = r.json()["refresh_token"] # first refresh succeeds normally r1 = await client.post( "/api/v1/auth/refresh", json={"refresh_token": refresh_token}, ) assert r1.status_code == 200 # simulate race: patch decode_token to skip the blacklist check # (as if two requests decoded in parallel before either blacklisted) original_decode = fullauth.token_engine.decode_token blacklist = fullauth.token_engine.blacklist async def decode_skipping_blacklist(token, **kwargs): # temporarily disable blacklist during decode fullauth.token_engine.blacklist = type(blacklist)() try: return await original_decode(token, **kwargs) finally: fullauth.token_engine.blacklist = blacklist with patch.object(fullauth.token_engine, "decode_token", decode_skipping_blacklist): r2 = await client.post( "/api/v1/auth/refresh", json={"refresh_token": refresh_token}, ) # the explicit is_blacklisted check in the route catches it assert r2.status_code == 401 await engine.dispose() @pytest.mark.asyncio async def test_refresh_no_rotation(): engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) fullauth = FullAuth( config=FullAuthConfig( SECRET_KEY="test-secret-key-that-is-long-enough-32b", REFRESH_TOKEN_ROTATION=False, ), adapter=adapter, ) app = FastAPI() fullauth.init_app(app) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) original_refresh = r.json()["refresh_token"] r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": original_refresh}, ) assert r.status_code == 200 assert r.json()["refresh_token"] == original_refresh # same token returned # can use it again (no rotation = reuse is fine) r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": original_refresh}, ) assert r.status_code == 200 await engine.dispose() @pytest.mark.asyncio async def test_logout_revokes_refresh_family(): app, adapter, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) tokens = r.json() r = await client.post( "/api/v1/auth/logout", headers={"Authorization": f"Bearer {tokens['access_token']}"}, json={"refresh_token": tokens["refresh_token"]}, ) assert r.status_code == 204 # refresh token family should be revoked stored = await adapter.get_refresh_token(hash_refresh_token(tokens["refresh_token"])) assert stored is not None assert stored.revoked is True await engine.dispose() @pytest.mark.asyncio async def test_logout_without_body_still_works(): app, adapter, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) token = r.json()["access_token"] r = await client.post( "/api/v1/auth/logout", headers={"Authorization": f"Bearer {token}"}, ) assert r.status_code == 204 await engine.dispose() @pytest.mark.asyncio async def test_logout_without_refresh_token_ends_the_session(): """Logout with only the access token (no refresh in the body) must still end the session: the family is revoked via the access token's family_id, and the refresh token can no longer mint new tokens.""" app, adapter, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) tokens = r.json() # Log out presenting ONLY the access token - no refresh_token body. r = await client.post( "/api/v1/auth/logout", headers={"Authorization": f"Bearer {tokens['access_token']}"}, ) assert r.status_code == 204 # The refresh-token family is revoked... stored = await adapter.get_refresh_token(hash_refresh_token(tokens["refresh_token"])) assert stored is not None assert stored.revoked is True # ...so the refresh token can no longer be used. r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": tokens["refresh_token"]}, ) assert r.status_code == 401 await engine.dispose() # Redis blacklist @pytest.mark.asyncio async def test_redis_blacklist_add_and_check(): import fakeredis.aioredis from fastapi_fullauth.core.blacklist import RedisTokenBlacklist bl = RedisTokenBlacklist.__new__(RedisTokenBlacklist) bl._redis = fakeredis.aioredis.FakeRedis(decode_responses=True) bl._default_ttl = 300 bl._prefix = "fullauth:blacklist:" assert await bl.is_blacklisted("jti-123") is False await bl.add("jti-123") assert await bl.is_blacklisted("jti-123") is True assert await bl.is_blacklisted("jti-other") is False @pytest.mark.asyncio async def test_redis_blacklist_custom_ttl(): import fakeredis.aioredis from fastapi_fullauth.core.blacklist import RedisTokenBlacklist bl = RedisTokenBlacklist.__new__(RedisTokenBlacklist) bl._redis = fakeredis.aioredis.FakeRedis(decode_responses=True) bl._default_ttl = 300 bl._prefix = "fullauth:blacklist:" await bl.add("jti-ttl", ttl_seconds=10) ttl = await bl._redis.ttl("fullauth:blacklist:jti-ttl") assert ttl <= 10 assert ttl > 0 def test_redis_blacklist_requires_redis_package(): """RedisTokenBlacklist raises helpful ImportError if redis not installed.""" import unittest.mock with unittest.mock.patch.dict("sys.modules", {"redis": None, "redis.asyncio": None}): # need to reimport to trigger the import check import importlib from fastapi_fullauth.core import blacklist importlib.reload(blacklist) with pytest.raises(ImportError, match="redis package is required"): blacklist.RedisTokenBlacklist("redis://localhost") # Email verification @pytest.fixture def sent_emails(): return [] @pytest.fixture async def verify_app(sent_emails): async def mock_send(email: str, token: str): sent_emails.append({"email": email, "token": token}) engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) config = FullAuthConfig(SECRET_KEY="test-secret-key-that-is-long-enough-32b") fullauth = FullAuth(config=config, adapter=adapter) fullauth.hooks.on("send_verification_email", mock_send) app = FastAPI() fullauth.init_app(app) @app.get("/me") async def me(user=Depends(current_user)): return user yield app await engine.dispose() @pytest.fixture async def verify_client(verify_app): transport = ASGITransport(app=verify_app) async with AsyncClient(transport=transport, base_url="http://test") as c: yield c async def _register_and_login_verify(client): await client.post( "/api/v1/auth/register", json={"email": "verify@test.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "verify@test.com", "password": "securepass123"}, ) return r.json() @pytest.mark.asyncio async def test_verify_email_full_flow(verify_client, sent_emails): tokens = await _register_and_login_verify(verify_client) headers = {"Authorization": f"Bearer {tokens['access_token']}"} # not verified yet r = await verify_client.get("/me", headers=headers) assert r.json()["is_verified"] is False # request verification r = await verify_client.post("/api/v1/auth/verify-email/request", headers=headers) assert r.status_code == 202 assert len(sent_emails) == 1 assert sent_emails[0]["email"] == "verify@test.com" # confirm r = await verify_client.post( "/api/v1/auth/verify-email/confirm", json={"token": sent_emails[0]["token"]}, ) assert r.status_code == 200 # now verified r = await verify_client.get("/me", headers=headers) assert r.json()["is_verified"] is True @pytest.mark.asyncio async def test_verify_token_single_use(verify_client, sent_emails): tokens = await _register_and_login_verify(verify_client) headers = {"Authorization": f"Bearer {tokens['access_token']}"} await verify_client.post("/api/v1/auth/verify-email/request", headers=headers) verify_token = sent_emails[0]["token"] # first use works r = await verify_client.post( "/api/v1/auth/verify-email/confirm", json={"token": verify_token}, ) assert r.status_code == 200 # second use fails r = await verify_client.post( "/api/v1/auth/verify-email/confirm", json={"token": verify_token}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_verify_email_token_burned_when_already_verified(): """A verification token must be single-use even when it resolves to an already-verified account (e.g. verified by another path), so it can't be replayed for its remaining lifetime.""" from fastapi_fullauth.core.blacklist import InMemoryTokenBlacklist from fastapi_fullauth.core.tokens import TokenEngine from fastapi_fullauth.exceptions import TokenBlacklistedError from fastapi_fullauth.flows.email_verify import ( create_email_verification_token, verify_email, ) from fastapi_fullauth.types import CreateUserSchema engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) config = FullAuthConfig(SECRET_KEY="test-secret-key-that-is-long-enough-32b") token_engine = TokenEngine(config=config, blacklist=InMemoryTokenBlacklist()) user = await adapter.create_user( CreateUserSchema(email="already@test.com", password="securepass123"), hashed_password="x", ) await adapter.set_user_verified(user.id) token = await create_email_verification_token(adapter, token_engine, user.id) assert token is not None # First use resolves the already-verified account and burns the token. await verify_email(adapter, token_engine, token) # Replay is rejected: the token was blacklisted on that first use. with pytest.raises(TokenBlacklistedError): await verify_email(adapter, token_engine, token) await engine.dispose() @pytest.mark.asyncio async def test_reset_password_blocked_for_deactivated_user(): """A held password-reset token must not be actionable once the account is deactivated (parity with login/OAuth). The token is also burned on rejection.""" from fastapi_fullauth.core.blacklist import InMemoryTokenBlacklist from fastapi_fullauth.core.tokens import TokenEngine from fastapi_fullauth.exceptions import TokenBlacklistedError, TokenError from fastapi_fullauth.flows.password_reset import request_password_reset, reset_password from fastapi_fullauth.types import CreateUserSchema engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) config = FullAuthConfig(SECRET_KEY="test-secret-key-that-is-long-enough-32b") token_engine = TokenEngine(config=config, blacklist=InMemoryTokenBlacklist()) user = await adapter.create_user( CreateUserSchema(email="off@test.com", password="securepass123"), hashed_password="x", ) token = await request_password_reset(adapter, token_engine, "off@test.com") assert token is not None await adapter.update_user(user.id, {"is_active": False}) with pytest.raises(TokenError, match="deactivated"): await reset_password(adapter, token_engine, token, "newsecurepass123") # The token was burned on rejection, so it can't be retried. with pytest.raises(TokenBlacklistedError): await reset_password(adapter, token_engine, token, "newsecurepass123") await engine.dispose() @pytest.mark.asyncio async def test_verify_email_blocked_for_deactivated_user(): """Email verification must not act on a deactivated account (parity with login/OAuth). The token is still burned so it can't be replayed.""" from fastapi_fullauth.core.blacklist import InMemoryTokenBlacklist from fastapi_fullauth.core.tokens import TokenEngine from fastapi_fullauth.exceptions import TokenBlacklistedError, TokenError from fastapi_fullauth.flows.email_verify import ( create_email_verification_token, verify_email, ) from fastapi_fullauth.types import CreateUserSchema engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) config = FullAuthConfig(SECRET_KEY="test-secret-key-that-is-long-enough-32b") token_engine = TokenEngine(config=config, blacklist=InMemoryTokenBlacklist()) user = await adapter.create_user( CreateUserSchema(email="off2@test.com", password="securepass123"), hashed_password="x", ) token = await create_email_verification_token(adapter, token_engine, user.id) assert token is not None await adapter.update_user(user.id, {"is_active": False}) with pytest.raises(TokenError, match="deactivated"): await verify_email(adapter, token_engine, token) with pytest.raises(TokenBlacklistedError): await verify_email(adapter, token_engine, token) await engine.dispose() @pytest.mark.asyncio async def test_verify_invalid_token(verify_client): r = await verify_client.post( "/api/v1/auth/verify-email/confirm", json={"token": "garbage"}, ) assert r.status_code == 401 @pytest.mark.asyncio async def test_verify_without_callback(client, auth_headers): """No email callback configured = request still returns 202 but no email sent.""" r = await client.post("/api/v1/auth/verify-email/request", headers=auth_headers) assert r.status_code == 202 # expires_in in token response @pytest.mark.asyncio async def test_login_returns_expires_in(): app, _, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) tokens = r.json() assert "expires_in" in tokens assert tokens["expires_in"] == 30 * 60 # default 30 minutes await engine.dispose() @pytest.mark.asyncio async def test_refresh_returns_expires_in(): app, _, _, engine = await _make_app() transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) tokens = r.json() r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": tokens["refresh_token"]}, ) assert r.status_code == 200 assert r.json()["expires_in"] == 30 * 60 await engine.dispose() # Auth route rate limiting @pytest.mark.asyncio async def test_login_rate_limited(): engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) fullauth = FullAuth( config=FullAuthConfig( SECRET_KEY="test-secret-key-that-is-long-enough-32b", AUTH_RATE_LIMIT_ENABLED=True, AUTH_RATE_LIMITS=AuthRateLimits(login=2), AUTH_RATE_LIMIT_WINDOW_SECONDS=60, ), adapter=adapter, ) app = FastAPI() fullauth.init_app(app) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "t@t.com", "password": "securepass123"}, ) # first 2 login attempts should work (even if wrong password) for _ in range(2): await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) # 3rd should be rate limited r = await client.post( "/api/v1/auth/login", json={"email": "t@t.com", "password": "securepass123"}, ) assert r.status_code == 429 assert r.headers["X-RateLimit-Limit"] == "2" assert r.headers["X-RateLimit-Remaining"] == "0" assert int(r.headers["X-RateLimit-Reset"]) >= 0 assert int(r.headers["Retry-After"]) >= 0 await engine.dispose() @pytest.mark.asyncio async def test_register_rate_limited(): engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) fullauth = FullAuth( config=FullAuthConfig( SECRET_KEY="test-secret-key-that-is-long-enough-32b", AUTH_RATE_LIMIT_ENABLED=True, AUTH_RATE_LIMITS=AuthRateLimits(register=1), AUTH_RATE_LIMIT_WINDOW_SECONDS=60, ), adapter=adapter, ) app = FastAPI() fullauth.init_app(app) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "a@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/register", json={"email": "b@t.com", "password": "securepass123"}, ) assert r.status_code == 429 await engine.dispose() @pytest.mark.asyncio async def test_refresh_rate_limited(): engine, session_maker = await _make_db() adapter = make_test_adapter(session_maker) fullauth = FullAuth( config=FullAuthConfig( SECRET_KEY="test-secret-key-that-is-long-enough-32b", AUTH_RATE_LIMIT_ENABLED=True, AUTH_RATE_LIMITS=AuthRateLimits(refresh=1), AUTH_RATE_LIMIT_WINDOW_SECONDS=60, ), adapter=adapter, ) app = FastAPI() fullauth.init_app(app) transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as client: await client.post( "/api/v1/auth/register", json={"email": "rl@t.com", "password": "securepass123"}, ) r = await client.post( "/api/v1/auth/login", json={"email": "rl@t.com", "password": "securepass123"}, ) refresh_token = r.json()["refresh_token"] # first refresh consumes the budget r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": refresh_token}, ) assert r.status_code == 200 new_refresh = r.json()["refresh_token"] # second refresh hits the limiter before token validation r = await client.post( "/api/v1/auth/refresh", json={"refresh_token": new_refresh}, ) assert r.status_code == 429 await engine.dispose()
"""Tests for authentication flows: register, login, logout, refresh tokens, email
verification, password reset, token expiry, and auth rate limiting."""
import pytest
from fastapi import Depends, FastAPI
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlmodel import SQLModel
from fastapi_fullauth import AuthRateLimits, FullAuth, FullAuthConfig
from fastapi_fullauth.core.crypto import hash_refresh_token
from fastapi_fullauth.dependencies import current_token_payload, current_user
from tests.conftest import make_test_adapter
async def _make_db():
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)
return engine, session_maker
async def _make_app(adapter=None, **fullauth_kwargs):
"""Helper to build a test app with given FullAuth config."""
engine, session_maker = await _make_db()
adapter = adapter or make_test_adapter(session_maker)
fullauth = FullAuth(
config=FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
),
adapter=adapter,
**fullauth_kwargs,
)
app = FastAPI()
fullauth.init_app(app)
return app, adapter, fullauth, engine
# Basic auth flows (register, login, logout, /me)
@pytest.mark.asyncio
async def test_register(client):
r = await client.post(
"/api/v1/auth/register",
json={"email": "new@test.com", "password": "securepass123"},
)
assert r.status_code == 201
data = r.json()
assert data["email"] == "new@test.com"
assert data["is_active"] is True
assert data["is_verified"] is False
@pytest.mark.asyncio
async def test_register_duplicate(client, registered_user):
r = await client.post(
"/api/v1/auth/register",
json={"email": "user@test.com", "password": "securepass123"},
)
assert r.status_code == 409
@pytest.mark.asyncio
async def test_refresh_tokens_are_hashed_at_rest(adapter, login_tokens):
"""The database stores only the sha256 digest of a refresh token, so a
leaked database cannot hand out live sessions. Lookups hash the presented
token; the raw token must not match a stored row."""
raw = login_tokens["refresh_token"]
assert await adapter.get_refresh_token(raw) is None
stored = await adapter.get_refresh_token(hash_refresh_token(raw))
assert stored is not None
assert stored.token != raw
@pytest.mark.asyncio
async def test_register_anti_enumeration_same_response_for_new_and_existing():
"""With PREVENT_REGISTRATION_ENUMERATION (the default): new and duplicate
emails produce identical 202 responses so the endpoint can't be used to
probe whether an email is registered."""
engine, session_maker = await _make_db()
adapter = make_test_adapter(session_maker)
fullauth = FullAuth(
config=FullAuthConfig(
SECRET_KEY="test-secret-key-that-is-long-enough-32b",
PREVENT_REGISTRATION_ENUMERATION=True,
),
adapter=adapter,
)
app = FastAPI()
fullauth.init_app(app)
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
first = await client.post(
"/api/v1/auth/register",
json={"email": "new@test.com", "password": "securepass123"},
)
second = await client.post(

Xet Storage Details

Size:
40.1 kB
·
Xet hash:
a8c05e0cf9b7effcb2860aeeaa21dd98685d6a2aa2d577801163db203482eb73

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