|
39 | 39 | from synapse.rest import admin |
40 | 40 | from synapse.rest.client import login, room |
41 | 41 | from synapse.server import HomeServer |
42 | | -from synapse.types import StreamToken, create_requester |
| 42 | +from synapse.types import StreamToken, UserID, UserInfo, create_requester |
43 | 43 | from synapse.util import Clock |
44 | 44 |
|
45 | 45 | from tests.handlers.test_sync import generate_sync_config |
@@ -349,6 +349,169 @@ def test_accept_invite_local_user( |
349 | 349 | join_updates, _ = sync_join(self, invited_user_id) |
350 | 350 | self.assertEqual(len(join_updates), 0) |
351 | 351 |
|
| 352 | + @override_config( |
| 353 | + { |
| 354 | + "auto_accept_invites": { |
| 355 | + "enabled": True, |
| 356 | + }, |
| 357 | + } |
| 358 | + ) |
| 359 | + async def test_ignore_invite_for_missing_user(self) -> None: |
| 360 | + """Tests that receiving an invite for a missing user is ignored.""" |
| 361 | + inviting_user_id = self.register_user("inviter", "pass") |
| 362 | + inviting_user_tok = self.login("inviter", "pass") |
| 363 | + |
| 364 | + # A local user who receives an invite |
| 365 | + invited_user_id = "@fake:" + self.hs.config.server.server_name |
| 366 | + |
| 367 | + # Create a room and send an invite to the other user |
| 368 | + room_id = self.helper.create_room_as( |
| 369 | + inviting_user_id, |
| 370 | + tok=inviting_user_tok, |
| 371 | + ) |
| 372 | + |
| 373 | + self.helper.invite( |
| 374 | + room_id, |
| 375 | + inviting_user_id, |
| 376 | + invited_user_id, |
| 377 | + tok=inviting_user_tok, |
| 378 | + ) |
| 379 | + |
| 380 | + join_updates, _ = sync_join(self, inviting_user_id) |
| 381 | + # Assert that the last event in the room was not a member event for the target user. |
| 382 | + self.assertEqual( |
| 383 | + join_updates[0].timeline.events[-1].content["membership"], "invite" |
| 384 | + ) |
| 385 | + |
| 386 | + @override_config( |
| 387 | + { |
| 388 | + "auto_accept_invites": { |
| 389 | + "enabled": True, |
| 390 | + }, |
| 391 | + } |
| 392 | + ) |
| 393 | + async def test_ignore_invite_for_deactivated_user(self) -> None: |
| 394 | + """Tests that receiving an invite for a deactivated user is ignored.""" |
| 395 | + inviting_user_id = self.register_user("inviter", "pass", admin=True) |
| 396 | + inviting_user_tok = self.login("inviter", "pass") |
| 397 | + |
| 398 | + # A local user who receives an invite |
| 399 | + invited_user_id = self.register_user("invitee", "pass") |
| 400 | + |
| 401 | + # Create a room and send an invite to the other user |
| 402 | + room_id = self.helper.create_room_as( |
| 403 | + inviting_user_id, |
| 404 | + tok=inviting_user_tok, |
| 405 | + ) |
| 406 | + |
| 407 | + channel = self.make_request( |
| 408 | + "PUT", |
| 409 | + "/_synapse/admin/v2/users/%s" % invited_user_id, |
| 410 | + {"deactivated": True}, |
| 411 | + access_token=inviting_user_tok, |
| 412 | + ) |
| 413 | + |
| 414 | + assert channel.code == 200 |
| 415 | + |
| 416 | + self.helper.invite( |
| 417 | + room_id, |
| 418 | + inviting_user_id, |
| 419 | + invited_user_id, |
| 420 | + tok=inviting_user_tok, |
| 421 | + ) |
| 422 | + |
| 423 | + join_updates, b = sync_join(self, inviting_user_id) |
| 424 | + # Assert that the last event in the room was not a member event for the target user. |
| 425 | + self.assertEqual( |
| 426 | + join_updates[0].timeline.events[-1].content["membership"], "invite" |
| 427 | + ) |
| 428 | + |
| 429 | + @override_config( |
| 430 | + { |
| 431 | + "auto_accept_invites": { |
| 432 | + "enabled": True, |
| 433 | + }, |
| 434 | + } |
| 435 | + ) |
| 436 | + async def test_ignore_invite_for_suspended_user(self) -> None: |
| 437 | + """Tests that receiving an invite for a suspended user is ignored.""" |
| 438 | + inviting_user_id = self.register_user("inviter", "pass", admin=True) |
| 439 | + inviting_user_tok = self.login("inviter", "pass") |
| 440 | + |
| 441 | + # A local user who receives an invite |
| 442 | + invited_user_id = self.register_user("invitee", "pass") |
| 443 | + |
| 444 | + # Create a room and send an invite to the other user |
| 445 | + room_id = self.helper.create_room_as( |
| 446 | + inviting_user_id, |
| 447 | + tok=inviting_user_tok, |
| 448 | + ) |
| 449 | + |
| 450 | + channel = self.make_request( |
| 451 | + "PUT", |
| 452 | + f"/_synapse/admin/v1/suspend/{invited_user_id}", |
| 453 | + {"suspend": True}, |
| 454 | + access_token=inviting_user_tok, |
| 455 | + ) |
| 456 | + |
| 457 | + assert channel.code == 200 |
| 458 | + |
| 459 | + self.helper.invite( |
| 460 | + room_id, |
| 461 | + inviting_user_id, |
| 462 | + invited_user_id, |
| 463 | + tok=inviting_user_tok, |
| 464 | + ) |
| 465 | + |
| 466 | + join_updates, b = sync_join(self, inviting_user_id) |
| 467 | + # Assert that the last event in the room was not a member event for the target user. |
| 468 | + self.assertEqual( |
| 469 | + join_updates[0].timeline.events[-1].content["membership"], "invite" |
| 470 | + ) |
| 471 | + |
| 472 | + @override_config( |
| 473 | + { |
| 474 | + "auto_accept_invites": { |
| 475 | + "enabled": True, |
| 476 | + }, |
| 477 | + } |
| 478 | + ) |
| 479 | + async def test_ignore_invite_for_locked_user(self) -> None: |
| 480 | + """Tests that receiving an invite for a suspended user is ignored.""" |
| 481 | + inviting_user_id = self.register_user("inviter", "pass", admin=True) |
| 482 | + inviting_user_tok = self.login("inviter", "pass") |
| 483 | + |
| 484 | + # A local user who receives an invite |
| 485 | + invited_user_id = self.register_user("invitee", "pass") |
| 486 | + |
| 487 | + # Create a room and send an invite to the other user |
| 488 | + room_id = self.helper.create_room_as( |
| 489 | + inviting_user_id, |
| 490 | + tok=inviting_user_tok, |
| 491 | + ) |
| 492 | + |
| 493 | + channel = self.make_request( |
| 494 | + "PUT", |
| 495 | + f"/_synapse/admin/v2/users/{invited_user_id}", |
| 496 | + {"locked": True}, |
| 497 | + access_token=inviting_user_tok, |
| 498 | + ) |
| 499 | + |
| 500 | + assert channel.code == 200 |
| 501 | + |
| 502 | + self.helper.invite( |
| 503 | + room_id, |
| 504 | + inviting_user_id, |
| 505 | + invited_user_id, |
| 506 | + tok=inviting_user_tok, |
| 507 | + ) |
| 508 | + |
| 509 | + join_updates, b = sync_join(self, inviting_user_id) |
| 510 | + # Assert that the last event in the room was not a member event for the target user. |
| 511 | + self.assertEqual( |
| 512 | + join_updates[0].timeline.events[-1].content["membership"], "invite" |
| 513 | + ) |
| 514 | + |
352 | 515 |
|
353 | 516 | _request_key = 0 |
354 | 517 |
|
@@ -647,6 +810,22 @@ def create_module( |
647 | 810 | module_api.is_mine.side_effect = lambda a: a.split(":")[1] == "test" |
648 | 811 | module_api.worker_name = worker_name |
649 | 812 | module_api.sleep.return_value = make_multiple_awaitable(None) |
| 813 | + module_api.get_userinfo_by_id.return_value = UserInfo( |
| 814 | + user_id=UserID.from_string("@user:test"), |
| 815 | + is_admin=False, |
| 816 | + is_guest=False, |
| 817 | + consent_server_notice_sent=None, |
| 818 | + consent_ts=None, |
| 819 | + consent_version=None, |
| 820 | + appservice_id=None, |
| 821 | + creation_ts=0, |
| 822 | + user_type=None, |
| 823 | + is_deactivated=False, |
| 824 | + locked=False, |
| 825 | + is_shadow_banned=False, |
| 826 | + approved=True, |
| 827 | + suspended=False, |
| 828 | + ) |
650 | 829 |
|
651 | 830 | if config_override is None: |
652 | 831 | config_override = {} |
|
0 commit comments