1
0

test_mutual_rooms.py 5.6 KB

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