You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

337 lines
12 KiB

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