test_upgrade_room.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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.api.constants import EventContentFields, EventTypes, RoomTypes
  16. from synapse.config.server import DEFAULT_ROOM_VERSION
  17. from synapse.rest import admin
  18. from synapse.rest.client import login, room, room_upgrade_rest_servlet
  19. from synapse.server import HomeServer
  20. from tests import unittest
  21. from tests.server import FakeChannel
  22. class UpgradeRoomTest(unittest.HomeserverTestCase):
  23. servlets = [
  24. admin.register_servlets,
  25. login.register_servlets,
  26. room.register_servlets,
  27. room_upgrade_rest_servlet.register_servlets,
  28. ]
  29. def prepare(self, reactor, clock, hs: "HomeServer"):
  30. self.store = hs.get_datastore()
  31. self.creator = self.register_user("creator", "pass")
  32. self.creator_token = self.login(self.creator, "pass")
  33. self.other = self.register_user("user", "pass")
  34. self.other_token = self.login(self.other, "pass")
  35. self.room_id = self.helper.create_room_as(self.creator, tok=self.creator_token)
  36. self.helper.join(self.room_id, self.other, tok=self.other_token)
  37. def _upgrade_room(
  38. self, token: Optional[str] = None, room_id: Optional[str] = None
  39. ) -> FakeChannel:
  40. # We never want a cached response.
  41. self.reactor.advance(5 * 60 + 1)
  42. if room_id is None:
  43. room_id = self.room_id
  44. return self.make_request(
  45. "POST",
  46. f"/_matrix/client/r0/rooms/{room_id}/upgrade",
  47. # This will upgrade a room to the same version, but that's fine.
  48. content={"new_version": DEFAULT_ROOM_VERSION},
  49. access_token=token or self.creator_token,
  50. )
  51. def test_upgrade(self):
  52. """
  53. Upgrading a room should work fine.
  54. """
  55. channel = self._upgrade_room()
  56. self.assertEquals(200, channel.code, channel.result)
  57. self.assertIn("replacement_room", channel.json_body)
  58. def test_not_in_room(self):
  59. """
  60. Upgrading a room should work fine.
  61. """
  62. # THe user isn't in the room.
  63. roomless = self.register_user("roomless", "pass")
  64. roomless_token = self.login(roomless, "pass")
  65. channel = self._upgrade_room(roomless_token)
  66. self.assertEquals(403, channel.code, channel.result)
  67. def test_power_levels(self):
  68. """
  69. Another user can upgrade the room if their power level is increased.
  70. """
  71. # The other user doesn't have the proper power level.
  72. channel = self._upgrade_room(self.other_token)
  73. self.assertEquals(403, channel.code, channel.result)
  74. # Increase the power levels so that this user can upgrade.
  75. power_levels = self.helper.get_state(
  76. self.room_id,
  77. "m.room.power_levels",
  78. tok=self.creator_token,
  79. )
  80. power_levels["users"][self.other] = 100
  81. self.helper.send_state(
  82. self.room_id,
  83. "m.room.power_levels",
  84. body=power_levels,
  85. tok=self.creator_token,
  86. )
  87. # The upgrade should succeed!
  88. channel = self._upgrade_room(self.other_token)
  89. self.assertEquals(200, channel.code, channel.result)
  90. def test_power_levels_user_default(self):
  91. """
  92. Another user can upgrade the room if the default power level for users is increased.
  93. """
  94. # The other user doesn't have the proper power level.
  95. channel = self._upgrade_room(self.other_token)
  96. self.assertEquals(403, channel.code, channel.result)
  97. # Increase the power levels so that this user can upgrade.
  98. power_levels = self.helper.get_state(
  99. self.room_id,
  100. "m.room.power_levels",
  101. tok=self.creator_token,
  102. )
  103. power_levels["users_default"] = 100
  104. self.helper.send_state(
  105. self.room_id,
  106. "m.room.power_levels",
  107. body=power_levels,
  108. tok=self.creator_token,
  109. )
  110. # The upgrade should succeed!
  111. channel = self._upgrade_room(self.other_token)
  112. self.assertEquals(200, channel.code, channel.result)
  113. def test_power_levels_tombstone(self):
  114. """
  115. Another user can upgrade the room if they can send the tombstone event.
  116. """
  117. # The other user doesn't have the proper power level.
  118. channel = self._upgrade_room(self.other_token)
  119. self.assertEquals(403, channel.code, channel.result)
  120. # Increase the power levels so that this user can upgrade.
  121. power_levels = self.helper.get_state(
  122. self.room_id,
  123. "m.room.power_levels",
  124. tok=self.creator_token,
  125. )
  126. power_levels["events"]["m.room.tombstone"] = 0
  127. self.helper.send_state(
  128. self.room_id,
  129. "m.room.power_levels",
  130. body=power_levels,
  131. tok=self.creator_token,
  132. )
  133. # The upgrade should succeed!
  134. channel = self._upgrade_room(self.other_token)
  135. self.assertEquals(200, channel.code, channel.result)
  136. power_levels = self.helper.get_state(
  137. self.room_id,
  138. "m.room.power_levels",
  139. tok=self.creator_token,
  140. )
  141. self.assertNotIn(self.other, power_levels["users"])
  142. def test_space(self):
  143. """Test upgrading a space."""
  144. # Create a space.
  145. space_id = self.helper.create_room_as(
  146. self.creator,
  147. tok=self.creator_token,
  148. extra_content={
  149. "creation_content": {EventContentFields.ROOM_TYPE: RoomTypes.SPACE}
  150. },
  151. )
  152. # Add the room as a child room.
  153. self.helper.send_state(
  154. space_id,
  155. event_type=EventTypes.SpaceChild,
  156. body={"via": [self.hs.hostname]},
  157. tok=self.creator_token,
  158. state_key=self.room_id,
  159. )
  160. # Also add a room that was removed.
  161. old_room_id = "!notaroom:" + self.hs.hostname
  162. self.helper.send_state(
  163. space_id,
  164. event_type=EventTypes.SpaceChild,
  165. body={},
  166. tok=self.creator_token,
  167. state_key=old_room_id,
  168. )
  169. # Upgrade the room!
  170. channel = self._upgrade_room(room_id=space_id)
  171. self.assertEquals(200, channel.code, channel.result)
  172. self.assertIn("replacement_room", channel.json_body)
  173. new_space_id = channel.json_body["replacement_room"]
  174. state_ids = self.get_success(self.store.get_current_state_ids(new_space_id))
  175. # Ensure the new room is still a space.
  176. create_event = self.get_success(
  177. self.store.get_event(state_ids[(EventTypes.Create, "")])
  178. )
  179. self.assertEqual(
  180. create_event.content.get(EventContentFields.ROOM_TYPE), RoomTypes.SPACE
  181. )
  182. # The child link should have been copied over.
  183. self.assertIn((EventTypes.SpaceChild, self.room_id), state_ids)
  184. # The child that was removed should not be copied over.
  185. self.assertNotIn((EventTypes.SpaceChild, old_room_id), state_ids)