test_appservice.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 Any, 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[Any]", 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 self.service.is_interested_in_event(
  49. self.event.event_id, self.event, self.store
  50. )
  51. )
  52. )
  53. @defer.inlineCallbacks
  54. def test_regex_user_id_prefix_no_match(
  55. self,
  56. ) -> Generator["defer.Deferred[Any]", object, None]:
  57. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  58. self.event.sender = "@someone_else:matrix.org"
  59. self.assertFalse(
  60. (
  61. yield self.service.is_interested_in_event(
  62. self.event.event_id, self.event, self.store
  63. )
  64. )
  65. )
  66. @defer.inlineCallbacks
  67. def test_regex_room_member_is_checked(
  68. self,
  69. ) -> Generator["defer.Deferred[Any]", object, None]:
  70. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  71. self.event.sender = "@someone_else:matrix.org"
  72. self.event.type = "m.room.member"
  73. self.event.state_key = "@irc_foobar:matrix.org"
  74. self.assertTrue(
  75. (
  76. yield self.service.is_interested_in_event(
  77. self.event.event_id, self.event, self.store
  78. )
  79. )
  80. )
  81. @defer.inlineCallbacks
  82. def test_regex_room_id_match(
  83. self,
  84. ) -> Generator["defer.Deferred[Any]", object, None]:
  85. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  86. _regex("!some_prefix.*some_suffix:matrix.org")
  87. )
  88. self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
  89. self.assertTrue(
  90. (
  91. yield self.service.is_interested_in_event(
  92. self.event.event_id, self.event, self.store
  93. )
  94. )
  95. )
  96. @defer.inlineCallbacks
  97. def test_regex_room_id_no_match(
  98. self,
  99. ) -> Generator["defer.Deferred[Any]", object, None]:
  100. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  101. _regex("!some_prefix.*some_suffix:matrix.org")
  102. )
  103. self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org"
  104. self.assertFalse(
  105. (
  106. yield self.service.is_interested_in_event(
  107. self.event.event_id, self.event, self.store
  108. )
  109. )
  110. )
  111. @defer.inlineCallbacks
  112. def test_regex_alias_match(self) -> Generator["defer.Deferred[Any]", object, None]:
  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_local_users_in_room = simple_async_mock([])
  120. self.assertTrue(
  121. (
  122. yield self.service.is_interested_in_event(
  123. self.event.event_id, self.event, self.store
  124. )
  125. )
  126. )
  127. def test_non_exclusive_alias(self) -> None:
  128. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  129. _regex("#irc_.*:matrix.org", exclusive=False)
  130. )
  131. self.assertFalse(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  132. def test_non_exclusive_room(self) -> None:
  133. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  134. _regex("!irc_.*:matrix.org", exclusive=False)
  135. )
  136. self.assertFalse(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  137. def test_non_exclusive_user(self) -> None:
  138. self.service.namespaces[ApplicationService.NS_USERS].append(
  139. _regex("@irc_.*:matrix.org", exclusive=False)
  140. )
  141. self.assertFalse(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  142. def test_exclusive_alias(self) -> None:
  143. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  144. _regex("#irc_.*:matrix.org", exclusive=True)
  145. )
  146. self.assertTrue(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  147. def test_exclusive_user(self) -> None:
  148. self.service.namespaces[ApplicationService.NS_USERS].append(
  149. _regex("@irc_.*:matrix.org", exclusive=True)
  150. )
  151. self.assertTrue(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  152. def test_exclusive_room(self) -> None:
  153. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  154. _regex("!irc_.*:matrix.org", exclusive=True)
  155. )
  156. self.assertTrue(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  157. @defer.inlineCallbacks
  158. def test_regex_alias_no_match(
  159. self,
  160. ) -> Generator["defer.Deferred[Any]", object, None]:
  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_local_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(
  179. self,
  180. ) -> Generator["defer.Deferred[Any]", object, None]:
  181. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  182. _regex("#irc_.*:matrix.org")
  183. )
  184. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  185. self.event.sender = "@irc_foobar:matrix.org"
  186. self.store.get_aliases_for_room = simple_async_mock(["#irc_barfoo:matrix.org"])
  187. self.store.get_local_users_in_room = simple_async_mock([])
  188. self.assertTrue(
  189. (
  190. yield self.service.is_interested_in_event(
  191. self.event.event_id, self.event, self.store
  192. )
  193. )
  194. )
  195. @defer.inlineCallbacks
  196. def test_interested_in_self(self) -> Generator["defer.Deferred[Any]", object, None]:
  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 self.service.is_interested_in_event(
  206. self.event.event_id, self.event, self.store
  207. )
  208. )
  209. )
  210. @defer.inlineCallbacks
  211. def test_member_list_match(self) -> Generator["defer.Deferred[Any]", object, None]:
  212. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  213. # Note that @irc_fo:here is the AS user.
  214. self.store.get_local_users_in_room = simple_async_mock(
  215. ["@alice:here", "@irc_fo:here", "@bob:here"]
  216. )
  217. self.store.get_aliases_for_room = simple_async_mock([])
  218. self.event.sender = "@xmpp_foobar:matrix.org"
  219. self.assertTrue(
  220. (
  221. yield self.service.is_interested_in_event(
  222. self.event.event_id, self.event, self.store
  223. )
  224. )
  225. )