test_upgrade_room.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 unittest.mock import patch
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.api.constants import EventContentFields, EventTypes, RoomTypes
  18. from synapse.config.server import DEFAULT_ROOM_VERSION
  19. from synapse.rest import admin
  20. from synapse.rest.client import login, room, room_upgrade_rest_servlet
  21. from synapse.server import HomeServer
  22. from synapse.util import Clock
  23. from tests import unittest
  24. from tests.server import FakeChannel
  25. class UpgradeRoomTest(unittest.HomeserverTestCase):
  26. servlets = [
  27. admin.register_servlets,
  28. login.register_servlets,
  29. room.register_servlets,
  30. room_upgrade_rest_servlet.register_servlets,
  31. ]
  32. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  33. self.store = hs.get_datastores().main
  34. self.creator = self.register_user("creator", "pass")
  35. self.creator_token = self.login(self.creator, "pass")
  36. self.other = self.register_user("user", "pass")
  37. self.other_token = self.login(self.other, "pass")
  38. self.room_id = self.helper.create_room_as(self.creator, tok=self.creator_token)
  39. self.helper.join(self.room_id, self.other, tok=self.other_token)
  40. def _upgrade_room(
  41. self, token: Optional[str] = None, room_id: Optional[str] = None
  42. ) -> FakeChannel:
  43. # We never want a cached response.
  44. self.reactor.advance(5 * 60 + 1)
  45. if room_id is None:
  46. room_id = self.room_id
  47. return self.make_request(
  48. "POST",
  49. f"/_matrix/client/r0/rooms/{room_id}/upgrade",
  50. # This will upgrade a room to the same version, but that's fine.
  51. content={"new_version": DEFAULT_ROOM_VERSION},
  52. access_token=token or self.creator_token,
  53. )
  54. def test_upgrade(self) -> None:
  55. """
  56. Upgrading a room should work fine.
  57. """
  58. channel = self._upgrade_room()
  59. self.assertEqual(200, channel.code, channel.result)
  60. self.assertIn("replacement_room", channel.json_body)
  61. def test_not_in_room(self) -> None:
  62. """
  63. Upgrading a room should work fine.
  64. """
  65. # The user isn't in the room.
  66. roomless = self.register_user("roomless", "pass")
  67. roomless_token = self.login(roomless, "pass")
  68. channel = self._upgrade_room(roomless_token)
  69. self.assertEqual(403, channel.code, channel.result)
  70. def test_power_levels(self) -> None:
  71. """
  72. Another user can upgrade the room if their power level is increased.
  73. """
  74. # The other user doesn't have the proper power level.
  75. channel = self._upgrade_room(self.other_token)
  76. self.assertEqual(403, channel.code, channel.result)
  77. # Increase the power levels so that this user can upgrade.
  78. power_levels = self.helper.get_state(
  79. self.room_id,
  80. "m.room.power_levels",
  81. tok=self.creator_token,
  82. )
  83. power_levels["users"][self.other] = 100
  84. self.helper.send_state(
  85. self.room_id,
  86. "m.room.power_levels",
  87. body=power_levels,
  88. tok=self.creator_token,
  89. )
  90. # The upgrade should succeed!
  91. channel = self._upgrade_room(self.other_token)
  92. self.assertEqual(200, channel.code, channel.result)
  93. def test_power_levels_user_default(self) -> None:
  94. """
  95. Another user can upgrade the room if the default power level for users is increased.
  96. """
  97. # The other user doesn't have the proper power level.
  98. channel = self._upgrade_room(self.other_token)
  99. self.assertEqual(403, channel.code, channel.result)
  100. # Increase the power levels so that this user can upgrade.
  101. power_levels = self.helper.get_state(
  102. self.room_id,
  103. "m.room.power_levels",
  104. tok=self.creator_token,
  105. )
  106. power_levels["users_default"] = 100
  107. self.helper.send_state(
  108. self.room_id,
  109. "m.room.power_levels",
  110. body=power_levels,
  111. tok=self.creator_token,
  112. )
  113. # The upgrade should succeed!
  114. channel = self._upgrade_room(self.other_token)
  115. self.assertEqual(200, channel.code, channel.result)
  116. def test_power_levels_tombstone(self) -> None:
  117. """
  118. Another user can upgrade the room if they can send the tombstone event.
  119. """
  120. # The other user doesn't have the proper power level.
  121. channel = self._upgrade_room(self.other_token)
  122. self.assertEqual(403, channel.code, channel.result)
  123. # Increase the power levels so that this user can upgrade.
  124. power_levels = self.helper.get_state(
  125. self.room_id,
  126. "m.room.power_levels",
  127. tok=self.creator_token,
  128. )
  129. power_levels["events"]["m.room.tombstone"] = 0
  130. self.helper.send_state(
  131. self.room_id,
  132. "m.room.power_levels",
  133. body=power_levels,
  134. tok=self.creator_token,
  135. )
  136. # The upgrade should succeed!
  137. channel = self._upgrade_room(self.other_token)
  138. self.assertEqual(200, channel.code, channel.result)
  139. power_levels = self.helper.get_state(
  140. self.room_id,
  141. "m.room.power_levels",
  142. tok=self.creator_token,
  143. )
  144. self.assertNotIn(self.other, power_levels["users"])
  145. def test_stringy_power_levels(self) -> None:
  146. """The room upgrade converts stringy power levels to proper integers."""
  147. # Retrieve the room's current power levels.
  148. power_levels = self.helper.get_state(
  149. self.room_id,
  150. "m.room.power_levels",
  151. tok=self.creator_token,
  152. )
  153. # Set creator's power level to the string "100" instead of the integer `100`.
  154. power_levels["users"][self.creator] = "100"
  155. # Synapse refuses to accept new stringy power level events. Bypass this by
  156. # neutering the validation.
  157. with patch("synapse.events.validator.jsonschema.validate"):
  158. # Note: https://github.com/matrix-org/matrix-spec/issues/853 plans to forbid
  159. # string power levels in new rooms. For this test to have a clean
  160. # conscience, we ought to ensure it's upgrading from a sufficiently old
  161. # version of room.
  162. self.helper.send_state(
  163. self.room_id,
  164. "m.room.power_levels",
  165. body=power_levels,
  166. tok=self.creator_token,
  167. )
  168. # Upgrade the room. Check the homeserver reports success.
  169. channel = self._upgrade_room()
  170. self.assertEqual(200, channel.code, channel.result)
  171. # Extract the new room ID.
  172. new_room_id = channel.json_body["replacement_room"]
  173. # Fetch the new room's power level event.
  174. new_power_levels = self.helper.get_state(
  175. new_room_id,
  176. "m.room.power_levels",
  177. tok=self.creator_token,
  178. )
  179. # We should now have an integer power level.
  180. self.assertEqual(new_power_levels["users"][self.creator], 100, new_power_levels)
  181. def test_space(self) -> None:
  182. """Test upgrading a space."""
  183. # Create a space.
  184. space_id = self.helper.create_room_as(
  185. self.creator,
  186. tok=self.creator_token,
  187. extra_content={
  188. "creation_content": {EventContentFields.ROOM_TYPE: RoomTypes.SPACE}
  189. },
  190. )
  191. # Add the room as a child room.
  192. self.helper.send_state(
  193. space_id,
  194. event_type=EventTypes.SpaceChild,
  195. body={"via": [self.hs.hostname]},
  196. tok=self.creator_token,
  197. state_key=self.room_id,
  198. )
  199. # Also add a room that was removed.
  200. old_room_id = "!notaroom:" + self.hs.hostname
  201. self.helper.send_state(
  202. space_id,
  203. event_type=EventTypes.SpaceChild,
  204. body={},
  205. tok=self.creator_token,
  206. state_key=old_room_id,
  207. )
  208. # Upgrade the room!
  209. channel = self._upgrade_room(room_id=space_id)
  210. self.assertEqual(200, channel.code, channel.result)
  211. self.assertIn("replacement_room", channel.json_body)
  212. new_space_id = channel.json_body["replacement_room"]
  213. state_ids = self.get_success(self.store.get_current_state_ids(new_space_id))
  214. # Ensure the new room is still a space.
  215. create_event = self.get_success(
  216. self.store.get_event(state_ids[(EventTypes.Create, "")])
  217. )
  218. self.assertEqual(
  219. create_event.content.get(EventContentFields.ROOM_TYPE), RoomTypes.SPACE
  220. )
  221. # The child link should have been copied over.
  222. self.assertIn((EventTypes.SpaceChild, self.room_id), state_ids)
  223. # The child that was removed should not be copied over.
  224. self.assertNotIn((EventTypes.SpaceChild, old_room_id), state_ids)
  225. def test_custom_room_type(self) -> None:
  226. """Test upgrading a room that has a custom room type set."""
  227. test_room_type = "com.example.my_custom_room_type"
  228. # Create a room with a custom room type.
  229. room_id = self.helper.create_room_as(
  230. self.creator,
  231. tok=self.creator_token,
  232. extra_content={
  233. "creation_content": {EventContentFields.ROOM_TYPE: test_room_type}
  234. },
  235. )
  236. # Upgrade the room!
  237. channel = self._upgrade_room(room_id=room_id)
  238. self.assertEqual(200, channel.code, channel.result)
  239. self.assertIn("replacement_room", channel.json_body)
  240. new_room_id = channel.json_body["replacement_room"]
  241. state_ids = self.get_success(self.store.get_current_state_ids(new_room_id))
  242. # Ensure the new room is the same type as the old room.
  243. create_event = self.get_success(
  244. self.store.get_event(state_ids[(EventTypes.Create, "")])
  245. )
  246. self.assertEqual(
  247. create_event.content.get(EventContentFields.ROOM_TYPE), test_room_type
  248. )