__init__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015 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):
  35. return {
  36. "user": UserID.from_string(self.USER_ID),
  37. "token_id": 1,
  38. }
  39. hs.get_auth()._get_user_by_access_token = _get_user_by_access_token
  40. for r in self.TO_REGISTER:
  41. r.register_servlets(hs, self.mock_resource)
  42. def make_datastore_mock(self):
  43. store = Mock(spec=[
  44. "insert_client_ip",
  45. ])
  46. store.get_app_service_by_token = Mock(return_value=None)
  47. return store