1# Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com> 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 15 16from typing import List 17 18from synapse.api.constants import ReadReceiptEventFields 19from synapse.types import JsonDict 20 21from tests import unittest 22 23 24class ReceiptsTestCase(unittest.HomeserverTestCase): 25 def prepare(self, reactor, clock, hs): 26 self.event_source = hs.get_event_sources().sources.receipt 27 28 # In the first param of _test_filters_hidden we use "hidden" instead of 29 # ReadReceiptEventFields.MSC2285_HIDDEN. We do this because we're mocking 30 # the data from the database which doesn't use the prefix 31 32 def test_filters_out_hidden_receipt(self): 33 self._test_filters_hidden( 34 [ 35 { 36 "content": { 37 "$1435641916114394fHBLK:matrix.org": { 38 "m.read": { 39 "@rikj:jki.re": { 40 "ts": 1436451550453, 41 "hidden": True, 42 } 43 } 44 } 45 }, 46 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 47 "type": "m.receipt", 48 } 49 ], 50 [], 51 ) 52 53 def test_does_not_filter_out_our_hidden_receipt(self): 54 self._test_filters_hidden( 55 [ 56 { 57 "content": { 58 "$1435641916hfgh4394fHBLK:matrix.org": { 59 "m.read": { 60 "@me:server.org": { 61 "ts": 1436451550453, 62 "hidden": True, 63 }, 64 } 65 } 66 }, 67 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 68 "type": "m.receipt", 69 } 70 ], 71 [ 72 { 73 "content": { 74 "$1435641916hfgh4394fHBLK:matrix.org": { 75 "m.read": { 76 "@me:server.org": { 77 "ts": 1436451550453, 78 ReadReceiptEventFields.MSC2285_HIDDEN: True, 79 }, 80 } 81 } 82 }, 83 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 84 "type": "m.receipt", 85 } 86 ], 87 ) 88 89 def test_filters_out_hidden_receipt_and_ignores_rest(self): 90 self._test_filters_hidden( 91 [ 92 { 93 "content": { 94 "$1dgdgrd5641916114394fHBLK:matrix.org": { 95 "m.read": { 96 "@rikj:jki.re": { 97 "ts": 1436451550453, 98 "hidden": True, 99 }, 100 "@user:jki.re": { 101 "ts": 1436451550453, 102 }, 103 } 104 } 105 }, 106 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 107 "type": "m.receipt", 108 } 109 ], 110 [ 111 { 112 "content": { 113 "$1dgdgrd5641916114394fHBLK:matrix.org": { 114 "m.read": { 115 "@user:jki.re": { 116 "ts": 1436451550453, 117 } 118 } 119 } 120 }, 121 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 122 "type": "m.receipt", 123 } 124 ], 125 ) 126 127 def test_filters_out_event_with_only_hidden_receipts_and_ignores_the_rest(self): 128 self._test_filters_hidden( 129 [ 130 { 131 "content": { 132 "$14356419edgd14394fHBLK:matrix.org": { 133 "m.read": { 134 "@rikj:jki.re": { 135 "ts": 1436451550453, 136 "hidden": True, 137 }, 138 } 139 }, 140 "$1435641916114394fHBLK:matrix.org": { 141 "m.read": { 142 "@user:jki.re": { 143 "ts": 1436451550453, 144 } 145 } 146 }, 147 }, 148 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 149 "type": "m.receipt", 150 } 151 ], 152 [ 153 { 154 "content": { 155 "$1435641916114394fHBLK:matrix.org": { 156 "m.read": { 157 "@user:jki.re": { 158 "ts": 1436451550453, 159 } 160 } 161 } 162 }, 163 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 164 "type": "m.receipt", 165 } 166 ], 167 ) 168 169 def test_handles_missing_content_of_m_read(self): 170 self._test_filters_hidden( 171 [ 172 { 173 "content": { 174 "$14356419ggffg114394fHBLK:matrix.org": {"m.read": {}}, 175 "$1435641916114394fHBLK:matrix.org": { 176 "m.read": { 177 "@user:jki.re": { 178 "ts": 1436451550453, 179 } 180 } 181 }, 182 }, 183 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 184 "type": "m.receipt", 185 } 186 ], 187 [ 188 { 189 "content": { 190 "$14356419ggffg114394fHBLK:matrix.org": {"m.read": {}}, 191 "$1435641916114394fHBLK:matrix.org": { 192 "m.read": { 193 "@user:jki.re": { 194 "ts": 1436451550453, 195 } 196 } 197 }, 198 }, 199 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 200 "type": "m.receipt", 201 } 202 ], 203 ) 204 205 def test_handles_empty_event(self): 206 self._test_filters_hidden( 207 [ 208 { 209 "content": { 210 "$143564gdfg6114394fHBLK:matrix.org": {}, 211 "$1435641916114394fHBLK:matrix.org": { 212 "m.read": { 213 "@user:jki.re": { 214 "ts": 1436451550453, 215 } 216 } 217 }, 218 }, 219 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 220 "type": "m.receipt", 221 } 222 ], 223 [ 224 { 225 "content": { 226 "$143564gdfg6114394fHBLK:matrix.org": {}, 227 "$1435641916114394fHBLK:matrix.org": { 228 "m.read": { 229 "@user:jki.re": { 230 "ts": 1436451550453, 231 } 232 } 233 }, 234 }, 235 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 236 "type": "m.receipt", 237 } 238 ], 239 ) 240 241 def test_filters_out_receipt_event_with_only_hidden_receipt_and_ignores_rest(self): 242 self._test_filters_hidden( 243 [ 244 { 245 "content": { 246 "$14356419edgd14394fHBLK:matrix.org": { 247 "m.read": { 248 "@rikj:jki.re": { 249 "ts": 1436451550453, 250 "hidden": True, 251 }, 252 } 253 }, 254 }, 255 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 256 "type": "m.receipt", 257 }, 258 { 259 "content": { 260 "$1435641916114394fHBLK:matrix.org": { 261 "m.read": { 262 "@user:jki.re": { 263 "ts": 1436451550453, 264 } 265 } 266 }, 267 }, 268 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 269 "type": "m.receipt", 270 }, 271 ], 272 [ 273 { 274 "content": { 275 "$1435641916114394fHBLK:matrix.org": { 276 "m.read": { 277 "@user:jki.re": { 278 "ts": 1436451550453, 279 } 280 } 281 } 282 }, 283 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 284 "type": "m.receipt", 285 } 286 ], 287 ) 288 289 def test_handles_string_data(self): 290 """ 291 Tests that an invalid shape for read-receipts is handled. 292 Context: https://github.com/matrix-org/synapse/issues/10603 293 """ 294 295 self._test_filters_hidden( 296 [ 297 { 298 "content": { 299 "$14356419edgd14394fHBLK:matrix.org": { 300 "m.read": { 301 "@rikj:jki.re": "string", 302 } 303 }, 304 }, 305 "room_id": "!jEsUZKDJdhlrceRyVU:example.org", 306 "type": "m.receipt", 307 }, 308 ], 309 [], 310 ) 311 312 def _test_filters_hidden( 313 self, events: List[JsonDict], expected_output: List[JsonDict] 314 ): 315 """Tests that the _filter_out_hidden returns the expected output""" 316 filtered_events = self.event_source.filter_out_hidden(events, "@me:server.org") 317 self.assertEquals(filtered_events, expected_output) 318