feat: add users chats and member access

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-01 21:22:54 +03:00
parent 666a37c136
commit ac739713b3
4 changed files with 167 additions and 9 deletions
+36 -1
View File
@@ -42,8 +42,10 @@ class APIContractTests(unittest.TestCase):
cls.thread.join(timeout=5)
@classmethod
def call(cls, method, path, body=None, content_type="application/json"):
def call(cls, method, path, body=None, content_type="application/json", actor=None):
headers = {"Content-Type": content_type} if body is not None else {}
if actor is not None:
headers["X-User-Id"] = actor
request = Request(cls.url + path, data=body, headers=headers, method=method)
try:
response = urlopen(request, timeout=3)
@@ -84,6 +86,39 @@ class APIContractTests(unittest.TestCase):
self.assertEqual(payload["error"]["code"], "internal_error")
self.assertNotIn("private exception detail", json.dumps(payload))
def test_users_chats_and_cursor(self):
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 ("u2_alice", "u2_bob", "u2_eve"))
self.assert_error(*self.call("POST", "/api/v1/users", b'{"username":"u2_alice","display_name":"Other"}'), 409)
self.assert_error(*self.call("GET", "/api/v1/users/me"), 401)
self.assert_error(*self.call("GET", "/api/v1/users/me", actor="00000000-0000-4000-8000-000000000000"), 401)
self.assertEqual(self.call("GET", "/api/v1/users/me", actor=alice["id"])[2], alice)
def chat(member_ids):
body = json.dumps({"title": "Чат 👋", "member_ids": member_ids}).encode()
return self.call("POST", "/api/v1/chats", body, actor=alice["id"])
for members in ([alice["id"], alice["id"]], [bob["id"]], [alice["id"], "00000000-0000-4000-8000-000000000000"]):
self.assert_error(*chat(members), 400)
self.assertEqual(self.call("GET", "/api/v1/chats", actor=alice["id"])[2]["items"], [])
first = chat([alice["id"], bob["id"]])[2]
second = chat([alice["id"], bob["id"]])[2]
self.assertEqual(self.call("GET", f'/api/v1/chats/{first["id"]}', actor=bob["id"])[2], first)
self.assert_error(*self.call("GET", f'/api/v1/chats/{first["id"]}', actor=eve["id"]), 404)
self.assertEqual(self.call("GET", "/api/v1/chats", actor=eve["id"])[2]["items"], [])
page = self.call("GET", "/api/v1/chats?limit=1", actor=alice["id"])[2]
self.assertEqual(page["items"], [first])
self.assertTrue(page["next_cursor"])
from urllib.parse import quote
next_path = "/api/v1/chats?limit=1&cursor=" + quote(page["next_cursor"], safe="")
self.assertEqual(self.call("GET", next_path, actor=alice["id"])[2], {"items": [second], "next_cursor": None})
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)
if __name__ == "__main__":
unittest.main()