test_event_push_actions.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. import tests.unittest
  17. import tests.utils
  18. from mock import Mock
  19. USER_ID = "@user:example.com"
  20. PlAIN_NOTIF = ["notify", {"set_tweak": "highlight", "value": False}]
  21. HIGHLIGHT = [
  22. "notify", {"set_tweak": "sound", "value": "default"}, {"set_tweak": "highlight"}
  23. ]
  24. class EventPushActionsStoreTestCase(tests.unittest.TestCase):
  25. @defer.inlineCallbacks
  26. def setUp(self):
  27. hs = yield tests.utils.setup_test_homeserver()
  28. self.store = hs.get_datastore()
  29. @defer.inlineCallbacks
  30. def test_get_unread_push_actions_for_user_in_range_for_http(self):
  31. yield self.store.get_unread_push_actions_for_user_in_range_for_http(
  32. USER_ID, 0, 1000, 20
  33. )
  34. @defer.inlineCallbacks
  35. def test_get_unread_push_actions_for_user_in_range_for_email(self):
  36. yield self.store.get_unread_push_actions_for_user_in_range_for_email(
  37. USER_ID, 0, 1000, 20
  38. )
  39. @defer.inlineCallbacks
  40. def test_count_aggregation(self):
  41. room_id = "!foo:example.com"
  42. user_id = "@user1235:example.com"
  43. @defer.inlineCallbacks
  44. def _assert_counts(noitf_count, highlight_count):
  45. counts = yield self.store.runInteraction(
  46. "", self.store._get_unread_counts_by_pos_txn,
  47. room_id, user_id, 0, 0
  48. )
  49. self.assertEquals(
  50. counts,
  51. {"notify_count": noitf_count, "highlight_count": highlight_count}
  52. )
  53. def _inject_actions(stream, action):
  54. event = Mock()
  55. event.room_id = room_id
  56. event.event_id = "$test:example.com"
  57. event.internal_metadata.stream_ordering = stream
  58. event.depth = stream
  59. tuples = [(user_id, action)]
  60. return self.store.runInteraction(
  61. "", self.store._set_push_actions_for_event_and_users_txn,
  62. event, tuples
  63. )
  64. def _rotate(stream):
  65. return self.store.runInteraction(
  66. "", self.store._rotate_notifs_before_txn, stream
  67. )
  68. def _mark_read(stream, depth):
  69. return self.store.runInteraction(
  70. "", self.store._remove_old_push_actions_before_txn,
  71. room_id, user_id, depth, stream
  72. )
  73. yield _assert_counts(0, 0)
  74. yield _inject_actions(1, PlAIN_NOTIF)
  75. yield _assert_counts(1, 0)
  76. yield _rotate(2)
  77. yield _assert_counts(1, 0)
  78. yield _inject_actions(3, PlAIN_NOTIF)
  79. yield _assert_counts(2, 0)
  80. yield _rotate(4)
  81. yield _assert_counts(2, 0)
  82. yield _inject_actions(5, PlAIN_NOTIF)
  83. yield _mark_read(3, 3)
  84. yield _assert_counts(1, 0)
  85. yield _mark_read(5, 5)
  86. yield _assert_counts(0, 0)
  87. yield _inject_actions(6, PlAIN_NOTIF)
  88. yield _rotate(7)
  89. yield self.store._simple_delete(
  90. table="event_push_actions",
  91. keyvalues={"1": 1},
  92. desc="",
  93. )
  94. yield _assert_counts(1, 0)
  95. yield _mark_read(7, 7)
  96. yield _assert_counts(0, 0)
  97. yield _inject_actions(8, HIGHLIGHT)
  98. yield _assert_counts(1, 1)
  99. yield _rotate(9)
  100. yield _assert_counts(1, 1)
  101. yield _rotate(10)
  102. yield _assert_counts(1, 1)