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