test_power_levels.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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. from synapse.rest import admin
  15. from synapse.rest.client.v1 import login, room
  16. from synapse.rest.client.v2_alpha import sync
  17. from tests.unittest import HomeserverTestCase
  18. class PowerLevelsTestCase(HomeserverTestCase):
  19. """Tests that power levels are enforced in various situations"""
  20. servlets = [
  21. admin.register_servlets,
  22. room.register_servlets,
  23. login.register_servlets,
  24. sync.register_servlets,
  25. ]
  26. def make_homeserver(self, reactor, clock):
  27. config = self.default_config()
  28. return self.setup_test_homeserver(config=config)
  29. def prepare(self, reactor, clock, hs):
  30. # register a room admin, moderator and regular user
  31. self.admin_user_id = self.register_user("admin", "pass")
  32. self.admin_access_token = self.login("admin", "pass")
  33. self.mod_user_id = self.register_user("mod", "pass")
  34. self.mod_access_token = self.login("mod", "pass")
  35. self.user_user_id = self.register_user("user", "pass")
  36. self.user_access_token = self.login("user", "pass")
  37. # Create a room
  38. self.room_id = self.helper.create_room_as(
  39. self.admin_user_id, tok=self.admin_access_token
  40. )
  41. # Invite the other users
  42. self.helper.invite(
  43. room=self.room_id,
  44. src=self.admin_user_id,
  45. tok=self.admin_access_token,
  46. targ=self.mod_user_id,
  47. )
  48. self.helper.invite(
  49. room=self.room_id,
  50. src=self.admin_user_id,
  51. tok=self.admin_access_token,
  52. targ=self.user_user_id,
  53. )
  54. # Make the other users join the room
  55. self.helper.join(
  56. room=self.room_id, user=self.mod_user_id, tok=self.mod_access_token
  57. )
  58. self.helper.join(
  59. room=self.room_id, user=self.user_user_id, tok=self.user_access_token
  60. )
  61. # Mod the mod
  62. room_power_levels = self.helper.get_state(
  63. self.room_id,
  64. "m.room.power_levels",
  65. tok=self.admin_access_token,
  66. )
  67. # Update existing power levels with mod at PL50
  68. room_power_levels["users"].update({self.mod_user_id: 50})
  69. self.helper.send_state(
  70. self.room_id,
  71. "m.room.power_levels",
  72. room_power_levels,
  73. tok=self.admin_access_token,
  74. )
  75. def test_non_admins_cannot_enable_room_encryption(self):
  76. # have the mod try to enable room encryption
  77. self.helper.send_state(
  78. self.room_id,
  79. "m.room.encryption",
  80. {"algorithm": "m.megolm.v1.aes-sha2"},
  81. tok=self.mod_access_token,
  82. expect_code=403, # expect failure
  83. )
  84. # have the user try to enable room encryption
  85. self.helper.send_state(
  86. self.room_id,
  87. "m.room.encryption",
  88. {"algorithm": "m.megolm.v1.aes-sha2"},
  89. tok=self.user_access_token,
  90. expect_code=403, # expect failure
  91. )
  92. def test_non_admins_cannot_send_server_acl(self):
  93. # have the mod try to send a server ACL
  94. self.helper.send_state(
  95. self.room_id,
  96. "m.room.server_acl",
  97. {
  98. "allow": ["*"],
  99. "allow_ip_literals": False,
  100. "deny": ["*.evil.com", "evil.com"],
  101. },
  102. tok=self.mod_access_token,
  103. expect_code=403, # expect failure
  104. )
  105. # have the user try to send a server ACL
  106. self.helper.send_state(
  107. self.room_id,
  108. "m.room.server_acl",
  109. {
  110. "allow": ["*"],
  111. "allow_ip_literals": False,
  112. "deny": ["*.evil.com", "evil.com"],
  113. },
  114. tok=self.user_access_token,
  115. expect_code=403, # expect failure
  116. )
  117. def test_non_admins_cannot_tombstone_room(self):
  118. # Create another room that will serve as our "upgraded room"
  119. self.upgraded_room_id = self.helper.create_room_as(
  120. self.admin_user_id, tok=self.admin_access_token
  121. )
  122. # have the mod try to send a tombstone event
  123. self.helper.send_state(
  124. self.room_id,
  125. "m.room.tombstone",
  126. {
  127. "body": "This room has been replaced",
  128. "replacement_room": self.upgraded_room_id,
  129. },
  130. tok=self.mod_access_token,
  131. expect_code=403, # expect failure
  132. )
  133. # have the user try to send a tombstone event
  134. self.helper.send_state(
  135. self.room_id,
  136. "m.room.tombstone",
  137. {
  138. "body": "This room has been replaced",
  139. "replacement_room": self.upgraded_room_id,
  140. },
  141. tok=self.user_access_token,
  142. expect_code=403, # expect failure
  143. )
  144. def test_admins_can_enable_room_encryption(self):
  145. # have the admin try to enable room encryption
  146. self.helper.send_state(
  147. self.room_id,
  148. "m.room.encryption",
  149. {"algorithm": "m.megolm.v1.aes-sha2"},
  150. tok=self.admin_access_token,
  151. expect_code=200, # expect success
  152. )
  153. def test_admins_can_send_server_acl(self):
  154. # have the admin try to send a server ACL
  155. self.helper.send_state(
  156. self.room_id,
  157. "m.room.server_acl",
  158. {
  159. "allow": ["*"],
  160. "allow_ip_literals": False,
  161. "deny": ["*.evil.com", "evil.com"],
  162. },
  163. tok=self.admin_access_token,
  164. expect_code=200, # expect success
  165. )
  166. def test_admins_can_tombstone_room(self):
  167. # Create another room that will serve as our "upgraded room"
  168. self.upgraded_room_id = self.helper.create_room_as(
  169. self.admin_user_id, tok=self.admin_access_token
  170. )
  171. # have the admin try to send a tombstone event
  172. self.helper.send_state(
  173. self.room_id,
  174. "m.room.tombstone",
  175. {
  176. "body": "This room has been replaced",
  177. "replacement_room": self.upgraded_room_id,
  178. },
  179. tok=self.admin_access_token,
  180. expect_code=200, # expect success
  181. )