test_receipts.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  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 http import HTTPStatus
  15. from typing import Optional
  16. from twisted.test.proto_helpers import MemoryReactor
  17. import synapse.rest.admin
  18. from synapse.api.constants import EduTypes, EventTypes, HistoryVisibility, ReceiptTypes
  19. from synapse.rest.client import login, receipts, room, sync
  20. from synapse.server import HomeServer
  21. from synapse.types import JsonDict
  22. from synapse.util import Clock
  23. from tests import unittest
  24. class ReceiptsTestCase(unittest.HomeserverTestCase):
  25. servlets = [
  26. login.register_servlets,
  27. receipts.register_servlets,
  28. synapse.rest.admin.register_servlets,
  29. room.register_servlets,
  30. sync.register_servlets,
  31. ]
  32. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  33. self.url = "/sync?since=%s"
  34. self.next_batch = "s0"
  35. # Register the first user
  36. self.user_id = self.register_user("kermit", "monkey")
  37. self.tok = self.login("kermit", "monkey")
  38. # Create the room
  39. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  40. # Register the second user
  41. self.user2 = self.register_user("kermit2", "monkey")
  42. self.tok2 = self.login("kermit2", "monkey")
  43. # Join the second user
  44. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  45. def test_send_receipt(self) -> None:
  46. # Send a message.
  47. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  48. # Send a read receipt
  49. channel = self.make_request(
  50. "POST",
  51. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  52. {},
  53. access_token=self.tok2,
  54. )
  55. self.assertEqual(channel.code, 200)
  56. self.assertNotEqual(self._get_read_receipt(), None)
  57. def test_send_receipt_unknown_event(self) -> None:
  58. """Receipts sent for unknown events are ignored to not break message retention."""
  59. # Attempt to send a receipt to an unknown room.
  60. channel = self.make_request(
  61. "POST",
  62. "/rooms/!abc:beep/receipt/m.read/$def",
  63. content={},
  64. access_token=self.tok2,
  65. )
  66. self.assertEqual(channel.code, 200, channel.result)
  67. self.assertIsNone(self._get_read_receipt())
  68. # Attempt to send a receipt to an unknown event.
  69. channel = self.make_request(
  70. "POST",
  71. f"/rooms/{self.room_id}/receipt/m.read/$def",
  72. content={},
  73. access_token=self.tok2,
  74. )
  75. self.assertEqual(channel.code, 200, channel.result)
  76. self.assertIsNone(self._get_read_receipt())
  77. def test_send_receipt_unviewable_event(self) -> None:
  78. """Receipts sent for unviewable events are errors."""
  79. # Create a room where new users can't see events from before their join
  80. # & send events into it.
  81. room_id = self.helper.create_room_as(
  82. self.user_id,
  83. tok=self.tok,
  84. extra_content={
  85. "preset": "private_chat",
  86. "initial_state": [
  87. {
  88. "content": {"history_visibility": HistoryVisibility.JOINED},
  89. "state_key": "",
  90. "type": EventTypes.RoomHistoryVisibility,
  91. }
  92. ],
  93. },
  94. )
  95. res = self.helper.send(room_id, body="hello", tok=self.tok)
  96. # Attempt to send a receipt from the wrong user.
  97. channel = self.make_request(
  98. "POST",
  99. f"/rooms/{room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  100. content={},
  101. access_token=self.tok2,
  102. )
  103. self.assertEqual(channel.code, 403, channel.result)
  104. # Join the user to the room, but they still can't see the event.
  105. self.helper.invite(room_id, self.user_id, self.user2, tok=self.tok)
  106. self.helper.join(room=room_id, user=self.user2, tok=self.tok2)
  107. channel = self.make_request(
  108. "POST",
  109. f"/rooms/{room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  110. content={},
  111. access_token=self.tok2,
  112. )
  113. self.assertEqual(channel.code, 403, channel.result)
  114. def test_send_receipt_invalid_room_id(self) -> None:
  115. channel = self.make_request(
  116. "POST",
  117. "/rooms/not-a-room-id/receipt/m.read/$def",
  118. content={},
  119. access_token=self.tok,
  120. )
  121. self.assertEqual(channel.code, 400, channel.result)
  122. self.assertEqual(
  123. channel.json_body["error"], "A valid room ID and event ID must be specified"
  124. )
  125. def test_send_receipt_invalid_event_id(self) -> None:
  126. channel = self.make_request(
  127. "POST",
  128. "/rooms/!abc:beep/receipt/m.read/not-an-event-id",
  129. content={},
  130. access_token=self.tok,
  131. )
  132. self.assertEqual(channel.code, 400, channel.result)
  133. self.assertEqual(
  134. channel.json_body["error"], "A valid room ID and event ID must be specified"
  135. )
  136. def test_send_receipt_invalid_receipt_type(self) -> None:
  137. channel = self.make_request(
  138. "POST",
  139. "/rooms/!abc:beep/receipt/invalid-receipt-type/$def",
  140. content={},
  141. access_token=self.tok,
  142. )
  143. self.assertEqual(channel.code, 400, channel.result)
  144. def test_private_read_receipts(self) -> None:
  145. # Send a message as the first user
  146. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  147. # Send a private read receipt to tell the server the first user's message was read
  148. channel = self.make_request(
  149. "POST",
  150. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  151. {},
  152. access_token=self.tok2,
  153. )
  154. self.assertEqual(channel.code, 200)
  155. # Test that the first user can't see the other user's private read receipt
  156. self.assertIsNone(self._get_read_receipt())
  157. def test_public_receipt_can_override_private(self) -> None:
  158. """
  159. Sending a public read receipt to the same event which has a private read
  160. receipt should cause that receipt to become public.
  161. """
  162. # Send a message as the first user
  163. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  164. # Send a private read receipt
  165. channel = self.make_request(
  166. "POST",
  167. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  168. {},
  169. access_token=self.tok2,
  170. )
  171. self.assertEqual(channel.code, 200)
  172. self.assertIsNone(self._get_read_receipt())
  173. # Send a public read receipt
  174. channel = self.make_request(
  175. "POST",
  176. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  177. {},
  178. access_token=self.tok2,
  179. )
  180. self.assertEqual(channel.code, 200)
  181. # Test that we did override the private read receipt
  182. self.assertNotEqual(self._get_read_receipt(), None)
  183. def test_private_receipt_cannot_override_public(self) -> None:
  184. """
  185. Sending a private read receipt to the same event which has a public read
  186. receipt should cause no change.
  187. """
  188. # Send a message as the first user
  189. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  190. # Send a public read receipt
  191. channel = self.make_request(
  192. "POST",
  193. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  194. {},
  195. access_token=self.tok2,
  196. )
  197. self.assertEqual(channel.code, 200)
  198. self.assertNotEqual(self._get_read_receipt(), None)
  199. # Send a private read receipt
  200. channel = self.make_request(
  201. "POST",
  202. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  203. {},
  204. access_token=self.tok2,
  205. )
  206. self.assertEqual(channel.code, 200)
  207. # Test that we didn't override the public read receipt
  208. self.assertIsNone(self._get_read_receipt())
  209. def test_read_receipt_with_empty_body_is_rejected(self) -> None:
  210. # Send a message as the first user
  211. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  212. # Send a read receipt for this message with an empty body
  213. channel = self.make_request(
  214. "POST",
  215. f"/rooms/{self.room_id}/receipt/m.read/{res['event_id']}",
  216. access_token=self.tok2,
  217. )
  218. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST)
  219. self.assertEqual(channel.json_body["errcode"], "M_NOT_JSON", channel.json_body)
  220. def _get_read_receipt(self) -> Optional[JsonDict]:
  221. """Syncs and returns the read receipt."""
  222. # Checks if event is a read receipt
  223. def is_read_receipt(event: JsonDict) -> bool:
  224. return event["type"] == EduTypes.RECEIPT
  225. # Sync
  226. channel = self.make_request(
  227. "GET",
  228. self.url % self.next_batch,
  229. access_token=self.tok,
  230. )
  231. self.assertEqual(channel.code, 200)
  232. # Store the next batch for the next request.
  233. self.next_batch = channel.json_body["next_batch"]
  234. if channel.json_body.get("rooms", None) is None:
  235. return None
  236. # Return the read receipt
  237. ephemeral_events = channel.json_body["rooms"]["join"][self.room_id][
  238. "ephemeral"
  239. ]["events"]
  240. receipt_event = filter(is_read_receipt, ephemeral_events)
  241. return next(receipt_event, None)