test_shared_rooms.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. import synapse.rest.admin
  15. from synapse.rest.client import login, room, shared_rooms
  16. from tests import unittest
  17. from tests.server import FakeChannel
  18. class UserSharedRoomsTest(unittest.HomeserverTestCase):
  19. """
  20. Tests the UserSharedRoomsServlet.
  21. """
  22. servlets = [
  23. login.register_servlets,
  24. synapse.rest.admin.register_servlets_for_client_rest_resource,
  25. room.register_servlets,
  26. shared_rooms.register_servlets,
  27. ]
  28. def make_homeserver(self, reactor, clock):
  29. config = self.default_config()
  30. config["update_user_directory"] = True
  31. return self.setup_test_homeserver(config=config)
  32. def prepare(self, reactor, clock, hs):
  33. self.store = hs.get_datastore()
  34. self.handler = hs.get_user_directory_handler()
  35. def _get_shared_rooms(self, token, other_user) -> FakeChannel:
  36. return self.make_request(
  37. "GET",
  38. "/_matrix/client/unstable/uk.half-shot.msc2666/user/shared_rooms/%s"
  39. % other_user,
  40. access_token=token,
  41. )
  42. def test_shared_room_list_public(self):
  43. """
  44. A room should show up in the shared list of rooms between two users
  45. if it is public.
  46. """
  47. self._check_shared_rooms_with(room_one_is_public=True, room_two_is_public=True)
  48. def test_shared_room_list_private(self):
  49. """
  50. A room should show up in the shared list of rooms between two users
  51. if it is private.
  52. """
  53. self._check_shared_rooms_with(
  54. room_one_is_public=False, room_two_is_public=False
  55. )
  56. def test_shared_room_list_mixed(self):
  57. """
  58. The shared room list between two users should contain both public and private
  59. rooms.
  60. """
  61. self._check_shared_rooms_with(room_one_is_public=True, room_two_is_public=False)
  62. def _check_shared_rooms_with(
  63. self, room_one_is_public: bool, room_two_is_public: bool
  64. ):
  65. """Checks that shared public or private rooms between two users appear in
  66. their shared room lists
  67. """
  68. u1 = self.register_user("user1", "pass")
  69. u1_token = self.login(u1, "pass")
  70. u2 = self.register_user("user2", "pass")
  71. u2_token = self.login(u2, "pass")
  72. # Create a room. user1 invites user2, who joins
  73. room_id_one = self.helper.create_room_as(
  74. u1, is_public=room_one_is_public, tok=u1_token
  75. )
  76. self.helper.invite(room_id_one, src=u1, targ=u2, tok=u1_token)
  77. self.helper.join(room_id_one, user=u2, tok=u2_token)
  78. # Check shared rooms from user1's perspective.
  79. # We should see the one room in common
  80. channel = self._get_shared_rooms(u1_token, u2)
  81. self.assertEquals(200, channel.code, channel.result)
  82. self.assertEquals(len(channel.json_body["joined"]), 1)
  83. self.assertEquals(channel.json_body["joined"][0], room_id_one)
  84. # Create another room and invite user2 to it
  85. room_id_two = self.helper.create_room_as(
  86. u1, is_public=room_two_is_public, tok=u1_token
  87. )
  88. self.helper.invite(room_id_two, src=u1, targ=u2, tok=u1_token)
  89. self.helper.join(room_id_two, user=u2, tok=u2_token)
  90. # Check shared rooms again. We should now see both rooms.
  91. channel = self._get_shared_rooms(u1_token, u2)
  92. self.assertEquals(200, channel.code, channel.result)
  93. self.assertEquals(len(channel.json_body["joined"]), 2)
  94. for room_id_id in channel.json_body["joined"]:
  95. self.assertIn(room_id_id, [room_id_one, room_id_two])
  96. def test_shared_room_list_after_leave(self):
  97. """
  98. A room should no longer be considered shared if the other
  99. user has left it.
  100. """
  101. u1 = self.register_user("user1", "pass")
  102. u1_token = self.login(u1, "pass")
  103. u2 = self.register_user("user2", "pass")
  104. u2_token = self.login(u2, "pass")
  105. room = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
  106. self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
  107. self.helper.join(room, user=u2, tok=u2_token)
  108. # Assert user directory is not empty
  109. channel = self._get_shared_rooms(u1_token, u2)
  110. self.assertEquals(200, channel.code, channel.result)
  111. self.assertEquals(len(channel.json_body["joined"]), 1)
  112. self.assertEquals(channel.json_body["joined"][0], room)
  113. self.helper.leave(room, user=u1, tok=u1_token)
  114. # Check user1's view of shared rooms with user2
  115. channel = self._get_shared_rooms(u1_token, u2)
  116. self.assertEquals(200, channel.code, channel.result)
  117. self.assertEquals(len(channel.json_body["joined"]), 0)
  118. # Check user2's view of shared rooms with user1
  119. channel = self._get_shared_rooms(u2_token, u1)
  120. self.assertEquals(200, channel.code, channel.result)
  121. self.assertEquals(len(channel.json_body["joined"]), 0)