test_roommember.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.v1 import login, room
  18. from synapse.types import UserID, create_requester
  19. from tests import unittest
  20. from tests.test_utils import event_injection
  21. from tests.utils import TestHomeServer
  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. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  135. def prepare(self, reactor, clock, homeserver):
  136. self.store = homeserver.get_datastore()
  137. self.room_creator = homeserver.get_room_creation_handler()
  138. def test_can_rerun_update(self):
  139. # First make sure we have completed all updates.
  140. while not self.get_success(
  141. self.store.db_pool.updates.has_completed_background_updates()
  142. ):
  143. self.get_success(
  144. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  145. )
  146. # Now let's create a room, which will insert a membership
  147. user = UserID("alice", "test")
  148. requester = create_requester(user)
  149. self.get_success(self.room_creator.create_room(requester, {}))
  150. # Register the background update to run again.
  151. self.get_success(
  152. self.store.db_pool.simple_insert(
  153. table="background_updates",
  154. values={
  155. "update_name": "current_state_events_membership",
  156. "progress_json": "{}",
  157. "depends_on": None,
  158. },
  159. )
  160. )
  161. # ... and tell the DataStore that it hasn't finished all updates yet
  162. self.store.db_pool.updates._all_done = False
  163. # Now let's actually drive the updates to completion
  164. while not self.get_success(
  165. self.store.db_pool.updates.has_completed_background_updates()
  166. ):
  167. self.get_success(
  168. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  169. )