test_shared_rooms.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 Half-Shot
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import synapse.rest.admin
  16. from synapse.rest.client.v1 import login, room
  17. from synapse.rest.client.v2_alpha import shared_rooms
  18. from tests import unittest
  19. from tests.server import FakeChannel
  20. class UserSharedRoomsTest(unittest.HomeserverTestCase):
  21. """
  22. Tests the UserSharedRoomsServlet.
  23. """
  24. servlets = [
  25. login.register_servlets,
  26. synapse.rest.admin.register_servlets_for_client_rest_resource,
  27. room.register_servlets,
  28. shared_rooms.register_servlets,
  29. ]
  30. def make_homeserver(self, reactor, clock):
  31. config = self.default_config()
  32. config["update_user_directory"] = True
  33. return self.setup_test_homeserver(config=config)
  34. def prepare(self, reactor, clock, hs):
  35. self.store = hs.get_datastore()
  36. self.handler = hs.get_user_directory_handler()
  37. def _get_shared_rooms(self, token, other_user) -> FakeChannel:
  38. return self.make_request(
  39. "GET",
  40. "/_matrix/client/unstable/uk.half-shot.msc2666/user/shared_rooms/%s"
  41. % other_user,
  42. access_token=token,
  43. )
  44. def test_shared_room_list_public(self):
  45. """
  46. A room should show up in the shared list of rooms between two users
  47. if it is public.
  48. """
  49. u1 = self.register_user("user1", "pass")
  50. u1_token = self.login(u1, "pass")
  51. u2 = self.register_user("user2", "pass")
  52. u2_token = self.login(u2, "pass")
  53. room = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
  54. self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
  55. self.helper.join(room, user=u2, tok=u2_token)
  56. channel = self._get_shared_rooms(u1_token, u2)
  57. self.assertEquals(200, channel.code, channel.result)
  58. self.assertEquals(len(channel.json_body["joined"]), 1)
  59. self.assertEquals(channel.json_body["joined"][0], room)
  60. def test_shared_room_list_private(self):
  61. """
  62. A room should show up in the shared list of rooms between two users
  63. if it is private.
  64. """
  65. u1 = self.register_user("user1", "pass")
  66. u1_token = self.login(u1, "pass")
  67. u2 = self.register_user("user2", "pass")
  68. u2_token = self.login(u2, "pass")
  69. room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
  70. self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
  71. self.helper.join(room, user=u2, tok=u2_token)
  72. channel = self._get_shared_rooms(u1_token, u2)
  73. self.assertEquals(200, channel.code, channel.result)
  74. self.assertEquals(len(channel.json_body["joined"]), 1)
  75. self.assertEquals(channel.json_body["joined"][0], room)
  76. def test_shared_room_list_mixed(self):
  77. """
  78. The shared room list between two users should contain both public and private
  79. rooms.
  80. """
  81. u1 = self.register_user("user1", "pass")
  82. u1_token = self.login(u1, "pass")
  83. u2 = self.register_user("user2", "pass")
  84. u2_token = self.login(u2, "pass")
  85. room_public = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
  86. room_private = self.helper.create_room_as(u2, is_public=False, tok=u2_token)
  87. self.helper.invite(room_public, src=u1, targ=u2, tok=u1_token)
  88. self.helper.invite(room_private, src=u2, targ=u1, tok=u2_token)
  89. self.helper.join(room_public, user=u2, tok=u2_token)
  90. self.helper.join(room_private, user=u1, tok=u1_token)
  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. self.assertTrue(room_public in channel.json_body["joined"])
  95. self.assertTrue(room_private in channel.json_body["joined"])
  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. channel = self._get_shared_rooms(u2_token, u1)
  115. self.assertEquals(200, channel.code, channel.result)
  116. self.assertEquals(len(channel.json_body["joined"]), 0)