Coverage for src/tests/test_conversations.py: 100%
935 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-11-21 04:21 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-11-21 04:21 +0000
1from datetime import timedelta
3import grpc
4import pytest
5from google.protobuf import wrappers_pb2
7from couchers import errors
8from couchers.db import session_scope
9from couchers.jobs.worker import process_job
10from couchers.models import (
11 GroupChatRole,
12 GroupChatSubscription,
13 Notification,
14 NotificationDelivery,
15 NotificationDeliveryType,
16 NotificationTopicAction,
17)
18from couchers.sql import couchers_select as select
19from couchers.utils import Duration_from_timedelta, now, to_aware_datetime
20from proto import api_pb2, conversations_pb2, notification_data_pb2, notifications_pb2
21from tests.test_fixtures import ( # noqa
22 api_session,
23 conversations_session,
24 db,
25 generate_user,
26 make_friends,
27 make_user_block,
28 make_user_invisible,
29 notifications_session,
30 testconfig,
31)
34@pytest.fixture(autouse=True)
35def _(testconfig):
36 pass
39def test_list_group_chats(db):
40 user1, token1 = generate_user()
41 user2, token2 = generate_user()
42 user3, token3 = generate_user()
44 make_friends(user2, user1)
45 make_friends(user1, user3)
47 with conversations_session(token1) as c:
48 # no threads initially
49 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
50 assert len(res.group_chats) == 0
52 # create some group chats with messages
53 res = c.CreateGroupChat(
54 conversations_pb2.CreateGroupChatReq(
55 recipient_user_ids=[user2.id], title=wrappers_pb2.StringValue(value="Test title")
56 )
57 )
58 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 1"))
59 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 2"))
60 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
61 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test group message 1"))
62 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test group message 2"))
64 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
65 assert len(res.group_chats) == 2
66 assert res.no_more
68 with conversations_session(token2) as c:
69 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
70 assert len(res.group_chats) == 2
71 assert res.no_more
73 with conversations_session(token3) as c:
74 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
75 assert len(res.group_chats) == 1
76 assert res.no_more
79def test_list_empty_group_chats(db):
80 user1, token1 = generate_user()
81 user2, token2 = generate_user()
82 user3, token3 = generate_user()
84 make_friends(user1, user3)
85 make_friends(user2, user1)
86 make_friends(user2, user3)
88 with conversations_session(token1) as c:
89 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
90 assert len(res.group_chats) == 0
92 c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
93 c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
95 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
96 assert len(res.group_chats) == 2
97 assert res.no_more
99 with conversations_session(token2) as c:
100 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
101 assert len(res.group_chats) == 2
102 assert res.no_more
104 c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id]))
106 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
107 assert len(res.group_chats) == 3
108 assert res.no_more
110 with conversations_session(token3) as c:
111 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
112 assert len(res.group_chats) == 2
113 assert res.no_more
116def test_list_group_chats_ordering(db):
117 # user is member of 5 group chats, order them correctly
118 user, token = generate_user()
119 user2, token2 = generate_user()
120 user3, token3 = generate_user()
121 user4, token4 = generate_user()
123 make_friends(user2, user)
124 make_friends(user2, user3)
125 make_friends(user2, user4)
126 make_friends(user3, user)
127 make_friends(user3, user4)
128 make_friends(user, user4)
130 with conversations_session(token2) as c:
131 res = c.CreateGroupChat(
132 conversations_pb2.CreateGroupChatReq(
133 recipient_user_ids=[user.id], title=wrappers_pb2.StringValue(value="Chat 0")
134 )
135 )
136 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
137 res = c.CreateGroupChat(
138 conversations_pb2.CreateGroupChatReq(
139 recipient_user_ids=[user.id, user3.id], title=wrappers_pb2.StringValue(value="Chat 1")
140 )
141 )
142 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
143 res = c.CreateGroupChat(
144 conversations_pb2.CreateGroupChatReq(
145 recipient_user_ids=[user.id, user3.id], title=wrappers_pb2.StringValue(value="Chat 2")
146 )
147 )
148 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
150 with conversations_session(token3) as c:
151 res = c.CreateGroupChat(
152 conversations_pb2.CreateGroupChatReq(
153 recipient_user_ids=[user.id, user2.id, user4.id], title=wrappers_pb2.StringValue(value="Chat 3")
154 )
155 )
156 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
158 with conversations_session(token) as c:
159 res = c.CreateGroupChat(
160 conversations_pb2.CreateGroupChatReq(
161 recipient_user_ids=[user2.id, user3.id, user4.id], title=wrappers_pb2.StringValue(value="Chat 4")
162 )
163 )
164 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
165 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
166 assert len(res.group_chats) == 5
167 assert res.group_chats[0].title == "Chat 4"
168 assert res.group_chats[1].title == "Chat 3"
169 assert res.group_chats[2].title == "Chat 2"
170 assert res.group_chats[3].title == "Chat 1"
171 assert res.group_chats[4].title == "Chat 0"
173 c.SendMessage(
174 conversations_pb2.SendMessageReq(group_chat_id=res.group_chats[3].group_chat_id, text="Test message 2a")
175 )
176 c.SendMessage(
177 conversations_pb2.SendMessageReq(group_chat_id=res.group_chats[2].group_chat_id, text="Test message 2b")
178 )
180 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
181 assert len(res.group_chats) == 5
182 assert res.group_chats[0].title == "Chat 2"
183 assert res.group_chats[0].latest_message.text.text == "Test message 2b"
184 assert res.group_chats[1].title == "Chat 1"
185 assert res.group_chats[1].latest_message.text.text == "Test message 2a"
186 assert res.group_chats[2].title == "Chat 4"
187 assert res.group_chats[3].title == "Chat 3"
188 assert res.group_chats[4].title == "Chat 0"
191def test_list_group_chats_ordering_after_left(db):
192 # user is member to 4 group chats, and has left one.
193 # The one user left has the most recent message, but user left before then,
194 # this should display as e.g. 3rd most recent depending on last message when they were in the chat
195 user, token = generate_user()
196 user2, token2 = generate_user()
197 user3, token3 = generate_user()
198 user4, token4 = generate_user()
200 make_friends(user2, user)
201 make_friends(user2, user3)
202 make_friends(user2, user4)
203 make_friends(user3, user)
204 make_friends(user3, user4)
205 make_friends(user, user4)
207 with conversations_session(token2) as c:
208 res = c.CreateGroupChat(
209 conversations_pb2.CreateGroupChatReq(
210 recipient_user_ids=[user.id], title=wrappers_pb2.StringValue(value="Chat 0")
211 )
212 )
213 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
214 res = c.CreateGroupChat(
215 conversations_pb2.CreateGroupChatReq(
216 recipient_user_ids=[user.id, user3.id], title=wrappers_pb2.StringValue(value="Left Chat 1")
217 )
218 )
219 left_chat_id = res.group_chat_id
220 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=left_chat_id, text="Test message"))
221 res = c.CreateGroupChat(
222 conversations_pb2.CreateGroupChatReq(
223 recipient_user_ids=[user.id, user3.id], title=wrappers_pb2.StringValue(value="Chat 2")
224 )
225 )
226 chat2_id = res.group_chat_id
227 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=chat2_id, text="Test message"))
229 with conversations_session(token3) as c:
230 res = c.CreateGroupChat(
231 conversations_pb2.CreateGroupChatReq(
232 recipient_user_ids=[user.id, user2.id, user4.id], title=wrappers_pb2.StringValue(value="Chat 3")
233 )
234 )
235 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
237 with conversations_session(token) as c:
238 res = c.CreateGroupChat(
239 conversations_pb2.CreateGroupChatReq(
240 recipient_user_ids=[user2.id, user3.id, user4.id], title=wrappers_pb2.StringValue(value="Chat 4")
241 )
242 )
243 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message"))
245 # leave chat
246 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=left_chat_id))
248 with conversations_session(token3) as c:
249 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=chat2_id, text="Test message"))
250 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=left_chat_id, text="Test message"))
252 with conversations_session(token2) as c:
253 # other user sends a message to that chat
254 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=left_chat_id, text="Another test message"))
255 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
256 assert len(res.group_chats) == 5
257 assert res.group_chats[0].title == "Left Chat 1"
258 assert res.group_chats[1].title == "Chat 2"
259 assert res.group_chats[2].title == "Chat 4"
260 assert res.group_chats[3].title == "Chat 3"
261 assert res.group_chats[4].title == "Chat 0"
263 with conversations_session(token) as c:
264 # we can't see the new message since we left before it was sent
265 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
266 assert len(res.group_chats) == 5
267 assert res.group_chats[0].title == "Chat 2"
268 assert res.group_chats[1].title == "Left Chat 1"
269 assert res.group_chats[2].title == "Chat 4"
270 assert res.group_chats[3].title == "Chat 3"
271 assert res.group_chats[4].title == "Chat 0"
274def test_get_group_chat_messages(db):
275 user1, token1 = generate_user()
276 user2, token2 = generate_user()
277 user3, token3 = generate_user()
279 make_friends(user1, user2)
281 with conversations_session(token1) as c:
282 # create some threads with messages
283 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
284 group_chat_id = res.group_chat_id
285 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 1"))
286 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 2"))
288 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
289 # created + 2 normal
290 assert len(res.messages) == 3
291 assert res.no_more
293 assert res.messages[0].text.text == "Test message 2"
294 assert res.messages[1].text.text == "Test message 1"
295 assert res.messages[2].WhichOneof("content") == "chat_created"
297 # test that another user can't access the thread
298 with conversations_session(token3) as c:
299 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
300 assert len(res.messages) == 0
303def test_get_group_chat_messages_pagination(db):
304 user1, token1 = generate_user()
305 user2, token2 = generate_user()
306 make_friends(user1, user2)
308 with conversations_session(token1) as c:
309 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
310 group_chat_id = res.group_chat_id
311 for i in range(30):
312 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text=str(i)))
314 with conversations_session(token2) as c:
315 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
316 # pagination
317 assert len(res.messages) == 20
318 assert res.messages[0].text.text == "29"
319 assert res.messages[19].text.text == "10"
320 assert not res.no_more
321 res = c.GetGroupChatMessages(
322 conversations_pb2.GetGroupChatMessagesReq(
323 group_chat_id=group_chat_id, last_message_id=res.messages[19].message_id
324 )
325 )
326 assert len(res.messages) == 11
327 assert res.messages[0].text.text == "9"
328 assert res.messages[9].text.text == "0"
329 assert res.no_more
332def test_get_group_chat_messages_joined_left(db):
333 user1, token1 = generate_user()
334 user2, token2 = generate_user()
335 user3, token3 = generate_user()
336 user4, token4 = generate_user()
337 make_friends(user1, user2)
338 make_friends(user1, user3)
339 make_friends(user1, user4)
341 with conversations_session(token1) as c:
342 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user4.id]))
343 group_chat_id = res.group_chat_id
345 for i in range(10):
346 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text=str(i)))
348 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id))
350 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="10"))
352 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
354 # created + 10 normal + invited + normal
355 assert len(res.messages) == 13
357 with conversations_session(token3) as c:
358 # can only see last message after invited
359 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
360 # joined + normal
361 assert len(res.messages) == 2
362 assert res.messages[0].text.text == "10"
364 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
366 with conversations_session(token1) as c:
367 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="11"))
368 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="12"))
370 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id))
372 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="13"))
373 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="14"))
375 with conversations_session(token3) as c:
376 # can only see last message after invited
377 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
378 # joined + normal + left + invite + 2 normal
379 assert len(res.messages) == 6
380 assert res.messages[0].text.text == "14"
381 assert res.messages[1].text.text == "13"
382 assert res.messages[2].WhichOneof("content") == "user_invited"
383 assert res.messages[3].WhichOneof("content") == "user_left"
384 assert res.messages[4].text.text == "10"
385 assert res.messages[5].WhichOneof("content") == "user_invited"
388def test_get_group_chat_info(db):
389 user1, token1 = generate_user()
390 user2, token2 = generate_user()
391 user3, token3 = generate_user()
393 make_friends(user1, user2)
394 make_friends(user3, user1)
396 with conversations_session(token1) as c:
397 # create some threads with messages
398 res = c.CreateGroupChat(
399 conversations_pb2.CreateGroupChatReq(
400 recipient_user_ids=[user2.id], title=wrappers_pb2.StringValue(value="Test title")
401 )
402 )
403 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 1"))
404 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 2"))
405 group_chat1_id = res.group_chat_id
406 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
407 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test group message 1"))
408 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test group message 2"))
409 group_chat2_id = res.group_chat_id
411 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat1_id))
412 assert res.title == "Test title"
413 assert user2.id in res.member_user_ids
414 assert user1.id in res.admin_user_ids
415 assert to_aware_datetime(res.created) <= now()
416 assert res.only_admins_invite
417 assert res.is_dm
419 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat2_id))
420 assert not res.title
421 assert user2.id in res.member_user_ids
422 assert user3.id in res.member_user_ids
423 assert user1.id in res.admin_user_ids
424 assert to_aware_datetime(res.created) <= now()
425 assert res.only_admins_invite
426 assert not res.is_dm
429def test_get_group_chat_info_denied(db):
430 user1, token1 = generate_user()
431 user2, token2 = generate_user()
432 user3, token3 = generate_user()
433 user4, token4 = generate_user()
435 make_friends(user1, user2)
436 make_friends(user3, user1)
438 with conversations_session(token1) as c:
439 # create a group chat with messages
440 res = c.CreateGroupChat(
441 conversations_pb2.CreateGroupChatReq(
442 recipient_user_ids=[user2.id, user3.id], title=wrappers_pb2.StringValue(value="Test title")
443 )
444 )
445 group_chat_id = res.group_chat_id
446 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
448 with conversations_session(token4) as c:
449 with pytest.raises(grpc.RpcError) as e:
450 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
451 assert e.value.code() == grpc.StatusCode.NOT_FOUND
454def test_get_group_chat_info_left(db):
455 user1, token1 = generate_user()
456 user2, token2 = generate_user()
457 user3, token3 = generate_user()
458 user4, token4 = generate_user()
460 make_friends(user1, user2)
461 make_friends(user3, user1)
462 make_friends(user1, user4)
464 with conversations_session(token1) as c:
465 # create a group chat with messages
466 res = c.CreateGroupChat(
467 conversations_pb2.CreateGroupChatReq(
468 recipient_user_ids=[user2.id, user3.id], title=wrappers_pb2.StringValue(value="Test title")
469 )
470 )
471 group_chat_id = res.group_chat_id
472 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
474 with conversations_session(token3) as c:
475 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
477 with conversations_session(token1) as c:
478 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user4.id))
480 with conversations_session(token3) as c:
481 # this user left when user4 wasn't a member,
482 # so the returned members should be user1, user2, and user3 only
483 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
484 assert len(res.member_user_ids) == 3
485 assert user1.id in res.member_user_ids
486 assert user2.id in res.member_user_ids
487 assert user3.id in res.member_user_ids
490def test_remove_group_chat_user(db):
491 # create 3 uses and connect them
492 user1, token1 = generate_user()
493 user2, token2 = generate_user()
494 user3, token3 = generate_user()
495 make_friends(user1, user2)
496 make_friends(user1, user3)
498 # using user token, create a Conversations API for testing
499 with conversations_session(token1) as c:
500 # create a group chat
501 res = c.CreateGroupChat(
502 conversations_pb2.CreateGroupChatReq(
503 recipient_user_ids=[user2.id, user3.id], title=wrappers_pb2.StringValue(value="Test title")
504 )
505 )
506 group_chat_id = res.group_chat_id
508 # remove a user from group
509 c.RemoveGroupChatUser(conversations_pb2.RemoveGroupChatUserReq(group_chat_id=group_chat_id, user_id=user2.id))
510 assert user3.id in res.member_user_ids # other users are still in the group
512 # can't remove the same user twice
513 with pytest.raises(grpc.RpcError) as e:
514 c.RemoveGroupChatUser(
515 conversations_pb2.RemoveGroupChatUserReq(group_chat_id=group_chat_id, user_id=user2.id)
516 )
517 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
520def test_edit_group_chat(db):
521 user1, token1 = generate_user()
522 user2, token2 = generate_user()
523 user3, token3 = generate_user()
524 make_friends(user1, user2)
526 with conversations_session(token1) as c:
527 # create some threads with messages
528 res = c.CreateGroupChat(
529 conversations_pb2.CreateGroupChatReq(
530 recipient_user_ids=[user2.id], title=wrappers_pb2.StringValue(value="Test title")
531 )
532 )
533 group_chat_id = res.group_chat_id
535 c.EditGroupChat(
536 conversations_pb2.EditGroupChatReq(
537 group_chat_id=group_chat_id,
538 title=wrappers_pb2.StringValue(value="Modified title"),
539 only_admins_invite=wrappers_pb2.BoolValue(value=False),
540 )
541 )
542 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
543 assert res.title == "Modified title"
544 assert not res.only_admins_invite
546 # make sure non-admin is not allowed to modify
547 with conversations_session(token2) as c:
548 with pytest.raises(grpc.RpcError) as e:
549 c.EditGroupChat(
550 conversations_pb2.EditGroupChatReq(
551 group_chat_id=group_chat_id,
552 title=wrappers_pb2.StringValue(value="Other title"),
553 only_admins_invite=wrappers_pb2.BoolValue(value=True),
554 )
555 )
556 assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
558 # make sure non-recipient is not allowed to modify
559 with conversations_session(token3) as c:
560 with pytest.raises(grpc.RpcError) as e:
561 c.EditGroupChat(
562 conversations_pb2.EditGroupChatReq(
563 group_chat_id=group_chat_id,
564 title=wrappers_pb2.StringValue(value="Other title"),
565 only_admins_invite=wrappers_pb2.BoolValue(value=True),
566 )
567 )
568 assert e.value.code() == grpc.StatusCode.NOT_FOUND
571def test_make_remove_group_chat_admin(db):
572 user1, token1 = generate_user()
573 user2, token2 = generate_user()
574 user3, token3 = generate_user()
576 make_friends(user1, user2)
577 make_friends(user1, user3)
578 make_friends(user2, user3)
580 with conversations_session(token1) as c:
581 # create some threads with messages
582 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
583 group_chat_id = res.group_chat_id
585 # shouldn't be able to remove only admin
586 with pytest.raises(grpc.RpcError) as e:
587 c.RemoveGroupChatAdmin(
588 conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=group_chat_id, user_id=user1.id)
589 )
590 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
591 assert e.value.details() == errors.CANT_REMOVE_LAST_ADMIN
593 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=group_chat_id, user_id=user2.id))
595 # shouldn't be able to make admin again
596 with pytest.raises(grpc.RpcError) as e:
597 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=group_chat_id, user_id=user2.id))
598 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
599 assert e.value.details() == errors.ALREADY_ADMIN
601 with conversations_session(token2) as c:
602 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
603 assert user1.id in res.admin_user_ids
604 assert user2.id in res.admin_user_ids
606 with conversations_session(token1) as c:
607 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=group_chat_id, user_id=user2.id))
609 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
610 assert user1.id in res.admin_user_ids
611 assert user2.id not in res.admin_user_ids
613 with conversations_session(token2) as c:
614 # shouldn't be able to make admin if not admin
615 with pytest.raises(grpc.RpcError) as e:
616 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=group_chat_id, user_id=user3.id))
617 assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
620def test_send_message(db):
621 user1, token1 = generate_user()
622 user2, token2 = generate_user()
623 user3, token3 = generate_user()
624 make_friends(user1, user2)
625 make_friends(user1, user3)
627 with conversations_session(token1) as c:
628 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
629 group_chat_id = res.group_chat_id
630 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
631 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
632 assert res.messages[0].text.text == "Test message 1"
633 assert to_aware_datetime(res.messages[0].time) <= now()
634 assert res.messages[0].author_user_id == user1.id
636 # can't send message if not in chat
637 with conversations_session(token3) as c:
638 with pytest.raises(grpc.RpcError) as e:
639 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 2"))
640 assert e.value.code() == grpc.StatusCode.NOT_FOUND
643def test_leave_invite_to_group_chat(db):
644 user1, token1 = generate_user()
645 user2, token2 = generate_user()
646 user3, token3 = generate_user()
647 user4, token4 = generate_user()
648 user5, token5 = generate_user()
649 user6, token6 = generate_user(delete_user=True)
650 user7, token7 = generate_user()
651 user8, token8 = generate_user()
653 make_friends(user1, user2)
654 make_friends(user1, user3)
655 make_friends(user1, user5)
656 make_friends(user1, user7)
657 make_friends(user1, user8)
658 make_friends(user2, user3)
659 make_friends(user4, user3)
660 make_user_block(user1, user7)
661 make_user_block(user8, user1)
663 with conversations_session(token1) as c:
664 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user5.id]))
665 group_chat_id = res.group_chat_id
666 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
668 # other user not in chat
669 with conversations_session(token3) as c:
670 with pytest.raises(grpc.RpcError) as e:
671 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
672 assert e.value.code() == grpc.StatusCode.NOT_FOUND
673 with pytest.raises(grpc.RpcError) as e:
674 res = c.InviteToGroupChat(
675 conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user4.id)
676 )
677 assert e.value.code() == grpc.StatusCode.NOT_FOUND
678 with pytest.raises(grpc.RpcError) as e:
679 res = c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
680 assert e.value.code() == grpc.StatusCode.NOT_FOUND
682 with conversations_session(token2) as c:
683 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
684 assert user3.id not in res.member_user_ids
686 # only_admins_invite defaults to true so shouldn't be able to invite
687 with pytest.raises(grpc.RpcError) as e:
688 res = c.InviteToGroupChat(
689 conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id)
690 )
691 assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
692 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
694 with conversations_session(token1) as c:
695 # invite invisible user fails
696 with pytest.raises(grpc.RpcError) as e:
697 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user6.id))
698 assert e.value.code() == grpc.StatusCode.NOT_FOUND
699 assert e.value.details() == errors.USER_NOT_FOUND
700 # invite fake user fails
701 with pytest.raises(grpc.RpcError) as e:
702 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=999))
703 assert e.value.code() == grpc.StatusCode.NOT_FOUND
704 assert e.value.details() == errors.USER_NOT_FOUND
705 # invite blocked user fails
706 with pytest.raises(grpc.RpcError) as e:
707 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user7.id))
708 assert e.value.code() == grpc.StatusCode.NOT_FOUND
709 assert e.value.details() == errors.USER_NOT_FOUND
710 # invite blocking user fails
711 with pytest.raises(grpc.RpcError) as e:
712 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user8.id))
713 assert e.value.code() == grpc.StatusCode.NOT_FOUND
714 assert e.value.details() == errors.USER_NOT_FOUND
716 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id))
717 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
718 assert user1.id in res.member_user_ids
719 assert user5.id in res.member_user_ids
720 assert user3.id in res.member_user_ids
722 # test non-admin inviting
723 c.EditGroupChat(
724 conversations_pb2.EditGroupChatReq(
725 group_chat_id=group_chat_id, only_admins_invite=wrappers_pb2.BoolValue(value=False)
726 )
727 )
729 with conversations_session(token3) as c:
730 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user2.id))
731 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
732 assert user2.id in res.member_user_ids
735def test_group_chats_with_messages_before_join(db):
736 """
737 If user 1 and 2 have a group chat and send messages, then add user 3; user 3
738 should still see the group chat!
739 """
740 user1, token1 = generate_user()
741 user2, token2 = generate_user()
742 user3, token3 = generate_user()
743 user4, token4 = generate_user()
745 make_friends(user1, user2)
746 make_friends(user1, user3)
747 make_friends(user2, user3)
748 make_friends(user1, user4)
750 with conversations_session(token1) as c:
751 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user4.id]))
752 group_chat_id = res.group_chat_id
753 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
755 with conversations_session(token2) as c:
756 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 2"))
758 with conversations_session(token1) as c:
759 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 3"))
761 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id))
763 with conversations_session(token3) as c:
764 # should work
765 c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id))
767 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq())
768 assert len(res.group_chats) == 1
771def test_invite_to_dm(db):
772 user1, token1 = generate_user()
773 user2, token2 = generate_user()
774 user3, token3 = generate_user()
776 make_friends(user1, user2)
777 make_friends(user1, user3)
778 make_friends(user2, user3)
780 with conversations_session(token1) as c:
781 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
782 group_chat_id = res.group_chat_id
783 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
785 # dm, shou;dn't be able to invite someone else
786 with pytest.raises(grpc.RpcError) as e:
787 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id))
788 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
789 assert e.value.details() == errors.CANT_INVITE_TO_DM
792def test_sole_admin_leaves(db):
793 user1, token1 = generate_user()
794 user2, token2 = generate_user()
795 user3, token3 = generate_user()
797 make_friends(user1, user2)
798 make_friends(user1, user3)
799 make_friends(user2, user3)
801 with conversations_session(token1) as c:
802 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
803 group_chat_id = res.group_chat_id
804 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
806 # sole admin can't leave group chat
807 with pytest.raises(grpc.RpcError) as e:
808 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
809 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
810 assert e.value.details() == errors.LAST_ADMIN_CANT_LEAVE
812 with conversations_session(token2) as c:
813 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
815 with conversations_session(token3) as c:
816 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
818 # sole admin can leave when last in chat
819 with conversations_session(token1) as c:
820 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
823def test_search_messages(db):
824 user1, token1 = generate_user()
825 user2, token2 = generate_user()
826 user3, token3 = generate_user()
828 make_friends(user1, user2)
829 make_friends(user1, user3)
831 with conversations_session(token1) as c:
832 # create some threads with messages
833 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
834 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 1"))
835 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test message 2"))
836 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
837 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test group message 3"))
838 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res.group_chat_id, text="Test group message 4"))
840 res = c.SearchMessages(conversations_pb2.SearchMessagesReq(query="message "))
841 assert len(res.results) == 4
842 res = c.SearchMessages(conversations_pb2.SearchMessagesReq(query="group "))
843 assert len(res.results) == 2
844 res = c.SearchMessages(conversations_pb2.SearchMessagesReq(query="message 5"))
845 assert len(res.results) == 0
847 # outside user doesn't get results
848 with conversations_session(token3) as c:
849 res = c.SearchMessages(conversations_pb2.SearchMessagesReq(query="Test message"))
850 assert len(res.results) == 0
853def test_search_messages_left_joined(db):
854 user1, token1 = generate_user()
855 user2, token2 = generate_user()
856 user3, token3 = generate_user()
857 user4, token4 = generate_user()
858 make_friends(user1, user2)
859 make_friends(user1, user3)
860 make_friends(user1, user4)
862 with conversations_session(token1) as c:
863 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user4.id]))
864 group_chat_id = res.group_chat_id
865 for i in range(10):
866 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message " + str(i)))
868 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id))
869 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 10"))
870 res = c.SearchMessages(conversations_pb2.SearchMessagesReq(query="Test message"))
872 assert len(res.results) == 11
874 with conversations_session(token3) as c:
875 # can only see last message after invited
876 res = c.SearchMessages(conversations_pb2.SearchMessagesReq(query="Test message"))
878 assert len(res.results) == 1
879 assert res.results[0].message.text.text == "Test message 10"
881 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
883 with conversations_session(token1) as c:
884 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 11"))
885 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 12"))
886 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id))
887 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 13"))
888 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 14"))
890 with conversations_session(token3) as c:
891 # can only see last message after invited
892 res = c.SearchMessages(conversations_pb2.SearchMessagesReq(query="Test message"))
893 assert len(res.results) == 3
894 assert res.results[0].message.text.text == "Test message 14"
895 assert res.results[1].message.text.text == "Test message 13"
896 assert res.results[2].message.text.text == "Test message 10"
899def test_admin_behaviour(db):
900 user1, token1 = generate_user()
901 user2, token2 = generate_user()
902 user3, token3 = generate_user()
904 make_friends(user1, user2)
905 make_friends(user1, user3)
906 make_friends(user2, user3)
908 with conversations_session(token1) as c:
909 gcid = c.CreateGroupChat(
910 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id])
911 ).group_chat_id
912 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user2.id))
913 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
914 assert len(res.admin_user_ids) == 2
915 assert user1.id in res.admin_user_ids
916 assert user2.id in res.admin_user_ids
918 with conversations_session(token3) as c:
919 with pytest.raises(grpc.RpcError) as e:
920 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user3.id))
921 assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
922 with pytest.raises(grpc.RpcError) as e:
923 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user1.id))
924 assert e.value.code() == grpc.StatusCode.PERMISSION_DENIED
925 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
926 assert len(res.admin_user_ids) == 2
927 assert user1.id in res.admin_user_ids
928 assert user2.id in res.admin_user_ids
930 with conversations_session(token2) as c:
931 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user3.id))
932 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
933 assert len(res.admin_user_ids) == 3
934 assert user1.id in res.admin_user_ids
935 assert user2.id in res.admin_user_ids
936 assert user3.id in res.admin_user_ids
938 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user1.id))
939 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
940 assert len(res.admin_user_ids) == 2
941 assert user2.id in res.admin_user_ids
942 assert user3.id in res.admin_user_ids
944 with conversations_session(token1) as c:
945 with pytest.raises(grpc.RpcError):
946 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user1.id))
947 with pytest.raises(grpc.RpcError):
948 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user3.id))
949 with pytest.raises(grpc.RpcError):
950 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user2.id))
951 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
952 assert len(res.admin_user_ids) == 2
953 assert user2.id in res.admin_user_ids
954 assert user3.id in res.admin_user_ids
956 with conversations_session(token2) as c:
957 # can demote self if there are other admins
958 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user2.id))
959 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
960 assert len(res.admin_user_ids) == 1
961 assert user3.id in res.admin_user_ids
963 with conversations_session(token3) as c:
964 with pytest.raises(grpc.RpcError) as e:
965 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user3.id))
966 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
967 assert e.value.details() == errors.CANT_REMOVE_LAST_ADMIN
968 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
969 assert len(res.admin_user_ids) == 1
970 assert user3.id in res.admin_user_ids
972 # last admin can't leave
973 with pytest.raises(grpc.RpcError) as e:
974 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid))
975 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
976 assert e.value.details() == errors.LAST_ADMIN_CANT_LEAVE
978 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user1.id))
980 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid))
982 with conversations_session(token2) as c:
983 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid))
985 # last participant must be admin but can leave to orphan chat
986 with conversations_session(token1) as c:
987 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid))
990def test_add_remove_admin_failures(db):
991 user1, token1 = generate_user()
992 user2, token2 = generate_user()
993 user3, token3 = generate_user()
994 user4, token4 = generate_user()
995 user5, token5 = generate_user()
997 make_friends(user1, user2)
998 make_friends(user1, user3)
999 make_friends(user1, user4)
1000 make_friends(user1, user5)
1002 with conversations_session(token1) as c:
1003 gcid = c.CreateGroupChat(
1004 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id, user4.id, user5.id])
1005 ).group_chat_id
1007 make_user_invisible(user3.id)
1008 make_user_block(user1, user4)
1009 make_user_block(user5, user1)
1011 # make non-existent user admin
1012 with pytest.raises(grpc.RpcError) as e:
1013 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=999))
1014 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1015 assert e.value.details() == errors.USER_NOT_FOUND
1017 # make invisible user admin
1018 with pytest.raises(grpc.RpcError) as e:
1019 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user3.id))
1020 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1021 assert e.value.details() == errors.USER_NOT_FOUND
1023 # make blocked user admin
1024 with pytest.raises(grpc.RpcError) as e:
1025 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user4.id))
1026 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1027 assert e.value.details() == errors.USER_NOT_FOUND
1029 # make blocking user admin
1030 with pytest.raises(grpc.RpcError) as e:
1031 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user5.id))
1032 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1033 assert e.value.details() == errors.USER_NOT_FOUND
1035 with session_scope() as session:
1036 subscriptions = (
1037 session.execute(
1038 select(GroupChatSubscription)
1039 .where(GroupChatSubscription.group_chat_id == gcid)
1040 .where(GroupChatSubscription.role == GroupChatRole.participant)
1041 )
1042 .scalars()
1043 .all()
1044 )
1046 for subscription in subscriptions:
1047 subscription.role = GroupChatRole.admin
1049 with conversations_session(token1) as c:
1050 # remove non-existent user admin
1051 with pytest.raises(grpc.RpcError) as e:
1052 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=999))
1053 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1054 assert e.value.details() == errors.USER_NOT_FOUND
1056 # remove invisible admin
1057 with pytest.raises(grpc.RpcError) as e:
1058 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user3.id))
1059 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1060 assert e.value.details() == errors.USER_NOT_FOUND
1062 # remove blocked admin
1063 with pytest.raises(grpc.RpcError) as e:
1064 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user4.id))
1065 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1066 assert e.value.details() == errors.USER_NOT_FOUND
1068 # remove blocking admin
1069 with pytest.raises(grpc.RpcError) as e:
1070 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=gcid, user_id=user5.id))
1071 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1072 assert e.value.details() == errors.USER_NOT_FOUND
1075def test_last_seen(db):
1076 user1, token1 = generate_user()
1077 user2, token2 = generate_user()
1078 user3, token3 = generate_user()
1080 make_friends(user1, user2)
1081 make_friends(user1, user3)
1082 make_friends(user2, user3)
1084 with conversations_session(token3) as c:
1085 # this is just here to mess up any issues we get if we pretend there's only one group chat ever
1086 gcid_distraction = c.CreateGroupChat(
1087 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user1.id])
1088 ).group_chat_id
1090 with conversations_session(token1) as c:
1091 gcid = c.CreateGroupChat(
1092 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id])
1093 ).group_chat_id
1095 message_ids = []
1097 for i in range(6):
1098 c.SendMessage(
1099 conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text=f"gibberish message... {i}")
1100 )
1101 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text=f"test message {i}"))
1102 c.SendMessage(
1103 conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text=f"gibberish message {i}")
1104 )
1106 message_ids.append(
1107 c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid)).latest_message.message_id
1108 )
1110 # messages are automatically marked as seen when you send a new message
1111 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1112 assert res.unseen_message_count == 0
1114 with conversations_session(token2) as c:
1115 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1116 # created + 6 normal
1117 assert res.unseen_message_count == 7
1119 backward_offset = 3
1120 c.MarkLastSeenGroupChat(
1121 conversations_pb2.MarkLastSeenGroupChatReq(
1122 group_chat_id=gcid, last_seen_message_id=message_ids[-backward_offset - 1]
1123 )
1124 )
1126 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1127 assert res.unseen_message_count == backward_offset
1129 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text="test message ..."))
1131 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1132 assert res.unseen_message_count == 0
1134 with conversations_session(token3) as c:
1135 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1136 # created + 7 normal
1137 assert res.unseen_message_count == 8
1139 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text="test message ..."))
1141 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1142 assert res.unseen_message_count == 0
1145def test_one_dm_per_pair(db):
1146 user1, token1 = generate_user()
1147 user2, token2 = generate_user()
1148 user3, token3 = generate_user()
1150 make_friends(user1, user2)
1151 make_friends(user1, user3)
1152 make_friends(user2, user3)
1154 with conversations_session(token1) as c:
1155 # create DM with user 2
1156 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
1157 assert res.is_dm
1159 # create DM with user 3
1160 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id]))
1161 assert res.is_dm
1163 # can't create another group chat with just user 2
1164 with pytest.raises(grpc.RpcError) as e:
1165 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
1166 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
1168 # can't create another group chat with just user 3
1169 with pytest.raises(grpc.RpcError) as e:
1170 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id]))
1171 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
1173 # can create joined group chat
1174 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
1175 assert not res.is_dm
1177 with conversations_session(token2) as c:
1178 # can create DM with user 3
1179 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id]))
1180 assert res.is_dm
1182 # can't create another group chat with just user 1
1183 with pytest.raises(grpc.RpcError) as e:
1184 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user1.id]))
1185 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
1188def test_GetDirectMessage(db):
1189 user1, token1 = generate_user()
1190 user2, token2 = generate_user()
1191 user3, token3 = generate_user()
1193 make_friends(user1, user2)
1194 make_friends(user1, user3)
1195 make_friends(user2, user3)
1197 with conversations_session(token1) as c:
1198 # no group chat with user 2
1199 with pytest.raises(grpc.RpcError) as e:
1200 res = c.GetDirectMessage(conversations_pb2.GetDirectMessageReq(user_id=user2.id))
1201 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1203 # no group chat with nor user 3
1204 with pytest.raises(grpc.RpcError) as e:
1205 res = c.GetDirectMessage(conversations_pb2.GetDirectMessageReq(user_id=user3.id))
1206 assert e.value.code() == grpc.StatusCode.NOT_FOUND
1208 # create DM with user 2
1209 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
1210 assert res.is_dm
1211 gcid = res.group_chat_id
1213 # now should exist
1214 res = c.GetDirectMessage(conversations_pb2.GetDirectMessageReq(user_id=user2.id))
1215 assert res.group_chat_id == gcid
1217 # create DM with user 3
1218 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id]))
1219 assert res.is_dm
1221 # can create joined group chat
1222 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]))
1223 assert not res.is_dm
1225 with conversations_session(token2) as c:
1226 # can create DM with user 3
1227 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id]))
1228 assert res.is_dm
1229 gcid = res.group_chat_id
1231 # DM with 3 should exist
1232 res = c.GetDirectMessage(conversations_pb2.GetDirectMessageReq(user_id=user3.id))
1233 assert res.group_chat_id == gcid
1236def test_total_unseen(db):
1237 user1, token1 = generate_user()
1238 user2, token2 = generate_user()
1239 user3, token3 = generate_user()
1241 # distractions
1242 user4, token4 = generate_user()
1244 make_friends(user1, user2)
1245 make_friends(user1, user3)
1246 make_friends(user2, user3)
1248 # distractions
1249 make_friends(user1, user4)
1251 with conversations_session(token1) as c:
1252 # distractions
1253 gcid_distraction = c.CreateGroupChat(
1254 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user4.id])
1255 ).group_chat_id
1256 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction..."))
1258 gcid = c.CreateGroupChat(
1259 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id])
1260 ).group_chat_id
1262 for i in range(6):
1263 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text=f"test message {i}"))
1265 # distractions
1266 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction..."))
1268 # messages are automatically marked as seen when you send a new message
1269 with api_session(token1) as api:
1270 assert api.Ping(api_pb2.PingReq()).unseen_message_count == 0
1272 with api_session(token2) as api:
1273 # chat created + 6 normal messages
1274 assert api.Ping(api_pb2.PingReq()).unseen_message_count == 7
1276 # now leave chat with user2
1277 with conversations_session(token2) as c:
1278 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid))
1280 with api_session(token2) as api:
1281 # seen messages becomes 0 when leaving
1282 assert api.Ping(api_pb2.PingReq()).unseen_message_count == 0
1284 with conversations_session(token1) as c:
1285 # distractions
1286 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction..."))
1288 # send more stuff without user 2
1289 for i in range(3):
1290 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text=f"test message {i}"))
1292 # distractions
1293 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction..."))
1295 with api_session(token2) as api:
1296 # seen messages becomes 0 when leaving
1297 assert api.Ping(api_pb2.PingReq()).unseen_message_count == 0
1299 with conversations_session(token1) as c:
1300 # add user 2 back
1301 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=gcid, user_id=user2.id))
1303 # send more stuff with user 2
1304 for i in range(12):
1305 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text=f"test message {i}"))
1307 # distractions
1308 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction..."))
1310 with api_session(token2) as api:
1311 # joined + 12 normal
1312 assert api.Ping(api_pb2.PingReq()).unseen_message_count == 13
1315def test_regression_ListGroupChats_pagination(db):
1316 user1, token1 = generate_user()
1317 user2, token2 = generate_user()
1318 user3, token3 = generate_user()
1320 make_friends(user1, user2)
1321 make_friends(user1, user3)
1323 with conversations_session(token1) as c:
1324 # tuples of (group_chat_id, message_id)
1325 group_chat_and_message_ids = []
1326 for i in range(50):
1327 res1 = c.CreateGroupChat(
1328 conversations_pb2.CreateGroupChatReq(
1329 recipient_user_ids=[user2.id, user3.id], title=wrappers_pb2.StringValue(value=f"Chat {i}")
1330 )
1331 )
1333 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res1.group_chat_id, text=f"Test message {i}"))
1335 res2 = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=res1.group_chat_id))
1337 group_chat_and_message_ids.append((res2.group_chat_id, res2.latest_message.message_id))
1339 seen_group_chat_ids = []
1341 last_message_id = 0
1342 more = True
1343 while more:
1344 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq(last_message_id=last_message_id))
1345 last_message_id = res.last_message_id
1346 more = not res.no_more
1348 seen_group_chat_ids.extend([chat.group_chat_id for chat in res.group_chats])
1350 assert set(seen_group_chat_ids) == {x[0] for x in group_chat_and_message_ids}, "Not all group chats returned"
1353def test_muting(db):
1354 user1, token1 = generate_user()
1355 user2, token2 = generate_user()
1356 user3, token3 = generate_user()
1358 make_friends(user1, user2)
1359 make_friends(user1, user3)
1360 make_friends(user2, user3)
1362 with conversations_session(token3) as c:
1363 # this is just here to mess up any issues we get if we pretend there's only one group chat ever
1364 gcid_distraction = c.CreateGroupChat(
1365 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user1.id])
1366 ).group_chat_id
1368 with conversations_session(token1) as c:
1369 gcid = c.CreateGroupChat(
1370 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id])
1371 ).group_chat_id
1373 with conversations_session(token2) as c:
1374 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1375 assert not res.mute_info.muted
1376 assert not res.mute_info.HasField("muted_until")
1378 c.MuteGroupChat(conversations_pb2.MuteGroupChatReq(group_chat_id=gcid, forever=True))
1379 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1380 assert res.mute_info.muted
1381 assert not res.mute_info.HasField("muted_until")
1383 c.MuteGroupChat(conversations_pb2.MuteGroupChatReq(group_chat_id=gcid, unmute=True))
1384 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1385 assert not res.mute_info.muted
1386 assert not res.mute_info.HasField("muted_until")
1388 c.MuteGroupChat(
1389 conversations_pb2.MuteGroupChatReq(
1390 group_chat_id=gcid, for_duration=Duration_from_timedelta(timedelta(hours=2))
1391 )
1392 )
1393 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid))
1394 assert res.mute_info.muted
1395 assert res.mute_info.HasField("muted_until")
1396 assert to_aware_datetime(res.mute_info.muted_until) >= now() + timedelta(hours=1, minutes=59)
1397 assert to_aware_datetime(res.mute_info.muted_until) <= now() + timedelta(hours=2, minutes=1)
1400def test_chat_notifications(db):
1401 user1, token1 = generate_user()
1402 user2, token2 = generate_user()
1403 # notifs off
1404 user3, token3 = generate_user()
1405 user4, token4 = generate_user()
1406 user5, token5 = generate_user()
1407 user6, token6 = generate_user()
1409 make_friends(user1, user2)
1410 make_friends(user1, user3)
1411 make_friends(user1, user4)
1412 make_friends(user4, user5)
1413 make_friends(user4, user6)
1415 # have some of them enable/disable notifs
1416 topic_action = NotificationTopicAction.chat__message
1417 for token, enabled in [
1418 (token1, True),
1419 (token2, True),
1420 (token3, False),
1421 (token4, True),
1422 (token5, True),
1423 (token6, True),
1424 ]:
1425 with notifications_session(token) as notifications:
1426 notifications.SetNotificationSettings(
1427 notifications_pb2.SetNotificationSettingsReq(
1428 preferences=[
1429 notifications_pb2.SingleNotificationPreference(
1430 topic=topic_action.topic,
1431 action=topic_action.action,
1432 delivery_method=delivery_method,
1433 enabled=enabled,
1434 )
1435 for delivery_method in ["push", "email", "digest"]
1436 ],
1437 )
1438 )
1440 group_chat_id = None
1442 def send_msg(c, i):
1443 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text=f"Test message {i}"))
1445 with conversations_session(token1) as c:
1446 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id, user4.id]))
1447 group_chat_id = res.group_chat_id
1448 c.EditGroupChat(
1449 conversations_pb2.EditGroupChatReq(
1450 group_chat_id=group_chat_id, only_admins_invite=wrappers_pb2.BoolValue(value=False)
1451 )
1452 )
1453 send_msg(c, i=1)
1454 send_msg(c, i=2)
1456 with conversations_session(token4) as c:
1457 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user5.id))
1458 send_msg(c, i=3)
1459 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user6.id))
1460 send_msg(c, i=4)
1461 send_msg(c, i=5)
1463 with conversations_session(token3) as c:
1464 send_msg(c, i=6)
1465 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
1467 with conversations_session(token2) as c:
1468 send_msg(c, i=7)
1469 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id))
1471 with conversations_session(token6) as c:
1472 send_msg(c, i=8)
1474 # go through all bg jobs
1475 while process_job():
1476 pass
1478 # now check notifs...
1479 expected_notifs = [
1480 (user1, "user1", [3, 4, 5, 6, 7, 8]),
1481 (user2, "user2", [1, 2, 3, 4, 5, 6]),
1482 (user3, "user3", []), # notifs off
1483 (user4, "user4", [1, 2, 6, 7, 8]),
1484 (user5, "user5", [3, 4, 5, 6, 7, 8]),
1485 (user6, "user6", [4, 5, 6, 7]),
1486 ]
1488 with session_scope() as session:
1489 for user, label, expected_msgs in expected_notifs:
1490 deliv = (
1491 session.execute(
1492 select(Notification.data)
1493 .join(NotificationDelivery, NotificationDelivery.notification_id == Notification.id)
1494 .where(Notification.user_id == user.id)
1495 .where(Notification.topic_action == topic_action)
1496 .where(NotificationDelivery.delivery_type == NotificationDeliveryType.push)
1497 .order_by(Notification.created)
1498 )
1499 .scalars()
1500 .all()
1501 )
1503 def parse_message_payload(data):
1504 return notification_data_pb2.ChatMessage.FromString(data).text
1506 contents = [parse_message_payload(d) for d in deliv]
1508 print(contents)
1510 assert [f"Test message {i}" for i in expected_msgs] == contents, f"Wrong messages for {label}"
1513def test_incomplete_profile(db):
1514 user1, token1 = generate_user(complete_profile=True)
1515 user2, token2 = generate_user(complete_profile=False)
1516 user3, token3 = generate_user(complete_profile=True)
1517 make_friends(user1, user2)
1518 make_friends(user1, user3)
1520 # user 1 can make a chat
1521 with conversations_session(token1) as c:
1522 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id]))
1523 group_chat_id = res.group_chat_id
1524 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 1"))
1525 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id))
1526 assert res.messages[0].text.text == "Test message 1"
1527 assert to_aware_datetime(res.messages[0].time) <= now()
1528 assert res.messages[0].author_user_id == user1.id
1530 # user 2 cannot
1531 with conversations_session(token2) as c:
1532 with pytest.raises(grpc.RpcError) as e:
1533 c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id]))
1534 assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION
1535 assert e.value.details() == errors.INCOMPLETE_PROFILE_SEND_MESSAGE