1
1
# Copyright 2014-2016 OpenMarket Ltd
2
+ # Copyright 2021 Matrix.org Foundation C.I.C.
2
3
#
3
4
# Licensed under the Apache License, Version 2.0 (the "License");
4
5
# you may not use this file except in compliance with the License.
12
13
# See the License for the specific language governing permissions and
13
14
# limitations under the License.
14
15
15
-
16
16
from unittest .mock import Mock
17
17
18
18
import synapse .api .errors
19
19
import synapse .rest .admin
20
20
from synapse .api .constants import EventTypes
21
- from synapse .config .room_directory import RoomDirectoryConfig
22
21
from synapse .rest .client import directory , login , room
23
22
from synapse .types import RoomAlias , create_requester
24
23
@@ -394,30 +393,23 @@ class TestCreateAliasACL(unittest.HomeserverTestCase):
394
393
395
394
servlets = [directory .register_servlets , room .register_servlets ]
396
395
397
- def prepare (self , reactor , clock , hs ):
398
- # We cheekily override the config to add custom alias creation rules
399
- config = {}
396
+ def default_config (self ):
397
+ config = super ().default_config ()
398
+
399
+ # Add custom alias creation rules to the config.
400
400
config ["alias_creation_rules" ] = [
401
401
{"user_id" : "*" , "alias" : "#unofficial_*" , "action" : "allow" }
402
402
]
403
- config ["room_list_publication_rules" ] = []
404
403
405
- rd_config = RoomDirectoryConfig ()
406
- rd_config .read_config (config )
407
-
408
- self .hs .config .roomdirectory .is_alias_creation_allowed = (
409
- rd_config .is_alias_creation_allowed
410
- )
411
-
412
- return hs
404
+ return config
413
405
414
406
def test_denied (self ):
415
407
room_id = self .helper .create_room_as (self .user_id )
416
408
417
409
channel = self .make_request (
418
410
"PUT" ,
419
411
b"directory/room/%23test%3Atest" ,
420
- ( ' {"room_id":"%s"}' % ( room_id ,)). encode ( "ascii" ) ,
412
+ {"room_id" : room_id } ,
421
413
)
422
414
self .assertEquals (403 , channel .code , channel .result )
423
415
@@ -427,14 +419,35 @@ def test_allowed(self):
427
419
channel = self .make_request (
428
420
"PUT" ,
429
421
b"directory/room/%23unofficial_test%3Atest" ,
430
- ( ' {"room_id":"%s"}' % ( room_id ,)). encode ( "ascii" ) ,
422
+ {"room_id" : room_id } ,
431
423
)
432
424
self .assertEquals (200 , channel .code , channel .result )
433
425
426
+ def test_denied_during_creation (self ):
427
+ """A room alias that is not allowed should be rejected during creation."""
428
+ # Invalid room alias.
429
+ self .helper .create_room_as (
430
+ self .user_id ,
431
+ expect_code = 403 ,
432
+ extra_content = {"room_alias_name" : "foo" },
433
+ )
434
434
435
- class TestCreatePublishedRoomACL (unittest .HomeserverTestCase ):
436
- data = {"room_alias_name" : "unofficial_test" }
435
+ def test_allowed_during_creation (self ):
436
+ """A valid room alias should be allowed during creation."""
437
+ room_id = self .helper .create_room_as (
438
+ self .user_id ,
439
+ extra_content = {"room_alias_name" : "unofficial_test" },
440
+ )
437
441
442
+ channel = self .make_request (
443
+ "GET" ,
444
+ b"directory/room/%23unofficial_test%3Atest" ,
445
+ )
446
+ self .assertEquals (200 , channel .code , channel .result )
447
+ self .assertEquals (channel .json_body ["room_id" ], room_id )
448
+
449
+
450
+ class TestCreatePublishedRoomACL (unittest .HomeserverTestCase ):
438
451
servlets = [
439
452
synapse .rest .admin .register_servlets_for_client_rest_resource ,
440
453
login .register_servlets ,
@@ -443,27 +456,30 @@ class TestCreatePublishedRoomACL(unittest.HomeserverTestCase):
443
456
]
444
457
hijack_auth = False
445
458
446
- def prepare (self , reactor , clock , hs ):
447
- self .allowed_user_id = self .register_user ("allowed" , "pass" )
448
- self .allowed_access_token = self .login ("allowed" , "pass" )
459
+ data = {"room_alias_name" : "unofficial_test" }
460
+ allowed_localpart = "allowed"
449
461
450
- self . denied_user_id = self . register_user ( "denied" , "pass" )
451
- self . denied_access_token = self . login ( "denied" , "pass" )
462
+ def default_config ( self ):
463
+ config = super (). default_config ( )
452
464
453
- # This time we add custom room list publication rules
454
- config = {}
455
- config ["alias_creation_rules" ] = []
465
+ # Add custom room list publication rules to the config.
456
466
config ["room_list_publication_rules" ] = [
467
+ {
468
+ "user_id" : "@" + self .allowed_localpart + "*" ,
469
+ "alias" : "#unofficial_*" ,
470
+ "action" : "allow" ,
471
+ },
457
472
{"user_id" : "*" , "alias" : "*" , "action" : "deny" },
458
- {"user_id" : self .allowed_user_id , "alias" : "*" , "action" : "allow" },
459
473
]
460
474
461
- rd_config = RoomDirectoryConfig ()
462
- rd_config .read_config (config )
475
+ return config
463
476
464
- self .hs .config .roomdirectory .is_publishing_room_allowed = (
465
- rd_config .is_publishing_room_allowed
466
- )
477
+ def prepare (self , reactor , clock , hs ):
478
+ self .allowed_user_id = self .register_user (self .allowed_localpart , "pass" )
479
+ self .allowed_access_token = self .login (self .allowed_localpart , "pass" )
480
+
481
+ self .denied_user_id = self .register_user ("denied" , "pass" )
482
+ self .denied_access_token = self .login ("denied" , "pass" )
467
483
468
484
return hs
469
485
@@ -505,10 +521,23 @@ def test_allowed_with_publication_permission(self):
505
521
self .allowed_user_id ,
506
522
tok = self .allowed_access_token ,
507
523
extra_content = self .data ,
508
- is_public = False ,
524
+ is_public = True ,
509
525
expect_code = 200 ,
510
526
)
511
527
528
+ def test_denied_publication_with_invalid_alias (self ):
529
+ """
530
+ Try to create a room, register an alias for it, and publish it,
531
+ as a user WITH permission to publish rooms.
532
+ """
533
+ self .helper .create_room_as (
534
+ self .allowed_user_id ,
535
+ tok = self .allowed_access_token ,
536
+ extra_content = {"room_alias_name" : "foo" },
537
+ is_public = True ,
538
+ expect_code = 403 ,
539
+ )
540
+
512
541
def test_can_create_as_private_room_after_rejection (self ):
513
542
"""
514
543
After failing to publish a room with an alias as a user without publish permission,
0 commit comments