forked from mtusi2026/mtusi2026lab2
183 lines
8.1 KiB
Python
183 lines
8.1 KiB
Python
"""Общий код для собственных тестов: маленький HTTP-клиент и работа со стендом.
|
||
|
||
Только стандартная библиотека Python, ничего ставить не нужно.
|
||
Тесты ходят на BASE_URL (по умолчанию http://127.0.0.1:8080) и управляют
|
||
контейнерами через `docker compose`, запуская его прямо из Python.
|
||
Поэтому всё работает одинаково в PowerShell, cmd и Git Bash на Windows.
|
||
Вывод тестов — только латиница, чтобы консоль Windows не ломала кодировку.
|
||
"""
|
||
import base64
|
||
import hashlib
|
||
import json
|
||
import os
|
||
import subprocess
|
||
import time
|
||
import urllib.error
|
||
import urllib.parse
|
||
import urllib.request
|
||
import uuid
|
||
from pathlib import Path
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8080").rstrip("/")
|
||
PASSWORD = "Test-password-123"
|
||
|
||
|
||
class Response:
|
||
def __init__(self, status, headers, data):
|
||
self.status = status
|
||
self.headers = {name.lower(): value for name, value in headers.items()}
|
||
self.data = data
|
||
try:
|
||
self.json = json.loads(data.decode("utf-8")) if data else None
|
||
except ValueError:
|
||
self.json = None
|
||
|
||
|
||
def check(condition, message):
|
||
if not condition:
|
||
raise AssertionError(message)
|
||
|
||
|
||
def call(method, path, expected=200, *, body=None, raw=None, token=None, headers=None):
|
||
"""HTTP-запрос к API. expected — код или кортеж кодов, иначе AssertionError.
|
||
|
||
Заодно проверяет то, что контракт требует от любого ответа:
|
||
X-Request-Id, единый формат ошибки, WWW-Authenticate у 401.
|
||
"""
|
||
all_headers = dict(headers or {})
|
||
data = raw
|
||
if body is not None:
|
||
data = json.dumps(body).encode("utf-8")
|
||
if data is not None:
|
||
all_headers.setdefault("Content-Type", "application/json")
|
||
if token is not None:
|
||
all_headers["Authorization"] = "Bearer " + token
|
||
|
||
request = urllib.request.Request(BASE_URL + path, data=data, method=method, headers=all_headers)
|
||
try:
|
||
with urllib.request.urlopen(request, timeout=30) as reply:
|
||
response = Response(reply.status, reply.headers, reply.read())
|
||
except urllib.error.HTTPError as error:
|
||
response = Response(error.code, error.headers, error.read())
|
||
|
||
expected = expected if isinstance(expected, tuple) else (expected,)
|
||
check(response.status in expected,
|
||
f"{method} {path}: expected {expected}, got {response.status} {response.data[:200]!r}")
|
||
request_id = response.headers.get("x-request-id")
|
||
check(request_id, f"{method} {path}: no X-Request-Id header")
|
||
if response.status >= 400:
|
||
error = (response.json or {}).get("error") or {}
|
||
check(error.get("code") and error.get("message"), f"{method} {path}: wrong error format")
|
||
check(error.get("request_id") == request_id, f"{method} {path}: request_id differs from header")
|
||
if response.status == 401:
|
||
check(response.headers.get("www-authenticate"), f"{method} {path}: 401 without WWW-Authenticate")
|
||
return response
|
||
|
||
|
||
def basic(username, password):
|
||
"""Значение заголовка Authorization: Basic base64(username:password), пароль в UTF-8."""
|
||
return "Basic " + base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("ascii")
|
||
|
||
|
||
def login(username, password, expected=201):
|
||
return call("POST", "/api/v1/auth/sessions", expected, headers={"Authorization": basic(username, password)})
|
||
|
||
|
||
def new_user(prefix="user", password=PASSWORD):
|
||
"""Регистрирует пользователя и входит. Возвращает dict: id, username, password, token."""
|
||
username = f"{prefix}_{uuid.uuid4().hex[:10]}"
|
||
user = call("POST", "/api/v1/users", 201,
|
||
body={"username": username, "display_name": prefix.title(), "password": password}).json
|
||
session = login(username, password).json
|
||
return {**user, "password": password, "token": session["token"]}
|
||
|
||
|
||
def new_chat(owner, *others, title="Test chat"):
|
||
member_ids = [owner["id"]] + [user["id"] for user in others]
|
||
return call("POST", "/api/v1/chats", 201, token=owner["token"],
|
||
body={"title": title, "member_ids": member_ids}).json
|
||
|
||
|
||
def send(user, chat_id, text, expected=201):
|
||
return call("POST", f"/api/v1/chats/{chat_id}/messages", expected, token=user["token"], body={"text": text})
|
||
|
||
|
||
def read_history(user, chat_id, limit=100):
|
||
"""Вся история чата по страницам. Возвращает (сообщения, число_страниц)."""
|
||
items, pages, cursor = [], 0, None
|
||
while True:
|
||
query = f"?limit={limit}" + (f"&cursor={urllib.parse.quote(cursor, safe='')}" if cursor else "")
|
||
page = call("GET", f"/api/v1/chats/{chat_id}/messages{query}", 200, token=user["token"]).json
|
||
items += page["items"]
|
||
pages += 1
|
||
cursor = page["next_cursor"]
|
||
if cursor is None:
|
||
return items, pages
|
||
|
||
|
||
def wait_ready(seconds=90):
|
||
deadline = time.monotonic() + seconds
|
||
while True:
|
||
try:
|
||
with urllib.request.urlopen(BASE_URL + "/health/ready", timeout=3) as reply:
|
||
if reply.status == 200:
|
||
return
|
||
except (urllib.error.URLError, OSError):
|
||
pass
|
||
check(time.monotonic() < deadline, f"API is not ready after {seconds} s")
|
||
time.sleep(0.5)
|
||
|
||
|
||
def compose(*args, env=None):
|
||
"""docker compose <args> в корне репозитория. Вывод скрыт: в нём могут быть пути к секретам."""
|
||
subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True, env=env,
|
||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||
|
||
|
||
def compose_output(*args):
|
||
"""docker compose <args> и его вывод (для logs, ps)."""
|
||
result = subprocess.run(["docker", "compose", *args], cwd=ROOT, capture_output=True, check=True)
|
||
return result.stdout.decode("utf-8", "replace") + result.stderr.decode("utf-8", "replace")
|
||
|
||
|
||
def redis_cli(*args):
|
||
"""redis-cli внутри контейнера redis. Пароль читается там же из секрета и не попадает в вывод."""
|
||
command = "REDISCLI_AUTH=\"$(sed -n 's/^requirepass //p' /run/secrets/redis_auth)\" redis-cli " + " ".join(args)
|
||
result = subprocess.run(["docker", "compose", "exec", "-T", "redis", "sh", "-c", command],
|
||
cwd=ROOT, capture_output=True, check=True)
|
||
return result.stdout.decode("utf-8", "replace").strip()
|
||
|
||
|
||
def session_key(token):
|
||
"""Ключ сессии в Redis, как его считает auth.py: session:<SHA-256 токена>."""
|
||
return "session:" + hashlib.sha256(token.encode("utf-8")).hexdigest()
|
||
|
||
|
||
def psql(sql):
|
||
"""Выполняет SQL в контейнере postgres. Возвращает (код выхода, вывод, текст ошибки).
|
||
|
||
Значения в SQL тестов — только сгенерированные самим тестом (uuid), не ввод пользователя.
|
||
"""
|
||
result = subprocess.run(
|
||
["docker", "compose", "exec", "-T", "postgres",
|
||
"psql", "-U", "messenger", "-d", "messenger", "-X", "-A", "-t", "-v", "ON_ERROR_STOP=1", "-c", sql],
|
||
cwd=ROOT, capture_output=True, check=False,
|
||
)
|
||
output = result.stdout.decode("utf-8", "replace").strip()
|
||
error = result.stderr.decode("utf-8", "replace").strip()
|
||
return result.returncode, output, error
|
||
|
||
|
||
def run_tests(tests):
|
||
"""Запускает функции-тесты по очереди. Код выхода 1, если хоть один упал."""
|
||
failed = 0
|
||
for test in tests:
|
||
try:
|
||
test()
|
||
except Exception as exc: # noqa: BLE001 — нужно напечатать любую ошибку теста
|
||
failed += 1
|
||
print(f"FAIL: {test.__name__}: {exc}")
|
||
print(f"{len(tests) - failed} passed, {failed} failed")
|
||
raise SystemExit(1 if failed else 0)
|