66 lines
3.0 KiB
Python
66 lines
3.0 KiB
Python
"""Доступ к чужому чату: 404 для посторонних, 401 без токена."""
|
|
|
|
import uuid
|
|
|
|
from conftest import assert_error
|
|
|
|
|
|
def test_outsider_cannot_read_chat(client, eve, chat):
|
|
assert_error(client.get("/api/v1/chats/" + chat["id"], headers=eve.headers), 404, "not_found")
|
|
|
|
|
|
def test_outsider_cannot_read_history(client, alice, eve, chat):
|
|
client.post("/api/v1/chats/" + chat["id"] + "/messages", json={"text": "секрет"}, headers=alice.headers)
|
|
reply = client.get("/api/v1/chats/" + chat["id"] + "/messages", headers=eve.headers)
|
|
assert_error(reply, 404, "not_found")
|
|
assert "секрет" not in reply.text
|
|
|
|
|
|
def test_outsider_cannot_write(client, alice, eve, chat):
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
assert_error(client.post(url, json={"text": "чужое"}, headers=eve.headers), 404, "not_found")
|
|
assert client.get(url, headers=alice.headers).json()["items"] == []
|
|
|
|
|
|
def test_outsider_gets_404_even_with_bad_body(client, eve, chat):
|
|
"""Проверка доступа выполняется раньше проверки тела."""
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
assert_error(client.post(url, json={"text": ""}, headers=eve.headers), 404, "not_found")
|
|
|
|
|
|
def test_unknown_chat_looks_the_same_as_foreign(client, eve, chat):
|
|
foreign = client.get("/api/v1/chats/" + chat["id"], headers=eve.headers)
|
|
missing = client.get("/api/v1/chats/" + str(uuid.uuid4()), headers=eve.headers)
|
|
assert foreign.status_code == missing.status_code == 404
|
|
assert foreign.json()["error"]["message"] == missing.json()["error"]["message"]
|
|
|
|
|
|
def test_chat_list_shows_only_own_chats(client, bob, eve, chat):
|
|
mine = client.get("/api/v1/chats", headers=bob.headers).json()["items"]
|
|
others = client.get("/api/v1/chats", headers=eve.headers).json()["items"]
|
|
assert [c["id"] for c in mine] == [chat["id"]]
|
|
assert others == []
|
|
|
|
|
|
def test_bad_chat_id_is_400(client, alice):
|
|
assert_error(client.get("/api/v1/chats/not-a-uuid", headers=alice.headers), 400, "invalid_request")
|
|
|
|
|
|
def test_no_token_is_401_everywhere(client, chat):
|
|
chat_url = "/api/v1/chats/" + chat["id"]
|
|
for url in ["/api/v1/users/me", "/api/v1/chats", chat_url, chat_url + "/messages"]:
|
|
assert_error(client.get(url), 401, "unauthorized")
|
|
assert_error(client.post("/api/v1/chats", json={"title": "x", "member_ids": []}), 401, "unauthorized")
|
|
assert_error(client.post(chat_url + "/messages", json={"text": "x"}), 401, "unauthorized")
|
|
|
|
|
|
def test_401_comes_before_404(client, chat):
|
|
"""Без токена существование чата не раскрывается."""
|
|
assert_error(client.get("/api/v1/chats/" + chat["id"] + "/messages"), 401, "unauthorized")
|
|
|
|
|
|
def test_member_can_read_and_write(client, alice, bob, chat):
|
|
url = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
assert client.post(url, json={"text": "от Боба"}, headers=bob.headers).status_code == 201
|
|
assert client.get(url, headers=alice.headers).json()["items"][0]["text"] == "от Боба"
|