Files

96 lines
4.1 KiB
Python

from uuid import uuid4
import pytest
from httpx import AsyncClient, Response
from conftest import PASSWORD, actor, basic, keyed, make_chat, new_user, register
NUL_TEXTS = ["\x00", "\x00abc", "abc\x00def", "abc\x00"]
def rejected(response: Response, status: int, code: str) -> None:
assert response.status_code == status, response.text
assert response.headers["content-type"].startswith("application/json")
assert response.headers["X-Instance-Id"]
assert response.headers["X-Request-Id"]
assert set(response.json()) == {"error"}
error = response.json()["error"]
assert set(error) == {"code", "message", "request_id"}
assert error["code"] == code
assert error["message"]
assert error["request_id"] == response.headers["X-Request-Id"]
@pytest.mark.parametrize("username", NUL_TEXTS)
async def test_nul_basic_matches_wrong_password(client: AsyncClient, username: str) -> None:
user = await register(client, "nul_login")
responses = []
for name, password in [(user["username"], "wrong-password"), (username, PASSWORD)]:
response = await client.post(
"/api/v1/auth/sessions",
headers={**basic(name, password), "X-Request-Id": "nul-login"},
)
rejected(response, 401, "unauthorized")
assert response.headers["WWW-Authenticate"] == 'Basic realm="messenger", charset="UTF-8"'
responses.append(response.json())
assert responses[0] == responses[1]
@pytest.mark.parametrize("raw", NUL_TEXTS)
async def test_nul_display_name_does_not_create_user(client: AsyncClient, raw: str) -> None:
body = {**new_user("nul_display"), "display_name": raw}
rejected(await client.post("/api/v1/users", json=body), 400, "invalid_request")
response = await client.post("/api/v1/users", json={**body, "display_name": "Алиса 👋"})
assert response.status_code == 201
assert response.json()["display_name"] == "Алиса 👋"
@pytest.mark.parametrize("raw", NUL_TEXTS)
async def test_nul_title_does_not_create_chat(client: AsyncClient, raw: str) -> None:
user = await register(client, "nul_title")
body = {"title": raw, "member_ids": [user["id"]]}
rejected(
await client.post("/api/v1/chats", json=body, headers=actor(user)), 400, "invalid_request"
)
chats = await client.get("/api/v1/chats", headers=actor(user))
assert chats.status_code == 200
assert chats.json()["items"] == []
@pytest.mark.parametrize("raw", NUL_TEXTS)
async def test_nul_message_does_not_take_key(client: AsyncClient, raw: str) -> None:
user = await register(client, "nul_message")
chat = await make_chat(client, user)
path = f"/api/v1/chats/{chat['id']}/messages"
headers = keyed(user)
rejected(await client.post(path, json={"text": raw}, headers=headers), 400, "invalid_request")
history = await client.get(path, headers=actor(user))
assert history.status_code == 200
assert history.json()["items"] == []
text = "Исправлено\tбез потерь\n👋"
first = await client.post(path, json={"text": text}, headers=headers)
again = await client.post(path, json={"text": text}, headers=headers)
assert first.status_code == again.status_code == 201
assert first.json()["text"] == text
assert first.json() == again.json()
assert (await client.get(path, headers=actor(user))).json()["items"] == [first.json()]
async def test_nul_message_does_not_disclose_chat(client: AsyncClient) -> None:
owner = await register(client, "nul_owner")
outsider = await register(client, "nul_outsider")
chat = await make_chat(client, owner)
responses = []
for chat_id in [chat["id"], str(uuid4())]:
response = await client.post(
f"/api/v1/chats/{chat_id}/messages",
json={"text": "abc\x00def"},
headers={**keyed(outsider), "X-Request-Id": "nul-access"},
)
rejected(response, 404, "not_found")
responses.append(response.json())
assert responses[0] == responses[1]
history = await client.get(f"/api/v1/chats/{chat['id']}/messages", headers=actor(owner))
assert history.status_code == 200
assert history.json()["items"] == []