Coverage for src/tests/test_conversations.py: 100%

916 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-22 17:19 +0000

1from datetime import timedelta 

2 

3import grpc 

4import pytest 

5from google.protobuf import wrappers_pb2 

6 

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) 

32 

33 

34@pytest.fixture(autouse=True) 

35def _(testconfig): 

36 pass 

37 

38 

39def test_list_group_chats(db): 

40 user1, token1 = generate_user() 

41 user2, token2 = generate_user() 

42 user3, token3 = generate_user() 

43 

44 make_friends(user2, user1) 

45 make_friends(user1, user3) 

46 

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 

51 

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")) 

63 

64 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq()) 

65 assert len(res.group_chats) == 2 

66 assert res.no_more 

67 

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 

72 

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 

77 

78 

79def test_list_empty_group_chats(db): 

80 user1, token1 = generate_user() 

81 user2, token2 = generate_user() 

82 user3, token3 = generate_user() 

83 

84 make_friends(user1, user3) 

85 make_friends(user2, user1) 

86 make_friends(user2, user3) 

87 

88 with conversations_session(token1) as c: 

89 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq()) 

90 assert len(res.group_chats) == 0 

91 

92 c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id])) 

93 c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id])) 

94 

95 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq()) 

96 assert len(res.group_chats) == 2 

97 assert res.no_more 

98 

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 

103 

104 c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id])) 

105 

106 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq()) 

107 assert len(res.group_chats) == 3 

108 assert res.no_more 

109 

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 

114 

115 

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() 

122 

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) 

129 

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")) 

149 

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")) 

157 

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" 

172 

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 ) 

179 

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" 

189 

190 

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() 

199 

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) 

206 

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")) 

228 

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")) 

236 

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")) 

244 

245 # leave chat 

246 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=left_chat_id)) 

247 

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")) 

251 

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" 

262 

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" 

272 

273 

274def test_get_group_chat_messages(db): 

275 user1, token1 = generate_user() 

276 user2, token2 = generate_user() 

277 user3, token3 = generate_user() 

278 

279 make_friends(user1, user2) 

280 

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")) 

287 

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 

292 

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" 

296 

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 

301 

302 

303def test_get_group_chat_messages_pagination(db): 

304 user1, token1 = generate_user() 

305 user2, token2 = generate_user() 

306 make_friends(user1, user2) 

307 

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))) 

313 

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 

330 

331 

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) 

340 

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 

344 

345 for i in range(10): 

346 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text=str(i))) 

347 

348 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id)) 

349 

350 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="10")) 

351 

352 res = c.GetGroupChatMessages(conversations_pb2.GetGroupChatMessagesReq(group_chat_id=group_chat_id)) 

353 

354 # created + 10 normal + invited + normal 

355 assert len(res.messages) == 13 

356 

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" 

363 

364 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id)) 

365 

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")) 

369 

370 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id)) 

371 

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")) 

374 

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" 

386 

387 

388def test_get_group_chat_info(db): 

389 user1, token1 = generate_user() 

390 user2, token2 = generate_user() 

391 user3, token3 = generate_user() 

392 

393 make_friends(user1, user2) 

394 make_friends(user3, user1) 

395 

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 

410 

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 

418 

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 

427 

428 

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() 

434 

435 make_friends(user1, user2) 

436 make_friends(user3, user1) 

437 

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")) 

447 

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 

452 

453 

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() 

459 

460 make_friends(user1, user2) 

461 make_friends(user3, user1) 

462 make_friends(user1, user4) 

463 

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")) 

473 

474 with conversations_session(token3) as c: 

475 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id)) 

476 

477 with conversations_session(token1) as c: 

478 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user4.id)) 

479 

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 

488 

489 

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) 

497 

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 

507 

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 

511 

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 

518 

519 

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) 

525 

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 

534 

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 

545 

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 

557 

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 

569 

570 

571def test_make_remove_group_chat_admin(db): 

572 user1, token1 = generate_user() 

573 user2, token2 = generate_user() 

574 user3, token3 = generate_user() 

575 

576 make_friends(user1, user2) 

577 make_friends(user1, user3) 

578 make_friends(user2, user3) 

579 

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 

584 

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 

592 

593 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=group_chat_id, user_id=user2.id)) 

594 

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 

600 

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 

605 

606 with conversations_session(token1) as c: 

607 c.RemoveGroupChatAdmin(conversations_pb2.RemoveGroupChatAdminReq(group_chat_id=group_chat_id, user_id=user2.id)) 

608 

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 

612 

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 

618 

619 

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) 

626 

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 

635 

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 

641 

642 

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() 

652 

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) 

662 

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")) 

667 

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 

681 

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 

685 

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)) 

693 

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 

715 

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 

721 

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 ) 

728 

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 

733 

734 

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() 

744 

745 make_friends(user1, user2) 

746 make_friends(user1, user3) 

747 make_friends(user2, user3) 

748 make_friends(user1, user4) 

749 

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")) 

754 

755 with conversations_session(token2) as c: 

756 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 2")) 

757 

758 with conversations_session(token1) as c: 

759 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text="Test message 3")) 

760 

761 c.InviteToGroupChat(conversations_pb2.InviteToGroupChatReq(group_chat_id=group_chat_id, user_id=user3.id)) 

762 

763 with conversations_session(token3) as c: 

764 # should work 

765 c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=group_chat_id)) 

766 

767 res = c.ListGroupChats(conversations_pb2.ListGroupChatsReq()) 

768 assert len(res.group_chats) == 1 

769 

770 

771def test_invite_to_dm(db): 

772 user1, token1 = generate_user() 

773 user2, token2 = generate_user() 

774 user3, token3 = generate_user() 

775 

776 make_friends(user1, user2) 

777 make_friends(user1, user3) 

778 make_friends(user2, user3) 

779 

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")) 

784 

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 

790 

791 

792def test_sole_admin_leaves(db): 

793 user1, token1 = generate_user() 

794 user2, token2 = generate_user() 

795 user3, token3 = generate_user() 

796 

797 make_friends(user1, user2) 

798 make_friends(user1, user3) 

799 make_friends(user2, user3) 

800 

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")) 

805 

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 

811 

812 with conversations_session(token2) as c: 

813 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id)) 

814 

815 with conversations_session(token3) as c: 

816 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id)) 

817 

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)) 

821 

822 

823def test_search_messages(db): 

824 user1, token1 = generate_user() 

825 user2, token2 = generate_user() 

826 user3, token3 = generate_user() 

827 

828 make_friends(user1, user2) 

829 make_friends(user1, user3) 

830 

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")) 

839 

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 

846 

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 

851 

852 

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) 

861 

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))) 

867 

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")) 

871 

872 assert len(res.results) == 11 

873 

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")) 

877 

878 assert len(res.results) == 1 

879 assert res.results[0].message.text.text == "Test message 10" 

880 

881 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=group_chat_id)) 

882 

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")) 

889 

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" 

897 

898 

899def test_admin_behaviour(db): 

900 user1, token1 = generate_user() 

901 user2, token2 = generate_user() 

902 user3, token3 = generate_user() 

903 

904 make_friends(user1, user2) 

905 make_friends(user1, user3) 

906 make_friends(user2, user3) 

907 

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 

917 

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 

929 

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 

937 

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 

943 

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 

955 

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 

962 

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 

971 

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 

977 

978 c.MakeGroupChatAdmin(conversations_pb2.MakeGroupChatAdminReq(group_chat_id=gcid, user_id=user1.id)) 

979 

980 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid)) 

981 

982 with conversations_session(token2) as c: 

983 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid)) 

984 

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)) 

988 

989 

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() 

996 

997 make_friends(user1, user2) 

998 make_friends(user1, user3) 

999 make_friends(user1, user4) 

1000 make_friends(user1, user5) 

1001 

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 

1006 

1007 make_user_invisible(user3.id) 

1008 make_user_block(user1, user4) 

1009 make_user_block(user5, user1) 

1010 

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 

1016 

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 

1022 

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 

1028 

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 

1034 

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 ) 

1045 

1046 for subscription in subscriptions: 

1047 subscription.role = GroupChatRole.admin 

1048 

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 

1055 

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 

1061 

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 

1067 

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 

1073 

1074 

1075def test_last_seen(db): 

1076 user1, token1 = generate_user() 

1077 user2, token2 = generate_user() 

1078 user3, token3 = generate_user() 

1079 

1080 make_friends(user1, user2) 

1081 make_friends(user1, user3) 

1082 make_friends(user2, user3) 

1083 

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 

1089 

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 

1094 

1095 message_ids = [] 

1096 

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 ) 

1105 

1106 message_ids.append( 

1107 c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid)).latest_message.message_id 

1108 ) 

1109 

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 

1113 

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 

1118 

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 ) 

1125 

1126 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid)) 

1127 assert res.unseen_message_count == backward_offset 

1128 

1129 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text="test message ...")) 

1130 

1131 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid)) 

1132 assert res.unseen_message_count == 0 

1133 

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 

1138 

1139 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text="test message ...")) 

1140 

1141 res = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=gcid)) 

1142 assert res.unseen_message_count == 0 

1143 

1144 

1145def test_one_dm_per_pair(db): 

1146 user1, token1 = generate_user() 

1147 user2, token2 = generate_user() 

1148 user3, token3 = generate_user() 

1149 

1150 make_friends(user1, user2) 

1151 make_friends(user1, user3) 

1152 make_friends(user2, user3) 

1153 

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 

1158 

1159 # create DM with user 3 

1160 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id])) 

1161 assert res.is_dm 

1162 

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 

1167 

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 

1172 

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 

1176 

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 

1181 

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 

1186 

1187 

1188def test_GetDirectMessage(db): 

1189 user1, token1 = generate_user() 

1190 user2, token2 = generate_user() 

1191 user3, token3 = generate_user() 

1192 

1193 make_friends(user1, user2) 

1194 make_friends(user1, user3) 

1195 make_friends(user2, user3) 

1196 

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 

1202 

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 

1207 

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 

1212 

1213 # now should exist 

1214 res = c.GetDirectMessage(conversations_pb2.GetDirectMessageReq(user_id=user2.id)) 

1215 assert res.group_chat_id == gcid 

1216 

1217 # create DM with user 3 

1218 res = c.CreateGroupChat(conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user3.id])) 

1219 assert res.is_dm 

1220 

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 

1224 

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 

1230 

1231 # DM with 3 should exist 

1232 res = c.GetDirectMessage(conversations_pb2.GetDirectMessageReq(user_id=user3.id)) 

1233 assert res.group_chat_id == gcid 

1234 

1235 

1236def test_total_unseen(db): 

1237 user1, token1 = generate_user() 

1238 user2, token2 = generate_user() 

1239 user3, token3 = generate_user() 

1240 

1241 # distractions 

1242 user4, token4 = generate_user() 

1243 

1244 make_friends(user1, user2) 

1245 make_friends(user1, user3) 

1246 make_friends(user2, user3) 

1247 

1248 # distractions 

1249 make_friends(user1, user4) 

1250 

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...")) 

1257 

1258 gcid = c.CreateGroupChat( 

1259 conversations_pb2.CreateGroupChatReq(recipient_user_ids=[user2.id, user3.id]) 

1260 ).group_chat_id 

1261 

1262 for i in range(6): 

1263 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid, text=f"test message {i}")) 

1264 

1265 # distractions 

1266 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction...")) 

1267 

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 

1271 

1272 with api_session(token2) as api: 

1273 # chat created + 6 normal messages 

1274 assert api.Ping(api_pb2.PingReq()).unseen_message_count == 7 

1275 

1276 # now leave chat with user2 

1277 with conversations_session(token2) as c: 

1278 c.LeaveGroupChat(conversations_pb2.LeaveGroupChatReq(group_chat_id=gcid)) 

1279 

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 

1283 

1284 with conversations_session(token1) as c: 

1285 # distractions 

1286 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction...")) 

1287 

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}")) 

1291 

1292 # distractions 

1293 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction...")) 

1294 

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 

1298 

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)) 

1302 

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}")) 

1306 

1307 # distractions 

1308 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=gcid_distraction, text="distraction...")) 

1309 

1310 with api_session(token2) as api: 

1311 # joined + 12 normal 

1312 assert api.Ping(api_pb2.PingReq()).unseen_message_count == 13 

1313 

1314 

1315def test_regression_ListGroupChats_pagination(db): 

1316 user1, token1 = generate_user() 

1317 user2, token2 = generate_user() 

1318 user3, token3 = generate_user() 

1319 

1320 make_friends(user1, user2) 

1321 make_friends(user1, user3) 

1322 

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 ) 

1332 

1333 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=res1.group_chat_id, text=f"Test message {i}")) 

1334 

1335 res2 = c.GetGroupChat(conversations_pb2.GetGroupChatReq(group_chat_id=res1.group_chat_id)) 

1336 

1337 group_chat_and_message_ids.append((res2.group_chat_id, res2.latest_message.message_id)) 

1338 

1339 seen_group_chat_ids = [] 

1340 

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 

1347 

1348 seen_group_chat_ids.extend([chat.group_chat_id for chat in res.group_chats]) 

1349 

1350 assert set(seen_group_chat_ids) == {x[0] for x in group_chat_and_message_ids}, "Not all group chats returned" 

1351 

1352 

1353def test_muting(db): 

1354 user1, token1 = generate_user() 

1355 user2, token2 = generate_user() 

1356 user3, token3 = generate_user() 

1357 

1358 make_friends(user1, user2) 

1359 make_friends(user1, user3) 

1360 make_friends(user2, user3) 

1361 

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 

1367 

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 

1372 

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") 

1377 

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") 

1382 

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") 

1387 

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) 

1398 

1399 

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() 

1408 

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) 

1414 

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 ) 

1439 

1440 group_chat_id = None 

1441 

1442 def send_msg(c, i): 

1443 c.SendMessage(conversations_pb2.SendMessageReq(group_chat_id=group_chat_id, text=f"Test message {i}")) 

1444 

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) 

1455 

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) 

1462 

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)) 

1466 

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)) 

1470 

1471 with conversations_session(token6) as c: 

1472 send_msg(c, i=8) 

1473 

1474 # go through all bg jobs 

1475 while process_job(): 

1476 pass 

1477 

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 ] 

1487 

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 ) 

1502 

1503 def parse_message_payload(data): 

1504 return notification_data_pb2.ChatMessage.FromString(data).text 

1505 

1506 contents = [parse_message_payload(d) for d in deliv] 

1507 

1508 print(contents) 

1509 

1510 assert [f"Test message {i}" for i in expected_msgs] == contents, f"Wrong messages for {label}"