test_appservice.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 twisted.internet import defer
  16. from .. import unittest
  17. from synapse.handlers.appservice import ApplicationServicesHandler
  18. from mock import Mock
  19. class AppServiceHandlerTestCase(unittest.TestCase):
  20. """ Tests the ApplicationServicesHandler. """
  21. def setUp(self):
  22. self.mock_store = Mock()
  23. self.mock_as_api = Mock()
  24. hs = Mock()
  25. hs.get_datastore = Mock(return_value=self.mock_store)
  26. self.handler = ApplicationServicesHandler(
  27. hs, self.mock_as_api
  28. )
  29. @defer.inlineCallbacks
  30. def test_notify_interested_services(self):
  31. interested_service = self._mkservice(is_interested=True)
  32. services = [
  33. self._mkservice(is_interested=False),
  34. interested_service,
  35. self._mkservice(is_interested=False)
  36. ]
  37. self.mock_store.get_app_services = Mock(return_value=services)
  38. self.mock_store.get_user_by_id = Mock(return_value=[])
  39. event = Mock(
  40. sender="@someone:anywhere",
  41. type="m.room.message",
  42. room_id="!foo:bar"
  43. )
  44. self.mock_as_api.push = Mock()
  45. yield self.handler.notify_interested_services(event)
  46. self.mock_as_api.push.assert_called_once_with(interested_service, event)
  47. @defer.inlineCallbacks
  48. def test_query_room_alias_exists(self):
  49. room_alias_str = "#foo:bar"
  50. room_alias = Mock()
  51. room_alias.to_string = Mock(return_value=room_alias_str)
  52. room_id = "!alpha:bet"
  53. servers = ["aperture"]
  54. interested_service = self._mkservice(is_interested=True)
  55. services = [
  56. self._mkservice(is_interested=False),
  57. interested_service,
  58. self._mkservice(is_interested=False)
  59. ]
  60. self.mock_store.get_app_services = Mock(return_value=services)
  61. self.mock_store.get_association_from_room_alias = Mock(
  62. return_value=Mock(room_id=room_id, servers=servers)
  63. )
  64. result = yield self.handler.query_room_alias_exists(room_alias)
  65. self.mock_as_api.query_alias.assert_called_once_with(
  66. interested_service,
  67. room_alias_str
  68. )
  69. self.assertEquals(result.room_id, room_id)
  70. self.assertEquals(result.servers, servers)
  71. def _mkservice(self, is_interested):
  72. service = Mock()
  73. service.is_interested = Mock(return_value=is_interested)
  74. service.token = "mock_service_token"
  75. service.url = "mock_service_url"
  76. return service