Files
2026-11-13 20:42:10 +03:00

49 lines
3.2 KiB
Python

"""Неверный пароль, Basic/X-User-Id на бизнес-endpoint, logout, инвалидация кэша сессий."""
from common import *
def main():
alice = new_user("auth")
st, body, h, _ = call("POST", "/api/v1/auth/sessions", headers=basic(alice["username"], "wrong-password-123"))
check(st == 401 and body["error"]["code"] == "unauthorized", "wrong password must be 401")
check(h.get("WWW-Authenticate", "").startswith("Basic"), "wrong password must return Basic challenge")
st2, _, h2, _ = call("POST", "/api/v1/auth/sessions", headers=basic("no_such_user_xyz", "wrong-password-123"))
check(st2 == 401 and h2.get("WWW-Authenticate") == h.get("WWW-Authenticate"), "unknown user must look the same as wrong password")
print("ok: wrong password / unknown user -> identical 401")
check(call("GET", "/api/v1/users/me", headers=basic(alice["username"], PASSWORD))[0] == 401, "Basic on business endpoint")
check(call("GET", "/api/v1/users/me", headers={"X-User-Id": alice["id"]})[0] == 401, "X-User-Id alone must not authenticate")
st, _, h, _ = call("GET", "/api/v1/users/me", headers=bearer("x" * 43))
check(st == 401 and h.get("WWW-Authenticate") == 'Bearer realm="messenger"', "unknown token must be 401 Bearer")
print("ok: Basic / X-User-Id / unknown token on business endpoint -> 401")
session = login(alice)
token = session["token"]
check(len(token) >= 43 and alice["id"] not in token and alice["username"] not in token, "token must be opaque random")
# первый запрос кладёт сессию в кэш, второй читает её из Redis
check(call("GET", "/api/v1/users/me", headers=bearer(token))[0] == 200, "token works")
keys = compose("exec", "-T", "redis", "redis-cli", "--scan", "--pattern", "session:*")
check(keys.strip() != "", "session must be cached in redis")
check(call("GET", "/api/v1/users/me", headers=bearer(token))[0] == 200, "token works from cache")
print("ok: session cached in redis")
st, body, _, _ = call("DELETE", "/api/v1/auth/sessions/current", headers=bearer(token))
check(st == 204 and body is None, "logout must be 204 without body")
check(call("GET", "/api/v1/users/me", headers=bearer(token))[0] == 401, "token after logout must be 401 (cache invalidated)")
check(call("DELETE", "/api/v1/auth/sessions/current", headers=bearer(token))[0] == 401, "second logout must be 401")
print("ok: logout revokes token even though it was cached")
# Redis — только кэш: после FLUSHALL действующая сессия читается из PostgreSQL,
# а отозванная не "воскресает"
s2 = login(alice)
s3 = login(alice)
call("DELETE", "/api/v1/auth/sessions/current", headers=bearer(s3["token"]))
compose("exec", "-T", "redis", "redis-cli", "FLUSHALL")
check(call("GET", "/api/v1/users/me", headers=bearer(s2["token"]))[0] == 200, "valid session must survive redis flush")
check(call("GET", "/api/v1/users/me", headers=bearer(s3["token"]))[0] == 401, "revoked session must stay revoked after flush")
print("ok: redis FLUSHALL -> valid session still works, revoked stays revoked")
print("PASS: test_auth")
run(main)