test_room_directory.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import yaml
  16. from synapse.config.room_directory import RoomDirectoryConfig
  17. from tests import unittest
  18. class RoomDirectoryConfigTestCase(unittest.TestCase):
  19. def test_alias_creation_acl(self):
  20. config = yaml.load("""
  21. alias_creation_rules:
  22. - user_id: "*bob*"
  23. alias: "*"
  24. action: "deny"
  25. - user_id: "*"
  26. alias: "#unofficial_*"
  27. action: "allow"
  28. - user_id: "@foo*:example.com"
  29. alias: "*"
  30. action: "allow"
  31. - user_id: "@gah:example.com"
  32. alias: "#goo:example.com"
  33. action: "allow"
  34. room_list_publication_rules: []
  35. """)
  36. rd_config = RoomDirectoryConfig()
  37. rd_config.read_config(config)
  38. self.assertFalse(rd_config.is_alias_creation_allowed(
  39. user_id="@bob:example.com",
  40. room_id="!test",
  41. alias="#test:example.com",
  42. ))
  43. self.assertTrue(rd_config.is_alias_creation_allowed(
  44. user_id="@test:example.com",
  45. room_id="!test",
  46. alias="#unofficial_st:example.com",
  47. ))
  48. self.assertTrue(rd_config.is_alias_creation_allowed(
  49. user_id="@foobar:example.com",
  50. room_id="!test",
  51. alias="#test:example.com",
  52. ))
  53. self.assertTrue(rd_config.is_alias_creation_allowed(
  54. user_id="@gah:example.com",
  55. room_id="!test",
  56. alias="#goo:example.com",
  57. ))
  58. self.assertFalse(rd_config.is_alias_creation_allowed(
  59. user_id="@test:example.com",
  60. room_id="!test",
  61. alias="#test:example.com",
  62. ))
  63. def test_room_publish_acl(self):
  64. config = yaml.load("""
  65. alias_creation_rules: []
  66. room_list_publication_rules:
  67. - user_id: "*bob*"
  68. alias: "*"
  69. action: "deny"
  70. - user_id: "*"
  71. alias: "#unofficial_*"
  72. action: "allow"
  73. - user_id: "@foo*:example.com"
  74. alias: "*"
  75. action: "allow"
  76. - user_id: "@gah:example.com"
  77. alias: "#goo:example.com"
  78. action: "allow"
  79. - room_id: "!test-deny"
  80. action: "deny"
  81. """)
  82. rd_config = RoomDirectoryConfig()
  83. rd_config.read_config(config)
  84. self.assertFalse(rd_config.is_publishing_room_allowed(
  85. user_id="@bob:example.com",
  86. room_id="!test",
  87. aliases=["#test:example.com"],
  88. ))
  89. self.assertTrue(rd_config.is_publishing_room_allowed(
  90. user_id="@test:example.com",
  91. room_id="!test",
  92. aliases=["#unofficial_st:example.com"],
  93. ))
  94. self.assertTrue(rd_config.is_publishing_room_allowed(
  95. user_id="@foobar:example.com",
  96. room_id="!test",
  97. aliases=[],
  98. ))
  99. self.assertTrue(rd_config.is_publishing_room_allowed(
  100. user_id="@gah:example.com",
  101. room_id="!test",
  102. aliases=["#goo:example.com"],
  103. ))
  104. self.assertFalse(rd_config.is_publishing_room_allowed(
  105. user_id="@test:example.com",
  106. room_id="!test",
  107. aliases=["#test:example.com"],
  108. ))
  109. self.assertTrue(rd_config.is_publishing_room_allowed(
  110. user_id="@foobar:example.com",
  111. room_id="!test-deny",
  112. aliases=[],
  113. ))
  114. self.assertFalse(rd_config.is_publishing_room_allowed(
  115. user_id="@gah:example.com",
  116. room_id="!test-deny",
  117. aliases=[],
  118. ))
  119. self.assertTrue(rd_config.is_publishing_room_allowed(
  120. user_id="@test:example.com",
  121. room_id="!test",
  122. aliases=["#unofficial_st:example.com", "#blah:example.com"],
  123. ))