forked from mtusi2026/mtusi2026lab2
Complete Lab 2 for Grade 5 (Track 2)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import base64
|
||||
import time
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
BASE_URL = "http://localhost:8080"
|
||||
|
||||
def get_client():
|
||||
return httpx.Client(base_url=BASE_URL, trust_env=False, timeout=10.0)
|
||||
|
||||
def get_basic_header(u: str, p: str):
|
||||
token = base64.b64encode(f"{u}:{p}".encode("utf-8")).decode("utf-8")
|
||||
return {"Authorization": f"Basic {token}"}
|
||||
|
||||
def test_invalid_password_returns_401():
|
||||
uname = f"usr_bad_pw_{int(time.time() * 1000)}"
|
||||
pw = "super_secure_password_123"
|
||||
with get_client() as client:
|
||||
# Регистрация
|
||||
r1 = client.post("/users", json={"username": uname, "display_name": "Test", "password": pw})
|
||||
assert r1.status_code == 201
|
||||
|
||||
# Попытка входа с неверным паролем
|
||||
r2 = client.post("/auth/sessions", headers=get_basic_header(uname, "wrong_password_999"))
|
||||
assert r2.status_code == 401
|
||||
assert "Basic" in r2.headers.get("WWW-Authenticate", "")
|
||||
|
||||
def test_logout_and_revocation():
|
||||
uname = f"usr_logout_{int(time.time() * 1000)}"
|
||||
pw = "super_secure_password_123"
|
||||
with get_client() as client:
|
||||
client.post("/users", json={"username": uname, "display_name": "Test", "password": pw})
|
||||
|
||||
# Успешный вход
|
||||
login = client.post("/auth/sessions", headers=get_basic_header(uname, pw))
|
||||
assert login.status_code == 201
|
||||
token = login.json()["token"]
|
||||
|
||||
# Запрос с действующим токеном
|
||||
r_me = client.get("/users/me", headers={"Authorization": f"Bearer {token}"})
|
||||
assert r_me.status_code == 200
|
||||
|
||||
# Отзываем токен
|
||||
r_del = client.delete("/auth/sessions/current", headers={"Authorization": f"Bearer {token}"})
|
||||
assert r_del.status_code == 204
|
||||
|
||||
# Повторный запрос с отозванным токеном возвращает 401
|
||||
r_revoked = client.get("/users/me", headers={"Authorization": f"Bearer {token}"})
|
||||
assert r_revoked.status_code == 401
|
||||
assert "Bearer" in r_revoked.headers.get("WWW-Authenticate", "")
|
||||
|
||||
def test_basic_rejected_on_business_endpoint():
|
||||
with get_client() as client:
|
||||
r = client.get("/chats", headers=get_basic_header("test", "test"))
|
||||
assert r.status_code == 401
|
||||
assert "Bearer" in r.headers.get("WWW-Authenticate", "")
|
||||
|
||||
def test_chat_creation_rollback_on_failure():
|
||||
uname = f"usr_rollback_{int(time.time() * 1000)}"
|
||||
pw = "super_secure_password_123"
|
||||
with get_client() as client:
|
||||
u = client.post("/users", json={"username": uname, "display_name": "R", "password": pw}).json()
|
||||
token = client.post("/auth/sessions", headers=get_basic_header(uname, pw)).json()["token"]
|
||||
|
||||
# Пытаемся создать чат с несуществующим участником
|
||||
fake_uuid = "00000000-0000-0000-0000-000000000999"
|
||||
res = client.post(
|
||||
"/chats",
|
||||
json={"title": "Rollback Chat", "member_ids": [u["id"], fake_uuid]},
|
||||
headers={"Authorization": f"Bearer {token}"}
|
||||
)
|
||||
assert res.status_code == 400
|
||||
|
||||
# Проверяем, что чат не был создан в БД (транзакция откатилась)
|
||||
chats = client.get("/chats", headers={"Authorization": f"Bearer {token}"}).json()["items"]
|
||||
assert not any(c["title"] == "Rollback Chat" for c in chats)
|
||||
Reference in New Issue
Block a user