test_appservice.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 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. self.mock_scheduler = Mock()
  25. hs = Mock()
  26. hs.get_datastore = Mock(return_value=self.mock_store)
  27. self.handler = ApplicationServicesHandler(
  28. hs, self.mock_as_api, self.mock_scheduler
  29. )
  30. @defer.inlineCallbacks
  31. def test_notify_interested_services(self):
  32. interested_service = self._mkservice(is_interested=True)
  33. services = [
  34. self._mkservice(is_interested=False),
  35. interested_service,
  36. self._mkservice(is_interested=False)
  37. ]
  38. self.mock_store.get_app_services = Mock(return_value=services)
  39. self.mock_store.get_user_by_id = Mock(return_value=[])
  40. event = Mock(
  41. sender="@someone:anywhere",
  42. type="m.room.message",
  43. room_id="!foo:bar"
  44. )
  45. self.mock_as_api.push = Mock()
  46. yield self.handler.notify_interested_services(event)
  47. self.mock_scheduler.submit_event_for_as.assert_called_once_with(
  48. interested_service, event
  49. )
  50. @defer.inlineCallbacks
  51. def test_query_user_exists_unknown_user(self):
  52. user_id = "@someone:anywhere"
  53. services = [self._mkservice(is_interested=True)]
  54. services[0].is_interested_in_user = Mock(return_value=True)
  55. self.mock_store.get_app_services = Mock(return_value=services)
  56. self.mock_store.get_user_by_id = Mock(return_value=None)
  57. event = Mock(
  58. sender=user_id,
  59. type="m.room.message",
  60. room_id="!foo:bar"
  61. )
  62. self.mock_as_api.push = Mock()
  63. self.mock_as_api.query_user = Mock()
  64. yield self.handler.notify_interested_services(event)
  65. self.mock_as_api.query_user.assert_called_once_with(
  66. services[0], user_id
  67. )
  68. @defer.inlineCallbacks
  69. def test_query_user_exists_known_user(self):
  70. user_id = "@someone:anywhere"
  71. services = [self._mkservice(is_interested=True)]
  72. services[0].is_interested_in_user = Mock(return_value=True)
  73. self.mock_store.get_app_services = Mock(return_value=services)
  74. self.mock_store.get_user_by_id = Mock(return_value={
  75. "name": user_id
  76. })
  77. event = Mock(
  78. sender=user_id,
  79. type="m.room.message",
  80. room_id="!foo:bar"
  81. )
  82. self.mock_as_api.push = Mock()
  83. self.mock_as_api.query_user = Mock()
  84. yield self.handler.notify_interested_services(event)
  85. self.assertFalse(
  86. self.mock_as_api.query_user.called,
  87. "query_user called when it shouldn't have been."
  88. )
  89. @defer.inlineCallbacks
  90. def test_query_room_alias_exists(self):
  91. room_alias_str = "#foo:bar"
  92. room_alias = Mock()
  93. room_alias.to_string = Mock(return_value=room_alias_str)
  94. room_id = "!alpha:bet"
  95. servers = ["aperture"]
  96. interested_service = self._mkservice(is_interested=True)
  97. services = [
  98. self._mkservice(is_interested=False),
  99. interested_service,
  100. self._mkservice(is_interested=False)
  101. ]
  102. self.mock_store.get_app_services = Mock(return_value=services)
  103. self.mock_store.get_association_from_room_alias = Mock(
  104. return_value=Mock(room_id=room_id, servers=servers)
  105. )
  106. result = yield self.handler.query_room_alias_exists(room_alias)
  107. self.mock_as_api.query_alias.assert_called_once_with(
  108. interested_service,
  109. 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