test_receipts.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # Copyright 2016 OpenMarket Ltd
  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 parameterized import parameterized
  15. from synapse.api.constants import ReceiptTypes
  16. from synapse.types import UserID, create_requester
  17. from tests.test_utils.event_injection import create_event
  18. from tests.unittest import HomeserverTestCase
  19. OTHER_USER_ID = "@other:test"
  20. OUR_USER_ID = "@our:test"
  21. class ReceiptTestCase(HomeserverTestCase):
  22. def prepare(self, reactor, clock, homeserver) -> None:
  23. super().prepare(reactor, clock, homeserver)
  24. self.store = homeserver.get_datastores().main
  25. self.room_creator = homeserver.get_room_creation_handler()
  26. self.persist_event_storage_controller = (
  27. self.hs.get_storage_controllers().persistence
  28. )
  29. # Create a test user
  30. self.ourUser = UserID.from_string(OUR_USER_ID)
  31. self.ourRequester = create_requester(self.ourUser)
  32. # Create a second test user
  33. self.otherUser = UserID.from_string(OTHER_USER_ID)
  34. self.otherRequester = create_requester(self.otherUser)
  35. # Create a test room
  36. info, _ = self.get_success(self.room_creator.create_room(self.ourRequester, {}))
  37. self.room_id1 = info["room_id"]
  38. # Create a second test room
  39. info, _ = self.get_success(self.room_creator.create_room(self.ourRequester, {}))
  40. self.room_id2 = info["room_id"]
  41. # Join the second user to the first room
  42. memberEvent, memberEventContext = self.get_success(
  43. create_event(
  44. self.hs,
  45. room_id=self.room_id1,
  46. type="m.room.member",
  47. sender=self.otherRequester.user.to_string(),
  48. state_key=self.otherRequester.user.to_string(),
  49. content={"membership": "join"},
  50. )
  51. )
  52. self.get_success(
  53. self.persist_event_storage_controller.persist_event(
  54. memberEvent, memberEventContext
  55. )
  56. )
  57. # Join the second user to the second room
  58. memberEvent, memberEventContext = self.get_success(
  59. create_event(
  60. self.hs,
  61. room_id=self.room_id2,
  62. type="m.room.member",
  63. sender=self.otherRequester.user.to_string(),
  64. state_key=self.otherRequester.user.to_string(),
  65. content={"membership": "join"},
  66. )
  67. )
  68. self.get_success(
  69. self.persist_event_storage_controller.persist_event(
  70. memberEvent, memberEventContext
  71. )
  72. )
  73. def test_return_empty_with_no_data(self) -> None:
  74. res = self.get_success(
  75. self.store.get_receipts_for_user(
  76. OUR_USER_ID,
  77. [
  78. ReceiptTypes.READ,
  79. ReceiptTypes.READ_PRIVATE,
  80. ReceiptTypes.UNSTABLE_READ_PRIVATE,
  81. ],
  82. )
  83. )
  84. self.assertEqual(res, {})
  85. res = self.get_success(
  86. self.store.get_receipts_for_user_with_orderings(
  87. OUR_USER_ID,
  88. [
  89. ReceiptTypes.READ,
  90. ReceiptTypes.READ_PRIVATE,
  91. ReceiptTypes.UNSTABLE_READ_PRIVATE,
  92. ],
  93. )
  94. )
  95. self.assertEqual(res, {})
  96. res = self.get_success(
  97. self.store.get_last_receipt_event_id_for_user(
  98. OUR_USER_ID,
  99. self.room_id1,
  100. [
  101. ReceiptTypes.READ,
  102. ReceiptTypes.READ_PRIVATE,
  103. ReceiptTypes.UNSTABLE_READ_PRIVATE,
  104. ],
  105. )
  106. )
  107. self.assertEqual(res, None)
  108. @parameterized.expand(
  109. [ReceiptTypes.READ_PRIVATE, ReceiptTypes.UNSTABLE_READ_PRIVATE]
  110. )
  111. def test_get_receipts_for_user(self, receipt_type: str) -> None:
  112. # Send some events into the first room
  113. event1_1_id = self.create_and_send_event(
  114. self.room_id1, UserID.from_string(OTHER_USER_ID)
  115. )
  116. event1_2_id = self.create_and_send_event(
  117. self.room_id1, UserID.from_string(OTHER_USER_ID)
  118. )
  119. # Send public read receipt for the first event
  120. self.get_success(
  121. self.store.insert_receipt(
  122. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], {}
  123. )
  124. )
  125. # Send private read receipt for the second event
  126. self.get_success(
  127. self.store.insert_receipt(
  128. self.room_id1, receipt_type, OUR_USER_ID, [event1_2_id], {}
  129. )
  130. )
  131. # Test we get the latest event when we want both private and public receipts
  132. res = self.get_success(
  133. self.store.get_receipts_for_user(
  134. OUR_USER_ID, [ReceiptTypes.READ, receipt_type]
  135. )
  136. )
  137. self.assertEqual(res, {self.room_id1: event1_2_id})
  138. # Test we get the older event when we want only public receipt
  139. res = self.get_success(
  140. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
  141. )
  142. self.assertEqual(res, {self.room_id1: event1_1_id})
  143. # Test we get the latest event when we want only the public receipt
  144. res = self.get_success(
  145. self.store.get_receipts_for_user(OUR_USER_ID, [receipt_type])
  146. )
  147. self.assertEqual(res, {self.room_id1: event1_2_id})
  148. # Test receipt updating
  149. self.get_success(
  150. self.store.insert_receipt(
  151. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], {}
  152. )
  153. )
  154. res = self.get_success(
  155. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
  156. )
  157. self.assertEqual(res, {self.room_id1: event1_2_id})
  158. # Send some events into the second room
  159. event2_1_id = self.create_and_send_event(
  160. self.room_id2, UserID.from_string(OTHER_USER_ID)
  161. )
  162. # Test new room is reflected in what the method returns
  163. self.get_success(
  164. self.store.insert_receipt(
  165. self.room_id2, receipt_type, OUR_USER_ID, [event2_1_id], {}
  166. )
  167. )
  168. res = self.get_success(
  169. self.store.get_receipts_for_user(
  170. OUR_USER_ID, [ReceiptTypes.READ, receipt_type]
  171. )
  172. )
  173. self.assertEqual(res, {self.room_id1: event1_2_id, self.room_id2: event2_1_id})
  174. @parameterized.expand(
  175. [ReceiptTypes.READ_PRIVATE, ReceiptTypes.UNSTABLE_READ_PRIVATE]
  176. )
  177. def test_get_last_receipt_event_id_for_user(self, receipt_type: str) -> None:
  178. # Send some events into the first room
  179. event1_1_id = self.create_and_send_event(
  180. self.room_id1, UserID.from_string(OTHER_USER_ID)
  181. )
  182. event1_2_id = self.create_and_send_event(
  183. self.room_id1, UserID.from_string(OTHER_USER_ID)
  184. )
  185. # Send public read receipt for the first event
  186. self.get_success(
  187. self.store.insert_receipt(
  188. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], {}
  189. )
  190. )
  191. # Send private read receipt for the second event
  192. self.get_success(
  193. self.store.insert_receipt(
  194. self.room_id1, receipt_type, OUR_USER_ID, [event1_2_id], {}
  195. )
  196. )
  197. # Test we get the latest event when we want both private and public receipts
  198. res = self.get_success(
  199. self.store.get_last_receipt_event_id_for_user(
  200. OUR_USER_ID,
  201. self.room_id1,
  202. [ReceiptTypes.READ, receipt_type],
  203. )
  204. )
  205. self.assertEqual(res, event1_2_id)
  206. # Test we get the older event when we want only public receipt
  207. res = self.get_success(
  208. self.store.get_last_receipt_event_id_for_user(
  209. OUR_USER_ID, self.room_id1, [ReceiptTypes.READ]
  210. )
  211. )
  212. self.assertEqual(res, event1_1_id)
  213. # Test we get the latest event when we want only the private receipt
  214. res = self.get_success(
  215. self.store.get_last_receipt_event_id_for_user(
  216. OUR_USER_ID, self.room_id1, [receipt_type]
  217. )
  218. )
  219. self.assertEqual(res, event1_2_id)
  220. # Test receipt updating
  221. self.get_success(
  222. self.store.insert_receipt(
  223. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], {}
  224. )
  225. )
  226. res = self.get_success(
  227. self.store.get_last_receipt_event_id_for_user(
  228. OUR_USER_ID, self.room_id1, [ReceiptTypes.READ]
  229. )
  230. )
  231. self.assertEqual(res, event1_2_id)
  232. # Send some events into the second room
  233. event2_1_id = self.create_and_send_event(
  234. self.room_id2, UserID.from_string(OTHER_USER_ID)
  235. )
  236. # Test new room is reflected in what the method returns
  237. self.get_success(
  238. self.store.insert_receipt(
  239. self.room_id2, receipt_type, OUR_USER_ID, [event2_1_id], {}
  240. )
  241. )
  242. res = self.get_success(
  243. self.store.get_last_receipt_event_id_for_user(
  244. OUR_USER_ID,
  245. self.room_id2,
  246. [ReceiptTypes.READ, receipt_type],
  247. )
  248. )
  249. self.assertEqual(res, event2_1_id)