test_sync.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  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 synapse.api.errors import Codes, ResourceLimitError
  16. from synapse.api.filtering import DEFAULT_FILTER_COLLECTION
  17. from synapse.handlers.sync import SyncConfig
  18. from synapse.types import UserID
  19. import tests.unittest
  20. import tests.utils
  21. class SyncTestCase(tests.unittest.HomeserverTestCase):
  22. """ Tests Sync Handler. """
  23. def prepare(self, reactor, clock, hs):
  24. self.hs = hs
  25. self.sync_handler = self.hs.get_sync_handler()
  26. self.store = self.hs.get_datastore()
  27. # AuthBlocking reads from the hs' config on initialization. We need to
  28. # modify its config instead of the hs'
  29. self.auth_blocking = self.hs.get_auth()._auth_blocking
  30. def test_wait_for_sync_for_user_auth_blocking(self):
  31. user_id1 = "@user1:test"
  32. user_id2 = "@user2:test"
  33. sync_config = self._generate_sync_config(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(self.sync_handler.wait_for_sync_for_user(sync_config))
  40. # Test that global lock works
  41. self.auth_blocking._hs_disabled = True
  42. e = self.get_failure(
  43. self.sync_handler.wait_for_sync_for_user(sync_config), ResourceLimitError
  44. )
  45. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  46. self.auth_blocking._hs_disabled = False
  47. sync_config = self._generate_sync_config(user_id2)
  48. e = self.get_failure(
  49. self.sync_handler.wait_for_sync_for_user(sync_config), ResourceLimitError
  50. )
  51. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  52. def _generate_sync_config(self, user_id):
  53. return SyncConfig(
  54. user=UserID(user_id.split(":")[0][1:], user_id.split(":")[1]),
  55. filter_collection=DEFAULT_FILTER_COLLECTION,
  56. is_guest=False,
  57. request_key="request_key",
  58. device_id="device_id",
  59. )