test_push_rule_evaluator.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright 2020 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 synapse.api.room_versions import RoomVersions
  15. from synapse.events import FrozenEvent
  16. from synapse.push import push_rule_evaluator
  17. from synapse.push.push_rule_evaluator import PushRuleEvaluatorForEvent
  18. from tests import unittest
  19. class PushRuleEvaluatorTestCase(unittest.TestCase):
  20. def _get_evaluator(self, content):
  21. event = FrozenEvent(
  22. {
  23. "event_id": "$event_id",
  24. "type": "m.room.history_visibility",
  25. "sender": "@user:test",
  26. "state_key": "",
  27. "room_id": "#room:test",
  28. "content": content,
  29. },
  30. RoomVersions.V1,
  31. )
  32. room_member_count = 0
  33. sender_power_level = 0
  34. power_levels = {}
  35. return PushRuleEvaluatorForEvent(
  36. event, room_member_count, sender_power_level, power_levels
  37. )
  38. def test_display_name(self):
  39. """Check for a matching display name in the body of the event."""
  40. evaluator = self._get_evaluator({"body": "foo bar baz"})
  41. condition = {
  42. "kind": "contains_display_name",
  43. }
  44. # Blank names are skipped.
  45. self.assertFalse(evaluator.matches(condition, "@user:test", ""))
  46. # Check a display name that doesn't match.
  47. self.assertFalse(evaluator.matches(condition, "@user:test", "not found"))
  48. # Check a display name which matches.
  49. self.assertTrue(evaluator.matches(condition, "@user:test", "foo"))
  50. # A display name that matches, but not a full word does not result in a match.
  51. self.assertFalse(evaluator.matches(condition, "@user:test", "ba"))
  52. # A display name should not be interpreted as a regular expression.
  53. self.assertFalse(evaluator.matches(condition, "@user:test", "ba[rz]"))
  54. # A display name with spaces should work fine.
  55. self.assertTrue(evaluator.matches(condition, "@user:test", "foo bar"))
  56. def test_no_body(self):
  57. """Not having a body shouldn't break the evaluator."""
  58. evaluator = self._get_evaluator({})
  59. condition = {
  60. "kind": "contains_display_name",
  61. }
  62. self.assertFalse(evaluator.matches(condition, "@user:test", "foo"))
  63. def test_invalid_body(self):
  64. """A non-string body should not break the evaluator."""
  65. condition = {
  66. "kind": "contains_display_name",
  67. }
  68. for body in (1, True, {"foo": "bar"}):
  69. evaluator = self._get_evaluator({"body": body})
  70. self.assertFalse(evaluator.matches(condition, "@user:test", "foo"))
  71. def test_tweaks_for_actions(self):
  72. """
  73. This tests the behaviour of tweaks_for_actions.
  74. """
  75. actions = [
  76. {"set_tweak": "sound", "value": "default"},
  77. {"set_tweak": "highlight"},
  78. "notify",
  79. ]
  80. self.assertEqual(
  81. push_rule_evaluator.tweaks_for_actions(actions),
  82. {"sound": "default", "highlight": True},
  83. )