test_complexity.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 Matrix.org Foundation
  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 twisted.internet import defer
  16. from synapse.config.ratelimiting import FederationRateLimitConfig
  17. from synapse.federation.transport import server
  18. from synapse.rest import admin
  19. from synapse.rest.client.v1 import login, room
  20. from synapse.util.ratelimitutils import FederationRateLimiter
  21. from tests import unittest
  22. class RoomComplexityTests(unittest.HomeserverTestCase):
  23. servlets = [
  24. admin.register_servlets,
  25. room.register_servlets,
  26. login.register_servlets,
  27. ]
  28. def default_config(self, name="test"):
  29. config = super(RoomComplexityTests, self).default_config(name=name)
  30. config["limit_large_remote_room_joins"] = True
  31. config["limit_large_remote_room_complexity"] = 0.05
  32. return config
  33. def prepare(self, reactor, clock, homeserver):
  34. class Authenticator(object):
  35. def authenticate_request(self, request, content):
  36. return defer.succeed("otherserver.nottld")
  37. ratelimiter = FederationRateLimiter(
  38. clock,
  39. FederationRateLimitConfig(
  40. window_size=1,
  41. sleep_limit=1,
  42. sleep_msec=1,
  43. reject_limit=1000,
  44. concurrent_requests=1000,
  45. ),
  46. )
  47. server.register_servlets(
  48. homeserver, self.resource, Authenticator(), ratelimiter
  49. )
  50. def test_complexity_simple(self):
  51. u1 = self.register_user("u1", "pass")
  52. u1_token = self.login("u1", "pass")
  53. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  54. self.helper.send_state(
  55. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  56. )
  57. # Get the room complexity
  58. request, channel = self.make_request(
  59. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  60. )
  61. self.render(request)
  62. self.assertEquals(200, channel.code)
  63. complexity = channel.json_body["v1"]
  64. self.assertTrue(complexity > 0, complexity)
  65. # Artificially raise the complexity
  66. store = self.hs.get_datastore()
  67. store.get_current_state_event_counts = lambda x: defer.succeed(500 * 1.23)
  68. # Get the room complexity again -- make sure it's our artificial value
  69. request, channel = self.make_request(
  70. "GET", "/_matrix/federation/unstable/rooms/%s/complexity" % (room_1,)
  71. )
  72. self.render(request)
  73. self.assertEquals(200, channel.code)
  74. complexity = channel.json_body["v1"]
  75. self.assertEqual(complexity, 1.23)