forked from mtusi2026/mtusi2026lab4
131 lines
4.8 KiB
Python
131 lines
4.8 KiB
Python
import base64
|
|
import http.client
|
|
import json
|
|
import os
|
|
import uuid
|
|
from urllib.parse import urlsplit
|
|
|
|
base = urlsplit(os.environ.get("BASE_URL", "http://127.0.0.1:18084"))
|
|
seen = set()
|
|
checks = []
|
|
password = "Пароль-проверки-01"
|
|
|
|
|
|
def call(method, path, body=None, headers=None, status=200):
|
|
request_id = "validation-wire"
|
|
assert base.hostname
|
|
conn = http.client.HTTPConnection(base.hostname, base.port, timeout=12)
|
|
head = {"X-Request-Id": request_id, "Content-Type": "application/json", **(headers or {})}
|
|
conn.request(method, path, None if body is None else json.dumps(body).encode(), head)
|
|
response = conn.getresponse()
|
|
data = response.read()
|
|
instance = response.getheader("X-Instance-Id")
|
|
assert response.status == status, (method, path, response.status, status)
|
|
assert instance
|
|
assert response.getheader("X-Request-Id") == request_id
|
|
seen.add(instance)
|
|
result = json.loads(data) if data else None
|
|
if status >= 400:
|
|
assert result is not None
|
|
assert result["error"]["request_id"] == request_id
|
|
if status == 401:
|
|
assert response.getheader("WWW-Authenticate") == (
|
|
'Basic realm="messenger", charset="UTF-8"'
|
|
if path == "/api/v1/auth/sessions"
|
|
else 'Bearer realm="messenger"'
|
|
)
|
|
conn.close()
|
|
return result, instance
|
|
|
|
|
|
def basic(name, secret=password):
|
|
return {"Authorization": "Basic " + base64.b64encode(f"{name}:{secret}".encode()).decode()}
|
|
|
|
|
|
def enroll():
|
|
name = "wire_" + uuid.uuid4().hex[:15]
|
|
user, _ = call(
|
|
"POST",
|
|
"/api/v1/users",
|
|
{"username": name, "password": password, "display_name": "Алиса 👋"},
|
|
status=201,
|
|
)
|
|
login, _ = call("POST", "/api/v1/auth/sessions", headers=basic(name), status=201)
|
|
return user, {"Authorization": "Bearer " + login["token"]}
|
|
|
|
|
|
user, auth = enroll()
|
|
wrong, _ = call(
|
|
"POST", "/api/v1/auth/sessions", headers=basic(user["username"], "wrong-password"), status=401
|
|
)
|
|
for raw in [b"Basic \x80", b"Basic \xff", b"Basic \xc3\xa9", b"Basic YWxpY2U6c2VjcmV0\xff"]:
|
|
result, instance = call(
|
|
"POST", "/api/v1/auth/sessions", headers={"Authorization": raw}, status=401
|
|
)
|
|
assert result == wrong
|
|
checks.append(
|
|
{"case": "non_ascii_basic", "raw_hex": raw.hex(), "status": 401, "instance": instance}
|
|
)
|
|
for text in ["\x00", "\x00abc", "abc\x00def", "abc\x00"]:
|
|
result, instance = call("POST", "/api/v1/auth/sessions", headers=basic(text), status=401)
|
|
assert result == wrong
|
|
body = {"username": "wire_" + uuid.uuid4().hex[:15], "password": password, "display_name": text}
|
|
call("POST", "/api/v1/users", body, status=400)
|
|
call("POST", "/api/v1/users", {**body, "display_name": "Алиса 👋"}, status=201)
|
|
call("POST", "/api/v1/chats", {"title": text, "member_ids": [user["id"]]}, auth, status=400)
|
|
chats, _ = call("GET", "/api/v1/chats", headers=auth)
|
|
assert not chats["items"]
|
|
checks.append(
|
|
{
|
|
"case": "nul_basic_display_title",
|
|
"position": repr(text),
|
|
"status": [401, 400, 201, 400],
|
|
"instance": instance,
|
|
}
|
|
)
|
|
chat, _ = call(
|
|
"POST", "/api/v1/chats", {"title": "Проверка 👋", "member_ids": [user["id"]]}, auth, status=201
|
|
)
|
|
path = f"/api/v1/chats/{chat['id']}/messages"
|
|
messages = []
|
|
for text in ["\x00", "\x00abc", "abc\x00def", "abc\x00"]:
|
|
headers = {**auth, "Idempotency-Key": uuid.uuid4().hex}
|
|
call("POST", path, {"text": text}, headers, status=400)
|
|
page, _ = call("GET", path, headers=auth)
|
|
assert page["items"] == messages
|
|
first, one = call("POST", path, {"text": "Исправлено\tбез потерь\n👋"}, headers, status=201)
|
|
again, two = call("POST", path, {"text": first["text"]}, headers, status=201)
|
|
assert first == again and one != two
|
|
messages.append(first)
|
|
page, _ = call("GET", path, headers=auth)
|
|
assert page["items"] == messages
|
|
checks.append(
|
|
{
|
|
"case": "nul_message_corrected_retry",
|
|
"position": repr(text),
|
|
"instances": [one, two],
|
|
"status": [400, 201, 201],
|
|
"copies": 1,
|
|
}
|
|
)
|
|
_, outsider = enroll()
|
|
for target in [chat["id"], str(uuid.uuid4())]:
|
|
call(
|
|
"POST",
|
|
f"/api/v1/chats/{target}/messages",
|
|
{"text": "abc\x00def"},
|
|
{**outsider, "Idempotency-Key": uuid.uuid4().hex},
|
|
status=404,
|
|
)
|
|
checks.append({"case": "nul_access", "status": [404, 404]})
|
|
call("DELETE", "/api/v1/auth/sessions/current", headers=auth, status=204)
|
|
for _ in range(4):
|
|
call("GET", "/api/v1/users/me", headers=auth, status=401)
|
|
checks.append({"case": "logout_across_replicas", "status": [204, 401, 401, 401, 401]})
|
|
assert len(seen) >= 2
|
|
print(
|
|
json.dumps(
|
|
{"checks": checks, "instances": sorted(seen), "pass": True}, ensure_ascii=False, indent=2
|
|
)
|
|
)
|