test_roommember.py 8.7 KB

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