__init__.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket 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 tests import unittest
  16. from mock import Mock
  17. from ....utils import MockHttpResource, setup_test_homeserver
  18. from synapse.types import UserID
  19. from twisted.internet import defer
  20. PATH_PREFIX = "/_matrix/client/v2_alpha"
  21. class V2AlphaRestTestCase(unittest.TestCase):
  22. # Consumer must define
  23. # USER_ID = <some string>
  24. # TO_REGISTER = [<list of REST servlets to register>]
  25. @defer.inlineCallbacks
  26. def setUp(self):
  27. self.mock_resource = MockHttpResource(prefix=PATH_PREFIX)
  28. hs = yield setup_test_homeserver(
  29. datastore=self.make_datastore_mock(),
  30. http_client=None,
  31. resource_for_client=self.mock_resource,
  32. resource_for_federation=self.mock_resource,
  33. )
  34. def get_user_by_access_token(token=None, allow_guest=False):
  35. return {
  36. "user": UserID.from_string(self.USER_ID),
  37. "token_id": 1,
  38. "is_guest": False,
  39. }
  40. hs.get_auth().get_user_by_access_token = get_user_by_access_token
  41. for r in self.TO_REGISTER:
  42. r.register_servlets(hs, self.mock_resource)
  43. def make_datastore_mock(self):
  44. store = Mock(spec=[
  45. "insert_client_ip",
  46. ])
  47. store.get_app_service_by_token = Mock(return_value=None)
  48. return store