test_receipts.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 EduTypes, 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) -> None:
  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": EduTypes.RECEIPT,
  37. }
  38. ],
  39. [],
  40. )
  41. def test_filters_out_private_receipt_and_ignores_rest(self) -> None:
  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": EduTypes.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": EduTypes.RECEIPT,
  76. }
  77. ],
  78. )
  79. def test_filters_out_event_with_only_private_receipts_and_ignores_the_rest(
  80. self,
  81. ) -> None:
  82. self._test_filters_private(
  83. [
  84. {
  85. "content": {
  86. "$14356419edgd14394fHBLK:matrix.org": {
  87. ReceiptTypes.READ_PRIVATE: {
  88. "@rikj:jki.re": {
  89. "ts": 1436451550453,
  90. },
  91. }
  92. },
  93. "$1435641916114394fHBLK:matrix.org": {
  94. ReceiptTypes.READ: {
  95. "@user:jki.re": {
  96. "ts": 1436451550453,
  97. }
  98. }
  99. },
  100. },
  101. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  102. "type": EduTypes.RECEIPT,
  103. }
  104. ],
  105. [
  106. {
  107. "content": {
  108. "$1435641916114394fHBLK:matrix.org": {
  109. ReceiptTypes.READ: {
  110. "@user:jki.re": {
  111. "ts": 1436451550453,
  112. }
  113. }
  114. }
  115. },
  116. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  117. "type": EduTypes.RECEIPT,
  118. }
  119. ],
  120. )
  121. def test_handles_empty_event(self) -> None:
  122. self._test_filters_private(
  123. [
  124. {
  125. "content": {
  126. "$143564gdfg6114394fHBLK:matrix.org": {},
  127. "$1435641916114394fHBLK:matrix.org": {
  128. ReceiptTypes.READ: {
  129. "@user:jki.re": {
  130. "ts": 1436451550453,
  131. }
  132. }
  133. },
  134. },
  135. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  136. "type": EduTypes.RECEIPT,
  137. }
  138. ],
  139. [
  140. {
  141. "content": {
  142. "$1435641916114394fHBLK:matrix.org": {
  143. ReceiptTypes.READ: {
  144. "@user:jki.re": {
  145. "ts": 1436451550453,
  146. }
  147. }
  148. },
  149. },
  150. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  151. "type": EduTypes.RECEIPT,
  152. }
  153. ],
  154. )
  155. def test_filters_out_receipt_event_with_only_private_receipt_and_ignores_rest(
  156. self,
  157. ) -> None:
  158. self._test_filters_private(
  159. [
  160. {
  161. "content": {
  162. "$14356419edgd14394fHBLK:matrix.org": {
  163. ReceiptTypes.READ_PRIVATE: {
  164. "@rikj:jki.re": {
  165. "ts": 1436451550453,
  166. },
  167. }
  168. },
  169. },
  170. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  171. "type": EduTypes.RECEIPT,
  172. },
  173. {
  174. "content": {
  175. "$1435641916114394fHBLK:matrix.org": {
  176. ReceiptTypes.READ: {
  177. "@user:jki.re": {
  178. "ts": 1436451550453,
  179. }
  180. }
  181. },
  182. },
  183. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  184. "type": EduTypes.RECEIPT,
  185. },
  186. ],
  187. [
  188. {
  189. "content": {
  190. "$1435641916114394fHBLK:matrix.org": {
  191. ReceiptTypes.READ: {
  192. "@user:jki.re": {
  193. "ts": 1436451550453,
  194. }
  195. }
  196. }
  197. },
  198. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  199. "type": EduTypes.RECEIPT,
  200. }
  201. ],
  202. )
  203. def test_handles_string_data(self) -> None:
  204. """
  205. Tests that an invalid shape for read-receipts is handled.
  206. Context: https://github.com/matrix-org/synapse/issues/10603
  207. """
  208. self._test_filters_private(
  209. [
  210. {
  211. "content": {
  212. "$14356419edgd14394fHBLK:matrix.org": {
  213. ReceiptTypes.READ: {
  214. "@rikj:jki.re": "string",
  215. }
  216. },
  217. },
  218. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  219. "type": EduTypes.RECEIPT,
  220. },
  221. ],
  222. [
  223. {
  224. "content": {
  225. "$14356419edgd14394fHBLK:matrix.org": {
  226. ReceiptTypes.READ: {
  227. "@rikj:jki.re": "string",
  228. }
  229. },
  230. },
  231. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  232. "type": EduTypes.RECEIPT,
  233. },
  234. ],
  235. )
  236. def test_leaves_our_private_and_their_public(self) -> None:
  237. self._test_filters_private(
  238. [
  239. {
  240. "content": {
  241. "$1dgdgrd5641916114394fHBLK:matrix.org": {
  242. ReceiptTypes.READ_PRIVATE: {
  243. "@me:server.org": {
  244. "ts": 1436451550453,
  245. },
  246. },
  247. ReceiptTypes.READ: {
  248. "@rikj:jki.re": {
  249. "ts": 1436451550453,
  250. },
  251. },
  252. "a.receipt.type": {
  253. "@rikj:jki.re": {
  254. "ts": 1436451550453,
  255. },
  256. },
  257. },
  258. },
  259. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  260. "type": EduTypes.RECEIPT,
  261. }
  262. ],
  263. [
  264. {
  265. "content": {
  266. "$1dgdgrd5641916114394fHBLK:matrix.org": {
  267. ReceiptTypes.READ_PRIVATE: {
  268. "@me:server.org": {
  269. "ts": 1436451550453,
  270. },
  271. },
  272. ReceiptTypes.READ: {
  273. "@rikj:jki.re": {
  274. "ts": 1436451550453,
  275. },
  276. },
  277. "a.receipt.type": {
  278. "@rikj:jki.re": {
  279. "ts": 1436451550453,
  280. },
  281. },
  282. }
  283. },
  284. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  285. "type": EduTypes.RECEIPT,
  286. }
  287. ],
  288. )
  289. def test_we_do_not_mutate(self) -> None:
  290. """Ensure the input values are not modified."""
  291. events = [
  292. {
  293. "content": {
  294. "$1435641916114394fHBLK:matrix.org": {
  295. ReceiptTypes.READ_PRIVATE: {
  296. "@rikj:jki.re": {
  297. "ts": 1436451550453,
  298. }
  299. }
  300. }
  301. },
  302. "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
  303. "type": EduTypes.RECEIPT,
  304. }
  305. ]
  306. original_events = deepcopy(events)
  307. self._test_filters_private(events, [])
  308. # Since the events are fed in from a cache they should not be modified.
  309. self.assertEqual(events, original_events)
  310. def _test_filters_private(
  311. self, events: List[JsonDict], expected_output: List[JsonDict]
  312. ) -> None:
  313. """Tests that the _filter_out_private returns the expected output"""
  314. filtered_events = self.event_source.filter_out_private_receipts(
  315. events, "@me:server.org"
  316. )
  317. self.assertEqual(filtered_events, expected_output)