test_roommember.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.api.constants import Membership
  17. from synapse.rest.admin import register_servlets_for_client_rest_resource
  18. from synapse.rest.client import login, room
  19. from synapse.server import HomeServer
  20. from synapse.types import UserID, create_requester
  21. from synapse.util import Clock
  22. from tests import unittest
  23. from tests.server import TestHomeServer
  24. from tests.test_utils import event_injection
  25. class RoomMemberStoreTestCase(unittest.HomeserverTestCase):
  26. servlets = [
  27. login.register_servlets,
  28. register_servlets_for_client_rest_resource,
  29. room.register_servlets,
  30. ]
  31. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: TestHomeServer) -> None: # type: ignore[override]
  32. # We can't test the RoomMemberStore on its own without the other event
  33. # storage logic
  34. self.store = hs.get_datastores().main
  35. self.u_alice = self.register_user("alice", "pass")
  36. self.t_alice = self.login("alice", "pass")
  37. self.u_bob = self.register_user("bob", "pass")
  38. # User elsewhere on another host
  39. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  40. def test_one_member(self) -> None:
  41. # Alice creates the room, and is automatically joined
  42. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  43. rooms_for_user = self.get_success(
  44. self.store.get_rooms_for_local_user_where_membership_is(
  45. self.u_alice, [Membership.JOIN]
  46. )
  47. )
  48. self.assertEqual([self.room], [m.room_id for m in rooms_for_user])
  49. def test_count_known_servers(self) -> None:
  50. """
  51. _count_known_servers will calculate how many servers are in a room.
  52. """
  53. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  54. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  55. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  56. servers = self.get_success(self.store._count_known_servers())
  57. self.assertEqual(servers, 2)
  58. def test_count_known_servers_stat_counter_disabled(self) -> None:
  59. """
  60. If enabled, the metrics for how many servers are known will be counted.
  61. """
  62. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  63. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  64. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  65. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  66. self.pump()
  67. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  68. @unittest.override_config(
  69. {"enable_metrics": True, "metrics_flags": {"known_servers": True}}
  70. )
  71. def test_count_known_servers_stat_counter_enabled(self) -> None:
  72. """
  73. If enabled, the metrics for how many servers are known will be counted.
  74. """
  75. # Initialises to 1 -- itself
  76. self.assertEqual(self.store._known_servers_count, 1)
  77. self.pump()
  78. # No rooms have been joined, so technically the SQL returns 0, but it
  79. # will still say it knows about itself.
  80. self.assertEqual(self.store._known_servers_count, 1)
  81. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  82. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  83. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  84. self.pump(1)
  85. # It now knows about Charlie's server.
  86. self.assertEqual(self.store._known_servers_count, 2)
  87. def test__null_byte_in_display_name_properly_handled(self) -> None:
  88. room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  89. res = self.get_success(
  90. self.store.db_pool.simple_select_list(
  91. "room_memberships",
  92. {"user_id": "@alice:test"},
  93. ["display_name", "event_id"],
  94. )
  95. )
  96. # Check that we only got one result back
  97. self.assertEqual(len(res), 1)
  98. # Check that alice's display name is "alice"
  99. self.assertEqual(res[0]["display_name"], "alice")
  100. # Grab the event_id to use later
  101. event_id = res[0]["event_id"]
  102. # Create a profile with the offending null byte in the display name
  103. new_profile = {"displayname": "ali\u0000ce"}
  104. # Ensure that the change goes smoothly and does not fail due to the null byte
  105. self.helper.change_membership(
  106. room,
  107. self.u_alice,
  108. self.u_alice,
  109. "join",
  110. extra_data=new_profile,
  111. tok=self.t_alice,
  112. )
  113. res2 = self.get_success(
  114. self.store.db_pool.simple_select_list(
  115. "room_memberships",
  116. {"user_id": "@alice:test"},
  117. ["display_name", "event_id"],
  118. )
  119. )
  120. # Check that we only have two results
  121. self.assertEqual(len(res2), 2)
  122. # Filter out the previous event using the event_id we grabbed above
  123. row = [row for row in res2 if row["event_id"] != event_id]
  124. # Check that alice's display name is now None
  125. self.assertEqual(row[0]["display_name"], None)
  126. def test_room_is_locally_forgotten(self) -> None:
  127. """Test that when the last local user has forgotten a room it is known as forgotten."""
  128. # join two local and one remote user
  129. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  130. self.get_success(
  131. event_injection.inject_member_event(self.hs, self.room, self.u_bob, "join")
  132. )
  133. self.get_success(
  134. event_injection.inject_member_event(
  135. self.hs, self.room, self.u_charlie.to_string(), "join"
  136. )
  137. )
  138. self.assertFalse(
  139. self.get_success(self.store.is_locally_forgotten_room(self.room))
  140. )
  141. # local users leave the room and the room is not forgotten
  142. self.get_success(
  143. event_injection.inject_member_event(
  144. self.hs, self.room, self.u_alice, "leave"
  145. )
  146. )
  147. self.get_success(
  148. event_injection.inject_member_event(self.hs, self.room, self.u_bob, "leave")
  149. )
  150. self.assertFalse(
  151. self.get_success(self.store.is_locally_forgotten_room(self.room))
  152. )
  153. # first user forgets the room, room is not forgotten
  154. self.get_success(self.store.forget(self.u_alice, self.room))
  155. self.assertFalse(
  156. self.get_success(self.store.is_locally_forgotten_room(self.room))
  157. )
  158. # second (last local) user forgets the room and the room is forgotten
  159. self.get_success(self.store.forget(self.u_bob, self.room))
  160. self.assertTrue(
  161. self.get_success(self.store.is_locally_forgotten_room(self.room))
  162. )
  163. def test_join_locally_forgotten_room(self) -> None:
  164. """Tests if a user joins a forgotten room the room is not forgotten anymore."""
  165. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  166. self.assertFalse(
  167. self.get_success(self.store.is_locally_forgotten_room(self.room))
  168. )
  169. # after leaving and forget the room, it is forgotten
  170. self.get_success(
  171. event_injection.inject_member_event(
  172. self.hs, self.room, self.u_alice, "leave"
  173. )
  174. )
  175. self.get_success(self.store.forget(self.u_alice, self.room))
  176. self.assertTrue(
  177. self.get_success(self.store.is_locally_forgotten_room(self.room))
  178. )
  179. # after rejoin the room is not forgotten anymore
  180. self.get_success(
  181. event_injection.inject_member_event(
  182. self.hs, self.room, self.u_alice, "join"
  183. )
  184. )
  185. self.assertFalse(
  186. self.get_success(self.store.is_locally_forgotten_room(self.room))
  187. )
  188. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  189. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  190. self.store = hs.get_datastores().main
  191. self.room_creator = hs.get_room_creation_handler()
  192. def test_can_rerun_update(self) -> None:
  193. # First make sure we have completed all updates.
  194. self.wait_for_background_updates()
  195. # Now let's create a room, which will insert a membership
  196. user = UserID("alice", "test")
  197. requester = create_requester(user)
  198. self.get_success(self.room_creator.create_room(requester, {}))
  199. # Register the background update to run again.
  200. self.get_success(
  201. self.store.db_pool.simple_insert(
  202. table="background_updates",
  203. values={
  204. "update_name": "current_state_events_membership",
  205. "progress_json": "{}",
  206. "depends_on": None,
  207. },
  208. )
  209. )
  210. # ... and tell the DataStore that it hasn't finished all updates yet
  211. self.store.db_pool.updates._all_done = False
  212. # Now let's actually drive the updates to completion
  213. self.wait_for_background_updates()