test_roommember.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from synapse.api.constants import Membership
  17. from synapse.rest.admin import register_servlets_for_client_rest_resource
  18. from synapse.rest.client.v1 import login, room
  19. from synapse.types import UserID, create_requester
  20. from tests import unittest
  21. from tests.test_utils import event_injection
  22. from tests.utils import TestHomeServer
  23. class RoomMemberStoreTestCase(unittest.HomeserverTestCase):
  24. servlets = [
  25. login.register_servlets,
  26. register_servlets_for_client_rest_resource,
  27. room.register_servlets,
  28. ]
  29. def prepare(self, reactor, clock, hs: TestHomeServer):
  30. # We can't test the RoomMemberStore on its own without the other event
  31. # storage logic
  32. self.store = hs.get_datastore()
  33. self.u_alice = self.register_user("alice", "pass")
  34. self.t_alice = self.login("alice", "pass")
  35. self.u_bob = self.register_user("bob", "pass")
  36. # User elsewhere on another host
  37. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  38. def test_one_member(self):
  39. # Alice creates the room, and is automatically joined
  40. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  41. rooms_for_user = self.get_success(
  42. self.store.get_rooms_for_local_user_where_membership_is(
  43. self.u_alice, [Membership.JOIN]
  44. )
  45. )
  46. self.assertEquals([self.room], [m.room_id for m in rooms_for_user])
  47. def test_count_known_servers(self):
  48. """
  49. _count_known_servers will calculate how many servers are in a room.
  50. """
  51. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  52. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  53. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  54. servers = self.get_success(self.store._count_known_servers())
  55. self.assertEqual(servers, 2)
  56. def test_count_known_servers_stat_counter_disabled(self):
  57. """
  58. If enabled, the metrics for how many servers are known will be counted.
  59. """
  60. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  61. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  62. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  63. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  64. self.pump()
  65. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  66. @unittest.override_config(
  67. {"enable_metrics": True, "metrics_flags": {"known_servers": True}}
  68. )
  69. def test_count_known_servers_stat_counter_enabled(self):
  70. """
  71. If enabled, the metrics for how many servers are known will be counted.
  72. """
  73. # Initialises to 1 -- itself
  74. self.assertEqual(self.store._known_servers_count, 1)
  75. self.pump()
  76. # No rooms have been joined, so technically the SQL returns 0, but it
  77. # will still say it knows about itself.
  78. self.assertEqual(self.store._known_servers_count, 1)
  79. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  80. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  81. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  82. self.pump(1)
  83. # It now knows about Charlie's server.
  84. self.assertEqual(self.store._known_servers_count, 2)
  85. def test_get_joined_users_from_context(self):
  86. room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  87. bob_event = self.get_success(
  88. event_injection.inject_member_event(
  89. self.hs, room, self.u_bob, Membership.JOIN
  90. )
  91. )
  92. # first, create a regular event
  93. event, context = self.get_success(
  94. event_injection.create_event(
  95. self.hs,
  96. room_id=room,
  97. sender=self.u_alice,
  98. prev_event_ids=[bob_event.event_id],
  99. type="m.test.1",
  100. content={},
  101. )
  102. )
  103. users = self.get_success(
  104. self.store.get_joined_users_from_context(event, context)
  105. )
  106. self.assertEqual(users.keys(), {self.u_alice, self.u_bob})
  107. # Regression test for #7376: create a state event whose key matches bob's
  108. # user_id, but which is *not* a membership event, and persist that; then check
  109. # that `get_joined_users_from_context` returns the correct users for the next event.
  110. non_member_event = self.get_success(
  111. event_injection.inject_event(
  112. self.hs,
  113. room_id=room,
  114. sender=self.u_bob,
  115. prev_event_ids=[bob_event.event_id],
  116. type="m.test.2",
  117. state_key=self.u_bob,
  118. content={},
  119. )
  120. )
  121. event, context = self.get_success(
  122. event_injection.create_event(
  123. self.hs,
  124. room_id=room,
  125. sender=self.u_alice,
  126. prev_event_ids=[non_member_event.event_id],
  127. type="m.test.3",
  128. content={},
  129. )
  130. )
  131. users = self.get_success(
  132. self.store.get_joined_users_from_context(event, context)
  133. )
  134. self.assertEqual(users.keys(), {self.u_alice, self.u_bob})
  135. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  136. def prepare(self, reactor, clock, homeserver):
  137. self.store = homeserver.get_datastore()
  138. self.room_creator = homeserver.get_room_creation_handler()
  139. def test_can_rerun_update(self):
  140. # First make sure we have completed all updates.
  141. while not self.get_success(
  142. self.store.db_pool.updates.has_completed_background_updates()
  143. ):
  144. self.get_success(
  145. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  146. )
  147. # Now let's create a room, which will insert a membership
  148. user = UserID("alice", "test")
  149. requester = create_requester(user)
  150. self.get_success(self.room_creator.create_room(requester, {}))
  151. # Register the background update to run again.
  152. self.get_success(
  153. self.store.db_pool.simple_insert(
  154. table="background_updates",
  155. values={
  156. "update_name": "current_state_events_membership",
  157. "progress_json": "{}",
  158. "depends_on": None,
  159. },
  160. )
  161. )
  162. # ... and tell the DataStore that it hasn't finished all updates yet
  163. self.store.db_pool.updates._all_done = False
  164. # Now let's actually drive the updates to completion
  165. while not self.get_success(
  166. self.store.db_pool.updates.has_completed_background_updates()
  167. ):
  168. self.get_success(
  169. self.store.db_pool.updates.do_next_background_update(100), by=0.1
  170. )