Files
mtusi2026lab2/student-tests/auth_test.py

63 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
import base64
import time
import uuid
from stack import BASE_URL, SHORT_TTL_URL, Client, check, main_wrapper, ok
def basic(username, password):
return {"Authorization": "Basic " + base64.b64encode((username + ":" + password).encode("utf-8")).decode()}
def main():
c = Client(BASE_URL)
alice = c.user("auth")
reg = alice["registration"]
r1 = c.call("POST", "/api/v1/auth/sessions", 401, headers=basic(reg["username"], "wrong-password-123"))
r2 = c.call("POST", "/api/v1/auth/sessions", 401, headers=basic("nobody_" + uuid.uuid4().hex[:8], "wrong-password-123"))
check(r1["json"]["error"]["message"] == r2["json"]["error"]["message"], "unknown user must look like a wrong password")
ok("wrong password / unknown user -> identical 401 Basic")
c.call("POST", "/api/v1/auth/sessions", 401)
c.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Basic !!!not-base64"})
c.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Bearer " + alice["token"]})
ok("missing / malformed / non-Basic login -> 401")
tricky = "пароль:с:двоеточием-" + uuid.uuid4().hex[:6]
user = c.call("POST", "/api/v1/users", 201, body={"username": "u_colon_" + uuid.uuid4().hex[:8], "display_name": "Двоеточие", "password": tricky}, schema="User")["json"]
c.call("POST", "/api/v1/auth/sessions", 201, headers=basic(user["username"], tricky), schema="Session")
ok("UTF-8 password containing ':' -> 201")
c.call("POST", "/api/v1/users", 400, body={"username": "u_short_" + uuid.uuid4().hex[:8], "display_name": "x", "password": "short-11ch"})
c.call("POST", "/api/v1/users", 400, body={"username": "u_nopass_" + uuid.uuid4().hex[:8], "display_name": "x"})
ok("password shorter than 12 / missing -> 400")
me = c.call("GET", "/api/v1/users/me", 401, headers=basic(reg["username"], reg["password"]))
check(me["headers"].get("WWW-Authenticate", "").startswith("Bearer"), "Bearer challenge expected")
c.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]})
c.call("GET", "/api/v1/users/me", 401, headers={"Authorization": "Bearer " + uuid.uuid4().hex * 2})
ok("Basic / X-User-Id / unknown token on business endpoint -> 401")
session = c.call("POST", "/api/v1/auth/sessions", 201, headers=basic(reg["username"], reg["password"]), schema="Session")["json"]
check(session["token"] != alice["token"], "every login must issue a new token")
second = dict(alice, token=session["token"])
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice)
c.call("GET", "/api/v1/users/me", 401, actor=alice)
c.call("DELETE", "/api/v1/auth/sessions/current", 401, actor=alice)
c.call("GET", "/api/v1/users/me", 200, actor=second, schema="User")
ok("logout revokes only the current token; repeat logout -> 401")
s = Client(SHORT_TTL_URL)
bob = s.user("ttl")
s.call("GET", "/api/v1/users/me", 200, actor=bob, schema="User")
time.sleep(3)
s.call("GET", "/api/v1/users/me", 401, actor=bob)
s.call("DELETE", "/api/v1/auth/sessions/current", 401, actor=bob)
ok("session with TTL=2s is rejected after 3s (cache entry included)")
if __name__ == "__main__":
main_wrapper(main)