test_mutual_rooms.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright 2020 Half-Shot
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from twisted.test.proto_helpers import MemoryReactor
  15. import synapse.rest.admin
  16. from synapse.rest.client import login, mutual_rooms, room
  17. from synapse.server import HomeServer
  18. from synapse.util import Clock
  19. from tests import unittest
  20. from tests.server import FakeChannel
  21. class UserMutualRoomsTest(unittest.HomeserverTestCase):
  22. """
  23. Tests the UserMutualRoomsServlet.
  24. """
  25. servlets = [
  26. login.register_servlets,
  27. synapse.rest.admin.register_servlets_for_client_rest_resource,
  28. room.register_servlets,
  29. mutual_rooms.register_servlets,
  30. ]
  31. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  32. config = self.default_config()
  33. config["update_user_directory"] = True
  34. return self.setup_test_homeserver(config=config)
  35. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  36. self.store = hs.get_datastores().main
  37. self.handler = hs.get_user_directory_handler()
  38. def _get_mutual_rooms(self, token: str, other_user: str) -> FakeChannel:
  39. return self.make_request(
  40. "GET",
  41. "/_matrix/client/unstable/uk.half-shot.msc2666/user/mutual_rooms/%s"
  42. % other_user,
  43. access_token=token,
  44. )
  45. def test_shared_room_list_public(self) -> None:
  46. """
  47. A room should show up in the shared list of rooms between two users
  48. if it is public.
  49. """
  50. self._check_mutual_rooms_with(room_one_is_public=True, room_two_is_public=True)
  51. def test_shared_room_list_private(self) -> None:
  52. """
  53. A room should show up in the shared list of rooms between two users
  54. if it is private.
  55. """
  56. self._check_mutual_rooms_with(
  57. room_one_is_public=False, room_two_is_public=False
  58. )
  59. def test_shared_room_list_mixed(self) -> None:
  60. """
  61. The shared room list between two users should contain both public and private
  62. rooms.
  63. """
  64. self._check_mutual_rooms_with(room_one_is_public=True, room_two_is_public=False)
  65. def _check_mutual_rooms_with(
  66. self, room_one_is_public: bool, room_two_is_public: bool
  67. ) -> None:
  68. """Checks that shared public or private rooms between two users appear in
  69. their shared room lists
  70. """
  71. u1 = self.register_user("user1", "pass")
  72. u1_token = self.login(u1, "pass")
  73. u2 = self.register_user("user2", "pass")
  74. u2_token = self.login(u2, "pass")
  75. # Create a room. user1 invites user2, who joins
  76. room_id_one = self.helper.create_room_as(
  77. u1, is_public=room_one_is_public, tok=u1_token
  78. )
  79. self.helper.invite(room_id_one, src=u1, targ=u2, tok=u1_token)
  80. self.helper.join(room_id_one, user=u2, tok=u2_token)
  81. # Check shared rooms from user1's perspective.
  82. # We should see the one room in common
  83. channel = self._get_mutual_rooms(u1_token, u2)
  84. self.assertEqual(200, channel.code, channel.result)
  85. self.assertEqual(len(channel.json_body["joined"]), 1)
  86. self.assertEqual(channel.json_body["joined"][0], room_id_one)
  87. # Create another room and invite user2 to it
  88. room_id_two = self.helper.create_room_as(
  89. u1, is_public=room_two_is_public, tok=u1_token
  90. )
  91. self.helper.invite(room_id_two, src=u1, targ=u2, tok=u1_token)
  92. self.helper.join(room_id_two, user=u2, tok=u2_token)
  93. # Check shared rooms again. We should now see both rooms.
  94. channel = self._get_mutual_rooms(u1_token, u2)
  95. self.assertEqual(200, channel.code, channel.result)
  96. self.assertEqual(len(channel.json_body["joined"]), 2)
  97. for room_id_id in channel.json_body["joined"]:
  98. self.assertIn(room_id_id, [room_id_one, room_id_two])
  99. def test_shared_room_list_after_leave(self) -> None:
  100. """
  101. A room should no longer be considered shared if the other
  102. user has left it.
  103. """
  104. u1 = self.register_user("user1", "pass")
  105. u1_token = self.login(u1, "pass")
  106. u2 = self.register_user("user2", "pass")
  107. u2_token = self.login(u2, "pass")
  108. room = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
  109. self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
  110. self.helper.join(room, user=u2, tok=u2_token)
  111. # Assert user directory is not empty
  112. channel = self._get_mutual_rooms(u1_token, u2)
  113. self.assertEqual(200, channel.code, channel.result)
  114. self.assertEqual(len(channel.json_body["joined"]), 1)
  115. self.assertEqual(channel.json_body["joined"][0], room)
  116. self.helper.leave(room, user=u1, tok=u1_token)
  117. # Check user1's view of shared rooms with user2
  118. channel = self._get_mutual_rooms(u1_token, u2)
  119. self.assertEqual(200, channel.code, channel.result)
  120. self.assertEqual(len(channel.json_body["joined"]), 0)
  121. # Check user2's view of shared rooms with user1
  122. channel = self._get_mutual_rooms(u2_token, u1)
  123. self.assertEqual(200, channel.code, channel.result)
  124. self.assertEqual(len(channel.json_body["joined"]), 0)