1
0

test_sync.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, create_requester
  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. requester = create_requester(user_id1)
  35. self.reactor.advance(100) # So we get not 0 time
  36. self.auth_blocking._limit_usage_by_mau = True
  37. self.auth_blocking._max_mau_value = 1
  38. # Check that the happy case does not throw errors
  39. self.get_success(self.store.upsert_monthly_active_user(user_id1))
  40. self.get_success(
  41. self.sync_handler.wait_for_sync_for_user(requester, sync_config)
  42. )
  43. # Test that global lock works
  44. self.auth_blocking._hs_disabled = True
  45. e = self.get_failure(
  46. self.sync_handler.wait_for_sync_for_user(requester, sync_config),
  47. ResourceLimitError,
  48. )
  49. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  50. self.auth_blocking._hs_disabled = False
  51. sync_config = self._generate_sync_config(user_id2)
  52. requester = create_requester(user_id2)
  53. e = self.get_failure(
  54. self.sync_handler.wait_for_sync_for_user(requester, sync_config),
  55. ResourceLimitError,
  56. )
  57. self.assertEquals(e.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  58. def _generate_sync_config(self, user_id):
  59. return SyncConfig(
  60. user=UserID(user_id.split(":")[0][1:], user_id.split(":")[1]),
  61. filter_collection=DEFAULT_FILTER_COLLECTION,
  62. is_guest=False,
  63. request_key="request_key",
  64. device_id="device_id",
  65. )