test_appservice.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from unittest.mock import Mock
  15. from twisted.internet import defer
  16. from synapse.handlers.appservice import ApplicationServicesHandler
  17. from synapse.types import RoomStreamToken
  18. from tests.test_utils import make_awaitable
  19. from tests.utils import MockClock
  20. from .. import unittest
  21. class AppServiceHandlerTestCase(unittest.TestCase):
  22. """ Tests the ApplicationServicesHandler. """
  23. def setUp(self):
  24. self.mock_store = Mock()
  25. self.mock_as_api = Mock()
  26. self.mock_scheduler = Mock()
  27. hs = Mock()
  28. hs.get_datastore.return_value = self.mock_store
  29. self.mock_store.get_received_ts.return_value = make_awaitable(0)
  30. self.mock_store.set_appservice_last_pos.return_value = make_awaitable(None)
  31. hs.get_application_service_api.return_value = self.mock_as_api
  32. hs.get_application_service_scheduler.return_value = self.mock_scheduler
  33. hs.get_clock.return_value = MockClock()
  34. self.handler = ApplicationServicesHandler(hs)
  35. def test_notify_interested_services(self):
  36. interested_service = self._mkservice(is_interested=True)
  37. services = [
  38. self._mkservice(is_interested=False),
  39. interested_service,
  40. self._mkservice(is_interested=False),
  41. ]
  42. self.mock_as_api.query_user.return_value = make_awaitable(True)
  43. self.mock_store.get_app_services.return_value = services
  44. self.mock_store.get_user_by_id.return_value = make_awaitable([])
  45. event = Mock(
  46. sender="@someone:anywhere", type="m.room.message", room_id="!foo:bar"
  47. )
  48. self.mock_store.get_new_events_for_appservice.side_effect = [
  49. make_awaitable((0, [event])),
  50. make_awaitable((0, [])),
  51. ]
  52. self.handler.notify_interested_services(RoomStreamToken(None, 0))
  53. self.mock_scheduler.submit_event_for_as.assert_called_once_with(
  54. interested_service, event
  55. )
  56. def test_query_user_exists_unknown_user(self):
  57. user_id = "@someone:anywhere"
  58. services = [self._mkservice(is_interested=True)]
  59. services[0].is_interested_in_user.return_value = True
  60. self.mock_store.get_app_services.return_value = services
  61. self.mock_store.get_user_by_id.return_value = make_awaitable(None)
  62. event = Mock(sender=user_id, type="m.room.message", room_id="!foo:bar")
  63. self.mock_as_api.query_user.return_value = make_awaitable(True)
  64. self.mock_store.get_new_events_for_appservice.side_effect = [
  65. make_awaitable((0, [event])),
  66. make_awaitable((0, [])),
  67. ]
  68. self.handler.notify_interested_services(RoomStreamToken(None, 0))
  69. self.mock_as_api.query_user.assert_called_once_with(services[0], user_id)
  70. def test_query_user_exists_known_user(self):
  71. user_id = "@someone:anywhere"
  72. services = [self._mkservice(is_interested=True)]
  73. services[0].is_interested_in_user.return_value = True
  74. self.mock_store.get_app_services.return_value = services
  75. self.mock_store.get_user_by_id.return_value = make_awaitable({"name": user_id})
  76. event = Mock(sender=user_id, type="m.room.message", room_id="!foo:bar")
  77. self.mock_as_api.query_user.return_value = make_awaitable(True)
  78. self.mock_store.get_new_events_for_appservice.side_effect = [
  79. make_awaitable((0, [event])),
  80. make_awaitable((0, [])),
  81. ]
  82. self.handler.notify_interested_services(RoomStreamToken(None, 0))
  83. self.assertFalse(
  84. self.mock_as_api.query_user.called,
  85. "query_user called when it shouldn't have been.",
  86. )
  87. def test_query_room_alias_exists(self):
  88. room_alias_str = "#foo:bar"
  89. room_alias = Mock()
  90. room_alias.to_string.return_value = room_alias_str
  91. room_id = "!alpha:bet"
  92. servers = ["aperture"]
  93. interested_service = self._mkservice_alias(is_interested_in_alias=True)
  94. services = [
  95. self._mkservice_alias(is_interested_in_alias=False),
  96. interested_service,
  97. self._mkservice_alias(is_interested_in_alias=False),
  98. ]
  99. self.mock_as_api.query_alias.return_value = make_awaitable(True)
  100. self.mock_store.get_app_services.return_value = services
  101. self.mock_store.get_association_from_room_alias.return_value = make_awaitable(
  102. Mock(room_id=room_id, servers=servers)
  103. )
  104. result = self.successResultOf(
  105. defer.ensureDeferred(self.handler.query_room_alias_exists(room_alias))
  106. )
  107. self.mock_as_api.query_alias.assert_called_once_with(
  108. interested_service, room_alias_str
  109. )
  110. self.assertEquals(result.room_id, room_id)
  111. self.assertEquals(result.servers, servers)
  112. def _mkservice(self, is_interested):
  113. service = Mock()
  114. service.is_interested.return_value = make_awaitable(is_interested)
  115. service.token = "mock_service_token"
  116. service.url = "mock_service_url"
  117. return service
  118. def _mkservice_alias(self, is_interested_in_alias):
  119. service = Mock()
  120. service.is_interested_in_alias.return_value = is_interested_in_alias
  121. service.token = "mock_service_token"
  122. service.url = "mock_service_url"
  123. return service