You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

820 lines
28 KiB

6 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
6 years ago
6 years ago
6 years ago
9 years ago
9 years ago
9 years ago
9 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. import json
  4. from unittest.mock import patch, Mock
  5. from django.core import mail
  6. from django.utils.timezone import now
  7. from hc.api.models import Channel, Check, Notification, TokenBucket
  8. from hc.test import BaseTestCase
  9. from requests.exceptions import ConnectionError, Timeout
  10. from django.test.utils import override_settings
  11. class NotifyTestCase(BaseTestCase):
  12. def _setup_data(self, kind, value, status="down", email_verified=True):
  13. self.check = Check(project=self.project)
  14. self.check.status = status
  15. self.check.last_ping = now() - td(minutes=61)
  16. self.check.save()
  17. self.channel = Channel(project=self.project)
  18. self.channel.kind = kind
  19. self.channel.value = value
  20. self.channel.email_verified = email_verified
  21. self.channel.save()
  22. self.channel.checks.add(self.check)
  23. @patch("hc.api.transports.requests.request")
  24. def test_webhook(self, mock_get):
  25. definition = {
  26. "method_down": "GET",
  27. "url_down": "http://example",
  28. "body_down": "",
  29. "headers_down": {},
  30. }
  31. self._setup_data("webhook", json.dumps(definition))
  32. mock_get.return_value.status_code = 200
  33. self.channel.notify(self.check)
  34. mock_get.assert_called_with(
  35. "get",
  36. "http://example",
  37. headers={"User-Agent": "healthchecks.io"},
  38. timeout=5,
  39. )
  40. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  41. def test_webhooks_handle_timeouts(self, mock_get):
  42. definition = {
  43. "method_down": "GET",
  44. "url_down": "http://example",
  45. "body_down": "",
  46. "headers_down": {},
  47. }
  48. self._setup_data("webhook", json.dumps(definition))
  49. self.channel.notify(self.check)
  50. # The transport should have retried 3 times
  51. self.assertEqual(mock_get.call_count, 3)
  52. n = Notification.objects.get()
  53. self.assertEqual(n.error, "Connection timed out")
  54. self.channel.refresh_from_db()
  55. self.assertEqual(self.channel.last_error, "Connection timed out")
  56. @patch("hc.api.transports.requests.request", side_effect=ConnectionError)
  57. def test_webhooks_handle_connection_errors(self, mock_get):
  58. definition = {
  59. "method_down": "GET",
  60. "url_down": "http://example",
  61. "body_down": "",
  62. "headers_down": {},
  63. }
  64. self._setup_data("webhook", json.dumps(definition))
  65. self.channel.notify(self.check)
  66. # The transport should have retried 3 times
  67. self.assertEqual(mock_get.call_count, 3)
  68. n = Notification.objects.get()
  69. self.assertEqual(n.error, "Connection failed")
  70. @patch("hc.api.transports.requests.request")
  71. def test_webhooks_handle_500(self, mock_get):
  72. definition = {
  73. "method_down": "GET",
  74. "url_down": "http://example",
  75. "body_down": "",
  76. "headers_down": {},
  77. }
  78. self._setup_data("webhook", json.dumps(definition))
  79. mock_get.return_value.status_code = 500
  80. self.channel.notify(self.check)
  81. # The transport should have retried 3 times
  82. self.assertEqual(mock_get.call_count, 3)
  83. n = Notification.objects.get()
  84. self.assertEqual(n.error, "Received status code 500")
  85. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  86. def test_webhooks_dont_retry_when_sending_test_notifications(self, mock_get):
  87. definition = {
  88. "method_down": "GET",
  89. "url_down": "http://example",
  90. "body_down": "",
  91. "headers_down": {},
  92. }
  93. self._setup_data("webhook", json.dumps(definition))
  94. self.channel.notify(self.check, is_test=True)
  95. # is_test flag is set, the transport should not retry:
  96. self.assertEqual(mock_get.call_count, 1)
  97. n = Notification.objects.get()
  98. self.assertEqual(n.error, "Connection timed out")
  99. @patch("hc.api.transports.requests.request")
  100. def test_webhooks_support_variables(self, mock_get):
  101. definition = {
  102. "method_down": "GET",
  103. "url_down": "http://host/$CODE/$STATUS/$TAG1/$TAG2/?name=$NAME",
  104. "body_down": "",
  105. "headers_down": {},
  106. }
  107. self._setup_data("webhook", json.dumps(definition))
  108. self.check.name = "Hello World"
  109. self.check.tags = "foo bar"
  110. self.check.save()
  111. self.channel.notify(self.check)
  112. url = "http://host/%s/down/foo/bar/?name=Hello%%20World" % self.check.code
  113. args, kwargs = mock_get.call_args
  114. self.assertEqual(args[0], "get")
  115. self.assertEqual(args[1], url)
  116. self.assertEqual(kwargs["headers"], {"User-Agent": "healthchecks.io"})
  117. self.assertEqual(kwargs["timeout"], 5)
  118. @patch("hc.api.transports.requests.request")
  119. def test_webhooks_handle_variable_variables(self, mock_get):
  120. definition = {
  121. "method_down": "GET",
  122. "url_down": "http://host/$$NAMETAG1",
  123. "body_down": "",
  124. "headers_down": {},
  125. }
  126. self._setup_data("webhook", json.dumps(definition))
  127. self.check.tags = "foo bar"
  128. self.check.save()
  129. self.channel.notify(self.check)
  130. # $$NAMETAG1 should *not* get transformed to "foo"
  131. args, kwargs = mock_get.call_args
  132. self.assertEqual(args[1], "http://host/$TAG1")
  133. @patch("hc.api.transports.requests.request")
  134. def test_webhooks_support_post(self, mock_request):
  135. definition = {
  136. "method_down": "POST",
  137. "url_down": "http://example.com",
  138. "body_down": "The Time Is $NOW",
  139. "headers_down": {},
  140. }
  141. self._setup_data("webhook", json.dumps(definition))
  142. self.check.save()
  143. self.channel.notify(self.check)
  144. args, kwargs = mock_request.call_args
  145. self.assertEqual(args[0], "post")
  146. self.assertEqual(args[1], "http://example.com")
  147. # spaces should not have been urlencoded:
  148. payload = kwargs["data"].decode()
  149. self.assertTrue(payload.startswith("The Time Is 2"))
  150. @patch("hc.api.transports.requests.request")
  151. def test_webhooks_dollarsign_escaping(self, mock_get):
  152. # If name or tag contains what looks like a variable reference,
  153. # that should be left alone:
  154. definition = {
  155. "method_down": "GET",
  156. "url_down": "http://host/$NAME",
  157. "body_down": "",
  158. "headers_down": {},
  159. }
  160. self._setup_data("webhook", json.dumps(definition))
  161. self.check.name = "$TAG1"
  162. self.check.tags = "foo"
  163. self.check.save()
  164. self.channel.notify(self.check)
  165. url = "http://host/%24TAG1"
  166. mock_get.assert_called_with(
  167. "get", url, headers={"User-Agent": "healthchecks.io"}, timeout=5
  168. )
  169. @patch("hc.api.transports.requests.request")
  170. def test_webhooks_handle_up_events(self, mock_get):
  171. definition = {
  172. "method_up": "GET",
  173. "url_up": "http://bar",
  174. "body_up": "",
  175. "headers_up": {},
  176. }
  177. self._setup_data("webhook", json.dumps(definition), status="up")
  178. self.channel.notify(self.check)
  179. mock_get.assert_called_with(
  180. "get", "http://bar", headers={"User-Agent": "healthchecks.io"}, timeout=5
  181. )
  182. @patch("hc.api.transports.requests.request")
  183. def test_webhooks_handle_noop_up_events(self, mock_get):
  184. definition = {
  185. "method_up": "GET",
  186. "url_up": "",
  187. "body_up": "",
  188. "headers_up": {},
  189. }
  190. self._setup_data("webhook", json.dumps(definition), status="up")
  191. self.channel.notify(self.check)
  192. self.assertFalse(mock_get.called)
  193. self.assertEqual(Notification.objects.count(), 0)
  194. @patch("hc.api.transports.requests.request")
  195. def test_webhooks_handle_unicode_post_body(self, mock_request):
  196. definition = {
  197. "method_down": "POST",
  198. "url_down": "http://foo.com",
  199. "body_down": "(╯°□°)╯︵ ┻━┻",
  200. "headers_down": {},
  201. }
  202. self._setup_data("webhook", json.dumps(definition))
  203. self.check.save()
  204. self.channel.notify(self.check)
  205. args, kwargs = mock_request.call_args
  206. # unicode should be encoded into utf-8
  207. self.assertIsInstance(kwargs["data"], bytes)
  208. @patch("hc.api.transports.requests.request")
  209. def test_webhooks_handle_post_headers(self, mock_request):
  210. definition = {
  211. "method_down": "POST",
  212. "url_down": "http://foo.com",
  213. "body_down": "data",
  214. "headers_down": {"Content-Type": "application/json"},
  215. }
  216. self._setup_data("webhook", json.dumps(definition))
  217. self.channel.notify(self.check)
  218. headers = {"User-Agent": "healthchecks.io", "Content-Type": "application/json"}
  219. mock_request.assert_called_with(
  220. "post", "http://foo.com", data=b"data", headers=headers, timeout=5
  221. )
  222. @patch("hc.api.transports.requests.request")
  223. def test_webhooks_handle_get_headers(self, mock_request):
  224. definition = {
  225. "method_down": "GET",
  226. "url_down": "http://foo.com",
  227. "body_down": "",
  228. "headers_down": {"Content-Type": "application/json"},
  229. }
  230. self._setup_data("webhook", json.dumps(definition))
  231. self.channel.notify(self.check)
  232. headers = {"User-Agent": "healthchecks.io", "Content-Type": "application/json"}
  233. mock_request.assert_called_with(
  234. "get", "http://foo.com", headers=headers, timeout=5
  235. )
  236. @patch("hc.api.transports.requests.request")
  237. def test_webhooks_allow_user_agent_override(self, mock_request):
  238. definition = {
  239. "method_down": "GET",
  240. "url_down": "http://foo.com",
  241. "body_down": "",
  242. "headers_down": {"User-Agent": "My-Agent"},
  243. }
  244. self._setup_data("webhook", json.dumps(definition))
  245. self.channel.notify(self.check)
  246. headers = {"User-Agent": "My-Agent"}
  247. mock_request.assert_called_with(
  248. "get", "http://foo.com", headers=headers, timeout=5
  249. )
  250. @patch("hc.api.transports.requests.request")
  251. def test_webhooks_support_variables_in_headers(self, mock_request):
  252. definition = {
  253. "method_down": "GET",
  254. "url_down": "http://foo.com",
  255. "body_down": "",
  256. "headers_down": {"X-Message": "$NAME is DOWN"},
  257. }
  258. self._setup_data("webhook", json.dumps(definition))
  259. self.check.name = "Foo"
  260. self.check.save()
  261. self.channel.notify(self.check)
  262. headers = {"User-Agent": "healthchecks.io", "X-Message": "Foo is DOWN"}
  263. mock_request.assert_called_with(
  264. "get", "http://foo.com", headers=headers, timeout=5
  265. )
  266. @patch("hc.api.transports.requests.request")
  267. def test_pd(self, mock_post):
  268. self._setup_data("pd", "123")
  269. mock_post.return_value.status_code = 200
  270. self.channel.notify(self.check)
  271. assert Notification.objects.count() == 1
  272. args, kwargs = mock_post.call_args
  273. payload = kwargs["json"]
  274. self.assertEqual(payload["event_type"], "trigger")
  275. self.assertEqual(payload["service_key"], "123")
  276. @patch("hc.api.transports.requests.request")
  277. def test_pd_complex(self, mock_post):
  278. self._setup_data("pd", json.dumps({"service_key": "456"}))
  279. mock_post.return_value.status_code = 200
  280. self.channel.notify(self.check)
  281. assert Notification.objects.count() == 1
  282. args, kwargs = mock_post.call_args
  283. payload = kwargs["json"]
  284. self.assertEqual(payload["event_type"], "trigger")
  285. self.assertEqual(payload["service_key"], "456")
  286. @patch("hc.api.transports.requests.request")
  287. def test_pagertree(self, mock_post):
  288. self._setup_data("pagertree", "123")
  289. mock_post.return_value.status_code = 200
  290. self.channel.notify(self.check)
  291. assert Notification.objects.count() == 1
  292. args, kwargs = mock_post.call_args
  293. payload = kwargs["json"]
  294. self.assertEqual(payload["event_type"], "trigger")
  295. @patch("hc.api.transports.requests.request")
  296. def test_pagerteam(self, mock_post):
  297. self._setup_data("pagerteam", "123")
  298. self.channel.notify(self.check)
  299. self.assertFalse(mock_post.called)
  300. self.assertEqual(Notification.objects.count(), 0)
  301. @patch("hc.api.transports.requests.request")
  302. def test_slack(self, mock_post):
  303. self._setup_data("slack", "123")
  304. mock_post.return_value.status_code = 200
  305. self.channel.notify(self.check)
  306. assert Notification.objects.count() == 1
  307. args, kwargs = mock_post.call_args
  308. payload = kwargs["json"]
  309. attachment = payload["attachments"][0]
  310. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  311. self.assertEqual(fields["Last Ping"], "an hour ago")
  312. @patch("hc.api.transports.requests.request")
  313. def test_slack_with_complex_value(self, mock_post):
  314. v = json.dumps({"incoming_webhook": {"url": "123"}})
  315. self._setup_data("slack", v)
  316. mock_post.return_value.status_code = 200
  317. self.channel.notify(self.check)
  318. assert Notification.objects.count() == 1
  319. args, kwargs = mock_post.call_args
  320. self.assertEqual(args[1], "123")
  321. @patch("hc.api.transports.requests.request")
  322. def test_slack_handles_500(self, mock_post):
  323. self._setup_data("slack", "123")
  324. mock_post.return_value.status_code = 500
  325. self.channel.notify(self.check)
  326. n = Notification.objects.get()
  327. self.assertEqual(n.error, "Received status code 500")
  328. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  329. def test_slack_handles_timeout(self, mock_post):
  330. self._setup_data("slack", "123")
  331. self.channel.notify(self.check)
  332. n = Notification.objects.get()
  333. self.assertEqual(n.error, "Connection timed out")
  334. @patch("hc.api.transports.requests.request")
  335. def test_slack_with_tabs_in_schedule(self, mock_post):
  336. self._setup_data("slack", "123")
  337. self.check.kind = "cron"
  338. self.check.schedule = "*\t* * * *"
  339. self.check.save()
  340. mock_post.return_value.status_code = 200
  341. self.channel.notify(self.check)
  342. self.assertEqual(Notification.objects.count(), 1)
  343. self.assertTrue(mock_post.called)
  344. @patch("hc.api.transports.requests.request")
  345. def test_hipchat(self, mock_post):
  346. self._setup_data("hipchat", "123")
  347. self.channel.notify(self.check)
  348. self.assertFalse(mock_post.called)
  349. self.assertEqual(Notification.objects.count(), 0)
  350. @patch("hc.api.transports.requests.request")
  351. def test_opsgenie_with_legacy_value(self, mock_post):
  352. self._setup_data("opsgenie", "123")
  353. mock_post.return_value.status_code = 202
  354. self.channel.notify(self.check)
  355. n = Notification.objects.first()
  356. self.assertEqual(n.error, "")
  357. self.assertEqual(mock_post.call_count, 1)
  358. args, kwargs = mock_post.call_args
  359. self.assertIn("api.opsgenie.com", args[1])
  360. payload = kwargs["json"]
  361. self.assertIn("DOWN", payload["message"])
  362. @patch("hc.api.transports.requests.request")
  363. def test_opsgenie_up(self, mock_post):
  364. self._setup_data("opsgenie", "123", status="up")
  365. mock_post.return_value.status_code = 202
  366. self.channel.notify(self.check)
  367. n = Notification.objects.first()
  368. self.assertEqual(n.error, "")
  369. self.assertEqual(mock_post.call_count, 1)
  370. args, kwargs = mock_post.call_args
  371. method, url = args
  372. self.assertTrue(str(self.check.code) in url)
  373. @patch("hc.api.transports.requests.request")
  374. def test_opsgenie_with_json_value(self, mock_post):
  375. self._setup_data("opsgenie", json.dumps({"key": "456", "region": "eu"}))
  376. mock_post.return_value.status_code = 202
  377. self.channel.notify(self.check)
  378. n = Notification.objects.first()
  379. self.assertEqual(n.error, "")
  380. self.assertEqual(mock_post.call_count, 1)
  381. args, kwargs = mock_post.call_args
  382. self.assertIn("api.eu.opsgenie.com", args[1])
  383. @patch("hc.api.transports.requests.request")
  384. def test_opsgenie_returns_error(self, mock_post):
  385. self._setup_data("opsgenie", "123")
  386. mock_post.return_value.status_code = 403
  387. mock_post.return_value.json.return_value = {"message": "Nice try"}
  388. self.channel.notify(self.check)
  389. n = Notification.objects.first()
  390. self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"')
  391. @patch("hc.api.transports.requests.request")
  392. def test_pushover(self, mock_post):
  393. self._setup_data("po", "123|0")
  394. mock_post.return_value.status_code = 200
  395. self.channel.notify(self.check)
  396. assert Notification.objects.count() == 1
  397. args, kwargs = mock_post.call_args
  398. payload = kwargs["data"]
  399. self.assertIn("DOWN", payload["title"])
  400. @patch("hc.api.transports.requests.request")
  401. def test_pushover_up_priority(self, mock_post):
  402. self._setup_data("po", "123|0|2", status="up")
  403. mock_post.return_value.status_code = 200
  404. self.channel.notify(self.check)
  405. assert Notification.objects.count() == 1
  406. args, kwargs = mock_post.call_args
  407. payload = kwargs["data"]
  408. self.assertIn("UP", payload["title"])
  409. self.assertEqual(payload["priority"], 2)
  410. self.assertIn("retry", payload)
  411. self.assertIn("expire", payload)
  412. @patch("hc.api.transports.requests.request")
  413. def test_victorops(self, mock_post):
  414. self._setup_data("victorops", "123")
  415. mock_post.return_value.status_code = 200
  416. self.channel.notify(self.check)
  417. assert Notification.objects.count() == 1
  418. args, kwargs = mock_post.call_args
  419. payload = kwargs["json"]
  420. self.assertEqual(payload["message_type"], "CRITICAL")
  421. @patch("hc.api.transports.requests.request")
  422. def test_discord(self, mock_post):
  423. v = json.dumps({"webhook": {"url": "123"}})
  424. self._setup_data("discord", v)
  425. mock_post.return_value.status_code = 200
  426. self.channel.notify(self.check)
  427. assert Notification.objects.count() == 1
  428. args, kwargs = mock_post.call_args
  429. payload = kwargs["json"]
  430. attachment = payload["attachments"][0]
  431. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  432. self.assertEqual(fields["Last Ping"], "an hour ago")
  433. @patch("hc.api.transports.requests.request")
  434. def test_discord_rewrites_discordapp_com(self, mock_post):
  435. v = json.dumps({"webhook": {"url": "https://discordapp.com/foo"}})
  436. self._setup_data("discord", v)
  437. mock_post.return_value.status_code = 200
  438. self.channel.notify(self.check)
  439. assert Notification.objects.count() == 1
  440. args, kwargs = mock_post.call_args
  441. url = args[1]
  442. # discordapp.com is deprecated. For existing webhook URLs, wwe should
  443. # rewrite discordapp.com to discord.com:
  444. self.assertEqual(url, "https://discord.com/foo/slack")
  445. @patch("hc.api.transports.requests.request")
  446. def test_pushbullet(self, mock_post):
  447. self._setup_data("pushbullet", "fake-token")
  448. mock_post.return_value.status_code = 200
  449. self.channel.notify(self.check)
  450. assert Notification.objects.count() == 1
  451. _, kwargs = mock_post.call_args
  452. self.assertEqual(kwargs["json"]["type"], "note")
  453. self.assertEqual(kwargs["headers"]["Access-Token"], "fake-token")
  454. @patch("hc.api.transports.requests.request")
  455. def test_telegram(self, mock_post):
  456. v = json.dumps({"id": 123})
  457. self._setup_data("telegram", v)
  458. mock_post.return_value.status_code = 200
  459. self.channel.notify(self.check)
  460. assert Notification.objects.count() == 1
  461. args, kwargs = mock_post.call_args
  462. payload = kwargs["json"]
  463. self.assertEqual(payload["chat_id"], 123)
  464. self.assertTrue("The check" in payload["text"])
  465. @patch("hc.api.transports.requests.request")
  466. def test_telegram_returns_error(self, mock_post):
  467. self._setup_data("telegram", json.dumps({"id": 123}))
  468. mock_post.return_value.status_code = 400
  469. mock_post.return_value.json.return_value = {"description": "Hi"}
  470. self.channel.notify(self.check)
  471. n = Notification.objects.first()
  472. self.assertEqual(n.error, 'Received status code 400 with a message: "Hi"')
  473. def test_telegram_obeys_rate_limit(self):
  474. self._setup_data("telegram", json.dumps({"id": 123}))
  475. TokenBucket.objects.create(value="tg-123", tokens=0)
  476. self.channel.notify(self.check)
  477. n = Notification.objects.first()
  478. self.assertEqual(n.error, "Rate limit exceeded")
  479. @patch("hc.api.transports.requests.request")
  480. def test_call(self, mock_post):
  481. self.profile.call_limit = 1
  482. self.profile.save()
  483. value = {"label": "foo", "value": "+1234567890"}
  484. self._setup_data("call", json.dumps(value))
  485. self.check.last_ping = now() - td(hours=2)
  486. mock_post.return_value.status_code = 200
  487. self.channel.notify(self.check)
  488. args, kwargs = mock_post.call_args
  489. payload = kwargs["data"]
  490. self.assertEqual(payload["To"], "+1234567890")
  491. n = Notification.objects.get()
  492. callback_path = f"/api/v1/notifications/{n.code}/status"
  493. self.assertTrue(payload["StatusCallback"].endswith(callback_path))
  494. @patch("hc.api.transports.requests.request")
  495. def test_call_limit(self, mock_post):
  496. # At limit already:
  497. self.profile.last_call_date = now()
  498. self.profile.calls_sent = 50
  499. self.profile.save()
  500. definition = {"value": "+1234567890"}
  501. self._setup_data("call", json.dumps(definition))
  502. self.channel.notify(self.check)
  503. self.assertFalse(mock_post.called)
  504. n = Notification.objects.get()
  505. self.assertTrue("Monthly phone call limit exceeded" in n.error)
  506. # And email should have been sent
  507. self.assertEqual(len(mail.outbox), 1)
  508. email = mail.outbox[0]
  509. self.assertEqual(email.to[0], "[email protected]")
  510. self.assertEqual(email.subject, "Monthly Phone Call Limit Reached")
  511. @patch("hc.api.transports.requests.request")
  512. def test_call_limit_reset(self, mock_post):
  513. # At limit, but also into a new month
  514. self.profile.calls_sent = 50
  515. self.profile.last_call_date = now() - td(days=100)
  516. self.profile.save()
  517. self._setup_data("sms", "+1234567890")
  518. mock_post.return_value.status_code = 200
  519. self.channel.notify(self.check)
  520. self.assertTrue(mock_post.called)
  521. @patch("apprise.Apprise")
  522. @override_settings(APPRISE_ENABLED=True)
  523. def test_apprise_enabled(self, mock_apprise):
  524. self._setup_data("apprise", "123")
  525. mock_aobj = Mock()
  526. mock_aobj.add.return_value = True
  527. mock_aobj.notify.return_value = True
  528. mock_apprise.return_value = mock_aobj
  529. self.channel.notify(self.check)
  530. self.assertEqual(Notification.objects.count(), 1)
  531. self.check.status = "up"
  532. self.assertEqual(Notification.objects.count(), 1)
  533. @patch("apprise.Apprise")
  534. @override_settings(APPRISE_ENABLED=False)
  535. def test_apprise_disabled(self, mock_apprise):
  536. self._setup_data("apprise", "123")
  537. mock_aobj = Mock()
  538. mock_aobj.add.return_value = True
  539. mock_aobj.notify.return_value = True
  540. mock_apprise.return_value = mock_aobj
  541. self.channel.notify(self.check)
  542. self.assertEqual(Notification.objects.count(), 1)
  543. def test_not_implimented(self):
  544. self._setup_data("webhook", "http://example")
  545. self.channel.kind = "invalid"
  546. with self.assertRaises(NotImplementedError):
  547. self.channel.notify(self.check)
  548. @patch("hc.api.transports.requests.request")
  549. def test_msteams(self, mock_post):
  550. self._setup_data("msteams", "http://example.com/webhook")
  551. mock_post.return_value.status_code = 200
  552. self.check.name = "_underscores_ & more"
  553. self.channel.notify(self.check)
  554. assert Notification.objects.count() == 1
  555. args, kwargs = mock_post.call_args
  556. payload = kwargs["json"]
  557. self.assertEqual(payload["@type"], "MessageCard")
  558. # summary and title should be the same, except
  559. # title should have any special HTML characters escaped
  560. self.assertEqual(payload["summary"], "“_underscores_ & more” is DOWN.")
  561. self.assertEqual(payload["title"], "“_underscores_ & more” is DOWN.")
  562. @patch("hc.api.transports.requests.request")
  563. def test_msteams_escapes_html_and_markdown_in_desc(self, mock_post):
  564. self._setup_data("msteams", "http://example.com/webhook")
  565. mock_post.return_value.status_code = 200
  566. self.check.desc = """
  567. TEST _underscore_ `backticks` <u>underline</u> \\backslash\\ "quoted"
  568. """
  569. self.channel.notify(self.check)
  570. args, kwargs = mock_post.call_args
  571. text = kwargs["json"]["sections"][0]["text"]
  572. self.assertIn(r"\_underscore\_", text)
  573. self.assertIn(r"\`backticks\`", text)
  574. self.assertIn("&lt;u&gt;underline&lt;/u&gt;", text)
  575. self.assertIn(r"\\backslash\\ ", text)
  576. self.assertIn("&quot;quoted&quot;", text)
  577. @patch("hc.api.transports.os.system")
  578. @override_settings(SHELL_ENABLED=True)
  579. def test_shell(self, mock_system):
  580. definition = {"cmd_down": "logger hello", "cmd_up": ""}
  581. self._setup_data("shell", json.dumps(definition))
  582. mock_system.return_value = 0
  583. self.channel.notify(self.check)
  584. mock_system.assert_called_with("logger hello")
  585. @patch("hc.api.transports.os.system")
  586. @override_settings(SHELL_ENABLED=True)
  587. def test_shell_handles_nonzero_exit_code(self, mock_system):
  588. definition = {"cmd_down": "logger hello", "cmd_up": ""}
  589. self._setup_data("shell", json.dumps(definition))
  590. mock_system.return_value = 123
  591. self.channel.notify(self.check)
  592. n = Notification.objects.get()
  593. self.assertEqual(n.error, "Command returned exit code 123")
  594. @patch("hc.api.transports.os.system")
  595. @override_settings(SHELL_ENABLED=True)
  596. def test_shell_supports_variables(self, mock_system):
  597. definition = {"cmd_down": "logger $NAME is $STATUS ($TAG1)", "cmd_up": ""}
  598. self._setup_data("shell", json.dumps(definition))
  599. mock_system.return_value = 0
  600. self.check.name = "Database"
  601. self.check.tags = "foo bar"
  602. self.check.save()
  603. self.channel.notify(self.check)
  604. mock_system.assert_called_with("logger Database is down (foo)")
  605. @patch("hc.api.transports.os.system")
  606. @override_settings(SHELL_ENABLED=False)
  607. def test_shell_disabled(self, mock_system):
  608. definition = {"cmd_down": "logger hello", "cmd_up": ""}
  609. self._setup_data("shell", json.dumps(definition))
  610. self.channel.notify(self.check)
  611. self.assertFalse(mock_system.called)
  612. n = Notification.objects.get()
  613. self.assertEqual(n.error, "Shell commands are not enabled")
  614. @patch("hc.api.transports.requests.request")
  615. def test_zulip(self, mock_post):
  616. definition = {
  617. "bot_email": "[email protected]",
  618. "api_key": "fake-key",
  619. "mtype": "stream",
  620. "to": "general",
  621. }
  622. self._setup_data("zulip", json.dumps(definition))
  623. mock_post.return_value.status_code = 200
  624. self.channel.notify(self.check)
  625. assert Notification.objects.count() == 1
  626. args, kwargs = mock_post.call_args
  627. payload = kwargs["data"]
  628. self.assertIn("DOWN", payload["topic"])
  629. @patch("hc.api.transports.requests.request")
  630. def test_zulip_returns_error(self, mock_post):
  631. definition = {
  632. "bot_email": "[email protected]",
  633. "api_key": "fake-key",
  634. "mtype": "stream",
  635. "to": "general",
  636. }
  637. self._setup_data("zulip", json.dumps(definition))
  638. mock_post.return_value.status_code = 403
  639. mock_post.return_value.json.return_value = {"msg": "Nice try"}
  640. self.channel.notify(self.check)
  641. n = Notification.objects.first()
  642. self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"')