test_appservice.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import re
  16. from mock import Mock
  17. from twisted.internet import defer
  18. from synapse.appservice import ApplicationService
  19. from tests import unittest
  20. def _regex(regex, exclusive=True):
  21. return {"regex": re.compile(regex), "exclusive": exclusive}
  22. class ApplicationServiceTestCase(unittest.TestCase):
  23. def setUp(self):
  24. self.service = ApplicationService(
  25. id="unique_identifier",
  26. url="some_url",
  27. token="some_token",
  28. hostname="matrix.org", # only used by get_groups_for_user
  29. namespaces={
  30. ApplicationService.NS_USERS: [],
  31. ApplicationService.NS_ROOMS: [],
  32. ApplicationService.NS_ALIASES: [],
  33. },
  34. )
  35. self.event = Mock(
  36. type="m.something", room_id="!foo:bar", sender="@someone:somewhere"
  37. )
  38. self.store = 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. (yield defer.ensureDeferred(self.service.is_interested(self.event)))
  45. )
  46. @defer.inlineCallbacks
  47. def test_regex_user_id_prefix_no_match(self):
  48. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  49. self.event.sender = "@someone_else:matrix.org"
  50. self.assertFalse(
  51. (yield defer.ensureDeferred(self.service.is_interested(self.event)))
  52. )
  53. @defer.inlineCallbacks
  54. def test_regex_room_member_is_checked(self):
  55. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  56. self.event.sender = "@someone_else:matrix.org"
  57. self.event.type = "m.room.member"
  58. self.event.state_key = "@irc_foobar:matrix.org"
  59. self.assertTrue(
  60. (yield defer.ensureDeferred(self.service.is_interested(self.event)))
  61. )
  62. @defer.inlineCallbacks
  63. def test_regex_room_id_match(self):
  64. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  65. _regex("!some_prefix.*some_suffix:matrix.org")
  66. )
  67. self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
  68. self.assertTrue(
  69. (yield defer.ensureDeferred(self.service.is_interested(self.event)))
  70. )
  71. @defer.inlineCallbacks
  72. def test_regex_room_id_no_match(self):
  73. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  74. _regex("!some_prefix.*some_suffix:matrix.org")
  75. )
  76. self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org"
  77. self.assertFalse(
  78. (yield defer.ensureDeferred(self.service.is_interested(self.event)))
  79. )
  80. @defer.inlineCallbacks
  81. def test_regex_alias_match(self):
  82. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  83. _regex("#irc_.*:matrix.org")
  84. )
  85. self.store.get_aliases_for_room.return_value = defer.succeed(
  86. ["#irc_foobar:matrix.org", "#athing:matrix.org"]
  87. )
  88. self.store.get_users_in_room.return_value = defer.succeed([])
  89. self.assertTrue(
  90. (
  91. yield defer.ensureDeferred(
  92. self.service.is_interested(self.event, self.store)
  93. )
  94. )
  95. )
  96. def test_non_exclusive_alias(self):
  97. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  98. _regex("#irc_.*:matrix.org", exclusive=False)
  99. )
  100. self.assertFalse(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  101. def test_non_exclusive_room(self):
  102. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  103. _regex("!irc_.*:matrix.org", exclusive=False)
  104. )
  105. self.assertFalse(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  106. def test_non_exclusive_user(self):
  107. self.service.namespaces[ApplicationService.NS_USERS].append(
  108. _regex("@irc_.*:matrix.org", exclusive=False)
  109. )
  110. self.assertFalse(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  111. def test_exclusive_alias(self):
  112. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  113. _regex("#irc_.*:matrix.org", exclusive=True)
  114. )
  115. self.assertTrue(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  116. def test_exclusive_user(self):
  117. self.service.namespaces[ApplicationService.NS_USERS].append(
  118. _regex("@irc_.*:matrix.org", exclusive=True)
  119. )
  120. self.assertTrue(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  121. def test_exclusive_room(self):
  122. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  123. _regex("!irc_.*:matrix.org", exclusive=True)
  124. )
  125. self.assertTrue(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  126. @defer.inlineCallbacks
  127. def test_regex_alias_no_match(self):
  128. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  129. _regex("#irc_.*:matrix.org")
  130. )
  131. self.store.get_aliases_for_room.return_value = defer.succeed(
  132. ["#xmpp_foobar:matrix.org", "#athing:matrix.org"]
  133. )
  134. self.store.get_users_in_room.return_value = defer.succeed([])
  135. self.assertFalse(
  136. (
  137. yield defer.ensureDeferred(
  138. self.service.is_interested(self.event, self.store)
  139. )
  140. )
  141. )
  142. @defer.inlineCallbacks
  143. def test_regex_multiple_matches(self):
  144. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  145. _regex("#irc_.*:matrix.org")
  146. )
  147. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  148. self.event.sender = "@irc_foobar:matrix.org"
  149. self.store.get_aliases_for_room.return_value = defer.succeed(
  150. ["#irc_barfoo:matrix.org"]
  151. )
  152. self.store.get_users_in_room.return_value = defer.succeed([])
  153. self.assertTrue(
  154. (
  155. yield defer.ensureDeferred(
  156. self.service.is_interested(self.event, self.store)
  157. )
  158. )
  159. )
  160. @defer.inlineCallbacks
  161. def test_interested_in_self(self):
  162. # make sure invites get through
  163. self.service.sender = "@appservice:name"
  164. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  165. self.event.type = "m.room.member"
  166. self.event.content = {"membership": "invite"}
  167. self.event.state_key = self.service.sender
  168. self.assertTrue(
  169. (yield defer.ensureDeferred(self.service.is_interested(self.event)))
  170. )
  171. @defer.inlineCallbacks
  172. def test_member_list_match(self):
  173. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  174. # Note that @irc_fo:here is the AS user.
  175. self.store.get_users_in_room.return_value = defer.succeed(
  176. ["@alice:here", "@irc_fo:here", "@bob:here"]
  177. )
  178. self.store.get_aliases_for_room.return_value = defer.succeed([])
  179. self.event.sender = "@xmpp_foobar:matrix.org"
  180. self.assertTrue(
  181. (
  182. yield defer.ensureDeferred(
  183. self.service.is_interested(event=self.event, store=self.store)
  184. )
  185. )
  186. )