test_appservice.py 9.7 KB

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