async def test_get_chat_messages_as_supervisor(supervisor1_client, visitors, users): visitor_id = visitors[-1]["id"] # Create some dummy chat messages chat = await Chat.add(visitor_id=visitor_id) messages = [] for sequence_num in range(1, 25): content = {"value": fake.sentence(nb_words=10)} sender = users[-6]["id"] if randint(0, 1) else None chat_msg = { "chat_id": chat["id"], "sequence_num": sequence_num, "content": content, "sender": sender, } await ChatMessage.add(**chat_msg) chat_msg["sender"] = users[-6] if sender else None messages.append(chat_msg) # Try getting the chat messages res = await supervisor1_client.get( "/visitors/{}/messages".format(visitor_id)) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert len(body["data"]) == 15 # The limit for expected, actual in zip(messages[9:], body["data"]): assert profile_created_from_origin(expected, actual, ignore={"sender"}) assert profile_created_from_origin(expected["sender"], actual["sender"]) # Ensure that getting the next messages work too prev_page_link = get_prev_page_link(body) res = await supervisor1_client.get(prev_page_link) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert len(body["data"]) == 9 for expected, actual in zip(messages[:9], body["data"]): assert profile_created_from_origin(expected, actual, ignore={"sender"}) assert profile_created_from_origin(expected["sender"], actual["sender"]) # Ensure that getting the next messages are empty prev_page_link = get_prev_page_link(body) res = await supervisor1_client.get(prev_page_link) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert not body["data"] assert isinstance(body["links"], dict) assert not body["links"]
async def test_update_last_seen_message_of_staff(supervisor1_client): # Create a lot more visitors to test visitors = [get_fake_visitor() for _ in range(3)] for visitor in visitors: _visitor = deepcopy(visitor) # Anonymous users don't have passwords if "password" in _visitor: _visitor["password"] = hash_password(_visitor["password"]) await Visitor(**_visitor).create() # Create some dummy chat messages created_at = 1 chat_messages = {} for visitor in visitors[::-1]: chat = await Chat.add(visitor_id=visitor["id"]) for sequence_num in range(4): content = {"content": fake.sentence(nb_words=10)} chat_msg = { "chat_id": chat["id"], "sequence_num": sequence_num, "content": content, "sender": None, "created_at": created_at, } created_at += 1 created_msg = await ChatMessage.add(**chat_msg) chat_messages.setdefault(chat["id"], []).append(created_msg) for chat_id, value in chat_messages.items(): chat = await Chat.get(id=chat_id) res = await supervisor1_client.patch( "/visitors/{}/last_seen".format(chat["visitor_id"]), json={"last_seen_msg_id": value[2]["id"]}, ) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], dict) assert body["data"]["last_seen_msg_id"] == value[2]["id"]
async def test_get_visitors_most_recent(users, supervisor1_client): # Create a lot more visitors to test visitors = [get_fake_visitor() for _ in range(32)] for visitor in visitors: _visitor = deepcopy(visitor) # Anonymous users don't have passwords if "password" in _visitor: _visitor["password"] = hash_password(_visitor["password"]) await Visitor(**_visitor).create() # Create some dummy chat messages created_at = 1 staff = users[-5] for visitor in visitors[::-1]: chat = await Chat.add(visitor_id=visitor["id"]) for sequence_num in range(3): content = {"content": fake.sentence(nb_words=10)} chat_msg = { "chat_id": chat["id"], "sequence_num": sequence_num, "content": content, "sender": None, "created_at": created_at, } created_at += 1 await ChatMessage.add(**chat_msg) # Let a staff send a message, as staffs can only fetch visitors # that chatted with the staffs from his org await ChatMessage.add( **{ "chat_id": chat["id"], "sequence_num": sequence_num + 1, "content": { "content": fake.sentence(nb_words=10) }, "sender": staff["id"], "created_at": created_at, }) # Get the most recent visitors that chatted with the staffs from org res = await supervisor1_client.get("/visitors/most_recent") assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert len(body["data"]) == 15 assert "links" in body and "next" in body["links"] for expected, actual in zip(visitors, body["data"]): assert profile_created_from_origin(expected, actual) # Ensure that the next links work as well next_page_link = get_next_page_link(body) res = await supervisor1_client.get(next_page_link) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert len(body["data"]) == 15 assert "links" in body and "next" in body["links"] for expected, actual in zip(visitors[15:], body["data"]): assert profile_created_from_origin(expected, actual) # Last page next_page_link = get_next_page_link(body) res = await supervisor1_client.get(next_page_link) assert res.status == 200 assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert len(body["data"]) == 2 assert "links" in body and "next" in body["links"] for expected, actual in zip(visitors[30:], body["data"]): assert profile_created_from_origin(expected, actual)
async def test_get_unread_visitors_for_staff(supervisor1_client): # Create some visitors visitors = [] for _ in range(30): new_visitor = get_fake_visitor() new_visitor.pop("id") res = await supervisor1_client.post("/visitors", json=new_visitor) assert res.status == 200 body = await res.json() visitors.append(body["data"]) # Create some dummy chat messages created_at = 1 chat_messages = {} chats = [] for visitor in visitors: chat = await Chat.add(visitor_id=visitor["id"]) chats.append(chat) for sequence_num in range(4): content = {"content": fake.sentence(nb_words=10)} chat_msg = { "chat_id": chat["id"], "sequence_num": sequence_num, "content": content, "sender": None, "created_at": created_at, } created_at += 1 created_msg = await ChatMessage.add(**chat_msg) chat_messages.setdefault(visitor["id"], []).append(created_msg) # Mark some visitors as read for visitor in visitors[12:17] + visitors[22:26]: res = await supervisor1_client.patch( "/visitors/{}/last_seen".format(visitor["id"]), json={"last_seen_msg_id": chat_messages[visitor["id"]][-1]["id"]}, ) assert res.status == 200 # Mark some visitors as partially read for visitor in visitors[5:10]: res = await supervisor1_client.patch( "/visitors/{}/last_seen".format(visitor["id"]), json={"last_seen_msg_id": chat_messages[visitor["id"]][2]["id"]}, ) assert res.status == 200 # Get the first unread visitors res = await supervisor1_client.get("/visitors/unread") assert res.status == 200 body = await res.json() assert "data" in body assert "links" not in body assert isinstance(body["data"], list) assert len(body["data"]) == 15 for expected_visitor, expected_chat, item in zip( reversed(visitors[:12] + visitors[17:22] + visitors[26:]), reversed(chats[:12] + chats[17:22] + chats[26:]), body["data"], ): actual_visitor = item["user"] actual_chat = item["room"] assert profile_created_from_origin(expected_visitor, actual_visitor) assert profile_created_from_origin(expected_chat, actual_chat) # Mark some visitors as fully read for visitor in visitors[2:8] + visitors[26:29]: res = await supervisor1_client.patch( "/visitors/{}/last_seen".format(visitor["id"]), json={"last_seen_msg_id": chat_messages[visitor["id"]][3]["id"]}, ) assert res.status == 200 # Get the second unread visitors res = await supervisor1_client.get("/visitors/unread") assert res.status == 200 body = await res.json() assert "data" in body assert "links" not in body assert isinstance(body["data"], list) assert len(body["data"]) == 12 for expected_visitor, expected_chat, item in zip( reversed(visitors[:2] + visitors[8:12] + visitors[17:22] + visitors[29:]), reversed(chats[:2] + chats[8:12] + chats[17:22] + chats[29:]), body["data"], ): actual_visitor = item["user"] actual_chat = item["room"] assert profile_created_from_origin(expected_visitor, actual_visitor) assert profile_created_from_origin(expected_chat, actual_chat) # Mark all visitors as read for visitor in visitors: res = await supervisor1_client.patch( "/visitors/{}/last_seen".format(visitor["id"]), json={"last_seen_msg_id": chat_messages[visitor["id"]][-1]["id"]}, ) assert res.status == 200 # Get the last unread visitors res = await supervisor1_client.get("/visitors/unread") assert res.status == 200 body = await res.json() assert "data" in body assert "links" not in body assert isinstance(body["data"], list) assert not body["data"]
async def test_get_messages_unread_from_top(supervisor1_client, visitors, users): visitor_id = visitors[-1]["id"] # Create some dummy chat messages chat = await Chat.add(visitor_id=visitor_id) messages = [] for sequence_num in range(1, 37): content = {"value": fake.sentence(nb_words=10)} sender = users[-6]["id"] if randint(0, 1) else None chat_msg = { "chat_id": chat["id"], "sequence_num": sequence_num, "content": content, "sender": sender, } created_msg = await ChatMessage.add(**chat_msg) created_msg["sender"] = users[-6] if sender else None messages.append(created_msg) # When the chat has no unread, starts from top res = await supervisor1_client.get( "/visitors/{}/messages?starts_from_unread=true".format(visitor_id)) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert "links" in body and "next" in body["links"] and "prev" in body[ "links"] assert len(body["data"]) == 15 for expected, actual in zip(messages[:15], body["data"]): assert profile_created_from_origin(expected, actual, ignore={"sender"}) assert profile_created_from_origin(expected["sender"], actual["sender"]) next_page_link = get_next_page_link(body) prev_page_link = get_prev_page_link(body) # Get the prev pages until no more res = await supervisor1_client.get(prev_page_link) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert ("links" in body and "next" not in body["links"] and "prev" not in body["links"]) assert not body["data"] # Get the next messages from unread til most recent res = await supervisor1_client.get(next_page_link) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert "links" in body and "next" in body["links"] and "prev" in body[ "links"] assert len(body["data"]) == 15 for expected, actual in zip(messages[15:30], body["data"]): assert profile_created_from_origin(expected, actual, ignore={"sender"}) assert profile_created_from_origin(expected["sender"], actual["sender"]) # Get the next messages until end next_page_link = get_next_page_link(body) res = await supervisor1_client.get(next_page_link) assert res.status == 200 body = await res.json() assert "data" in body assert isinstance(body["data"], list) assert "links" in body and "next" in body["links"] and "prev" in body[ "links"] assert len(body["data"]) == 6 for expected, actual in zip(messages[30:], body["data"]): assert profile_created_from_origin(expected, actual, ignore={"sender"}) assert profile_created_from_origin(expected["sender"], actual["sender"])