test_bulk_push_rule_evaluator.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from unittest.mock import patch
  2. from synapse.api.room_versions import RoomVersions
  3. from synapse.push.bulk_push_rule_evaluator import BulkPushRuleEvaluator
  4. from synapse.rest import admin
  5. from synapse.rest.client import login, register, room
  6. from synapse.types import create_requester
  7. from tests import unittest
  8. class TestBulkPushRuleEvaluator(unittest.HomeserverTestCase):
  9. servlets = [
  10. admin.register_servlets_for_client_rest_resource,
  11. room.register_servlets,
  12. login.register_servlets,
  13. register.register_servlets,
  14. ]
  15. def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
  16. """We should convert floats and strings to integers before passing to Rust.
  17. Reproduces #14060.
  18. A lack of validation: the gift that keeps on giving.
  19. """
  20. # Create a new user and room.
  21. alice = self.register_user("alice", "pass")
  22. token = self.login(alice, "pass")
  23. room_id = self.helper.create_room_as(
  24. alice, room_version=RoomVersions.V9.identifier, tok=token
  25. )
  26. # Alter the power levels in that room to include stringy and floaty levels.
  27. # We need to suppress the validation logic or else it will reject these dodgy
  28. # values. (Presumably this validation was not always present.)
  29. event_creation_handler = self.hs.get_event_creation_handler()
  30. requester = create_requester(alice)
  31. with patch("synapse.events.validator.validate_canonicaljson"), patch(
  32. "synapse.events.validator.jsonschema.validate"
  33. ):
  34. self.helper.send_state(
  35. room_id,
  36. "m.room.power_levels",
  37. {
  38. "users": {alice: "100"}, # stringy
  39. "notifications": {"room": 100.0}, # float
  40. },
  41. token,
  42. state_key="",
  43. )
  44. # Create a new message event, and try to evaluate it under the dodgy
  45. # power level event.
  46. event, context = self.get_success(
  47. event_creation_handler.create_event(
  48. requester,
  49. {
  50. "type": "m.room.message",
  51. "room_id": room_id,
  52. "content": {
  53. "msgtype": "m.text",
  54. "body": "helo",
  55. },
  56. "sender": alice,
  57. },
  58. )
  59. )
  60. bulk_evaluator = BulkPushRuleEvaluator(self.hs)
  61. # should not raise
  62. self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))