test_appservice.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. import re
  15. from unittest.mock import Mock
  16. from twisted.internet import defer
  17. from synapse.appservice import ApplicationService, Namespace
  18. from tests import unittest
  19. from tests.test_utils import simple_async_mock
  20. def _regex(regex: str, exclusive: bool = True) -> Namespace:
  21. return Namespace(exclusive, None, re.compile(regex))
  22. class ApplicationServiceTestCase(unittest.TestCase):
  23. def setUp(self):
  24. self.service = ApplicationService(
  25. id="unique_identifier",
  26. sender="@as:test",
  27. url="some_url",
  28. token="some_token",
  29. hostname="matrix.org", # only used by get_groups_for_user
  30. )
  31. self.event = Mock(
  32. event_id="$abc:xyz",
  33. type="m.something",
  34. room_id="!foo:bar",
  35. sender="@someone:somewhere",
  36. )
  37. self.store = Mock()
  38. self.store.get_aliases_for_room = simple_async_mock([])
  39. self.store.get_users_in_room = simple_async_mock([])
  40. @defer.inlineCallbacks
  41. def test_regex_user_id_prefix_match(self):
  42. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  43. self.event.sender = "@irc_foobar:matrix.org"
  44. self.assertTrue(
  45. (
  46. yield defer.ensureDeferred(
  47. self.service.is_interested_in_event(
  48. self.event.event_id, self.event, self.store
  49. )
  50. )
  51. )
  52. )
  53. @defer.inlineCallbacks
  54. def test_regex_user_id_prefix_no_match(self):
  55. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  56. self.event.sender = "@someone_else:matrix.org"
  57. self.assertFalse(
  58. (
  59. yield defer.ensureDeferred(
  60. self.service.is_interested_in_event(
  61. self.event.event_id, self.event, self.store
  62. )
  63. )
  64. )
  65. )
  66. @defer.inlineCallbacks
  67. def test_regex_room_member_is_checked(self):
  68. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  69. self.event.sender = "@someone_else:matrix.org"
  70. self.event.type = "m.room.member"
  71. self.event.state_key = "@irc_foobar:matrix.org"
  72. self.assertTrue(
  73. (
  74. yield defer.ensureDeferred(
  75. self.service.is_interested_in_event(
  76. self.event.event_id, self.event, self.store
  77. )
  78. )
  79. )
  80. )
  81. @defer.inlineCallbacks
  82. def test_regex_room_id_match(self):
  83. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  84. _regex("!some_prefix.*some_suffix:matrix.org")
  85. )
  86. self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
  87. self.assertTrue(
  88. (
  89. yield defer.ensureDeferred(
  90. self.service.is_interested_in_event(
  91. self.event.event_id, self.event, self.store
  92. )
  93. )
  94. )
  95. )
  96. @defer.inlineCallbacks
  97. def test_regex_room_id_no_match(self):
  98. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  99. _regex("!some_prefix.*some_suffix:matrix.org")
  100. )
  101. self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org"
  102. self.assertFalse(
  103. (
  104. yield defer.ensureDeferred(
  105. self.service.is_interested_in_event(
  106. self.event.event_id, self.event, self.store
  107. )
  108. )
  109. )
  110. )
  111. @defer.inlineCallbacks
  112. def test_regex_alias_match(self):
  113. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  114. _regex("#irc_.*:matrix.org")
  115. )
  116. self.store.get_aliases_for_room = simple_async_mock(
  117. ["#irc_foobar:matrix.org", "#athing:matrix.org"]
  118. )
  119. self.store.get_users_in_room = simple_async_mock([])
  120. self.assertTrue(
  121. (
  122. yield defer.ensureDeferred(
  123. self.service.is_interested_in_event(
  124. self.event.event_id, self.event, self.store
  125. )
  126. )
  127. )
  128. )
  129. def test_non_exclusive_alias(self):
  130. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  131. _regex("#irc_.*:matrix.org", exclusive=False)
  132. )
  133. self.assertFalse(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  134. def test_non_exclusive_room(self):
  135. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  136. _regex("!irc_.*:matrix.org", exclusive=False)
  137. )
  138. self.assertFalse(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  139. def test_non_exclusive_user(self):
  140. self.service.namespaces[ApplicationService.NS_USERS].append(
  141. _regex("@irc_.*:matrix.org", exclusive=False)
  142. )
  143. self.assertFalse(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  144. def test_exclusive_alias(self):
  145. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  146. _regex("#irc_.*:matrix.org", exclusive=True)
  147. )
  148. self.assertTrue(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  149. def test_exclusive_user(self):
  150. self.service.namespaces[ApplicationService.NS_USERS].append(
  151. _regex("@irc_.*:matrix.org", exclusive=True)
  152. )
  153. self.assertTrue(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  154. def test_exclusive_room(self):
  155. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  156. _regex("!irc_.*:matrix.org", exclusive=True)
  157. )
  158. self.assertTrue(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  159. @defer.inlineCallbacks
  160. def test_regex_alias_no_match(self):
  161. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  162. _regex("#irc_.*:matrix.org")
  163. )
  164. self.store.get_aliases_for_room = simple_async_mock(
  165. ["#xmpp_foobar:matrix.org", "#athing:matrix.org"]
  166. )
  167. self.store.get_users_in_room = simple_async_mock([])
  168. self.assertFalse(
  169. (
  170. yield defer.ensureDeferred(
  171. self.service.is_interested_in_event(
  172. self.event.event_id, self.event, self.store
  173. )
  174. )
  175. )
  176. )
  177. @defer.inlineCallbacks
  178. def test_regex_multiple_matches(self):
  179. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  180. _regex("#irc_.*:matrix.org")
  181. )
  182. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  183. self.event.sender = "@irc_foobar:matrix.org"
  184. self.store.get_aliases_for_room = simple_async_mock(["#irc_barfoo:matrix.org"])
  185. self.store.get_users_in_room = simple_async_mock([])
  186. self.assertTrue(
  187. (
  188. yield defer.ensureDeferred(
  189. self.service.is_interested_in_event(
  190. self.event.event_id, self.event, self.store
  191. )
  192. )
  193. )
  194. )
  195. @defer.inlineCallbacks
  196. def test_interested_in_self(self):
  197. # make sure invites get through
  198. self.service.sender = "@appservice:name"
  199. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  200. self.event.type = "m.room.member"
  201. self.event.content = {"membership": "invite"}
  202. self.event.state_key = self.service.sender
  203. self.assertTrue(
  204. (
  205. yield defer.ensureDeferred(
  206. self.service.is_interested_in_event(
  207. self.event.event_id, self.event, self.store
  208. )
  209. )
  210. )
  211. )
  212. @defer.inlineCallbacks
  213. def test_member_list_match(self):
  214. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  215. # Note that @irc_fo:here is the AS user.
  216. self.store.get_users_in_room = simple_async_mock(
  217. ["@alice:here", "@irc_fo:here", "@bob:here"]
  218. )
  219. self.store.get_aliases_for_room = simple_async_mock([])
  220. self.event.sender = "@xmpp_foobar:matrix.org"
  221. self.assertTrue(
  222. (
  223. yield defer.ensureDeferred(
  224. self.service.is_interested_in_event(
  225. self.event.event_id, self.event, self.store
  226. )
  227. )
  228. )
  229. )