test_appservice.py 8.9 KB

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