feat: add password login and bearer sessions
- Argon2id password hashes, KDF in a thread with its own limit - POST /auth/sessions with Basic, DELETE /auth/sessions/current - Bearer sessions in PostgreSQL, X-User-Id ignored - migration drops lab 1 data without passwords - student-tests on tokens, sessions spec and synced specs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,19 +3,25 @@
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from api_client import API, Actor, Api, unique_username
|
||||
from contract import assert_error, assert_ok
|
||||
from api_client import API, Actor, Api, registration, unique_username
|
||||
from contract import (
|
||||
BASIC_CHALLENGE,
|
||||
assert_error,
|
||||
assert_ok,
|
||||
assert_unauthorized,
|
||||
)
|
||||
|
||||
|
||||
def register_raw(
|
||||
api: Api,
|
||||
username: str,
|
||||
display_name: str = "User",
|
||||
password: str | None = None,
|
||||
) -> httpx.Response:
|
||||
return api.request(
|
||||
"POST",
|
||||
f"{API}/users",
|
||||
json={"username": username, "display_name": display_name},
|
||||
json=registration(username, display_name, password),
|
||||
)
|
||||
|
||||
|
||||
@@ -69,3 +75,42 @@ def test_display_name_blank(api: Api) -> None:
|
||||
def test_me_equals_registration(api: Api, alice: Actor) -> None:
|
||||
resp = api.request("GET", f"{API}/users/me", actor=alice)
|
||||
assert assert_ok(resp, 200, "User") == alice.user
|
||||
|
||||
|
||||
@pytest.mark.spec("users", "Нет пароля")
|
||||
def test_password_missing(api: Api) -> None:
|
||||
resp = api.request(
|
||||
"POST",
|
||||
f"{API}/users",
|
||||
json={"username": unique_username(), "display_name": "User"},
|
||||
)
|
||||
assert_error(resp, 400)
|
||||
|
||||
|
||||
@pytest.mark.spec("users", "Граница длины пароля")
|
||||
def test_password_length(api: Api) -> None:
|
||||
# длина — в code points: «ж» занимает два байта UTF-8
|
||||
for length, status in ((11, 400), (12, 201), (128, 201), (129, 400)):
|
||||
resp = register_raw(api, unique_username(), password="ж" * length)
|
||||
if status == 201:
|
||||
assert_ok(resp, 201, "User")
|
||||
else:
|
||||
assert_error(resp, status)
|
||||
|
||||
|
||||
@pytest.mark.spec("users", "Пароль из пробелов")
|
||||
def test_password_blank(api: Api) -> None:
|
||||
assert_error(register_raw(api, unique_username(), password=" " * 12), 400)
|
||||
# значимые пробелы по краям допустимы и не обрезаются
|
||||
actor = api.register(password=" spaced-pass ")
|
||||
assert api.login(actor.user, " spaced-pass ").user == actor.user
|
||||
|
||||
|
||||
@pytest.mark.spec("users", "Длинный пароль в UTF-8 целиком")
|
||||
def test_long_password_compared_whole(api: Api) -> None:
|
||||
password = "ж" * 128
|
||||
actor = api.register(password=password)
|
||||
changed = password[:-1] + "з"
|
||||
resp = api.login_response(actor.username, changed)
|
||||
assert_unauthorized(resp, BASIC_CHALLENGE)
|
||||
assert api.login(actor.user, password).user == actor.user
|
||||
|
||||
Reference in New Issue
Block a user