test_sync.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 twisted.internet import defer
  16. from synapse.api.errors import AuthError, Codes
  17. from synapse.api.filtering import DEFAULT_FILTER_COLLECTION
  18. from synapse.handlers.sync import SyncConfig, SyncHandler
  19. from synapse.types import UserID
  20. import tests.unittest
  21. import tests.utils
  22. from tests.utils import setup_test_homeserver
  23. class SyncTestCase(tests.unittest.TestCase):
  24. """ Tests Sync Handler. """
  25. @defer.inlineCallbacks
  26. def setUp(self):
  27. self.hs = yield setup_test_homeserver(self.addCleanup)
  28. self.sync_handler = SyncHandler(self.hs)
  29. self.store = self.hs.get_datastore()
  30. @defer.inlineCallbacks
  31. def test_wait_for_sync_for_user_auth_blocking(self):
  32. user_id1 = "@user1:server"
  33. user_id2 = "@user2:server"
  34. sync_config = self._generate_sync_config(user_id1)
  35. self.hs.config.limit_usage_by_mau = True
  36. self.hs.config.max_mau_value = 1
  37. # Check that the happy case does not throw errors
  38. yield self.store.upsert_monthly_active_user(user_id1)
  39. yield self.sync_handler.wait_for_sync_for_user(sync_config)
  40. # Test that global lock works
  41. self.hs.config.hs_disabled = True
  42. with self.assertRaises(AuthError) as e:
  43. yield self.sync_handler.wait_for_sync_for_user(sync_config)
  44. self.assertEquals(e.exception.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  45. self.hs.config.hs_disabled = False
  46. sync_config = self._generate_sync_config(user_id2)
  47. with self.assertRaises(AuthError) as e:
  48. yield self.sync_handler.wait_for_sync_for_user(sync_config)
  49. self.assertEquals(e.exception.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  50. def _generate_sync_config(self, user_id):
  51. return SyncConfig(
  52. user=UserID(user_id.split(":")[0][1:], user_id.split(":")[1]),
  53. filter_collection=DEFAULT_FILTER_COLLECTION,
  54. is_guest=False,
  55. request_key="request_key",
  56. device_id="device_id",
  57. )