78 lines
3.2 KiB
Python
78 lines
3.2 KiB
Python
import unittest
|
|
import uuid
|
|
|
|
from api import call, new_chat, new_user, send
|
|
|
|
|
|
class ForeignChatTest(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.alice, cls.bob, cls.eve = new_user("alice"), new_user("bob"), new_user("eve")
|
|
cls.chat = new_chat(cls.alice, cls.bob)
|
|
cls.path = "/api/v1/chats/" + cls.chat["id"]
|
|
send(cls.alice, cls.chat, "секрет")
|
|
|
|
def test_outsider_gets_404_everywhere(self):
|
|
self.assertEqual(call("GET", self.path, user=self.eve)[0], 404)
|
|
self.assertEqual(call("GET", self.path + "/messages", user=self.eve)[0], 404)
|
|
status, body, _ = send(self.eve, self.chat, "я тут")
|
|
self.assertEqual(status, 404)
|
|
self.assertEqual(body["error"]["code"], "not_found")
|
|
|
|
def test_foreign_chat_looks_like_missing_chat(self):
|
|
_, foreign, _ = call("GET", self.path, user=self.eve)
|
|
_, missing, _ = call("GET", "/api/v1/chats/" + str(uuid.uuid4()), user=self.eve)
|
|
self.assertEqual(foreign["error"]["message"], missing["error"]["message"])
|
|
|
|
def test_outsider_message_is_not_saved(self):
|
|
send(self.eve, self.chat, "не должно появиться")
|
|
_, page, _ = call("GET", self.path + "/messages", user=self.alice)
|
|
self.assertNotIn("не должно появиться", [item["text"] for item in page["items"]])
|
|
|
|
def test_chat_is_not_listed_for_outsider(self):
|
|
_, page, _ = call("GET", "/api/v1/chats?limit=100", user=self.eve)
|
|
self.assertNotIn(self.chat["id"], [item["id"] for item in page["items"]])
|
|
_, page, _ = call("GET", "/api/v1/chats?limit=100", user=self.bob)
|
|
self.assertIn(self.chat["id"], [item["id"] for item in page["items"]])
|
|
|
|
def test_members_can_read_and_write(self):
|
|
self.assertEqual(send(self.bob, self.chat, "ответ")[0], 201)
|
|
self.assertEqual(call("GET", self.path, user=self.bob)[0], 200)
|
|
|
|
def test_bad_chat_id(self):
|
|
self.assertEqual(call("GET", "/api/v1/chats/123", user=self.alice)[0], 400)
|
|
|
|
|
|
class CreateChatTest(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.alice, cls.bob = new_user("alice"), new_user("bob")
|
|
|
|
def create(self, member_ids, title="Чат"):
|
|
return call("POST", "/api/v1/chats", {"title": title, "member_ids": member_ids}, user=self.alice)[0]
|
|
|
|
def test_creator_must_be_member(self):
|
|
self.assertEqual(self.create([self.bob["id"]]), 400)
|
|
|
|
def test_duplicate_members(self):
|
|
self.assertEqual(self.create([self.alice["id"], self.bob["id"], self.bob["id"]]), 400)
|
|
|
|
def test_unknown_member(self):
|
|
self.assertEqual(self.create([self.alice["id"], str(uuid.uuid4())]), 400)
|
|
|
|
def test_empty_members(self):
|
|
self.assertEqual(self.create([]), 400)
|
|
|
|
def test_blank_title(self):
|
|
self.assertEqual(self.create([self.alice["id"]], title=" "), 400)
|
|
|
|
def test_failed_create_leaves_no_chat(self):
|
|
_, before, _ = call("GET", "/api/v1/chats?limit=100", user=self.bob)
|
|
self.create([self.alice["id"], self.bob["id"], str(uuid.uuid4())])
|
|
_, after, _ = call("GET", "/api/v1/chats?limit=100", user=self.bob)
|
|
self.assertEqual(before, after)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|