1
0

test_roommember.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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:
  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_get_joined_users_from_context(self) -> None:
  88. room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  89. bob_event = self.get_success(
  90. event_injection.inject_member_event(
  91. self.hs, room, self.u_bob, Membership.JOIN
  92. )
  93. )
  94. # first, create a regular event
  95. event, context = self.get_success(
  96. event_injection.create_event(
  97. self.hs,
  98. room_id=room,
  99. sender=self.u_alice,
  100. prev_event_ids=[bob_event.event_id],
  101. type="m.test.1",
  102. content={},
  103. )
  104. )
  105. users = self.get_success(
  106. self.store.get_joined_users_from_context(event, context)
  107. )
  108. self.assertEqual(users.keys(), {self.u_alice, self.u_bob})
  109. # Regression test for #7376: create a state event whose key matches bob's
  110. # user_id, but which is *not* a membership event, and persist that; then check
  111. # that `get_joined_users_from_context` returns the correct users for the next event.
  112. non_member_event = self.get_success(
  113. event_injection.inject_event(
  114. self.hs,
  115. room_id=room,
  116. sender=self.u_bob,
  117. prev_event_ids=[bob_event.event_id],
  118. type="m.test.2",
  119. state_key=self.u_bob,
  120. content={},
  121. )
  122. )
  123. event, context = self.get_success(
  124. event_injection.create_event(
  125. self.hs,
  126. room_id=room,
  127. sender=self.u_alice,
  128. prev_event_ids=[non_member_event.event_id],
  129. type="m.test.3",
  130. content={},
  131. )
  132. )
  133. users = self.get_success(
  134. self.store.get_joined_users_from_context(event, context)
  135. )
  136. self.assertEqual(users.keys(), {self.u_alice, self.u_bob})
  137. def test__null_byte_in_display_name_properly_handled(self) -> None:
  138. room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  139. res = self.get_success(
  140. self.store.db_pool.simple_select_list(
  141. "room_memberships",
  142. {"user_id": "@alice:test"},
  143. ["display_name", "event_id"],
  144. )
  145. )
  146. # Check that we only got one result back
  147. self.assertEqual(len(res), 1)
  148. # Check that alice's display name is "alice"
  149. self.assertEqual(res[0]["display_name"], "alice")
  150. # Grab the event_id to use later
  151. event_id = res[0]["event_id"]
  152. # Create a profile with the offending null byte in the display name
  153. new_profile = {"displayname": "ali\u0000ce"}
  154. # Ensure that the change goes smoothly and does not fail due to the null byte
  155. self.helper.change_membership(
  156. room,
  157. self.u_alice,
  158. self.u_alice,
  159. "join",
  160. extra_data=new_profile,
  161. tok=self.t_alice,
  162. )
  163. res2 = self.get_success(
  164. self.store.db_pool.simple_select_list(
  165. "room_memberships",
  166. {"user_id": "@alice:test"},
  167. ["display_name", "event_id"],
  168. )
  169. )
  170. # Check that we only have two results
  171. self.assertEqual(len(res2), 2)
  172. # Filter out the previous event using the event_id we grabbed above
  173. row = [row for row in res2 if row["event_id"] != event_id]
  174. # Check that alice's display name is now None
  175. self.assertEqual(row[0]["display_name"], None)
  176. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  177. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  178. self.store = hs.get_datastores().main
  179. self.room_creator = hs.get_room_creation_handler()
  180. def test_can_rerun_update(self) -> None:
  181. # First make sure we have completed all updates.
  182. self.wait_for_background_updates()
  183. # Now let's create a room, which will insert a membership
  184. user = UserID("alice", "test")
  185. requester = create_requester(user)
  186. self.get_success(self.room_creator.create_room(requester, {}))
  187. # Register the background update to run again.
  188. self.get_success(
  189. self.store.db_pool.simple_insert(
  190. table="background_updates",
  191. values={
  192. "update_name": "current_state_events_membership",
  193. "progress_json": "{}",
  194. "depends_on": None,
  195. },
  196. )
  197. )
  198. # ... and tell the DataStore that it hasn't finished all updates yet
  199. self.store.db_pool.updates._all_done = False
  200. # Now let's actually drive the updates to completion
  201. self.wait_for_background_updates()