1
0

test_roommember.py 7.8 KB

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