test_event_push_actions.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Copyright 2016-2021 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 unittest.mock import Mock
  15. from tests.unittest import HomeserverTestCase
  16. USER_ID = "@user:example.com"
  17. PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
  18. HIGHLIGHT = [
  19. "notify",
  20. {"set_tweak": "sound", "value": "default"},
  21. {"set_tweak": "highlight"},
  22. ]
  23. class EventPushActionsStoreTestCase(HomeserverTestCase):
  24. def prepare(self, reactor, clock, hs):
  25. self.store = hs.get_datastore()
  26. self.persist_events_store = hs.get_datastores().persist_events
  27. def test_get_unread_push_actions_for_user_in_range_for_http(self):
  28. self.get_success(
  29. self.store.get_unread_push_actions_for_user_in_range_for_http(
  30. USER_ID, 0, 1000, 20
  31. )
  32. )
  33. def test_get_unread_push_actions_for_user_in_range_for_email(self):
  34. self.get_success(
  35. self.store.get_unread_push_actions_for_user_in_range_for_email(
  36. USER_ID, 0, 1000, 20
  37. )
  38. )
  39. def test_count_aggregation(self):
  40. room_id = "!foo:example.com"
  41. user_id = "@user1235:example.com"
  42. def _assert_counts(noitf_count, highlight_count):
  43. counts = self.get_success(
  44. self.store.db_pool.runInteraction(
  45. "", self.store._get_unread_counts_by_pos_txn, room_id, user_id, 0
  46. )
  47. )
  48. self.assertEquals(
  49. counts,
  50. {
  51. "notify_count": noitf_count,
  52. "unread_count": 0, # Unread counts are tested in the sync tests.
  53. "highlight_count": highlight_count,
  54. },
  55. )
  56. def _inject_actions(stream, action):
  57. event = Mock()
  58. event.room_id = room_id
  59. event.event_id = "$test:example.com"
  60. event.internal_metadata.stream_ordering = stream
  61. event.internal_metadata.is_outlier.return_value = False
  62. event.depth = stream
  63. self.get_success(
  64. self.store.add_push_actions_to_staging(
  65. event.event_id,
  66. {user_id: action},
  67. False,
  68. )
  69. )
  70. self.get_success(
  71. self.store.db_pool.runInteraction(
  72. "",
  73. self.persist_events_store._set_push_actions_for_event_and_users_txn,
  74. [(event, None)],
  75. [(event, None)],
  76. )
  77. )
  78. def _rotate(stream):
  79. self.get_success(
  80. self.store.db_pool.runInteraction(
  81. "", self.store._rotate_notifs_before_txn, stream
  82. )
  83. )
  84. def _mark_read(stream, depth):
  85. self.get_success(
  86. self.store.db_pool.runInteraction(
  87. "",
  88. self.store._remove_old_push_actions_before_txn,
  89. room_id,
  90. user_id,
  91. stream,
  92. )
  93. )
  94. _assert_counts(0, 0)
  95. _inject_actions(1, PlAIN_NOTIF)
  96. _assert_counts(1, 0)
  97. _rotate(2)
  98. _assert_counts(1, 0)
  99. _inject_actions(3, PlAIN_NOTIF)
  100. _assert_counts(2, 0)
  101. _rotate(4)
  102. _assert_counts(2, 0)
  103. _inject_actions(5, PlAIN_NOTIF)
  104. _mark_read(3, 3)
  105. _assert_counts(1, 0)
  106. _mark_read(5, 5)
  107. _assert_counts(0, 0)
  108. _inject_actions(6, PlAIN_NOTIF)
  109. _rotate(7)
  110. self.get_success(
  111. self.store.db_pool.simple_delete(
  112. table="event_push_actions", keyvalues={"1": 1}, desc=""
  113. )
  114. )
  115. _assert_counts(1, 0)
  116. _mark_read(7, 7)
  117. _assert_counts(0, 0)
  118. _inject_actions(8, HIGHLIGHT)
  119. _assert_counts(1, 1)
  120. _rotate(9)
  121. _assert_counts(1, 1)
  122. _rotate(10)
  123. _assert_counts(1, 1)
  124. def test_find_first_stream_ordering_after_ts(self):
  125. def add_event(so, ts):
  126. self.get_success(
  127. self.store.db_pool.simple_insert(
  128. "events",
  129. {
  130. "stream_ordering": so,
  131. "received_ts": ts,
  132. "event_id": "event%i" % so,
  133. "type": "",
  134. "room_id": "",
  135. "content": "",
  136. "processed": True,
  137. "outlier": False,
  138. "topological_ordering": 0,
  139. "depth": 0,
  140. },
  141. )
  142. )
  143. # start with the base case where there are no events in the table
  144. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  145. self.assertEqual(r, 0)
  146. # now with one event
  147. add_event(2, 10)
  148. r = self.get_success(self.store.find_first_stream_ordering_after_ts(9))
  149. self.assertEqual(r, 2)
  150. r = self.get_success(self.store.find_first_stream_ordering_after_ts(10))
  151. self.assertEqual(r, 2)
  152. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  153. self.assertEqual(r, 3)
  154. # add a bunch of dummy events to the events table
  155. for (stream_ordering, ts) in (
  156. (3, 110),
  157. (4, 120),
  158. (5, 120),
  159. (10, 130),
  160. (20, 140),
  161. ):
  162. add_event(stream_ordering, ts)
  163. r = self.get_success(self.store.find_first_stream_ordering_after_ts(110))
  164. self.assertEqual(r, 3, "First event after 110ms should be 3, was %i" % r)
  165. # 4 and 5 are both after 120: we want 4 rather than 5
  166. r = self.get_success(self.store.find_first_stream_ordering_after_ts(120))
  167. self.assertEqual(r, 4, "First event after 120ms should be 4, was %i" % r)
  168. r = self.get_success(self.store.find_first_stream_ordering_after_ts(129))
  169. self.assertEqual(r, 10, "First event after 129ms should be 10, was %i" % r)
  170. # check we can get the last event
  171. r = self.get_success(self.store.find_first_stream_ordering_after_ts(140))
  172. self.assertEqual(r, 20, "First event after 14ms should be 20, was %i" % r)
  173. # off the end
  174. r = self.get_success(self.store.find_first_stream_ordering_after_ts(160))
  175. self.assertEqual(r, 21)
  176. # check we can find an event at ordering zero
  177. add_event(0, 5)
  178. r = self.get_success(self.store.find_first_stream_ordering_after_ts(1))
  179. self.assertEqual(r, 0)