test_receipts.py 12 KB

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