52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
"""Чужой чат: посторонний получает 404, существование чата не раскрывается"""
|
|
|
|
import uuid
|
|
|
|
import httpx
|
|
from helpers import assert_error, auth, message_headers
|
|
|
|
|
|
def test_outsider_cannot_access_chat(
|
|
api: httpx.Client, make_user, make_chat, send_message
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
bob = make_user("bob")
|
|
eve = make_user("eve")
|
|
chat = make_chat(alice, [alice, bob])
|
|
send_message(alice, chat, "секрет")
|
|
path = f"/api/v1/chats/{chat['id']}"
|
|
|
|
assert_error(api.get(path, headers=auth(eve)), 404, "not_found")
|
|
assert_error(api.get(f"{path}/messages", headers=auth(eve)), 404, "not_found")
|
|
response = api.post(
|
|
f"{path}/messages", json={"text": "чужое"}, headers=message_headers(eve)
|
|
)
|
|
assert_error(response, 404, "not_found")
|
|
|
|
history = api.get(f"{path}/messages", headers=auth(bob)).json()
|
|
assert [item["text"] for item in history["items"]] == ["секрет"]
|
|
|
|
|
|
def test_foreign_and_missing_chat_are_indistinguishable(
|
|
api: httpx.Client, make_user, make_chat
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
eve = make_user("eve")
|
|
chat = make_chat(alice, [alice])
|
|
foreign = api.get(f"/api/v1/chats/{chat['id']}", headers=auth(eve)).json()
|
|
missing = api.get(f"/api/v1/chats/{uuid.uuid4()}", headers=auth(eve)).json()
|
|
assert foreign["error"]["code"] == missing["error"]["code"] == "not_found"
|
|
assert foreign["error"]["message"] == missing["error"]["message"]
|
|
|
|
|
|
def test_chat_list_contains_only_own_chats(
|
|
api: httpx.Client, make_user, make_chat
|
|
) -> None:
|
|
alice = make_user("alice")
|
|
eve = make_user("eve")
|
|
chat = make_chat(alice, [alice])
|
|
own = api.get("/api/v1/chats", headers=auth(eve)).json()
|
|
assert own == {"items": [], "next_cursor": None}
|
|
listed = api.get("/api/v1/chats", headers=auth(alice)).json()
|
|
assert [item["id"] for item in listed["items"]] == [chat["id"]]
|