test_roommember.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 twisted.test.proto_helpers import MemoryReactor
  16. from synapse.api.constants import Membership
  17. from synapse.rest.admin import register_servlets_for_client_rest_resource
  18. from synapse.rest.client import login, room
  19. from synapse.server import HomeServer
  20. from synapse.types import UserID, create_requester
  21. from synapse.util import Clock
  22. from tests import unittest
  23. from tests.server 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 prepare(self, reactor: MemoryReactor, clock: Clock, hs: TestHomeServer) -> None: # type: ignore[override]
  31. # We can't test the RoomMemberStore on its own without the other event
  32. # storage logic
  33. self.store = hs.get_datastores().main
  34. self.u_alice = self.register_user("alice", "pass")
  35. self.t_alice = self.login("alice", "pass")
  36. self.u_bob = self.register_user("bob", "pass")
  37. # User elsewhere on another host
  38. self.u_charlie = UserID.from_string("@charlie:elsewhere")
  39. def test_one_member(self) -> None:
  40. # Alice creates the room, and is automatically joined
  41. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  42. rooms_for_user = self.get_success(
  43. self.store.get_rooms_for_local_user_where_membership_is(
  44. self.u_alice, [Membership.JOIN]
  45. )
  46. )
  47. self.assertEqual([self.room], [m.room_id for m in rooms_for_user])
  48. def test_count_known_servers(self) -> None:
  49. """
  50. _count_known_servers will calculate how many servers are in a room.
  51. """
  52. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  53. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  54. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  55. servers = self.get_success(self.store._count_known_servers())
  56. self.assertEqual(servers, 2)
  57. def test_count_known_servers_stat_counter_disabled(self) -> None:
  58. """
  59. If enabled, the metrics for how many servers are known will be counted.
  60. """
  61. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  62. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  63. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  64. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  65. self.pump()
  66. self.assertTrue("_known_servers_count" not in self.store.__dict__.keys())
  67. @unittest.override_config(
  68. {"enable_metrics": True, "metrics_flags": {"known_servers": True}}
  69. )
  70. def test_count_known_servers_stat_counter_enabled(self) -> None:
  71. """
  72. If enabled, the metrics for how many servers are known will be counted.
  73. """
  74. # Initialises to 1 -- itself
  75. self.assertEqual(self.store._known_servers_count, 1)
  76. self.pump()
  77. # No rooms have been joined, so technically the SQL returns 0, but it
  78. # will still say it knows about itself.
  79. self.assertEqual(self.store._known_servers_count, 1)
  80. self.room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  81. self.inject_room_member(self.room, self.u_bob, Membership.JOIN)
  82. self.inject_room_member(self.room, self.u_charlie.to_string(), Membership.JOIN)
  83. self.pump(1)
  84. # It now knows about Charlie's server.
  85. self.assertEqual(self.store._known_servers_count, 2)
  86. def test__null_byte_in_display_name_properly_handled(self) -> None:
  87. room = self.helper.create_room_as(self.u_alice, tok=self.t_alice)
  88. res = self.get_success(
  89. self.store.db_pool.simple_select_list(
  90. "room_memberships",
  91. {"user_id": "@alice:test"},
  92. ["display_name", "event_id"],
  93. )
  94. )
  95. # Check that we only got one result back
  96. self.assertEqual(len(res), 1)
  97. # Check that alice's display name is "alice"
  98. self.assertEqual(res[0]["display_name"], "alice")
  99. # Grab the event_id to use later
  100. event_id = res[0]["event_id"]
  101. # Create a profile with the offending null byte in the display name
  102. new_profile = {"displayname": "ali\u0000ce"}
  103. # Ensure that the change goes smoothly and does not fail due to the null byte
  104. self.helper.change_membership(
  105. room,
  106. self.u_alice,
  107. self.u_alice,
  108. "join",
  109. extra_data=new_profile,
  110. tok=self.t_alice,
  111. )
  112. res2 = self.get_success(
  113. self.store.db_pool.simple_select_list(
  114. "room_memberships",
  115. {"user_id": "@alice:test"},
  116. ["display_name", "event_id"],
  117. )
  118. )
  119. # Check that we only have two results
  120. self.assertEqual(len(res2), 2)
  121. # Filter out the previous event using the event_id we grabbed above
  122. row = [row for row in res2 if row["event_id"] != event_id]
  123. # Check that alice's display name is now None
  124. self.assertEqual(row[0]["display_name"], None)
  125. class CurrentStateMembershipUpdateTestCase(unittest.HomeserverTestCase):
  126. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  127. self.store = hs.get_datastores().main
  128. self.room_creator = hs.get_room_creation_handler()
  129. def test_can_rerun_update(self) -> None:
  130. # First make sure we have completed all updates.
  131. self.wait_for_background_updates()
  132. # Now let's create a room, which will insert a membership
  133. user = UserID("alice", "test")
  134. requester = create_requester(user)
  135. self.get_success(self.room_creator.create_room(requester, {}))
  136. # Register the background update to run again.
  137. self.get_success(
  138. self.store.db_pool.simple_insert(
  139. table="background_updates",
  140. values={
  141. "update_name": "current_state_events_membership",
  142. "progress_json": "{}",
  143. "depends_on": None,
  144. },
  145. )
  146. )
  147. # ... and tell the DataStore that it hasn't finished all updates yet
  148. self.store.db_pool.updates._all_done = False
  149. # Now let's actually drive the updates to completion
  150. self.wait_for_background_updates()