diff --git a/tests/api/test_ratelimiting.py b/tests/api/test_ratelimiting.py
index 973c7e007c..12425b1faa 100644
--- a/tests/api/test_ratelimiting.py
+++ b/tests/api/test_ratelimiting.py
@@ -5,7 +5,7 @@ from tests import unittest
class TestRatelimiter(unittest.TestCase):
def test_allowed(self):
- limiter = Ratelimiter(rate_hz=0.1, burst_count=1)
+ limiter = Ratelimiter(clock=None, rate_hz=0.1, burst_count=1)
allowed, time_allowed = limiter.can_do_action(key="test_id", time_now_s=0)
self.assertTrue(allowed)
self.assertEquals(10.0, time_allowed)
@@ -19,7 +19,7 @@ class TestRatelimiter(unittest.TestCase):
self.assertEquals(20.0, time_allowed)
def test_pruning(self):
- limiter = Ratelimiter(rate_hz=0.1, burst_count=1)
+ limiter = Ratelimiter(clock=None, rate_hz=0.1, burst_count=1)
_, _ = limiter.can_do_action(key="test_id_1", time_now_s=0)
self.assertIn("test_id_1", limiter.actions)
diff --git a/tests/handlers/test_profile.py b/tests/handlers/test_profile.py
index 891c986fbc..5af3db2cd5 100644
--- a/tests/handlers/test_profile.py
+++ b/tests/handlers/test_profile.py
@@ -14,12 +14,13 @@
# limitations under the License.
-from mock import Mock, NonCallableMock
+from mock import Mock, patch
from twisted.internet import defer
import synapse.types
from synapse.api.errors import AuthError, SynapseError
+from synapse.api.ratelimiting import Ratelimiter
from synapse.handlers.profile import MasterProfileHandler
from synapse.types import UserID
@@ -55,17 +56,15 @@ class ProfileTestCase(unittest.TestCase):
federation_client=self.mock_federation,
federation_server=Mock(),
federation_registry=self.mock_registry,
- request_ratelimiter=NonCallableMock(
- spec_set=["can_do_action", "ratelimit"]
- ),
- login_ratelimiter=NonCallableMock(spec_set=["can_do_action", "ratelimit"]),
)
- self.request_ratelimiter = hs.get_request_ratelimiter()
- self.request_ratelimiter.can_do_action.return_value = (True, 0)
-
- self.login_ratelimiter = hs.get_login_ratelimiter()
- self.login_ratelimiter.can_do_action.return_value = (True, 0)
+ # Patch Ratelimiter to allow all requests
+ patch.object(
+ Ratelimiter, "can_do_action", new_callable=lambda *args, **kwargs: (True, 0.0)
+ )
+ patch.object(
+ Ratelimiter, "ratelimit", new_callable=lambda *args, **kwargs: None
+ )
self.store = hs.get_datastore()
diff --git a/tests/replication/slave/storage/_base.py b/tests/replication/slave/storage/_base.py
index 49d22d9487..8b9dc57b44 100644
--- a/tests/replication/slave/storage/_base.py
+++ b/tests/replication/slave/storage/_base.py
@@ -13,8 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from mock import Mock, NonCallableMock
+from mock import Mock, patch
+from synapse.api.ratelimiting import Ratelimiter
from tests.replication._base import BaseStreamTestCase
@@ -23,18 +24,15 @@ class BaseSlavedStoreTestCase(BaseStreamTestCase):
hs = self.setup_test_homeserver(
federation_client=Mock(),
- request_ratelimiter=NonCallableMock(
- spec_set=["can_do_action", "ratelimit"]
- ),
- login_ratelimiter=NonCallableMock(spec_set=["can_do_action", "ratelimit"]),
)
- # Prevent ratelimiting
- self.request_ratelimiter = hs.get_request_ratelimiter()
- self.request_ratelimiter.can_do_action.return_value = (True, 0)
-
- self.login_ratelimiter = hs.get_login_ratelimiter()
- self.login_ratelimiter.can_do_action.return_value = (True, 0)
+ # Patch Ratelimiter to allow all requests
+ patch.object(
+ Ratelimiter, "can_do_action", new_callable=lambda *args, **kwargs: (True, 0.0)
+ )
+ patch.object(
+ Ratelimiter, "ratelimit", new_callable=lambda *args, **kwargs: None
+ )
return hs
diff --git a/tests/rest/client/v1/test_events.py b/tests/rest/client/v1/test_events.py
index 1ceba01494..1b946388a6 100644
--- a/tests/rest/client/v1/test_events.py
+++ b/tests/rest/client/v1/test_events.py
@@ -15,10 +15,11 @@
""" Tests REST events for /events paths."""
-from mock import Mock, NonCallableMock
+from mock import Mock, patch
import synapse.rest.admin
from synapse.rest.client.v1 import events, login, room
+from synapse.api.ratelimiting import Ratelimiter
from tests import unittest
@@ -42,17 +43,15 @@ class EventStreamPermissionsTestCase(unittest.HomeserverTestCase):
hs = self.setup_test_homeserver(
config=config,
- request_ratelimiter=NonCallableMock(
- # rate_hz and burst_count are overridden in BaseHandler
- spec_set=["can_do_action", "ratelimit", "rate_hz", "burst_count"]
- ),
- login_ratelimiter=NonCallableMock(spec_set=["can_do_action", "ratelimit"]),
)
- self.request_ratelimiter = hs.get_request_ratelimiter()
- self.request_ratelimiter.can_do_action.return_value = (True, 0)
- self.login_ratelimiter = hs.get_login_ratelimiter()
- self.login_ratelimiter.can_do_action.return_value = (True, 0)
+ # Patch Ratelimiter to allow all requests
+ patch.object(
+ Ratelimiter, "can_do_action", new_callable=lambda *args, **kwargs: (True, 0.0)
+ )
+ patch.object(
+ Ratelimiter, "ratelimit", new_callable=lambda *args, **kwargs: None
+ )
hs.get_handlers().federation_handler = Mock()
diff --git a/tests/rest/client/v1/test_login.py b/tests/rest/client/v1/test_login.py
index c01738ed69..a9952f53a6 100644
--- a/tests/rest/client/v1/test_login.py
+++ b/tests/rest/client/v1/test_login.py
@@ -7,6 +7,7 @@ import synapse.rest.admin
from synapse.rest.client.v1 import login, logout
from synapse.rest.client.v2_alpha import devices
from synapse.rest.client.v2_alpha.account import WhoamiRestServlet
+from synapse.api.ratelimiting import Ratelimiter
from tests import unittest
from tests.unittest import override_config
@@ -26,7 +27,6 @@ class LoginRestServletTestCase(unittest.HomeserverTestCase):
]
def make_homeserver(self, reactor, clock):
-
self.hs = self.setup_test_homeserver()
self.hs.config.enable_registration = True
self.hs.config.registrations_require_3pid = []
@@ -35,10 +35,17 @@ class LoginRestServletTestCase(unittest.HomeserverTestCase):
return self.hs
+ @override_config(
+ {
+ "rc_login": {
+ "account": {
+ "per_second": 0.17,
+ "burst_count": 5,
+ }
+ }
+ }
+ )
def test_POST_ratelimiting_per_address(self):
- self.hs.get_login_ratelimiter().burst_count = 5
- self.hs.get_login_ratelimiter().rate_hz = 0.17
-
# Create different users so we're sure not to be bothered by the per-user
# ratelimiter.
for i in range(0, 6):
@@ -77,10 +84,17 @@ class LoginRestServletTestCase(unittest.HomeserverTestCase):
self.assertEquals(channel.result["code"], b"200", channel.result)
+ @override_config(
+ {
+ "rc_login": {
+ "account": {
+ "per_second": 0.17,
+ "burst_count": 5,
+ }
+ }
+ }
+ )
def test_POST_ratelimiting_per_account(self):
- self.hs.get_login_ratelimiter().burst_count = 5
- self.hs.get_login_ratelimiter().rate_hz = 0.17
-
self.register_user("kermit", "monkey")
for i in range(0, 6):
@@ -116,10 +130,23 @@ class LoginRestServletTestCase(unittest.HomeserverTestCase):
self.assertEquals(channel.result["code"], b"200", channel.result)
+ @override_config(
+ {
+ "rc_login": {
+ # Prevent the generic login ratelimiter from raising first
+ "address": {
+ "per_second": 1000,
+ "burst_count": 1000,
+ },
+ "failed_attempts": {
+ "per_second": 0.17,
+ "burst_count": 5,
+ }
+ }
+ }
+ )
+ @unittest.DEBUG
def test_POST_ratelimiting_per_account_failed_attempts(self):
- self.hs.get_login_failed_attempts_ratelimiter().burst_count = 5
- self.hs.get_login_failed_attempts_ratelimiter().rate_hz = 0.17
-
self.register_user("kermit", "monkey")
for i in range(0, 6):
@@ -128,8 +155,7 @@ class LoginRestServletTestCase(unittest.HomeserverTestCase):
"identifier": {"type": "m.id.user", "user": "kermit"},
"password": "notamonkey",
}
- request_data = json.dumps(params)
- request, channel = self.make_request(b"POST", LOGIN_URL, request_data)
+ request, channel = self.make_request(b"POST", LOGIN_URL, params)
self.render(request)
if i == 5:
@@ -149,7 +175,6 @@ class LoginRestServletTestCase(unittest.HomeserverTestCase):
"identifier": {"type": "m.id.user", "user": "kermit"},
"password": "notamonkey",
}
- request_data = json.dumps(params)
request, channel = self.make_request(b"POST", LOGIN_URL, params)
self.render(request)
diff --git a/tests/rest/client/v1/test_rooms.py b/tests/rest/client/v1/test_rooms.py
index ba10f34468..8b19bcef60 100644
--- a/tests/rest/client/v1/test_rooms.py
+++ b/tests/rest/client/v1/test_rooms.py
@@ -20,12 +20,13 @@
import json
-from mock import Mock, NonCallableMock
+from mock import Mock, patch
from six.moves.urllib import parse as urlparse
from twisted.internet import defer
import synapse.rest.admin
+from synapse.api.ratelimiting import Ratelimiter
from synapse.api.constants import EventContentFields, EventTypes, Membership
from synapse.handlers.pagination import PurgeStatus
from synapse.rest.client.v1 import directory, login, profile, room
@@ -49,16 +50,15 @@ class RoomBase(unittest.HomeserverTestCase):
"red",
http_client=None,
federation_client=Mock(),
- request_ratelimiter=NonCallableMock(
- spec_set=["can_do_action", "ratelimit"]
- ),
- login_ratelimiter=NonCallableMock(spec_set=["can_do_action", "ratelimit"]),
)
- self.request_ratelimiter = self.hs.get_request_ratelimiter()
- self.request_ratelimiter.can_do_action.return_value = (True, 0)
- self.login_ratelimiter = self.hs.get_login_ratelimiter()
- self.login_ratelimiter.can_do_action.return_value = (True, 0)
+ # Patch Ratelimiter to allow all requests
+ patch.object(
+ Ratelimiter, "can_do_action", new_callable=lambda *args, **kwargs: (True, 0.0)
+ )
+ patch.object(
+ Ratelimiter, "ratelimit", new_callable=lambda *args, **kwargs: None
+ )
self.hs.get_federation_handler = Mock(return_value=Mock())
diff --git a/tests/rest/client/v1/test_typing.py b/tests/rest/client/v1/test_typing.py
index 2ec678a2a2..f57d2f3356 100644
--- a/tests/rest/client/v1/test_typing.py
+++ b/tests/rest/client/v1/test_typing.py
@@ -16,12 +16,13 @@
"""Tests REST events for /rooms paths."""
-from mock import Mock, NonCallableMock
+from mock import Mock, NonCallableMock, patch
from twisted.internet import defer
from synapse.rest.client.v1 import room
from synapse.types import UserID
+from synapse.api.ratelimiting import Ratelimiter
from tests import unittest
@@ -42,19 +43,17 @@ class RoomTypingTestCase(unittest.HomeserverTestCase):
"red",
http_client=None,
federation_client=Mock(),
- request_ratelimiter=NonCallableMock(
- spec_set=["can_do_action", "ratelimit"]
- ),
- login_ratelimiter=NonCallableMock(spec_set=["can_do_action", "ratelimit"]),
)
- self.event_source = hs.get_event_sources().sources["typing"]
-
- self.request_ratelimiter = hs.get_request_ratelimiter()
- self.request_ratelimiter.can_do_action.return_value = (True, 0)
+ # Patch Ratelimiter to allow all requests
+ patch.object(
+ Ratelimiter, "can_do_action", new_callable=lambda *args, **kwargs: (True, 0.0)
+ )
+ patch.object(
+ Ratelimiter, "ratelimit", new_callable=lambda *args, **kwargs: None
+ )
- self.login_ratelimiter = hs.get_login_ratelimiter()
- self.login_ratelimiter.can_do_action.return_value = (True, 0)
+ self.event_source = hs.get_event_sources().sources["typing"]
hs.get_handlers().federation_handler = Mock()
|