diff options
author | Mark Haines <mark.haines@matrix.org> | 2014-09-03 09:15:52 +0100 |
---|---|---|
committer | Mark Haines <mark.haines@matrix.org> | 2014-09-03 09:15:52 +0100 |
commit | 30ad0c567437d19ebb4b172c12a2e22f65d2dd9a (patch) | |
tree | 9961b92b2b0ea6084d0bdda11bf32cfde88fa833 /tests | |
parent | howto: Link jsfiddles correctly. Hide ugly TODOs. (diff) | |
parent | Fix tests to support ratelimiting (diff) | |
download | synapse-30ad0c567437d19ebb4b172c12a2e22f65d2dd9a.tar.xz |
Merge branch 'ratelimiting' into develop
Diffstat (limited to 'tests')
-rw-r--r-- | tests/api/__init__.py | 0 | ||||
-rw-r--r-- | tests/api/test_ratelimiting.py | 39 | ||||
-rw-r--r-- | tests/handlers/test_room.py | 13 | ||||
-rw-r--r-- | tests/rest/test_events.py | 9 | ||||
-rw-r--r-- | tests/rest/test_rooms.py | 44 |
5 files changed, 103 insertions, 2 deletions
diff --git a/tests/api/__init__.py b/tests/api/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/api/__init__.py diff --git a/tests/api/test_ratelimiting.py b/tests/api/test_ratelimiting.py new file mode 100644 index 0000000000..dc2f83c7eb --- /dev/null +++ b/tests/api/test_ratelimiting.py @@ -0,0 +1,39 @@ +from synapse.api.ratelimiting import Ratelimiter + +import unittest + +class TestRatelimiter(unittest.TestCase): + + def test_allowed(self): + limiter = Ratelimiter() + allowed, time_allowed = limiter.send_message( + user_id="test_id", time_now_s=0, msg_rate_hz=0.1, burst_count=1, + ) + self.assertTrue(allowed) + self.assertEquals(10., time_allowed) + + allowed, time_allowed = limiter.send_message( + user_id="test_id", time_now_s=5, msg_rate_hz=0.1, burst_count=1, + ) + self.assertFalse(allowed) + self.assertEquals(10., time_allowed) + + allowed, time_allowed = limiter.send_message( + user_id="test_id", time_now_s=10, msg_rate_hz=0.1, burst_count=1 + ) + self.assertTrue(allowed) + self.assertEquals(20., time_allowed) + + def test_pruning(self): + limiter = Ratelimiter() + allowed, time_allowed = limiter.send_message( + user_id="test_id_1", time_now_s=0, msg_rate_hz=0.1, burst_count=1, + ) + + self.assertIn("test_id_1", limiter.message_counts) + + allowed, time_allowed = limiter.send_message( + user_id="test_id_2", time_now_s=10, msg_rate_hz=0.1, burst_count=1 + ) + + self.assertNotIn("test_id_1", limiter.message_counts) diff --git a/tests/handlers/test_room.py b/tests/handlers/test_room.py index 219a53c426..4591a5ea58 100644 --- a/tests/handlers/test_room.py +++ b/tests/handlers/test_room.py @@ -39,6 +39,10 @@ class RoomMemberHandlerTestCase(unittest.TestCase): hs = HomeServer( self.hostname, db_pool=None, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), datastore=NonCallableMock(spec_set=[ "persist_event", "get_joined_hosts_for_room", @@ -82,6 +86,8 @@ class RoomMemberHandlerTestCase(unittest.TestCase): self.snapshot = Mock() self.datastore.snapshot_room.return_value = self.snapshot + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) @defer.inlineCallbacks def test_invite(self): @@ -342,6 +348,10 @@ class RoomCreationTest(unittest.TestCase): ]), auth=NonCallableMock(spec_set=["check"]), state_handler=NonCallableMock(spec_set=["handle_new_event"]), + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) self.federation = NonCallableMock(spec_set=[ @@ -368,6 +378,9 @@ class RoomCreationTest(unittest.TestCase): return defer.succeed([]) self.datastore.get_joined_hosts_for_room.side_effect = hosts + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + @defer.inlineCallbacks def test_room_creation(self): user_id = "@foo:red" diff --git a/tests/rest/test_events.py b/tests/rest/test_events.py index 1d1336d12d..c8527f3517 100644 --- a/tests/rest/test_events.py +++ b/tests/rest/test_events.py @@ -32,7 +32,7 @@ import logging from ..utils import MockHttpResource, MemoryDataStore from .utils import RestTestCase -from mock import Mock +from mock import Mock, NonCallableMock logging.getLogger().addHandler(logging.NullHandler()) @@ -136,8 +136,15 @@ class EventStreamPermissionsTestCase(RestTestCase): "call_later", "cancel_call_later", "time_msec", + "time" ]), + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) hs.get_handlers().federation_handler = Mock() diff --git a/tests/rest/test_rooms.py b/tests/rest/test_rooms.py index cdaf948a3b..23c50824c7 100644 --- a/tests/rest/test_rooms.py +++ b/tests/rest/test_rooms.py @@ -30,7 +30,7 @@ import urllib from ..utils import MockHttpResource, MemoryDataStore from .utils import RestTestCase -from mock import Mock +from mock import Mock, NonCallableMock PATH_PREFIX = "/_matrix/client/api/v1" @@ -58,7 +58,14 @@ class RoomPermissionsTestCase(RestTestCase): replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): @@ -405,7 +412,14 @@ class RoomsMemberListTestCase(RestTestCase): replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + hs.get_handlers().federation_handler = Mock() self.auth_user_id = self.user_id @@ -483,7 +497,14 @@ class RoomsCreateTestCase(RestTestCase): replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): @@ -573,7 +594,14 @@ class RoomTopicTestCase(RestTestCase): replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): @@ -676,7 +704,14 @@ class RoomMemberStateTestCase(RestTestCase): replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): @@ -801,7 +836,14 @@ class RoomMessagesTestCase(RestTestCase): replication_layer=Mock(), state_handler=state_handler, persistence_service=persistence_service, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=NonCallableMock(), ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + hs.get_handlers().federation_handler = Mock() def _get_user_by_token(token=None): |