test_push_rule_evaluator.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 typing import Any, Dict
  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 _assert_matches(
  58. self, condition: Dict[str, Any], content: Dict[str, Any], msg=None
  59. ) -> None:
  60. evaluator = self._get_evaluator(content)
  61. self.assertTrue(evaluator.matches(condition, "@user:test", "display_name"), msg)
  62. def _assert_not_matches(
  63. self, condition: Dict[str, Any], content: Dict[str, Any], msg=None
  64. ) -> None:
  65. evaluator = self._get_evaluator(content)
  66. self.assertFalse(
  67. evaluator.matches(condition, "@user:test", "display_name"), msg
  68. )
  69. def test_event_match_body(self):
  70. """Check that event_match conditions on content.body work as expected"""
  71. # if the key is `content.body`, the pattern matches substrings.
  72. # non-wildcards should match
  73. condition = {
  74. "kind": "event_match",
  75. "key": "content.body",
  76. "pattern": "foobaz",
  77. }
  78. self._assert_matches(
  79. condition,
  80. {"body": "aaa FoobaZ zzz"},
  81. "patterns should match and be case-insensitive",
  82. )
  83. self._assert_not_matches(
  84. condition,
  85. {"body": "aa xFoobaZ yy"},
  86. "pattern should only match at word boundaries",
  87. )
  88. self._assert_not_matches(
  89. condition,
  90. {"body": "aa foobazx yy"},
  91. "pattern should only match at word boundaries",
  92. )
  93. # wildcards should match
  94. condition = {
  95. "kind": "event_match",
  96. "key": "content.body",
  97. "pattern": "f?o*baz",
  98. }
  99. self._assert_matches(
  100. condition,
  101. {"body": "aaa FoobarbaZ zzz"},
  102. "* should match string and pattern should be case-insensitive",
  103. )
  104. self._assert_matches(
  105. condition, {"body": "aa foobaz yy"}, "* should match 0 characters"
  106. )
  107. self._assert_not_matches(
  108. condition, {"body": "aa fobbaz yy"}, "? should not match 0 characters"
  109. )
  110. self._assert_not_matches(
  111. condition, {"body": "aa fiiobaz yy"}, "? should not match 2 characters"
  112. )
  113. self._assert_not_matches(
  114. condition,
  115. {"body": "aa xfooxbaz yy"},
  116. "pattern should only match at word boundaries",
  117. )
  118. self._assert_not_matches(
  119. condition,
  120. {"body": "aa fooxbazx yy"},
  121. "pattern should only match at word boundaries",
  122. )
  123. # test backslashes
  124. condition = {
  125. "kind": "event_match",
  126. "key": "content.body",
  127. "pattern": r"f\oobaz",
  128. }
  129. self._assert_matches(
  130. condition,
  131. {"body": r"F\oobaz"},
  132. "backslash should match itself",
  133. )
  134. condition = {
  135. "kind": "event_match",
  136. "key": "content.body",
  137. "pattern": r"f\?obaz",
  138. }
  139. self._assert_matches(
  140. condition,
  141. {"body": r"F\oobaz"},
  142. r"? after \ should match any character",
  143. )
  144. def test_event_match_non_body(self):
  145. """Check that event_match conditions on other keys work as expected"""
  146. # if the key is anything other than 'content.body', the pattern must match the
  147. # whole value.
  148. # non-wildcards should match
  149. condition = {
  150. "kind": "event_match",
  151. "key": "content.value",
  152. "pattern": "foobaz",
  153. }
  154. self._assert_matches(
  155. condition,
  156. {"value": "FoobaZ"},
  157. "patterns should match and be case-insensitive",
  158. )
  159. self._assert_not_matches(
  160. condition,
  161. {"value": "xFoobaZ"},
  162. "pattern should only match at the start/end of the value",
  163. )
  164. self._assert_not_matches(
  165. condition,
  166. {"value": "FoobaZz"},
  167. "pattern should only match at the start/end of the value",
  168. )
  169. # wildcards should match
  170. condition = {
  171. "kind": "event_match",
  172. "key": "content.value",
  173. "pattern": "f?o*baz",
  174. }
  175. self._assert_matches(
  176. condition,
  177. {"value": "FoobarbaZ"},
  178. "* should match string and pattern should be case-insensitive",
  179. )
  180. self._assert_matches(
  181. condition, {"value": "foobaz"}, "* should match 0 characters"
  182. )
  183. self._assert_not_matches(
  184. condition, {"value": "fobbaz"}, "? should not match 0 characters"
  185. )
  186. self._assert_not_matches(
  187. condition, {"value": "fiiobaz"}, "? should not match 2 characters"
  188. )
  189. self._assert_not_matches(
  190. condition,
  191. {"value": "xfooxbaz"},
  192. "pattern should only match at the start/end of the value",
  193. )
  194. self._assert_not_matches(
  195. condition,
  196. {"value": "fooxbazx"},
  197. "pattern should only match at the start/end of the value",
  198. )
  199. self._assert_not_matches(
  200. condition,
  201. {"value": "x\nfooxbaz"},
  202. "pattern should not match after a newline",
  203. )
  204. self._assert_not_matches(
  205. condition,
  206. {"value": "fooxbaz\nx"},
  207. "pattern should not match before a newline",
  208. )
  209. def test_no_body(self):
  210. """Not having a body shouldn't break the evaluator."""
  211. evaluator = self._get_evaluator({})
  212. condition = {
  213. "kind": "contains_display_name",
  214. }
  215. self.assertFalse(evaluator.matches(condition, "@user:test", "foo"))
  216. def test_invalid_body(self):
  217. """A non-string body should not break the evaluator."""
  218. condition = {
  219. "kind": "contains_display_name",
  220. }
  221. for body in (1, True, {"foo": "bar"}):
  222. evaluator = self._get_evaluator({"body": body})
  223. self.assertFalse(evaluator.matches(condition, "@user:test", "foo"))
  224. def test_tweaks_for_actions(self):
  225. """
  226. This tests the behaviour of tweaks_for_actions.
  227. """
  228. actions = [
  229. {"set_tweak": "sound", "value": "default"},
  230. {"set_tweak": "highlight"},
  231. "notify",
  232. ]
  233. self.assertEqual(
  234. push_rule_evaluator.tweaks_for_actions(actions),
  235. {"sound": "default", "highlight": True},
  236. )