test_receipts.py 9.4 KB

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