test_sync.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Optional
  15. from unittest.mock import Mock
  16. from synapse.api.constants import EventTypes, JoinRules
  17. from synapse.api.errors import Codes, ResourceLimitError
  18. from synapse.api.filtering import Filtering
  19. from synapse.api.room_versions import RoomVersions
  20. from synapse.handlers.sync import SyncConfig
  21. from synapse.rest import admin
  22. from synapse.rest.client import knock, login, room
  23. from synapse.server import HomeServer
  24. from synapse.types import UserID, create_requester
  25. import tests.unittest
  26. import tests.utils
  27. class SyncTestCase(tests.unittest.HomeserverTestCase):
  28. """Tests Sync Handler."""
  29. servlets = [
  30. admin.register_servlets,
  31. knock.register_servlets,
  32. login.register_servlets,
  33. room.register_servlets,
  34. ]
  35. def prepare(self, reactor, clock, hs: HomeServer):
  36. self.sync_handler = self.hs.get_sync_handler()
  37. self.store = self.hs.get_datastore()
  38. # AuthBlocking reads from the hs' config on initialization. We need to
  39. # modify its config instead of the hs'
  40. self.auth_blocking = self.hs.get_auth()._auth_blocking
  41. def test_wait_for_sync_for_user_auth_blocking(self):
  42. user_id1 = "@user1:test"
  43. user_id2 = "@user2:test"
  44. sync_config = generate_sync_config(user_id1)
  45. requester = create_requester(user_id1)
  46. self.reactor.advance(100) # So we get not 0 time
  47. self.auth_blocking._limit_usage_by_mau = True
  48. self.auth_blocking._max_mau_value = 1
  49. # Check that the happy case does not throw errors
  50. self.get_success(self.store.upsert_monthly_active_user(user_id1))
  51. self.get_success(
  52. self.sync_handler.wait_for_sync_for_user(requester, sync_config)
  53. )
  54. # Test that global lock works
  55. self.auth_blocking._hs_disabled = True
  56. e = self.get_failure(
  57. self.sync_handler.wait_for_sync_for_user(requester, sync_config),
  58. ResourceLimitError,
  59. )
  60. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  61. self.auth_blocking._hs_disabled = False
  62. sync_config = generate_sync_config(user_id2)
  63. requester = create_requester(user_id2)
  64. e = self.get_failure(
  65. self.sync_handler.wait_for_sync_for_user(requester, sync_config),
  66. ResourceLimitError,
  67. )
  68. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  69. def test_unknown_room_version(self):
  70. """
  71. A room with an unknown room version should not break sync (and should be excluded).
  72. """
  73. inviter = self.register_user("creator", "pass", admin=True)
  74. inviter_tok = self.login("@creator:test", "pass")
  75. user = self.register_user("user", "pass")
  76. tok = self.login("user", "pass")
  77. # Do an initial sync on a different device.
  78. requester = create_requester(user)
  79. initial_result = self.get_success(
  80. self.sync_handler.wait_for_sync_for_user(
  81. requester, sync_config=generate_sync_config(user, device_id="dev")
  82. )
  83. )
  84. # Create a room as the user.
  85. joined_room = self.helper.create_room_as(user, tok=tok)
  86. # Invite the user to the room as someone else.
  87. invite_room = self.helper.create_room_as(inviter, tok=inviter_tok)
  88. self.helper.invite(invite_room, targ=user, tok=inviter_tok)
  89. knock_room = self.helper.create_room_as(
  90. inviter, room_version=RoomVersions.V7.identifier, tok=inviter_tok
  91. )
  92. self.helper.send_state(
  93. knock_room,
  94. EventTypes.JoinRules,
  95. {"join_rule": JoinRules.KNOCK},
  96. tok=inviter_tok,
  97. )
  98. channel = self.make_request(
  99. "POST",
  100. "/_matrix/client/r0/knock/%s" % (knock_room,),
  101. b"{}",
  102. tok,
  103. )
  104. self.assertEquals(200, channel.code, channel.result)
  105. # The rooms should appear in the sync response.
  106. result = self.get_success(
  107. self.sync_handler.wait_for_sync_for_user(
  108. requester, sync_config=generate_sync_config(user)
  109. )
  110. )
  111. self.assertIn(joined_room, [r.room_id for r in result.joined])
  112. self.assertIn(invite_room, [r.room_id for r in result.invited])
  113. self.assertIn(knock_room, [r.room_id for r in result.knocked])
  114. # Test a incremental sync (by providing a since_token).
  115. result = self.get_success(
  116. self.sync_handler.wait_for_sync_for_user(
  117. requester,
  118. sync_config=generate_sync_config(user, device_id="dev"),
  119. since_token=initial_result.next_batch,
  120. )
  121. )
  122. self.assertIn(joined_room, [r.room_id for r in result.joined])
  123. self.assertIn(invite_room, [r.room_id for r in result.invited])
  124. self.assertIn(knock_room, [r.room_id for r in result.knocked])
  125. # Poke the database and update the room version to an unknown one.
  126. for room_id in (joined_room, invite_room, knock_room):
  127. self.get_success(
  128. self.hs.get_datastores().main.db_pool.simple_update(
  129. "rooms",
  130. keyvalues={"room_id": room_id},
  131. updatevalues={"room_version": "unknown-room-version"},
  132. desc="updated-room-version",
  133. )
  134. )
  135. # Blow away caches (supported room versions can only change due to a restart).
  136. self.get_success(
  137. self.store.get_rooms_for_user_with_stream_ordering.invalidate_all()
  138. )
  139. self.store._get_event_cache.clear()
  140. # The rooms should be excluded from the sync response.
  141. # Get a new request key.
  142. result = self.get_success(
  143. self.sync_handler.wait_for_sync_for_user(
  144. requester, sync_config=generate_sync_config(user)
  145. )
  146. )
  147. self.assertNotIn(joined_room, [r.room_id for r in result.joined])
  148. self.assertNotIn(invite_room, [r.room_id for r in result.invited])
  149. self.assertNotIn(knock_room, [r.room_id for r in result.knocked])
  150. # The rooms should also not be in an incremental sync.
  151. result = self.get_success(
  152. self.sync_handler.wait_for_sync_for_user(
  153. requester,
  154. sync_config=generate_sync_config(user, device_id="dev"),
  155. since_token=initial_result.next_batch,
  156. )
  157. )
  158. self.assertNotIn(joined_room, [r.room_id for r in result.joined])
  159. self.assertNotIn(invite_room, [r.room_id for r in result.invited])
  160. self.assertNotIn(knock_room, [r.room_id for r in result.knocked])
  161. _request_key = 0
  162. def generate_sync_config(
  163. user_id: str, device_id: Optional[str] = "device_id"
  164. ) -> SyncConfig:
  165. """Generate a sync config (with a unique request key)."""
  166. global _request_key
  167. _request_key += 1
  168. return SyncConfig(
  169. user=UserID.from_string(user_id),
  170. filter_collection=Filtering(Mock()).DEFAULT_FILTER_COLLECTION,
  171. is_guest=False,
  172. request_key=("request_key", _request_key),
  173. device_id=device_id,
  174. )