- 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>
123 lines
4.0 KiB
Python
123 lines
4.0 KiB
Python
"""Обязательные сценарии уровня «4» (README.md, рубрика)."""
|
|
|
|
import uuid
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from api_client import API, Actor, Api, order_key, registration
|
|
from contract import (
|
|
BASIC_CHALLENGE,
|
|
BEARER_CHALLENGE,
|
|
assert_error,
|
|
assert_unauthorized,
|
|
)
|
|
|
|
|
|
@pytest.mark.spec("api-conventions", "Битый JSON")
|
|
def test_malformed_json(api: Api, alice: Actor, chat: dict[str, Any]) -> None:
|
|
headers = {"Content-Type": "application/json"}
|
|
for path, actor in (
|
|
(f"{API}/users", None),
|
|
(f"{API}/chats", alice),
|
|
(f"{API}/chats/{chat['id']}/messages", alice),
|
|
):
|
|
resp = api.request(
|
|
"POST",
|
|
path,
|
|
actor=actor,
|
|
headers=headers,
|
|
content=b"{bad",
|
|
)
|
|
assert_error(resp, 400)
|
|
|
|
|
|
@pytest.mark.spec("chats", "Посторонний")
|
|
@pytest.mark.spec("messages", "Посторонний пишет")
|
|
@pytest.mark.spec("messages", "Посторонний читает")
|
|
def test_foreign_chat(
|
|
api: Api,
|
|
alice: Actor,
|
|
eve: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
path = f"{API}/chats/{chat['id']}"
|
|
missing = f"{API}/chats/{uuid.uuid4()}"
|
|
requests = (
|
|
("GET", "", None),
|
|
("GET", "/messages", None),
|
|
("POST", "/messages", {"text": "чужое"}),
|
|
)
|
|
for method, suffix, body in requests:
|
|
foreign = assert_error(
|
|
api.request(method, path + suffix, actor=eve, json=body),
|
|
404,
|
|
)
|
|
absent = assert_error(
|
|
api.request(method, missing + suffix, actor=eve, json=body),
|
|
404,
|
|
)
|
|
# чужой чат неотличим от несуществующего
|
|
assert (foreign["code"], foreign["message"]) == (
|
|
absent["code"],
|
|
absent["message"],
|
|
)
|
|
assert api.all_messages(alice, chat["id"]) == []
|
|
|
|
|
|
@pytest.mark.spec("messages", "Пустой текст")
|
|
@pytest.mark.spec("api-conventions", "Строка из пробелов")
|
|
def test_empty_message(api: Api, alice: Actor, chat: dict[str, Any]) -> None:
|
|
path = f"{API}/chats/{chat['id']}/messages"
|
|
for text in ("", " ", "\t\n "):
|
|
resp = api.request("POST", path, actor=alice, json={"text": text})
|
|
assert_error(resp, 400)
|
|
assert api.all_messages(alice, chat["id"]) == []
|
|
|
|
|
|
@pytest.mark.spec("users", "Повтор username")
|
|
def test_duplicate_username(api: Api, alice: Actor) -> None:
|
|
resp = api.request(
|
|
"POST",
|
|
f"{API}/users",
|
|
json=registration(alice.username, "Другая Алиса"),
|
|
)
|
|
assert_error(resp, 409)
|
|
|
|
|
|
@pytest.mark.spec("sessions", "Неверный пароль")
|
|
def test_wrong_password(api: Api, alice: Actor) -> None:
|
|
resp = api.login_response(alice.username, alice.password + "-wrong")
|
|
assert_unauthorized(resp, BASIC_CHALLENGE)
|
|
assert "token" not in resp.json()
|
|
|
|
|
|
@pytest.mark.spec("sessions", "Выход")
|
|
def test_logout(api: Api, alice: Actor) -> None:
|
|
resp = api.logout(alice)
|
|
assert resp.status_code == 204, resp.text
|
|
assert resp.content == b""
|
|
after = api.request("GET", f"{API}/users/me", actor=alice)
|
|
assert_unauthorized(after, BEARER_CHALLENGE)
|
|
|
|
|
|
def test_history_walk_across_pages(
|
|
api: Api,
|
|
alice: Actor,
|
|
bob: Actor,
|
|
chat: dict[str, Any],
|
|
) -> None:
|
|
"""12 сообщений, страницы по 5, 3, 4: `limit` меняется между ними."""
|
|
sent = [
|
|
api.send_message(sender, chat["id"], f"walk-{number}")
|
|
for number, sender in enumerate([alice, bob] * 6)
|
|
]
|
|
pages = api.message_pages(bob, chat["id"], limits=(5, 3, 4))
|
|
|
|
assert [len(page["items"]) for page in pages] == [5, 3, 4]
|
|
assert all(page["next_cursor"] for page in pages[:-1])
|
|
assert pages[-1]["next_cursor"] is None
|
|
items = [item for page in pages for item in page["items"]]
|
|
assert len({item["id"] for item in items}) == len(sent)
|
|
assert items == sorted(sent, key=order_key)
|