test_appservice.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 synapse.appservice import ApplicationService
  16. from twisted.internet import defer
  17. from mock import Mock
  18. from tests import unittest
  19. def _regex(regex, exclusive=True):
  20. return {
  21. "regex": regex,
  22. "exclusive": exclusive
  23. }
  24. class ApplicationServiceTestCase(unittest.TestCase):
  25. def setUp(self):
  26. self.service = ApplicationService(
  27. id="unique_identifier",
  28. url="some_url",
  29. token="some_token",
  30. namespaces={
  31. ApplicationService.NS_USERS: [],
  32. ApplicationService.NS_ROOMS: [],
  33. ApplicationService.NS_ALIASES: []
  34. }
  35. )
  36. self.event = Mock(
  37. type="m.something", room_id="!foo:bar", sender="@someone:somewhere"
  38. )
  39. self.store = Mock()
  40. @defer.inlineCallbacks
  41. def test_regex_user_id_prefix_match(self):
  42. self.service.namespaces[ApplicationService.NS_USERS].append(
  43. _regex("@irc_.*")
  44. )
  45. self.event.sender = "@irc_foobar:matrix.org"
  46. self.assertTrue((yield self.service.is_interested(self.event)))
  47. @defer.inlineCallbacks
  48. def test_regex_user_id_prefix_no_match(self):
  49. self.service.namespaces[ApplicationService.NS_USERS].append(
  50. _regex("@irc_.*")
  51. )
  52. self.event.sender = "@someone_else:matrix.org"
  53. self.assertFalse((yield self.service.is_interested(self.event)))
  54. @defer.inlineCallbacks
  55. def test_regex_room_member_is_checked(self):
  56. self.service.namespaces[ApplicationService.NS_USERS].append(
  57. _regex("@irc_.*")
  58. )
  59. self.event.sender = "@someone_else:matrix.org"
  60. self.event.type = "m.room.member"
  61. self.event.state_key = "@irc_foobar:matrix.org"
  62. self.assertTrue((yield self.service.is_interested(self.event)))
  63. @defer.inlineCallbacks
  64. def test_regex_room_id_match(self):
  65. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  66. _regex("!some_prefix.*some_suffix:matrix.org")
  67. )
  68. self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
  69. self.assertTrue((yield self.service.is_interested(self.event)))
  70. @defer.inlineCallbacks
  71. def test_regex_room_id_no_match(self):
  72. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  73. _regex("!some_prefix.*some_suffix:matrix.org")
  74. )
  75. self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org"
  76. self.assertFalse((yield self.service.is_interested(self.event)))
  77. @defer.inlineCallbacks
  78. def test_regex_alias_match(self):
  79. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  80. _regex("#irc_.*:matrix.org")
  81. )
  82. self.store.get_aliases_for_room.return_value = [
  83. "#irc_foobar:matrix.org", "#athing:matrix.org"
  84. ]
  85. self.store.get_users_in_room.return_value = []
  86. self.assertTrue((yield self.service.is_interested(
  87. self.event, self.store
  88. )))
  89. def test_non_exclusive_alias(self):
  90. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  91. _regex("#irc_.*:matrix.org", exclusive=False)
  92. )
  93. self.assertFalse(self.service.is_exclusive_alias(
  94. "#irc_foobar:matrix.org"
  95. ))
  96. def test_non_exclusive_room(self):
  97. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  98. _regex("!irc_.*:matrix.org", exclusive=False)
  99. )
  100. self.assertFalse(self.service.is_exclusive_room(
  101. "!irc_foobar:matrix.org"
  102. ))
  103. def test_non_exclusive_user(self):
  104. self.service.namespaces[ApplicationService.NS_USERS].append(
  105. _regex("@irc_.*:matrix.org", exclusive=False)
  106. )
  107. self.assertFalse(self.service.is_exclusive_user(
  108. "@irc_foobar:matrix.org"
  109. ))
  110. def test_exclusive_alias(self):
  111. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  112. _regex("#irc_.*:matrix.org", exclusive=True)
  113. )
  114. self.assertTrue(self.service.is_exclusive_alias(
  115. "#irc_foobar:matrix.org"
  116. ))
  117. def test_exclusive_user(self):
  118. self.service.namespaces[ApplicationService.NS_USERS].append(
  119. _regex("@irc_.*:matrix.org", exclusive=True)
  120. )
  121. self.assertTrue(self.service.is_exclusive_user(
  122. "@irc_foobar:matrix.org"
  123. ))
  124. def test_exclusive_room(self):
  125. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  126. _regex("!irc_.*:matrix.org", exclusive=True)
  127. )
  128. self.assertTrue(self.service.is_exclusive_room(
  129. "!irc_foobar:matrix.org"
  130. ))
  131. @defer.inlineCallbacks
  132. def test_regex_alias_no_match(self):
  133. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  134. _regex("#irc_.*:matrix.org")
  135. )
  136. self.store.get_aliases_for_room.return_value = [
  137. "#xmpp_foobar:matrix.org", "#athing:matrix.org"
  138. ]
  139. self.store.get_users_in_room.return_value = []
  140. self.assertFalse((yield self.service.is_interested(
  141. self.event, self.store
  142. )))
  143. @defer.inlineCallbacks
  144. def test_regex_multiple_matches(self):
  145. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  146. _regex("#irc_.*:matrix.org")
  147. )
  148. self.service.namespaces[ApplicationService.NS_USERS].append(
  149. _regex("@irc_.*")
  150. )
  151. self.event.sender = "@irc_foobar:matrix.org"
  152. self.store.get_aliases_for_room.return_value = ["#irc_barfoo:matrix.org"]
  153. self.store.get_users_in_room.return_value = []
  154. self.assertTrue((yield self.service.is_interested(
  155. self.event, self.store
  156. )))
  157. @defer.inlineCallbacks
  158. def test_interested_in_self(self):
  159. # make sure invites get through
  160. self.service.sender = "@appservice:name"
  161. self.service.namespaces[ApplicationService.NS_USERS].append(
  162. _regex("@irc_.*")
  163. )
  164. self.event.type = "m.room.member"
  165. self.event.content = {
  166. "membership": "invite"
  167. }
  168. self.event.state_key = self.service.sender
  169. self.assertTrue((yield self.service.is_interested(self.event)))
  170. @defer.inlineCallbacks
  171. def test_member_list_match(self):
  172. self.service.namespaces[ApplicationService.NS_USERS].append(
  173. _regex("@irc_.*")
  174. )
  175. self.store.get_users_in_room.return_value = [
  176. "@alice:here",
  177. "@irc_fo:here", # AS user
  178. "@bob:here",
  179. ]
  180. self.store.get_aliases_for_room.return_value = []
  181. self.event.sender = "@xmpp_foobar:matrix.org"
  182. self.assertTrue((yield self.service.is_interested(
  183. event=self.event, store=self.store
  184. )))