test_push_rule_attrs.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. import synapse
  15. from synapse.api.errors import Codes
  16. from synapse.rest.client import login, push_rule, room
  17. from tests.unittest import HomeserverTestCase
  18. class PushRuleAttributesTestCase(HomeserverTestCase):
  19. servlets = [
  20. synapse.rest.admin.register_servlets_for_client_rest_resource,
  21. room.register_servlets,
  22. login.register_servlets,
  23. push_rule.register_servlets,
  24. ]
  25. hijack_auth = False
  26. def test_enabled_on_creation(self) -> None:
  27. """
  28. Tests the GET and PUT of push rules' `enabled` endpoints.
  29. Tests that a rule is enabled upon creation, even though a rule with that
  30. ruleId existed previously and was disabled.
  31. """
  32. self.register_user("user", "pass")
  33. token = self.login("user", "pass")
  34. body = {
  35. "conditions": [
  36. {"kind": "event_match", "key": "sender", "pattern": "@user2:hs"}
  37. ],
  38. "actions": ["notify", {"set_tweak": "highlight"}],
  39. }
  40. # PUT a new rule
  41. channel = self.make_request(
  42. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  43. )
  44. self.assertEqual(channel.code, 200)
  45. # GET enabled for that new rule
  46. channel = self.make_request(
  47. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  48. )
  49. self.assertEqual(channel.code, 200)
  50. self.assertEqual(channel.json_body["enabled"], True)
  51. def test_enabled_on_recreation(self) -> None:
  52. """
  53. Tests the GET and PUT of push rules' `enabled` endpoints.
  54. Tests that a rule is enabled upon creation, even if a rule with that
  55. ruleId existed previously and was disabled.
  56. """
  57. self.register_user("user", "pass")
  58. token = self.login("user", "pass")
  59. body = {
  60. "conditions": [
  61. {"kind": "event_match", "key": "sender", "pattern": "@user2:hs"}
  62. ],
  63. "actions": ["notify", {"set_tweak": "highlight"}],
  64. }
  65. # PUT a new rule
  66. channel = self.make_request(
  67. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  68. )
  69. self.assertEqual(channel.code, 200)
  70. # disable the rule
  71. channel = self.make_request(
  72. "PUT",
  73. "/pushrules/global/override/best.friend/enabled",
  74. {"enabled": False},
  75. access_token=token,
  76. )
  77. self.assertEqual(channel.code, 200)
  78. # check rule disabled
  79. channel = self.make_request(
  80. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  81. )
  82. self.assertEqual(channel.code, 200)
  83. self.assertEqual(channel.json_body["enabled"], False)
  84. # DELETE the rule
  85. channel = self.make_request(
  86. "DELETE", "/pushrules/global/override/best.friend", access_token=token
  87. )
  88. self.assertEqual(channel.code, 200)
  89. # PUT a new rule
  90. channel = self.make_request(
  91. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  92. )
  93. self.assertEqual(channel.code, 200)
  94. # GET enabled for that new rule
  95. channel = self.make_request(
  96. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  97. )
  98. self.assertEqual(channel.code, 200)
  99. self.assertEqual(channel.json_body["enabled"], True)
  100. def test_enabled_disable(self) -> None:
  101. """
  102. Tests the GET and PUT of push rules' `enabled` endpoints.
  103. Tests that a rule is disabled and enabled when we ask for it.
  104. """
  105. self.register_user("user", "pass")
  106. token = self.login("user", "pass")
  107. body = {
  108. "conditions": [
  109. {"kind": "event_match", "key": "sender", "pattern": "@user2:hs"}
  110. ],
  111. "actions": ["notify", {"set_tweak": "highlight"}],
  112. }
  113. # PUT a new rule
  114. channel = self.make_request(
  115. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  116. )
  117. self.assertEqual(channel.code, 200)
  118. # disable the rule
  119. channel = self.make_request(
  120. "PUT",
  121. "/pushrules/global/override/best.friend/enabled",
  122. {"enabled": False},
  123. access_token=token,
  124. )
  125. self.assertEqual(channel.code, 200)
  126. # check rule disabled
  127. channel = self.make_request(
  128. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  129. )
  130. self.assertEqual(channel.code, 200)
  131. self.assertEqual(channel.json_body["enabled"], False)
  132. # re-enable the rule
  133. channel = self.make_request(
  134. "PUT",
  135. "/pushrules/global/override/best.friend/enabled",
  136. {"enabled": True},
  137. access_token=token,
  138. )
  139. self.assertEqual(channel.code, 200)
  140. # check rule enabled
  141. channel = self.make_request(
  142. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  143. )
  144. self.assertEqual(channel.code, 200)
  145. self.assertEqual(channel.json_body["enabled"], True)
  146. def test_enabled_404_when_get_non_existent(self) -> None:
  147. """
  148. Tests that `enabled` gives 404 when the rule doesn't exist.
  149. """
  150. self.register_user("user", "pass")
  151. token = self.login("user", "pass")
  152. body = {
  153. "conditions": [
  154. {"kind": "event_match", "key": "sender", "pattern": "@user2:hs"}
  155. ],
  156. "actions": ["notify", {"set_tweak": "highlight"}],
  157. }
  158. # check 404 for never-heard-of rule
  159. channel = self.make_request(
  160. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  161. )
  162. self.assertEqual(channel.code, 404)
  163. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  164. # PUT a new rule
  165. channel = self.make_request(
  166. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  167. )
  168. self.assertEqual(channel.code, 200)
  169. # GET enabled for that new rule
  170. channel = self.make_request(
  171. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  172. )
  173. self.assertEqual(channel.code, 200)
  174. # DELETE the rule
  175. channel = self.make_request(
  176. "DELETE", "/pushrules/global/override/best.friend", access_token=token
  177. )
  178. self.assertEqual(channel.code, 200)
  179. # check 404 for deleted rule
  180. channel = self.make_request(
  181. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  182. )
  183. self.assertEqual(channel.code, 404)
  184. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  185. def test_enabled_404_when_get_non_existent_server_rule(self) -> None:
  186. """
  187. Tests that `enabled` gives 404 when the server-default rule doesn't exist.
  188. """
  189. self.register_user("user", "pass")
  190. token = self.login("user", "pass")
  191. # check 404 for never-heard-of rule
  192. channel = self.make_request(
  193. "GET", "/pushrules/global/override/.m.muahahaha/enabled", access_token=token
  194. )
  195. self.assertEqual(channel.code, 404)
  196. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  197. def test_enabled_404_when_put_non_existent_rule(self) -> None:
  198. """
  199. Tests that `enabled` gives 404 when we put to a rule that doesn't exist.
  200. """
  201. self.register_user("user", "pass")
  202. token = self.login("user", "pass")
  203. # enable & check 404 for never-heard-of rule
  204. channel = self.make_request(
  205. "PUT",
  206. "/pushrules/global/override/best.friend/enabled",
  207. {"enabled": True},
  208. access_token=token,
  209. )
  210. self.assertEqual(channel.code, 404)
  211. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  212. def test_enabled_404_when_put_non_existent_server_rule(self) -> None:
  213. """
  214. Tests that `enabled` gives 404 when we put to a server-default rule that doesn't exist.
  215. """
  216. self.register_user("user", "pass")
  217. token = self.login("user", "pass")
  218. # enable & check 404 for never-heard-of rule
  219. channel = self.make_request(
  220. "PUT",
  221. "/pushrules/global/override/.m.muahahah/enabled",
  222. {"enabled": True},
  223. access_token=token,
  224. )
  225. self.assertEqual(channel.code, 404)
  226. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  227. def test_actions_get(self) -> None:
  228. """
  229. Tests that `actions` gives you what you expect on a fresh rule.
  230. """
  231. self.register_user("user", "pass")
  232. token = self.login("user", "pass")
  233. body = {
  234. "conditions": [
  235. {"kind": "event_match", "key": "sender", "pattern": "@user2:hs"}
  236. ],
  237. "actions": ["notify", {"set_tweak": "highlight"}],
  238. }
  239. # PUT a new rule
  240. channel = self.make_request(
  241. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  242. )
  243. self.assertEqual(channel.code, 200)
  244. # GET actions for that new rule
  245. channel = self.make_request(
  246. "GET", "/pushrules/global/override/best.friend/actions", access_token=token
  247. )
  248. self.assertEqual(channel.code, 200)
  249. self.assertEqual(
  250. channel.json_body["actions"], ["notify", {"set_tweak": "highlight"}]
  251. )
  252. def test_actions_put(self) -> None:
  253. """
  254. Tests that PUT on actions updates the value you'd get from GET.
  255. """
  256. self.register_user("user", "pass")
  257. token = self.login("user", "pass")
  258. body = {
  259. "conditions": [
  260. {"kind": "event_match", "key": "sender", "pattern": "@user2:hs"}
  261. ],
  262. "actions": ["notify", {"set_tweak": "highlight"}],
  263. }
  264. # PUT a new rule
  265. channel = self.make_request(
  266. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  267. )
  268. self.assertEqual(channel.code, 200)
  269. # change the rule actions
  270. channel = self.make_request(
  271. "PUT",
  272. "/pushrules/global/override/best.friend/actions",
  273. {"actions": ["dont_notify"]},
  274. access_token=token,
  275. )
  276. self.assertEqual(channel.code, 200)
  277. # GET actions for that new rule
  278. channel = self.make_request(
  279. "GET", "/pushrules/global/override/best.friend/actions", access_token=token
  280. )
  281. self.assertEqual(channel.code, 200)
  282. self.assertEqual(channel.json_body["actions"], ["dont_notify"])
  283. def test_actions_404_when_get_non_existent(self) -> None:
  284. """
  285. Tests that `actions` gives 404 when the rule doesn't exist.
  286. """
  287. self.register_user("user", "pass")
  288. token = self.login("user", "pass")
  289. body = {
  290. "conditions": [
  291. {"kind": "event_match", "key": "sender", "pattern": "@user2:hs"}
  292. ],
  293. "actions": ["notify", {"set_tweak": "highlight"}],
  294. }
  295. # check 404 for never-heard-of rule
  296. channel = self.make_request(
  297. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  298. )
  299. self.assertEqual(channel.code, 404)
  300. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  301. # PUT a new rule
  302. channel = self.make_request(
  303. "PUT", "/pushrules/global/override/best.friend", body, access_token=token
  304. )
  305. self.assertEqual(channel.code, 200)
  306. # DELETE the rule
  307. channel = self.make_request(
  308. "DELETE", "/pushrules/global/override/best.friend", access_token=token
  309. )
  310. self.assertEqual(channel.code, 200)
  311. # check 404 for deleted rule
  312. channel = self.make_request(
  313. "GET", "/pushrules/global/override/best.friend/enabled", access_token=token
  314. )
  315. self.assertEqual(channel.code, 404)
  316. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  317. def test_actions_404_when_get_non_existent_server_rule(self) -> None:
  318. """
  319. Tests that `actions` gives 404 when the server-default rule doesn't exist.
  320. """
  321. self.register_user("user", "pass")
  322. token = self.login("user", "pass")
  323. # check 404 for never-heard-of rule
  324. channel = self.make_request(
  325. "GET", "/pushrules/global/override/.m.muahahaha/actions", access_token=token
  326. )
  327. self.assertEqual(channel.code, 404)
  328. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  329. def test_actions_404_when_put_non_existent_rule(self) -> None:
  330. """
  331. Tests that `actions` gives 404 when putting to a rule that doesn't exist.
  332. """
  333. self.register_user("user", "pass")
  334. token = self.login("user", "pass")
  335. # enable & check 404 for never-heard-of rule
  336. channel = self.make_request(
  337. "PUT",
  338. "/pushrules/global/override/best.friend/actions",
  339. {"actions": ["dont_notify"]},
  340. access_token=token,
  341. )
  342. self.assertEqual(channel.code, 404)
  343. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
  344. def test_actions_404_when_put_non_existent_server_rule(self) -> None:
  345. """
  346. Tests that `actions` gives 404 when putting to a server-default rule that doesn't exist.
  347. """
  348. self.register_user("user", "pass")
  349. token = self.login("user", "pass")
  350. # enable & check 404 for never-heard-of rule
  351. channel = self.make_request(
  352. "PUT",
  353. "/pushrules/global/override/.m.muahahah/actions",
  354. {"actions": ["dont_notify"]},
  355. access_token=token,
  356. )
  357. self.assertEqual(channel.code, 404)
  358. self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)