test_appservice.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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((yield self.service.is_interested(self.event)))
  44. @defer.inlineCallbacks
  45. def test_regex_user_id_prefix_no_match(self):
  46. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  47. self.event.sender = "@someone_else:matrix.org"
  48. self.assertFalse((yield self.service.is_interested(self.event)))
  49. @defer.inlineCallbacks
  50. def test_regex_room_member_is_checked(self):
  51. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  52. self.event.sender = "@someone_else:matrix.org"
  53. self.event.type = "m.room.member"
  54. self.event.state_key = "@irc_foobar:matrix.org"
  55. self.assertTrue((yield self.service.is_interested(self.event)))
  56. @defer.inlineCallbacks
  57. def test_regex_room_id_match(self):
  58. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  59. _regex("!some_prefix.*some_suffix:matrix.org")
  60. )
  61. self.event.room_id = "!some_prefixs0m3th1nGsome_suffix:matrix.org"
  62. self.assertTrue((yield self.service.is_interested(self.event)))
  63. @defer.inlineCallbacks
  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((yield self.service.is_interested(self.event)))
  70. @defer.inlineCallbacks
  71. def test_regex_alias_match(self):
  72. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  73. _regex("#irc_.*:matrix.org")
  74. )
  75. self.store.get_aliases_for_room.return_value = [
  76. "#irc_foobar:matrix.org",
  77. "#athing:matrix.org",
  78. ]
  79. self.store.get_users_in_room.return_value = []
  80. self.assertTrue((yield self.service.is_interested(self.event, self.store)))
  81. def test_non_exclusive_alias(self):
  82. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  83. _regex("#irc_.*:matrix.org", exclusive=False)
  84. )
  85. self.assertFalse(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  86. def test_non_exclusive_room(self):
  87. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  88. _regex("!irc_.*:matrix.org", exclusive=False)
  89. )
  90. self.assertFalse(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  91. def test_non_exclusive_user(self):
  92. self.service.namespaces[ApplicationService.NS_USERS].append(
  93. _regex("@irc_.*:matrix.org", exclusive=False)
  94. )
  95. self.assertFalse(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  96. def test_exclusive_alias(self):
  97. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  98. _regex("#irc_.*:matrix.org", exclusive=True)
  99. )
  100. self.assertTrue(self.service.is_exclusive_alias("#irc_foobar:matrix.org"))
  101. def test_exclusive_user(self):
  102. self.service.namespaces[ApplicationService.NS_USERS].append(
  103. _regex("@irc_.*:matrix.org", exclusive=True)
  104. )
  105. self.assertTrue(self.service.is_exclusive_user("@irc_foobar:matrix.org"))
  106. def test_exclusive_room(self):
  107. self.service.namespaces[ApplicationService.NS_ROOMS].append(
  108. _regex("!irc_.*:matrix.org", exclusive=True)
  109. )
  110. self.assertTrue(self.service.is_exclusive_room("!irc_foobar:matrix.org"))
  111. @defer.inlineCallbacks
  112. def test_regex_alias_no_match(self):
  113. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  114. _regex("#irc_.*:matrix.org")
  115. )
  116. self.store.get_aliases_for_room.return_value = [
  117. "#xmpp_foobar:matrix.org",
  118. "#athing:matrix.org",
  119. ]
  120. self.store.get_users_in_room.return_value = []
  121. self.assertFalse((yield self.service.is_interested(self.event, self.store)))
  122. @defer.inlineCallbacks
  123. def test_regex_multiple_matches(self):
  124. self.service.namespaces[ApplicationService.NS_ALIASES].append(
  125. _regex("#irc_.*:matrix.org")
  126. )
  127. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  128. self.event.sender = "@irc_foobar:matrix.org"
  129. self.store.get_aliases_for_room.return_value = ["#irc_barfoo:matrix.org"]
  130. self.store.get_users_in_room.return_value = []
  131. self.assertTrue((yield self.service.is_interested(self.event, self.store)))
  132. @defer.inlineCallbacks
  133. def test_interested_in_self(self):
  134. # make sure invites get through
  135. self.service.sender = "@appservice:name"
  136. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  137. self.event.type = "m.room.member"
  138. self.event.content = {"membership": "invite"}
  139. self.event.state_key = self.service.sender
  140. self.assertTrue((yield self.service.is_interested(self.event)))
  141. @defer.inlineCallbacks
  142. def test_member_list_match(self):
  143. self.service.namespaces[ApplicationService.NS_USERS].append(_regex("@irc_.*"))
  144. self.store.get_users_in_room.return_value = [
  145. "@alice:here",
  146. "@irc_fo:here", # AS user
  147. "@bob:here",
  148. ]
  149. self.store.get_aliases_for_room.return_value = []
  150. self.event.sender = "@xmpp_foobar:matrix.org"
  151. self.assertTrue(
  152. (yield self.service.is_interested(event=self.event, store=self.store))
  153. )