summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrendan Abolivier <babolivier@matrix.org>2019-07-24 16:38:49 +0200
committerBrendan Abolivier <babolivier@matrix.org>2019-07-24 16:38:49 +0200
commitdcdfdb3f17bd3b6b3d4f7ea933c7dc0450976b39 (patch)
tree00b2c836a875974a725c95cb9e731028f4f10d51
parentMerge pull request #5702 from matrix-org/babolivier/3pid-invite (diff)
downloadsynapse-dcdfdb3f17bd3b6b3d4f7ea933c7dc0450976b39.tar.xz
Add test case
-rw-r--r--tests/rest/client/test_room_access_rules.py108
1 files changed, 106 insertions, 2 deletions
diff --git a/tests/rest/client/test_room_access_rules.py b/tests/rest/client/test_room_access_rules.py
index df48a89e93..83da1238ba 100644
--- a/tests/rest/client/test_room_access_rules.py
+++ b/tests/rest/client/test_room_access_rules.py
@@ -22,7 +22,7 @@ from mock import Mock
 
 from twisted.internet import defer
 
-from synapse.api.constants import EventTypes
+from synapse.api.constants import EventTypes, JoinRules, RoomCreationPreset
 from synapse.rest import admin
 from synapse.rest.client.v1 import login, room
 from synapse.third_party_rules.access_rules import (
@@ -156,6 +156,84 @@ class RoomAccessTestCase(unittest.HomeserverTestCase):
         """
         self.create_room(direct=True, rule=ACCESS_RULE_RESTRICTED, expected_code=400)
 
+    def test_public_room(self):
+        """Tests that it's not possible to have a room with the public join rule and an
+        access rule that's not restricted.
+        """
+        # Creating a room with the public_chat preset should succeed and set the access
+        # rule to restricted.
+        preset_room_id = self.create_room(preset=RoomCreationPreset.PUBLIC_CHAT)
+        self.assertEqual(
+            self.current_rule_in_room(preset_room_id), ACCESS_RULE_RESTRICTED,
+        )
+
+        # Creating a room with the public join rule in its initial state should succeed
+        # and set the access rule to restricted.
+        init_state_room_id = self.create_room(initial_state=[{
+            "type": "m.room.join_rules",
+            "content": {
+                "join_rule": JoinRules.PUBLIC,
+            },
+        }])
+        self.assertEqual(
+            self.current_rule_in_room(init_state_room_id), ACCESS_RULE_RESTRICTED,
+        )
+
+        # Changing access rule to unrestricted should fail.
+        self.change_rule_in_room(
+            preset_room_id, ACCESS_RULE_UNRESTRICTED, expected_code=400,
+        )
+        self.change_rule_in_room(
+            init_state_room_id, ACCESS_RULE_UNRESTRICTED, expected_code=400,
+        )
+
+        # Changing access rule to direct should fail.
+        self.change_rule_in_room(
+            preset_room_id, ACCESS_RULE_DIRECT, expected_code=400,
+        )
+        self.change_rule_in_room(
+            init_state_room_id, ACCESS_RULE_DIRECT, expected_code=400,
+        )
+
+        # Changing join rule to public in an unrestricted room should fail.
+        self.change_join_rule_in_room(
+            self.unrestricted_room, JoinRules.PUBLIC, expected_code=400,
+        )
+        # Changing join rule to public in an direct room should fail.
+        self.change_join_rule_in_room(
+            self.direct_rooms[0], JoinRules.PUBLIC, expected_code=400,
+        )
+
+        # Creating a new room with the public_chat preset and an access rule that isn't
+        # restricted should fail.
+        self.create_room(
+            preset=RoomCreationPreset.PUBLIC_CHAT, rule=ACCESS_RULE_UNRESTRICTED,
+            expected_code=400,
+        )
+        self.create_room(
+            preset=RoomCreationPreset.PUBLIC_CHAT, rule=ACCESS_RULE_DIRECT,
+            expected_code=400,
+        )
+
+        # Creating a room with the public join rule in its initial state and an access
+        # rule that isn't restricted should fail.
+        self.create_room(
+            initial_state=[{
+                "type": "m.room.join_rules",
+                "content": {
+                    "join_rule": JoinRules.PUBLIC,
+                },
+            }], rule=ACCESS_RULE_UNRESTRICTED, expected_code=400,
+        )
+        self.create_room(
+            initial_state=[{
+                "type": "m.room.join_rules",
+                "content": {
+                    "join_rule": JoinRules.PUBLIC,
+                },
+            }], rule=ACCESS_RULE_DIRECT, expected_code=400,
+        )
+
     def test_restricted(self):
         """Tests that in restricted mode we're unable to invite users from blacklisted
         servers but can invite other users.
@@ -405,11 +483,17 @@ class RoomAccessTestCase(unittest.HomeserverTestCase):
             expected_code=403,
         )
 
-    def create_room(self, direct=False, rule=None, expected_code=200):
+    def create_room(
+        self, direct=False, rule=None, preset=None, initial_state=None,
+        expected_code=200,
+    ):
         content = {
             "is_direct": direct,
         }
 
+        if preset:
+            content["preset"] = preset
+
         if rule:
             content["initial_state"] = [{
                 "type": ACCESS_RULES_TYPE,
@@ -419,6 +503,12 @@ class RoomAccessTestCase(unittest.HomeserverTestCase):
                 }
             }]
 
+        if initial_state:
+            if "initial_state" not in content:
+                content["initial_state"] = []
+
+            content["initial_state"] += initial_state
+
         request, channel = self.make_request(
             "POST",
             "/_matrix/client/r0/createRoom",
@@ -457,6 +547,20 @@ class RoomAccessTestCase(unittest.HomeserverTestCase):
 
         self.assertEqual(channel.code, expected_code, channel.result)
 
+    def change_join_rule_in_room(self, room_id, new_join_rule, expected_code=200):
+        data = {
+            "join_rule": new_join_rule,
+        }
+        request, channel = self.make_request(
+            "PUT",
+            "/_matrix/client/r0/rooms/%s/state/%s" % (room_id, EventTypes.JoinRules),
+            json.dumps(data),
+            access_token=self.tok,
+        )
+        self.render(request)
+
+        self.assertEqual(channel.code, expected_code, channel.result)
+
     def send_threepid_invite(self, address, room_id, expected_code=200):
         params = {
             "id_server": "testis",