test_appservice.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. from synapse.appservice import ApplicationService
  16. from mock import Mock
  17. from tests import unittest
  18. def _regex(regex, exclusive=True):
  19. return {
  20. "regex": regex,
  21. "exclusive": exclusive
  22. }
  23. class ApplicationServiceTestCase(unittest.TestCase):
  24. def setUp(self):
  25. self.service = ApplicationService(
  26. id="unique_identifier",
  27. url="some_url",
  28. token="some_token",
  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. def test_regex_user_id_prefix_match(self):
  39. self.service.namespaces[ApplicationService.NS_USERS].append(
  40. _regex("@irc_.*")
  41. )
  42. self.event.sender = "@irc_foobar:matrix.org"
  43. self.assertTrue(self.service.is_interested(self.event))
  44. def test_regex_user_id_prefix_no_match(self):
  45. self.service.namespaces[ApplicationService.NS_USERS].append(
  46. _regex("@irc_.*")
  47. )
  48. self.event.sender = "@someone_else:matrix.org"
  49. self.assertFalse(self.service.is_interested(self.event))
  50. def test_regex_room_member_is_checked(self):
  51. self.service.namespaces[ApplicationService.NS_USERS].append(
  52. _regex("@irc_.*")
  53. )
  54. self.event.sender = "@someone_else:matrix.org"
  55. self.event.type = "m.room.member"
  56. self.event.state_key = "@irc_foobar:matrix.org"
  57. self.assertTrue(self.service.is_interested(self.event))
  58. def test_regex_room_id_match(self):
  59. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  60. _regex("!some_prefix.*some_suffix:matrix.org")
  61. )
  62. self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
  63. self.assertTrue(self.service.is_interested(self.event))
  64. def test_regex_room_id_no_match(self):
  65. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  66. _regex("!some_prefix.*some_suffix:matrix.org")
  67. )
  68. self.event.room_id = "!XqBunHwQIXUiqCaoxq:matrix.org"
  69. self.assertFalse(self.service.is_interested(self.event))
  70. def test_regex_alias_match(self):
  71. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  72. _regex("#irc_.*:matrix.org")
  73. )
  74. self.assertTrue(self.service.is_interested(
  75. self.event,
  76. aliases_for_event=["#irc_foobar:matrix.org", "#athing:matrix.org"]
  77. ))
  78. def test_non_exclusive_alias(self):
  79. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  80. _regex("#irc_.*:matrix.org", exclusive=False)
  81. )
  82. self.assertFalse(self.service.is_exclusive_alias(
  83. "#irc_foobar:matrix.org"
  84. ))
  85. def test_non_exclusive_room(self):
  86. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  87. _regex("!irc_.*:matrix.org", exclusive=False)
  88. )
  89. self.assertFalse(self.service.is_exclusive_room(
  90. "!irc_foobar:matrix.org"
  91. ))
  92. def test_non_exclusive_user(self):
  93. self.service.namespaces[ApplicationService.NS_USERS].append(
  94. _regex("@irc_.*:matrix.org", exclusive=False)
  95. )
  96. self.assertFalse(self.service.is_exclusive_user(
  97. "@irc_foobar:matrix.org"
  98. ))
  99. def test_exclusive_alias(self):
  100. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  101. _regex("#irc_.*:matrix.org", exclusive=True)
  102. )
  103. self.assertTrue(self.service.is_exclusive_alias(
  104. "#irc_foobar:matrix.org"
  105. ))
  106. def test_exclusive_user(self):
  107. self.service.namespaces[ApplicationService.NS_USERS].append(
  108. _regex("@irc_.*:matrix.org", exclusive=True)
  109. )
  110. self.assertTrue(self.service.is_exclusive_user(
  111. "@irc_foobar:matrix.org"
  112. ))
  113. def test_exclusive_room(self):
  114. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  115. _regex("!irc_.*:matrix.org", exclusive=True)
  116. )
  117. self.assertTrue(self.service.is_exclusive_room(
  118. "!irc_foobar:matrix.org"
  119. ))
  120. def test_regex_alias_no_match(self):
  121. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  122. _regex("#irc_.*:matrix.org")
  123. )
  124. self.assertFalse(self.service.is_interested(
  125. self.event,
  126. aliases_for_event=["#xmpp_foobar:matrix.org", "#athing:matrix.org"]
  127. ))
  128. def test_regex_multiple_matches(self):
  129. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  130. _regex("#irc_.*:matrix.org")
  131. )
  132. self.service.namespaces[ApplicationService.NS_USERS].append(
  133. _regex("@irc_.*")
  134. )
  135. self.event.sender = "@irc_foobar:matrix.org"
  136. self.assertTrue(self.service.is_interested(
  137. self.event,
  138. aliases_for_event=["#irc_barfoo:matrix.org"]
  139. ))
  140. def test_restrict_to_rooms(self):
  141. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  142. _regex("!flibble_.*:matrix.org")
  143. )
  144. self.service.namespaces[ApplicationService.NS_USERS].append(
  145. _regex("@irc_.*")
  146. )
  147. self.event.sender = "@irc_foobar:matrix.org"
  148. self.event.room_id = "!wibblewoo:matrix.org"
  149. self.assertFalse(self.service.is_interested(
  150. self.event,
  151. restrict_to=ApplicationService.NS_ROOMS
  152. ))
  153. def test_restrict_to_aliases(self):
  154. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  155. _regex("#xmpp_.*:matrix.org")
  156. )
  157. self.service.namespaces[ApplicationService.NS_USERS].append(
  158. _regex("@irc_.*")
  159. )
  160. self.event.sender = "@irc_foobar:matrix.org"
  161. self.assertFalse(self.service.is_interested(
  162. self.event,
  163. restrict_to=ApplicationService.NS_ALIASES,
  164. aliases_for_event=["#irc_barfoo:matrix.org"]
  165. ))
  166. def test_restrict_to_senders(self):
  167. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  168. _regex("#xmpp_.*:matrix.org")
  169. )
  170. self.service.namespaces[ApplicationService.NS_USERS].append(
  171. _regex("@irc_.*")
  172. )
  173. self.event.sender = "@xmpp_foobar:matrix.org"
  174. self.assertFalse(self.service.is_interested(
  175. self.event,
  176. restrict_to=ApplicationService.NS_USERS,
  177. aliases_for_event=["#xmpp_barfoo:matrix.org"]
  178. ))
  179. def test_interested_in_self(self):
  180. # make sure invites get through
  181. self.service.sender = "@appservice:name"
  182. self.service.namespaces[ApplicationService.NS_USERS].append(
  183. _regex("@irc_.*")
  184. )
  185. self.event.type = "m.room.member"
  186. self.event.content = {
  187. "membership": "invite"
  188. }
  189. self.event.state_key = self.service.sender
  190. self.assertTrue(self.service.is_interested(self.event))
  191. def test_member_list_match(self):
  192. self.service.namespaces[ApplicationService.NS_USERS].append(
  193. _regex("@irc_.*")
  194. )
  195. join_list = [
  196. "@alice:here",
  197. "@irc_fo:here", # AS user
  198. "@bob:here",
  199. ]
  200. self.event.sender = "@xmpp_foobar:matrix.org"
  201. self.assertTrue(self.service.is_interested(
  202. event=self.event,
  203. member_list=join_list
  204. ))