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 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 Requester, UserID
  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(20)
  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(20)
  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(20)
  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 = event_injection.inject_member_event(
  94. self.hs, room, self.u_bob, Membership.JOIN
  95. )
  96. # first, create a regular event
  97. event, context = event_injection.create_event(
  98. self.hs,
  99. room_id=room,
  100. sender=self.u_alice,
  101. prev_event_ids=[bob_event.event_id],
  102. type="m.test.1",
  103. content={},
  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 = event_injection.inject_event(
  113. self.hs,
  114. room_id=room,
  115. sender=self.u_bob,
  116. prev_event_ids=[bob_event.event_id],
  117. type="m.test.2",
  118. state_key=self.u_bob,
  119. content={},
  120. )
  121. event, context = 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. users = self.get_success(
  130. self.store.get_joined_users_from_context(event, context)
  131. )
  132. self.assertEqual(users.keys(), {self.u_alice, self.u_bob})
  133. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  134. def prepare(self, reactor, clock, homeserver):
  135. self.store = homeserver.get_datastore()
  136. self.room_creator = homeserver.get_room_creation_handler()
  137. def test_can_rerun_update(self):
  138. # First make sure we have completed all updates.
  139. while not self.get_success(
  140. self.store.db.updates.has_completed_background_updates()
  141. ):
  142. self.get_success(
  143. self.store.db.updates.do_next_background_update(100), by=0.1
  144. )
  145. # Now let's create a room, which will insert a membership
  146. user = UserID("alice", "test")
  147. requester = Requester(user, None, False, None, None)
  148. self.get_success(self.room_creator.create_room(requester, {}))
  149. # Register the background update to run again.
  150. self.get_success(
  151. self.store.db.simple_insert(
  152. table="background_updates",
  153. values={
  154. "update_name": "current_state_events_membership",
  155. "progress_json": "{}",
  156. "depends_on": None,
  157. },
  158. )
  159. )
  160. # ... and tell the DataStore that it hasn't finished all updates yet
  161. self.store.db.updates._all_done = False
  162. # Now let's actually drive the updates to completion
  163. while not self.get_success(
  164. self.store.db.updates.has_completed_background_updates()
  165. ):
  166. self.get_success(
  167. self.store.db.updates.do_next_background_update(100), by=0.1
  168. )