test_bulk_push_rule_evaluator.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2022 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 patch
  15. from twisted.test.proto_helpers import MemoryReactor
  16. from synapse.api.room_versions import RoomVersions
  17. from synapse.push.bulk_push_rule_evaluator import BulkPushRuleEvaluator
  18. from synapse.rest import admin
  19. from synapse.rest.client import login, register, room
  20. from synapse.server import HomeServer
  21. from synapse.types import create_requester
  22. from synapse.util import Clock
  23. from tests.test_utils import simple_async_mock
  24. from tests.unittest import HomeserverTestCase, override_config
  25. class TestBulkPushRuleEvaluator(HomeserverTestCase):
  26. servlets = [
  27. admin.register_servlets_for_client_rest_resource,
  28. room.register_servlets,
  29. login.register_servlets,
  30. register.register_servlets,
  31. ]
  32. def prepare(
  33. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  34. ) -> None:
  35. # Create a new user and room.
  36. self.alice = self.register_user("alice", "pass")
  37. self.token = self.login(self.alice, "pass")
  38. self.requester = create_requester(self.alice)
  39. self.room_id = self.helper.create_room_as(
  40. self.alice, room_version=RoomVersions.V9.identifier, tok=self.token
  41. )
  42. self.event_creation_handler = self.hs.get_event_creation_handler()
  43. def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
  44. """We should convert floats and strings to integers before passing to Rust.
  45. Reproduces #14060.
  46. A lack of validation: the gift that keeps on giving.
  47. """
  48. # Alter the power levels in that room to include stringy and floaty levels.
  49. # We need to suppress the validation logic or else it will reject these dodgy
  50. # values. (Presumably this validation was not always present.)
  51. with patch("synapse.events.validator.validate_canonicaljson"), patch(
  52. "synapse.events.validator.jsonschema.validate"
  53. ):
  54. self.helper.send_state(
  55. self.room_id,
  56. "m.room.power_levels",
  57. {
  58. "users": {self.alice: "100"}, # stringy
  59. "notifications": {"room": 100.0}, # float
  60. },
  61. self.token,
  62. state_key="",
  63. )
  64. # Create a new message event, and try to evaluate it under the dodgy
  65. # power level event.
  66. event, context = self.get_success(
  67. self.event_creation_handler.create_event(
  68. self.requester,
  69. {
  70. "type": "m.room.message",
  71. "room_id": self.room_id,
  72. "content": {
  73. "msgtype": "m.text",
  74. "body": "helo",
  75. },
  76. "sender": self.alice,
  77. },
  78. )
  79. )
  80. bulk_evaluator = BulkPushRuleEvaluator(self.hs)
  81. # should not raise
  82. self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))
  83. @override_config({"push": {"enabled": False}})
  84. def test_action_for_event_by_user_disabled_by_config(self) -> None:
  85. """Ensure that push rules are not calculated when disabled in the config"""
  86. # Create a new message event which should cause a notification.
  87. event, context = self.get_success(
  88. self.event_creation_handler.create_event(
  89. self.requester,
  90. {
  91. "type": "m.room.message",
  92. "room_id": self.room_id,
  93. "content": {
  94. "msgtype": "m.text",
  95. "body": "helo",
  96. },
  97. "sender": self.alice,
  98. },
  99. )
  100. )
  101. bulk_evaluator = BulkPushRuleEvaluator(self.hs)
  102. # Mock the method which calculates push rules -- we do this instead of
  103. # e.g. checking the results in the database because we want to ensure
  104. # that code isn't even running.
  105. bulk_evaluator._action_for_event_by_user = simple_async_mock() # type: ignore[assignment]
  106. # Ensure no actions are generated!
  107. self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))
  108. bulk_evaluator._action_for_event_by_user.assert_not_called()