test_appservice.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 mock import Mock
  16. from twisted.internet import defer
  17. from synapse.handlers.appservice import ApplicationServicesHandler
  18. from synapse.types import RoomStreamToken
  19. from tests.test_utils import make_awaitable
  20. from tests.utils import MockClock
  21. from .. import unittest
  22. class AppServiceHandlerTestCase(unittest.TestCase):
  23. """ Tests the ApplicationServicesHandler. """
  24. def setUp(self):
  25. self.mock_store = Mock()
  26. self.mock_as_api = Mock()
  27. self.mock_scheduler = Mock()
  28. hs = Mock()
  29. hs.get_datastore.return_value = self.mock_store
  30. self.mock_store.get_received_ts.return_value = defer.succeed(0)
  31. self.mock_store.set_appservice_last_pos.return_value = defer.succeed(None)
  32. hs.get_application_service_api.return_value = self.mock_as_api
  33. hs.get_application_service_scheduler.return_value = self.mock_scheduler
  34. hs.get_clock.return_value = MockClock()
  35. self.handler = ApplicationServicesHandler(hs)
  36. def test_notify_interested_services(self):
  37. interested_service = self._mkservice(is_interested=True)
  38. services = [
  39. self._mkservice(is_interested=False),
  40. interested_service,
  41. self._mkservice(is_interested=False),
  42. ]
  43. self.mock_as_api.query_user.return_value = defer.succeed(True)
  44. self.mock_store.get_app_services.return_value = services
  45. self.mock_store.get_user_by_id.return_value = defer.succeed([])
  46. event = Mock(
  47. sender="@someone:anywhere", type="m.room.message", room_id="!foo:bar"
  48. )
  49. self.mock_store.get_new_events_for_appservice.side_effect = [
  50. defer.succeed((0, [event])),
  51. defer.succeed((0, [])),
  52. ]
  53. self.handler.notify_interested_services(RoomStreamToken(None, 0))
  54. self.mock_scheduler.submit_event_for_as.assert_called_once_with(
  55. interested_service, event
  56. )
  57. def test_query_user_exists_unknown_user(self):
  58. user_id = "@someone:anywhere"
  59. services = [self._mkservice(is_interested=True)]
  60. services[0].is_interested_in_user.return_value = True
  61. self.mock_store.get_app_services.return_value = services
  62. self.mock_store.get_user_by_id.return_value = defer.succeed(None)
  63. event = Mock(sender=user_id, type="m.room.message", room_id="!foo:bar")
  64. self.mock_as_api.query_user.return_value = defer.succeed(True)
  65. self.mock_store.get_new_events_for_appservice.side_effect = [
  66. defer.succeed((0, [event])),
  67. defer.succeed((0, [])),
  68. ]
  69. self.handler.notify_interested_services(RoomStreamToken(None, 0))
  70. self.mock_as_api.query_user.assert_called_once_with(services[0], user_id)
  71. def test_query_user_exists_known_user(self):
  72. user_id = "@someone:anywhere"
  73. services = [self._mkservice(is_interested=True)]
  74. services[0].is_interested_in_user.return_value = True
  75. self.mock_store.get_app_services.return_value = services
  76. self.mock_store.get_user_by_id.return_value = defer.succeed({"name": user_id})
  77. event = Mock(sender=user_id, type="m.room.message", room_id="!foo:bar")
  78. self.mock_as_api.query_user.return_value = defer.succeed(True)
  79. self.mock_store.get_new_events_for_appservice.side_effect = [
  80. defer.succeed((0, [event])),
  81. defer.succeed((0, [])),
  82. ]
  83. self.handler.notify_interested_services(RoomStreamToken(None, 0))
  84. self.assertFalse(
  85. self.mock_as_api.query_user.called,
  86. "query_user called when it shouldn't have been.",
  87. )
  88. @defer.inlineCallbacks
  89. def test_query_room_alias_exists(self):
  90. room_alias_str = "#foo:bar"
  91. room_alias = Mock()
  92. room_alias.to_string.return_value = room_alias_str
  93. room_id = "!alpha:bet"
  94. servers = ["aperture"]
  95. interested_service = self._mkservice_alias(is_interested_in_alias=True)
  96. services = [
  97. self._mkservice_alias(is_interested_in_alias=False),
  98. interested_service,
  99. self._mkservice_alias(is_interested_in_alias=False),
  100. ]
  101. self.mock_as_api.query_alias.return_value = make_awaitable(True)
  102. self.mock_store.get_app_services.return_value = services
  103. self.mock_store.get_association_from_room_alias.return_value = make_awaitable(
  104. Mock(room_id=room_id, servers=servers)
  105. )
  106. result = yield defer.ensureDeferred(
  107. self.handler.query_room_alias_exists(room_alias)
  108. )
  109. self.mock_as_api.query_alias.assert_called_once_with(
  110. interested_service, room_alias_str
  111. )
  112. self.assertEquals(result.room_id, room_id)
  113. self.assertEquals(result.servers, servers)
  114. def _mkservice(self, is_interested):
  115. service = Mock()
  116. service.is_interested.return_value = make_awaitable(is_interested)
  117. service.token = "mock_service_token"
  118. service.url = "mock_service_url"
  119. return service
  120. def _mkservice_alias(self, is_interested_in_alias):
  121. service = Mock()
  122. service.is_interested_in_alias.return_value = is_interested_in_alias
  123. service.token = "mock_service_token"
  124. service.url = "mock_service_url"
  125. return service