test_event_push_actions.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.depth = stream
  62. self.get_success(
  63. self.store.add_push_actions_to_staging(
  64. event.event_id,
  65. {user_id: action},
  66. False,
  67. )
  68. )
  69. self.get_success(
  70. self.store.db_pool.runInteraction(
  71. "",
  72. self.persist_events_store._set_push_actions_for_event_and_users_txn,
  73. [(event, None)],
  74. [(event, None)],
  75. )
  76. )
  77. def _rotate(stream):
  78. self.get_success(
  79. self.store.db_pool.runInteraction(
  80. "", self.store._rotate_notifs_before_txn, stream
  81. )
  82. )
  83. def _mark_read(stream, depth):
  84. self.get_success(
  85. self.store.db_pool.runInteraction(
  86. "",
  87. self.store._remove_old_push_actions_before_txn,
  88. room_id,
  89. user_id,
  90. stream,
  91. )
  92. )
  93. _assert_counts(0, 0)
  94. _inject_actions(1, PlAIN_NOTIF)
  95. _assert_counts(1, 0)
  96. _rotate(2)
  97. _assert_counts(1, 0)
  98. _inject_actions(3, PlAIN_NOTIF)
  99. _assert_counts(2, 0)
  100. _rotate(4)
  101. _assert_counts(2, 0)
  102. _inject_actions(5, PlAIN_NOTIF)
  103. _mark_read(3, 3)
  104. _assert_counts(1, 0)
  105. _mark_read(5, 5)
  106. _assert_counts(0, 0)
  107. _inject_actions(6, PlAIN_NOTIF)
  108. _rotate(7)
  109. self.get_success(
  110. self.store.db_pool.simple_delete(
  111. table="event_push_actions", keyvalues={"1": 1}, desc=""
  112. )
  113. )
  114. _assert_counts(1, 0)
  115. _mark_read(7, 7)
  116. _assert_counts(0, 0)
  117. _inject_actions(8, HIGHLIGHT)
  118. _assert_counts(1, 1)
  119. _rotate(9)
  120. _assert_counts(1, 1)
  121. _rotate(10)
  122. _assert_counts(1, 1)
  123. def test_find_first_stream_ordering_after_ts(self):
  124. def add_event(so, ts):
  125. self.get_success(
  126. self.store.db_pool.simple_insert(
  127. "events",
  128. {
  129. "stream_ordering": so,
  130. "received_ts": ts,
  131. "event_id": "event%i" % so,
  132. "type": "",
  133. "room_id": "",
  134. "content": "",
  135. "processed": True,
  136. "outlier": False,
  137. "topological_ordering": 0,
  138. "depth": 0,
  139. },
  140. )
  141. )
  142. # start with the base case where there are no events in the table
  143. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  144. self.assertEqual(r, 0)
  145. # now with one event
  146. add_event(2, 10)
  147. r = self.get_success(self.store.find_first_stream_ordering_after_ts(9))
  148. self.assertEqual(r, 2)
  149. r = self.get_success(self.store.find_first_stream_ordering_after_ts(10))
  150. self.assertEqual(r, 2)
  151. r = self.get_success(self.store.find_first_stream_ordering_after_ts(11))
  152. self.assertEqual(r, 3)
  153. # add a bunch of dummy events to the events table
  154. for (stream_ordering, ts) in (
  155. (3, 110),
  156. (4, 120),
  157. (5, 120),
  158. (10, 130),
  159. (20, 140),
  160. ):
  161. add_event(stream_ordering, ts)
  162. r = self.get_success(self.store.find_first_stream_ordering_after_ts(110))
  163. self.assertEqual(r, 3, "First event after 110ms should be 3, was %i" % r)
  164. # 4 and 5 are both after 120: we want 4 rather than 5
  165. r = self.get_success(self.store.find_first_stream_ordering_after_ts(120))
  166. self.assertEqual(r, 4, "First event after 120ms should be 4, was %i" % r)
  167. r = self.get_success(self.store.find_first_stream_ordering_after_ts(129))
  168. self.assertEqual(r, 10, "First event after 129ms should be 10, was %i" % r)
  169. # check we can get the last event
  170. r = self.get_success(self.store.find_first_stream_ordering_after_ts(140))
  171. self.assertEqual(r, 20, "First event after 14ms should be 20, was %i" % r)
  172. # off the end
  173. r = self.get_success(self.store.find_first_stream_ordering_after_ts(160))
  174. self.assertEqual(r, 21)
  175. # check we can find an event at ordering zero
  176. add_event(0, 5)
  177. r = self.get_success(self.store.find_first_stream_ordering_after_ts(1))
  178. self.assertEqual(r, 0)