|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2019 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 | + |
| 16 | +from synapse.rest import admin |
| 17 | +from synapse.rest.client.v1 import login, room |
| 18 | +from synapse.rest.client.v2_alpha import sync |
| 19 | + |
| 20 | +from tests.unittest import HomeserverTestCase |
| 21 | + |
| 22 | + |
| 23 | +class RedactionsTestCase(HomeserverTestCase): |
| 24 | + """Tests that various redaction events are handled correctly""" |
| 25 | + |
| 26 | + servlets = [ |
| 27 | + admin.register_servlets, |
| 28 | + room.register_servlets, |
| 29 | + login.register_servlets, |
| 30 | + sync.register_servlets, |
| 31 | + ] |
| 32 | + |
| 33 | + def prepare(self, reactor, clock, hs): |
| 34 | + # register a couple of users |
| 35 | + self.mod_user_id = self.register_user("user1", "pass") |
| 36 | + self.mod_access_token = self.login("user1", "pass") |
| 37 | + self.other_user_id = self.register_user("otheruser", "pass") |
| 38 | + self.other_access_token = self.login("otheruser", "pass") |
| 39 | + |
| 40 | + # Create a room |
| 41 | + self.room_id = self.helper.create_room_as( |
| 42 | + self.mod_user_id, tok=self.mod_access_token |
| 43 | + ) |
| 44 | + |
| 45 | + # Invite the other user |
| 46 | + self.helper.invite( |
| 47 | + room=self.room_id, |
| 48 | + src=self.mod_user_id, |
| 49 | + tok=self.mod_access_token, |
| 50 | + targ=self.other_user_id, |
| 51 | + ) |
| 52 | + # The other user joins |
| 53 | + self.helper.join( |
| 54 | + room=self.room_id, user=self.other_user_id, tok=self.other_access_token |
| 55 | + ) |
| 56 | + |
| 57 | + def _redact_event(self, access_token, room_id, event_id, expect_code=200): |
| 58 | + """Helper function to send a redaction event. |
| 59 | +
|
| 60 | + Returns the json body. |
| 61 | + """ |
| 62 | + path = "/_matrix/client/r0/rooms/%s/redact/%s" % (room_id, event_id) |
| 63 | + |
| 64 | + request, channel = self.make_request( |
| 65 | + "POST", path, content={}, access_token=access_token |
| 66 | + ) |
| 67 | + self.render(request) |
| 68 | + self.assertEqual(int(channel.result["code"]), expect_code) |
| 69 | + return channel.json_body |
| 70 | + |
| 71 | + def _sync_room_timeline(self, access_token, room_id): |
| 72 | + request, channel = self.make_request( |
| 73 | + "GET", "sync", access_token=self.mod_access_token |
| 74 | + ) |
| 75 | + self.render(request) |
| 76 | + self.assertEqual(channel.result["code"], b"200") |
| 77 | + room_sync = channel.json_body["rooms"]["join"][room_id] |
| 78 | + return room_sync["timeline"]["events"] |
| 79 | + |
| 80 | + def test_redact_event_as_moderator(self): |
| 81 | + # as a regular user, send a message to redact |
| 82 | + b = self.helper.send(room_id=self.room_id, tok=self.other_access_token) |
| 83 | + msg_id = b["event_id"] |
| 84 | + |
| 85 | + # as the moderator, send a redaction |
| 86 | + b = self._redact_event(self.mod_access_token, self.room_id, msg_id) |
| 87 | + redaction_id = b["event_id"] |
| 88 | + |
| 89 | + # now sync |
| 90 | + timeline = self._sync_room_timeline(self.mod_access_token, self.room_id) |
| 91 | + |
| 92 | + # the last event should be the redaction |
| 93 | + self.assertEqual(timeline[-1]["event_id"], redaction_id) |
| 94 | + self.assertEqual(timeline[-1]["redacts"], msg_id) |
| 95 | + |
| 96 | + # and the penultimate should be the redacted original |
| 97 | + self.assertEqual(timeline[-2]["event_id"], msg_id) |
| 98 | + self.assertEqual(timeline[-2]["unsigned"]["redacted_by"], redaction_id) |
| 99 | + self.assertEqual(timeline[-2]["content"], {}) |
| 100 | + |
| 101 | + def test_redact_event_as_normal(self): |
| 102 | + # as a regular user, send a message to redact |
| 103 | + b = self.helper.send(room_id=self.room_id, tok=self.other_access_token) |
| 104 | + normal_msg_id = b["event_id"] |
| 105 | + |
| 106 | + # also send one as the admin |
| 107 | + b = self.helper.send(room_id=self.room_id, tok=self.mod_access_token) |
| 108 | + admin_msg_id = b["event_id"] |
| 109 | + |
| 110 | + # as a normal, try to redact the admin's event |
| 111 | + self._redact_event( |
| 112 | + self.other_access_token, self.room_id, admin_msg_id, expect_code=403 |
| 113 | + ) |
| 114 | + |
| 115 | + # now try to redact our own event |
| 116 | + b = self._redact_event(self.other_access_token, self.room_id, normal_msg_id) |
| 117 | + redaction_id = b["event_id"] |
| 118 | + |
| 119 | + # now sync |
| 120 | + timeline = self._sync_room_timeline(self.other_access_token, self.room_id) |
| 121 | + |
| 122 | + # the last event should be the redaction of the normal event |
| 123 | + self.assertEqual(timeline[-1]["event_id"], redaction_id) |
| 124 | + self.assertEqual(timeline[-1]["redacts"], normal_msg_id) |
| 125 | + |
| 126 | + # the penultimate should be the unredacted one from the admin |
| 127 | + self.assertEqual(timeline[-2]["event_id"], admin_msg_id) |
| 128 | + self.assertNotIn("redacted_by", timeline[-2]["unsigned"]) |
| 129 | + self.assertTrue(timeline[-2]["content"]["body"], {}) |
| 130 | + |
| 131 | + # and the antepenultimate should be the redacted normal |
| 132 | + self.assertEqual(timeline[-3]["event_id"], normal_msg_id) |
| 133 | + self.assertEqual(timeline[-3]["unsigned"]["redacted_by"], redaction_id) |
| 134 | + self.assertEqual(timeline[-3]["content"], {}) |
| 135 | + |
| 136 | + def test_redact_nonexistent_event(self): |
| 137 | + # control case: an existing event |
| 138 | + b = self.helper.send(room_id=self.room_id, tok=self.other_access_token) |
| 139 | + msg_id = b["event_id"] |
| 140 | + b = self._redact_event(self.other_access_token, self.room_id, msg_id) |
| 141 | + redaction_id = b["event_id"] |
| 142 | + |
| 143 | + # room moderators can send redactions for non-existent events |
| 144 | + self._redact_event(self.mod_access_token, self.room_id, "$zzz") |
| 145 | + |
| 146 | + # ... but normals cannot |
| 147 | + self._redact_event( |
| 148 | + self.other_access_token, self.room_id, "$zzz", expect_code=404 |
| 149 | + ) |
| 150 | + |
| 151 | + # when we sync, we should see only the valid redaction |
| 152 | + timeline = self._sync_room_timeline(self.other_access_token, self.room_id) |
| 153 | + self.assertEqual(timeline[-1]["event_id"], redaction_id) |
| 154 | + self.assertEqual(timeline[-1]["redacts"], msg_id) |
| 155 | + |
| 156 | + # and the penultimate should be the redacted original |
| 157 | + self.assertEqual(timeline[-2]["event_id"], msg_id) |
| 158 | + self.assertEqual(timeline[-2]["unsigned"]["redacted_by"], redaction_id) |
| 159 | + self.assertEqual(timeline[-2]["content"], {}) |
0 commit comments