test_receipts.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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):
  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):
  73. res = self.get_success(
  74. self.store.get_receipts_for_user(
  75. OUR_USER_ID, [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
  76. )
  77. )
  78. self.assertEqual(res, {})
  79. res = self.get_success(
  80. self.store.get_receipts_for_user_with_orderings(
  81. OUR_USER_ID,
  82. [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE],
  83. )
  84. )
  85. self.assertEqual(res, {})
  86. res = self.get_success(
  87. self.store.get_last_receipt_event_id_for_user(
  88. OUR_USER_ID,
  89. self.room_id1,
  90. [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE],
  91. )
  92. )
  93. self.assertEqual(res, None)
  94. def test_get_receipts_for_user(self):
  95. # Send some events into the first room
  96. event1_1_id = self.create_and_send_event(
  97. self.room_id1, UserID.from_string(OTHER_USER_ID)
  98. )
  99. event1_2_id = self.create_and_send_event(
  100. self.room_id1, UserID.from_string(OTHER_USER_ID)
  101. )
  102. # Send public read receipt for the first event
  103. self.get_success(
  104. self.store.insert_receipt(
  105. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], {}
  106. )
  107. )
  108. # Send private read receipt for the second event
  109. self.get_success(
  110. self.store.insert_receipt(
  111. self.room_id1, ReceiptTypes.READ_PRIVATE, OUR_USER_ID, [event1_2_id], {}
  112. )
  113. )
  114. # Test we get the latest event when we want both private and public receipts
  115. res = self.get_success(
  116. self.store.get_receipts_for_user(
  117. OUR_USER_ID, [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
  118. )
  119. )
  120. self.assertEqual(res, {self.room_id1: event1_2_id})
  121. # Test we get the older event when we want only public receipt
  122. res = self.get_success(
  123. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
  124. )
  125. self.assertEqual(res, {self.room_id1: event1_1_id})
  126. # Test we get the latest event when we want only the public receipt
  127. res = self.get_success(
  128. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ_PRIVATE])
  129. )
  130. self.assertEqual(res, {self.room_id1: event1_2_id})
  131. # Test receipt updating
  132. self.get_success(
  133. self.store.insert_receipt(
  134. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], {}
  135. )
  136. )
  137. res = self.get_success(
  138. self.store.get_receipts_for_user(OUR_USER_ID, [ReceiptTypes.READ])
  139. )
  140. self.assertEqual(res, {self.room_id1: event1_2_id})
  141. # Send some events into the second room
  142. event2_1_id = self.create_and_send_event(
  143. self.room_id2, UserID.from_string(OTHER_USER_ID)
  144. )
  145. # Test new room is reflected in what the method returns
  146. self.get_success(
  147. self.store.insert_receipt(
  148. self.room_id2, ReceiptTypes.READ_PRIVATE, OUR_USER_ID, [event2_1_id], {}
  149. )
  150. )
  151. res = self.get_success(
  152. self.store.get_receipts_for_user(
  153. OUR_USER_ID, [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE]
  154. )
  155. )
  156. self.assertEqual(res, {self.room_id1: event1_2_id, self.room_id2: event2_1_id})
  157. def test_get_last_receipt_event_id_for_user(self):
  158. # Send some events into the first room
  159. event1_1_id = self.create_and_send_event(
  160. self.room_id1, UserID.from_string(OTHER_USER_ID)
  161. )
  162. event1_2_id = self.create_and_send_event(
  163. self.room_id1, UserID.from_string(OTHER_USER_ID)
  164. )
  165. # Send public read receipt for the first event
  166. self.get_success(
  167. self.store.insert_receipt(
  168. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_1_id], {}
  169. )
  170. )
  171. # Send private read receipt for the second event
  172. self.get_success(
  173. self.store.insert_receipt(
  174. self.room_id1, ReceiptTypes.READ_PRIVATE, OUR_USER_ID, [event1_2_id], {}
  175. )
  176. )
  177. # Test we get the latest event when we want both private and public receipts
  178. res = self.get_success(
  179. self.store.get_last_receipt_event_id_for_user(
  180. OUR_USER_ID,
  181. self.room_id1,
  182. [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE],
  183. )
  184. )
  185. self.assertEqual(res, event1_2_id)
  186. # Test we get the older event when we want only public receipt
  187. res = self.get_success(
  188. self.store.get_last_receipt_event_id_for_user(
  189. OUR_USER_ID, self.room_id1, [ReceiptTypes.READ]
  190. )
  191. )
  192. self.assertEqual(res, event1_1_id)
  193. # Test we get the latest event when we want only the private receipt
  194. res = self.get_success(
  195. self.store.get_last_receipt_event_id_for_user(
  196. OUR_USER_ID, self.room_id1, [ReceiptTypes.READ_PRIVATE]
  197. )
  198. )
  199. self.assertEqual(res, event1_2_id)
  200. # Test receipt updating
  201. self.get_success(
  202. self.store.insert_receipt(
  203. self.room_id1, ReceiptTypes.READ, OUR_USER_ID, [event1_2_id], {}
  204. )
  205. )
  206. res = self.get_success(
  207. self.store.get_last_receipt_event_id_for_user(
  208. OUR_USER_ID, self.room_id1, [ReceiptTypes.READ]
  209. )
  210. )
  211. self.assertEqual(res, event1_2_id)
  212. # Send some events into the second room
  213. event2_1_id = self.create_and_send_event(
  214. self.room_id2, UserID.from_string(OTHER_USER_ID)
  215. )
  216. # Test new room is reflected in what the method returns
  217. self.get_success(
  218. self.store.insert_receipt(
  219. self.room_id2, ReceiptTypes.READ_PRIVATE, OUR_USER_ID, [event2_1_id], {}
  220. )
  221. )
  222. res = self.get_success(
  223. self.store.get_last_receipt_event_id_for_user(
  224. OUR_USER_ID,
  225. self.room_id2,
  226. [ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE],
  227. )
  228. )
  229. self.assertEqual(res, event2_1_id)