1
0

test_complexity.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # Copyright 2019 Matrix.org Foundation
  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. from unittest.mock import Mock
  15. from synapse.api.errors import Codes, SynapseError
  16. from synapse.rest import admin
  17. from synapse.rest.client.v1 import login, room
  18. from synapse.types import UserID
  19. from tests import unittest
  20. from tests.test_utils import make_awaitable
  21. class RoomComplexityTests(unittest.FederatingHomeserverTestCase):
  22. servlets = [
  23. admin.register_servlets,
  24. room.register_servlets,
  25. login.register_servlets,
  26. ]
  27. def default_config(self):
  28. config = super().default_config()
  29. config["limit_remote_rooms"] = {"enabled": True, "complexity": 0.05}
  30. return config
  31. def test_complexity_simple(self):
  32. u1 = self.register_user("u1", "pass")
  33. u1_token = self.login("u1", "pass")
  34. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  35. self.helper.send_state(
  36. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  37. )
  38. # Get the room complexity
  39. channel = self.make_request(
  40. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  41. )
  42. self.assertEquals(200, channel.code)
  43. complexity = channel.json_body["v1"]
  44. self.assertTrue(complexity > 0, complexity)
  45. # Artificially raise the complexity
  46. store = self.hs.get_datastore()
  47. store.get_current_state_event_counts = lambda x: make_awaitable(500 * 1.23)
  48. # Get the room complexity again -- make sure it's our artificial value
  49. channel = self.make_request(
  50. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  51. )
  52. self.assertEquals(200, channel.code)
  53. complexity = channel.json_body["v1"]
  54. self.assertEqual(complexity, 1.23)
  55. def test_join_too_large(self):
  56. u1 = self.register_user("u1", "pass")
  57. handler = self.hs.get_room_member_handler()
  58. fed_transport = self.hs.get_federation_transport_client()
  59. # Mock out some things, because we don't want to test the whole join
  60. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  61. handler.federation_handler.do_invite_join = Mock(
  62. return_value=make_awaitable(("", 1))
  63. )
  64. d = handler._remote_join(
  65. None,
  66. ["other.example.com"],
  67. "roomid",
  68. UserID.from_string(u1),
  69. {"membership": "join"},
  70. )
  71. self.pump()
  72. # The request failed with a SynapseError saying the resource limit was
  73. # exceeded.
  74. f = self.get_failure(d, SynapseError)
  75. self.assertEqual(f.value.code, 400, f.value)
  76. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  77. def test_join_too_large_admin(self):
  78. # Check whether an admin can join if option "admins_can_join" is undefined,
  79. # this option defaults to false, so the join should fail.
  80. u1 = self.register_user("u1", "pass", admin=True)
  81. handler = self.hs.get_room_member_handler()
  82. fed_transport = self.hs.get_federation_transport_client()
  83. # Mock out some things, because we don't want to test the whole join
  84. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  85. handler.federation_handler.do_invite_join = Mock(
  86. return_value=make_awaitable(("", 1))
  87. )
  88. d = handler._remote_join(
  89. None,
  90. ["other.example.com"],
  91. "roomid",
  92. UserID.from_string(u1),
  93. {"membership": "join"},
  94. )
  95. self.pump()
  96. # The request failed with a SynapseError saying the resource limit was
  97. # exceeded.
  98. f = self.get_failure(d, SynapseError)
  99. self.assertEqual(f.value.code, 400, f.value)
  100. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  101. def test_join_too_large_once_joined(self):
  102. u1 = self.register_user("u1", "pass")
  103. u1_token = self.login("u1", "pass")
  104. # Ok, this might seem a bit weird -- I want to test that we actually
  105. # leave the room, but I don't want to simulate two servers. So, we make
  106. # a local room, which we say we're joining remotely, even if there's no
  107. # remote, because we mock that out. Then, we'll leave the (actually
  108. # local) room, which will be propagated over federation in a real
  109. # scenario.
  110. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  111. handler = self.hs.get_room_member_handler()
  112. fed_transport = self.hs.get_federation_transport_client()
  113. # Mock out some things, because we don't want to test the whole join
  114. fed_transport.client.get_json = Mock(return_value=make_awaitable(None))
  115. handler.federation_handler.do_invite_join = Mock(
  116. return_value=make_awaitable(("", 1))
  117. )
  118. # Artificially raise the complexity
  119. self.hs.get_datastore().get_current_state_event_counts = (
  120. lambda x: make_awaitable(600)
  121. )
  122. d = handler._remote_join(
  123. None,
  124. ["other.example.com"],
  125. room_1,
  126. UserID.from_string(u1),
  127. {"membership": "join"},
  128. )
  129. self.pump()
  130. # The request failed with a SynapseError saying the resource limit was
  131. # exceeded.
  132. f = self.get_failure(d, SynapseError)
  133. self.assertEqual(f.value.code, 400)
  134. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  135. class RoomComplexityAdminTests(unittest.FederatingHomeserverTestCase):
  136. # Test the behavior of joining rooms which exceed the complexity if option
  137. # limit_remote_rooms.admins_can_join is True.
  138. servlets = [
  139. admin.register_servlets,
  140. room.register_servlets,
  141. login.register_servlets,
  142. ]
  143. def default_config(self):
  144. config = super().default_config()
  145. config["limit_remote_rooms"] = {
  146. "enabled": True,
  147. "complexity": 0.05,
  148. "admins_can_join": True,
  149. }
  150. return config
  151. def test_join_too_large_no_admin(self):
  152. # A user which is not an admin should not be able to join a remote room
  153. # which is too complex.
  154. u1 = self.register_user("u1", "pass")
  155. handler = self.hs.get_room_member_handler()
  156. fed_transport = self.hs.get_federation_transport_client()
  157. # Mock out some things, because we don't want to test the whole join
  158. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  159. handler.federation_handler.do_invite_join = Mock(
  160. return_value=make_awaitable(("", 1))
  161. )
  162. d = handler._remote_join(
  163. None,
  164. ["other.example.com"],
  165. "roomid",
  166. UserID.from_string(u1),
  167. {"membership": "join"},
  168. )
  169. self.pump()
  170. # The request failed with a SynapseError saying the resource limit was
  171. # exceeded.
  172. f = self.get_failure(d, SynapseError)
  173. self.assertEqual(f.value.code, 400, f.value)
  174. self.assertEqual(f.value.errcode, Codes.RESOURCE_LIMIT_EXCEEDED)
  175. def test_join_too_large_admin(self):
  176. # An admin should be able to join rooms where a complexity check fails.
  177. u1 = self.register_user("u1", "pass", admin=True)
  178. handler = self.hs.get_room_member_handler()
  179. fed_transport = self.hs.get_federation_transport_client()
  180. # Mock out some things, because we don't want to test the whole join
  181. fed_transport.client.get_json = Mock(return_value=make_awaitable({"v1": 9999}))
  182. handler.federation_handler.do_invite_join = Mock(
  183. return_value=make_awaitable(("", 1))
  184. )
  185. d = handler._remote_join(
  186. None,
  187. ["other.example.com"],
  188. "roomid",
  189. UserID.from_string(u1),
  190. {"membership": "join"},
  191. )
  192. self.pump()
  193. # The request success since the user is an admin
  194. self.get_success(d)