test_sync.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. def test_wait_for_sync_for_user_auth_blocking(self):
  28. user_id1 = "@user1:test"
  29. user_id2 = "@user2:test"
  30. sync_config = self._generate_sync_config(user_id1)
  31. self.reactor.advance(100) # So we get not 0 time
  32. self.hs.config.limit_usage_by_mau = True
  33. self.hs.config.max_mau_value = 1
  34. # Check that the happy case does not throw errors
  35. self.get_success(self.store.upsert_monthly_active_user(user_id1))
  36. self.get_success(self.sync_handler.wait_for_sync_for_user(sync_config))
  37. # Test that global lock works
  38. self.hs.config.hs_disabled = True
  39. e = self.get_failure(
  40. self.sync_handler.wait_for_sync_for_user(sync_config), ResourceLimitError
  41. )
  42. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  43. self.hs.config.hs_disabled = False
  44. sync_config = self._generate_sync_config(user_id2)
  45. e = self.get_failure(
  46. self.sync_handler.wait_for_sync_for_user(sync_config), ResourceLimitError
  47. )
  48. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  49. def _generate_sync_config(self, user_id):
  50. return SyncConfig(
  51. user=UserID(user_id.split(":")[0][1:], user_id.split(":")[1]),
  52. filter_collection=DEFAULT_FILTER_COLLECTION,
  53. is_guest=False,
  54. request_key="request_key",
  55. device_id="device_id",
  56. )