test_push_rule_evaluator.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.push_rule_evaluator import PushRuleEvaluatorForEvent
  18. from tests import unittest
  19. class PushRuleEvaluatorTestCase(unittest.TestCase):
  20. def setUp(self):
  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": {"body": "foo bar baz"},
  29. },
  30. RoomVersions.V1,
  31. )
  32. room_member_count = 0
  33. sender_power_level = 0
  34. power_levels = {}
  35. self.evaluator = 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. condition = {
  41. "kind": "contains_display_name",
  42. }
  43. # Blank names are skipped.
  44. self.assertFalse(self.evaluator.matches(condition, "@user:test", ""))
  45. # Check a display name that doesn't match.
  46. self.assertFalse(self.evaluator.matches(condition, "@user:test", "not found"))
  47. # Check a display name which matches.
  48. self.assertTrue(self.evaluator.matches(condition, "@user:test", "foo"))
  49. # A display name that matches, but not a full word does not result in a match.
  50. self.assertFalse(self.evaluator.matches(condition, "@user:test", "ba"))
  51. # A display name should not be interpreted as a regular expression.
  52. self.assertFalse(self.evaluator.matches(condition, "@user:test", "ba[rz]"))
  53. # A display name with spaces should work fine.
  54. self.assertTrue(self.evaluator.matches(condition, "@user:test", "foo bar"))