test_appservice.py 9.1 KB

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