test_sync.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 synapse.api.errors import Codes, ResourceLimitError
  15. from synapse.api.filtering import DEFAULT_FILTER_COLLECTION
  16. from synapse.handlers.sync import SyncConfig
  17. from synapse.types import UserID, create_requester
  18. import tests.unittest
  19. import tests.utils
  20. class SyncTestCase(tests.unittest.HomeserverTestCase):
  21. """ Tests Sync Handler. """
  22. def prepare(self, reactor, clock, hs):
  23. self.hs = hs
  24. self.sync_handler = self.hs.get_sync_handler()
  25. self.store = self.hs.get_datastore()
  26. # AuthBlocking reads from the hs' config on initialization. We need to
  27. # modify its config instead of the hs'
  28. self.auth_blocking = self.hs.get_auth()._auth_blocking
  29. def test_wait_for_sync_for_user_auth_blocking(self):
  30. user_id1 = "@user1:test"
  31. user_id2 = "@user2:test"
  32. sync_config = generate_sync_config(user_id1)
  33. requester = create_requester(user_id1)
  34. self.reactor.advance(100) # So we get not 0 time
  35. self.auth_blocking._limit_usage_by_mau = True
  36. self.auth_blocking._max_mau_value = 1
  37. # Check that the happy case does not throw errors
  38. self.get_success(self.store.upsert_monthly_active_user(user_id1))
  39. self.get_success(
  40. self.sync_handler.wait_for_sync_for_user(requester, sync_config)
  41. )
  42. # Test that global lock works
  43. self.auth_blocking._hs_disabled = True
  44. e = self.get_failure(
  45. self.sync_handler.wait_for_sync_for_user(requester, sync_config),
  46. ResourceLimitError,
  47. )
  48. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  49. self.auth_blocking._hs_disabled = False
  50. sync_config = generate_sync_config(user_id2)
  51. requester = create_requester(user_id2)
  52. e = self.get_failure(
  53. self.sync_handler.wait_for_sync_for_user(requester, sync_config),
  54. ResourceLimitError,
  55. )
  56. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  57. def generate_sync_config(user_id: str) -> SyncConfig:
  58. return SyncConfig(
  59. user=UserID(user_id.split(":")[0][1:], user_id.split(":")[1]),
  60. filter_collection=DEFAULT_FILTER_COLLECTION,
  61. is_guest=False,
  62. request_key="request_key",
  63. device_id="device_id",
  64. )