1
0

test_appservice.py 5.4 KB

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