test_receipts.py 12 KB

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