test_upgrade_room.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright 2021 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 typing import Optional
  15. from synapse.config.server import DEFAULT_ROOM_VERSION
  16. from synapse.rest import admin
  17. from synapse.rest.client import login, room, room_upgrade_rest_servlet
  18. from tests import unittest
  19. from tests.server import FakeChannel
  20. class UpgradeRoomTest(unittest.HomeserverTestCase):
  21. servlets = [
  22. admin.register_servlets,
  23. login.register_servlets,
  24. room.register_servlets,
  25. room_upgrade_rest_servlet.register_servlets,
  26. ]
  27. def prepare(self, reactor, clock, hs):
  28. self.store = hs.get_datastore()
  29. self.handler = hs.get_user_directory_handler()
  30. self.creator = self.register_user("creator", "pass")
  31. self.creator_token = self.login(self.creator, "pass")
  32. self.other = self.register_user("user", "pass")
  33. self.other_token = self.login(self.other, "pass")
  34. self.room_id = self.helper.create_room_as(self.creator, tok=self.creator_token)
  35. self.helper.join(self.room_id, self.other, tok=self.other_token)
  36. def _upgrade_room(self, token: Optional[str] = None) -> FakeChannel:
  37. # We never want a cached response.
  38. self.reactor.advance(5 * 60 + 1)
  39. return self.make_request(
  40. "POST",
  41. "/_matrix/client/r0/rooms/%s/upgrade" % self.room_id,
  42. # This will upgrade a room to the same version, but that's fine.
  43. content={"new_version": DEFAULT_ROOM_VERSION},
  44. access_token=token or self.creator_token,
  45. )
  46. def test_upgrade(self):
  47. """
  48. Upgrading a room should work fine.
  49. """
  50. channel = self._upgrade_room()
  51. self.assertEquals(200, channel.code, channel.result)
  52. self.assertIn("replacement_room", channel.json_body)
  53. def test_not_in_room(self):
  54. """
  55. Upgrading a room should work fine.
  56. """
  57. # THe user isn't in the room.
  58. roomless = self.register_user("roomless", "pass")
  59. roomless_token = self.login(roomless, "pass")
  60. channel = self._upgrade_room(roomless_token)
  61. self.assertEquals(403, channel.code, channel.result)
  62. def test_power_levels(self):
  63. """
  64. Another user can upgrade the room if their power level is increased.
  65. """
  66. # The other user doesn't have the proper power level.
  67. channel = self._upgrade_room(self.other_token)
  68. self.assertEquals(403, channel.code, channel.result)
  69. # Increase the power levels so that this user can upgrade.
  70. power_levels = self.helper.get_state(
  71. self.room_id,
  72. "m.room.power_levels",
  73. tok=self.creator_token,
  74. )
  75. power_levels["users"][self.other] = 100
  76. self.helper.send_state(
  77. self.room_id,
  78. "m.room.power_levels",
  79. body=power_levels,
  80. tok=self.creator_token,
  81. )
  82. # The upgrade should succeed!
  83. channel = self._upgrade_room(self.other_token)
  84. self.assertEquals(200, channel.code, channel.result)
  85. def test_power_levels_user_default(self):
  86. """
  87. Another user can upgrade the room if the default power level for users is increased.
  88. """
  89. # The other user doesn't have the proper power level.
  90. channel = self._upgrade_room(self.other_token)
  91. self.assertEquals(403, channel.code, channel.result)
  92. # Increase the power levels so that this user can upgrade.
  93. power_levels = self.helper.get_state(
  94. self.room_id,
  95. "m.room.power_levels",
  96. tok=self.creator_token,
  97. )
  98. power_levels["users_default"] = 100
  99. self.helper.send_state(
  100. self.room_id,
  101. "m.room.power_levels",
  102. body=power_levels,
  103. tok=self.creator_token,
  104. )
  105. # The upgrade should succeed!
  106. channel = self._upgrade_room(self.other_token)
  107. self.assertEquals(200, channel.code, channel.result)
  108. def test_power_levels_tombstone(self):
  109. """
  110. Another user can upgrade the room if they can send the tombstone event.
  111. """
  112. # The other user doesn't have the proper power level.
  113. channel = self._upgrade_room(self.other_token)
  114. self.assertEquals(403, channel.code, channel.result)
  115. # Increase the power levels so that this user can upgrade.
  116. power_levels = self.helper.get_state(
  117. self.room_id,
  118. "m.room.power_levels",
  119. tok=self.creator_token,
  120. )
  121. power_levels["events"]["m.room.tombstone"] = 0
  122. self.helper.send_state(
  123. self.room_id,
  124. "m.room.power_levels",
  125. body=power_levels,
  126. tok=self.creator_token,
  127. )
  128. # The upgrade should succeed!
  129. channel = self._upgrade_room(self.other_token)
  130. self.assertEquals(200, channel.code, channel.result)
  131. power_levels = self.helper.get_state(
  132. self.room_id,
  133. "m.room.power_levels",
  134. tok=self.creator_token,
  135. )
  136. self.assertNotIn(self.other, power_levels["users"])