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