feat: add message history and concurrent writes
This commit is contained in:
@@ -119,6 +119,58 @@ class APIContractTests(unittest.TestCase):
|
||||
self.assert_error(*self.call("GET", next_path, actor=bob["id"]), 400)
|
||||
self.assert_error(*self.call("GET", "/api/v1/chats?limit=0", actor=alice["id"]), 400)
|
||||
|
||||
def test_messages_access_validation_and_pages(self):
|
||||
from urllib.parse import quote
|
||||
|
||||
def user(name):
|
||||
status, _, value = self.call("POST", "/api/v1/users", json.dumps({"username": name, "display_name": name}).encode())
|
||||
self.assertEqual(status, 201)
|
||||
return value
|
||||
|
||||
alice, bob, eve = (user(name) for name in ("u3_alice", "u3_bob", "u3_eve"))
|
||||
|
||||
def chat():
|
||||
body = json.dumps({"title": "U3 👋", "member_ids": [alice["id"], bob["id"]]}).encode()
|
||||
status, _, value = self.call("POST", "/api/v1/chats", body, actor=alice["id"])
|
||||
self.assertEqual(status, 201)
|
||||
return value
|
||||
|
||||
first_chat, second_chat = chat(), chat()
|
||||
path = f'/api/v1/chats/{first_chat["id"]}/messages'
|
||||
other_path = f'/api/v1/chats/{second_chat["id"]}/messages'
|
||||
|
||||
def send(actor, body, target=path):
|
||||
return self.call("POST", target, json.dumps(body, ensure_ascii=False).encode(), actor=actor)
|
||||
|
||||
for actor, text in ((alice, "Привет 👋"), (bob, "Ответ Боба"), (alice, "Третье сообщение")):
|
||||
status, _, value = send(actor["id"], {"text": text})
|
||||
self.assertEqual(status, 201)
|
||||
self.assertEqual((value["chat_id"], value["sender_id"], value["text"]), (first_chat["id"], actor["id"], text))
|
||||
|
||||
seen = []
|
||||
cursor = None
|
||||
while True:
|
||||
suffix = "?limit=1" + ("&cursor=" + quote(cursor, safe="") if cursor else "")
|
||||
status, _, page = self.call("GET", path + suffix, actor=bob["id"])
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual(len(page["items"]), 1)
|
||||
seen.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None:
|
||||
break
|
||||
self.assertEqual([item["text"] for item in seen], ["Привет 👋", "Ответ Боба", "Третье сообщение"])
|
||||
self.assertEqual(len({item["id"] for item in seen}), 3)
|
||||
self.assertEqual(seen, sorted(seen, key=lambda item: (item["created_at"], item["id"])))
|
||||
|
||||
for body in ({"text": ""}, {"text": " "}, {"text": "a" * 4001}, {"text": "x", "sender_id": eve["id"]}):
|
||||
self.assert_error(*send(alice["id"], body), 400)
|
||||
self.assertEqual(send(alice["id"], {"text": "🌟" * 4000})[0], 201)
|
||||
self.assert_error(*send(eve["id"], {"text": "чужое"}), 404)
|
||||
self.assert_error(*self.call("GET", path, actor=eve["id"]), 404)
|
||||
self.assert_error(*self.call("GET", path + "?limit=0", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", path + "?cursor=broken", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", other_path + "?cursor=" + quote(self.call("GET", path + "?limit=1", actor=alice["id"])[2]["next_cursor"], safe=""), actor=alice["id"]), 400)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user