test_receipts.py 11 KB

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