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.

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