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