1
0

test_room_directory.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.safe_load(
  21. """
  22. alias_creation_rules:
  23. - user_id: "*bob*"
  24. alias: "*"
  25. action: "deny"
  26. - user_id: "*"
  27. alias: "#unofficial_*"
  28. action: "allow"
  29. - user_id: "@foo*:example.com"
  30. alias: "*"
  31. action: "allow"
  32. - user_id: "@gah:example.com"
  33. alias: "#goo:example.com"
  34. action: "allow"
  35. room_list_publication_rules: []
  36. """
  37. )
  38. rd_config = RoomDirectoryConfig()
  39. rd_config.read_config(config)
  40. self.assertFalse(
  41. rd_config.is_alias_creation_allowed(
  42. user_id="@bob:example.com", room_id="!test", alias="#test:example.com"
  43. )
  44. )
  45. self.assertTrue(
  46. rd_config.is_alias_creation_allowed(
  47. user_id="@test:example.com",
  48. room_id="!test",
  49. alias="#unofficial_st:example.com",
  50. )
  51. )
  52. self.assertTrue(
  53. rd_config.is_alias_creation_allowed(
  54. user_id="@foobar:example.com",
  55. room_id="!test",
  56. alias="#test:example.com",
  57. )
  58. )
  59. self.assertTrue(
  60. rd_config.is_alias_creation_allowed(
  61. user_id="@gah:example.com", room_id="!test", alias="#goo:example.com"
  62. )
  63. )
  64. self.assertFalse(
  65. rd_config.is_alias_creation_allowed(
  66. user_id="@test:example.com", room_id="!test", alias="#test:example.com"
  67. )
  68. )
  69. def test_room_publish_acl(self):
  70. config = yaml.safe_load(
  71. """
  72. alias_creation_rules: []
  73. room_list_publication_rules:
  74. - user_id: "*bob*"
  75. alias: "*"
  76. action: "deny"
  77. - user_id: "*"
  78. alias: "#unofficial_*"
  79. action: "allow"
  80. - user_id: "@foo*:example.com"
  81. alias: "*"
  82. action: "allow"
  83. - user_id: "@gah:example.com"
  84. alias: "#goo:example.com"
  85. action: "allow"
  86. - room_id: "!test-deny"
  87. action: "deny"
  88. """
  89. )
  90. rd_config = RoomDirectoryConfig()
  91. rd_config.read_config(config)
  92. self.assertFalse(
  93. rd_config.is_publishing_room_allowed(
  94. user_id="@bob:example.com",
  95. room_id="!test",
  96. aliases=["#test:example.com"],
  97. )
  98. )
  99. self.assertTrue(
  100. rd_config.is_publishing_room_allowed(
  101. user_id="@test:example.com",
  102. room_id="!test",
  103. aliases=["#unofficial_st:example.com"],
  104. )
  105. )
  106. self.assertTrue(
  107. rd_config.is_publishing_room_allowed(
  108. user_id="@foobar:example.com", room_id="!test", aliases=[]
  109. )
  110. )
  111. self.assertTrue(
  112. rd_config.is_publishing_room_allowed(
  113. user_id="@gah:example.com",
  114. room_id="!test",
  115. aliases=["#goo:example.com"],
  116. )
  117. )
  118. self.assertFalse(
  119. rd_config.is_publishing_room_allowed(
  120. user_id="@test:example.com",
  121. room_id="!test",
  122. aliases=["#test:example.com"],
  123. )
  124. )
  125. self.assertTrue(
  126. rd_config.is_publishing_room_allowed(
  127. user_id="@foobar:example.com", room_id="!test-deny", aliases=[]
  128. )
  129. )
  130. self.assertFalse(
  131. rd_config.is_publishing_room_allowed(
  132. user_id="@gah:example.com", room_id="!test-deny", aliases=[]
  133. )
  134. )
  135. self.assertTrue(
  136. rd_config.is_publishing_room_allowed(
  137. user_id="@test:example.com",
  138. room_id="!test",
  139. aliases=["#unofficial_st:example.com", "#blah:example.com"],
  140. )
  141. )