test_appservice.py 5.5 KB

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