test_room_directory.py 4.7 KB

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