test_push_rule_attrs.py 14 KB

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