test_push_rule_evaluator.py 3.6 KB

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