|
28 | 28 | from synapse.handlers.pagination import PurgeStatus
|
29 | 29 | from synapse.rest.client.v1 import directory, login, profile, room
|
30 | 30 | from synapse.rest.client.v2_alpha import account
|
31 |
| -from synapse.types import JsonDict, RoomAlias |
| 31 | +from synapse.types import JsonDict, RoomAlias, UserID |
32 | 32 | from synapse.util.stringutils import random_string
|
33 | 33 |
|
34 | 34 | from tests import unittest
|
@@ -675,6 +675,91 @@ def test_rooms_members_other_custom_keys(self):
|
675 | 675 | self.assertEquals(json.loads(content), channel.json_body)
|
676 | 676 |
|
677 | 677 |
|
| 678 | +class RoomJoinRatelimitTestCase(RoomBase): |
| 679 | + user_id = "@sid1:red" |
| 680 | + |
| 681 | + servlets = [ |
| 682 | + profile.register_servlets, |
| 683 | + room.register_servlets, |
| 684 | + ] |
| 685 | + |
| 686 | + @unittest.override_config( |
| 687 | + {"rc_joins": {"local": {"per_second": 3, "burst_count": 3}}} |
| 688 | + ) |
| 689 | + def test_join_local_ratelimit(self): |
| 690 | + """Tests that local joins are actually rate-limited.""" |
| 691 | + for i in range(5): |
| 692 | + self.helper.create_room_as(self.user_id) |
| 693 | + |
| 694 | + self.helper.create_room_as(self.user_id, expect_code=429) |
| 695 | + |
| 696 | + @unittest.override_config( |
| 697 | + {"rc_joins": {"local": {"per_second": 3, "burst_count": 3}}} |
| 698 | + ) |
| 699 | + def test_join_local_ratelimit_profile_change(self): |
| 700 | + """Tests that sending a profile update into all of the user's joined rooms isn't |
| 701 | + rate-limited by the rate-limiter on joins.""" |
| 702 | + |
| 703 | + # Create and join more rooms than the rate-limiting config allows in a second. |
| 704 | + room_ids = [ |
| 705 | + self.helper.create_room_as(self.user_id), |
| 706 | + self.helper.create_room_as(self.user_id), |
| 707 | + self.helper.create_room_as(self.user_id), |
| 708 | + ] |
| 709 | + self.reactor.advance(1) |
| 710 | + room_ids = room_ids + [ |
| 711 | + self.helper.create_room_as(self.user_id), |
| 712 | + self.helper.create_room_as(self.user_id), |
| 713 | + self.helper.create_room_as(self.user_id), |
| 714 | + ] |
| 715 | + |
| 716 | + # Create a profile for the user, since it hasn't been done on registration. |
| 717 | + store = self.hs.get_datastore() |
| 718 | + store.create_profile(UserID.from_string(self.user_id).localpart) |
| 719 | + |
| 720 | + # Update the display name for the user. |
| 721 | + path = "/_matrix/client/r0/profile/%s/displayname" % self.user_id |
| 722 | + request, channel = self.make_request("PUT", path, {"displayname": "John Doe"}) |
| 723 | + self.render(request) |
| 724 | + self.assertEquals(channel.code, 200, channel.json_body) |
| 725 | + |
| 726 | + # Check that all the rooms have been sent a profile update into. |
| 727 | + for room_id in room_ids: |
| 728 | + path = "/_matrix/client/r0/rooms/%s/state/m.room.member/%s" % ( |
| 729 | + room_id, |
| 730 | + self.user_id, |
| 731 | + ) |
| 732 | + |
| 733 | + request, channel = self.make_request("GET", path) |
| 734 | + self.render(request) |
| 735 | + self.assertEquals(channel.code, 200) |
| 736 | + |
| 737 | + self.assertIn("displayname", channel.json_body) |
| 738 | + self.assertEquals(channel.json_body["displayname"], "John Doe") |
| 739 | + |
| 740 | + @unittest.override_config( |
| 741 | + {"rc_joins": {"local": {"per_second": 3, "burst_count": 3}}} |
| 742 | + ) |
| 743 | + def test_join_local_ratelimit_idempotent(self): |
| 744 | + """Tests that the room join endpoints remain idempotent despite rate-limiting |
| 745 | + on room joins.""" |
| 746 | + room_id = self.helper.create_room_as(self.user_id) |
| 747 | + |
| 748 | + # Let's test both paths to be sure. |
| 749 | + paths_to_test = [ |
| 750 | + "/_matrix/client/r0/rooms/%s/join", |
| 751 | + "/_matrix/client/r0/join/%s", |
| 752 | + ] |
| 753 | + |
| 754 | + for path in paths_to_test: |
| 755 | + # Make sure we send more requests than the rate-limiting config would allow |
| 756 | + # if all of these requests ended up joining the user to a room. |
| 757 | + for i in range(6): |
| 758 | + request, channel = self.make_request("POST", path % room_id, {}) |
| 759 | + self.render(request) |
| 760 | + self.assertEquals(channel.code, 200) |
| 761 | + |
| 762 | + |
678 | 763 | class RoomMessagesTestCase(RoomBase):
|
679 | 764 | """ Tests /rooms/$room_id/messages/$user_id/$msg_id REST events. """
|
680 | 765 |
|
|
0 commit comments