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.

109 lines
3.5 KiB

  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. import json
  4. from unittest.mock import patch
  5. from django.core import mail
  6. from django.utils.timezone import now
  7. from hc.api.models import Channel, Check, Notification
  8. from hc.test import BaseTestCase
  9. class NotifyTestCase(BaseTestCase):
  10. def _setup_data(self, value):
  11. self.check = Check(project=self.project)
  12. self.check.status = "down"
  13. self.check.last_ping = now() - td(minutes=61)
  14. self.check.save()
  15. self.channel = Channel(project=self.project, kind="sms")
  16. self.channel.value = value
  17. self.channel.save()
  18. self.channel.checks.add(self.check)
  19. @patch("hc.api.transports.requests.request")
  20. def test_it_works(self, mock_post):
  21. self._setup_data("+1234567890")
  22. self.check.last_ping = now() - td(hours=2)
  23. mock_post.return_value.status_code = 200
  24. self.channel.notify(self.check)
  25. args, kwargs = mock_post.call_args
  26. payload = kwargs["data"]
  27. self.assertEqual(payload["To"], "+1234567890")
  28. self.assertFalse("\xa0" in payload["Body"])
  29. n = Notification.objects.get()
  30. callback_path = f"/api/v1/notifications/{n.code}/status"
  31. self.assertTrue(payload["StatusCallback"].endswith(callback_path))
  32. # sent SMS counter should go up
  33. self.profile.refresh_from_db()
  34. self.assertEqual(self.profile.sms_sent, 1)
  35. @patch("hc.api.transports.requests.request")
  36. def test_it_handles_json_value(self, mock_post):
  37. value = {"label": "foo", "value": "+1234567890"}
  38. self._setup_data(json.dumps(value))
  39. self.check.last_ping = now() - td(hours=2)
  40. mock_post.return_value.status_code = 200
  41. self.channel.notify(self.check)
  42. assert Notification.objects.count() == 1
  43. args, kwargs = mock_post.call_args
  44. payload = kwargs["data"]
  45. self.assertEqual(payload["To"], "+1234567890")
  46. @patch("hc.api.transports.requests.request")
  47. def test_it_enforces_limit(self, mock_post):
  48. # At limit already:
  49. self.profile.last_sms_date = now()
  50. self.profile.sms_sent = 50
  51. self.profile.save()
  52. self._setup_data("+1234567890")
  53. self.channel.notify(self.check)
  54. self.assertFalse(mock_post.called)
  55. n = Notification.objects.get()
  56. self.assertTrue("Monthly SMS limit exceeded" in n.error)
  57. # And email should have been sent
  58. self.assertEqual(len(mail.outbox), 1)
  59. email = mail.outbox[0]
  60. self.assertEqual(email.to[0], "[email protected]")
  61. self.assertEqual(email.subject, "Monthly SMS Limit Reached")
  62. @patch("hc.api.transports.requests.request")
  63. def test_it_resets_limit_next_month(self, mock_post):
  64. # At limit, but also into a new month
  65. self.profile.sms_sent = 50
  66. self.profile.last_sms_date = now() - td(days=100)
  67. self.profile.save()
  68. self._setup_data("+1234567890")
  69. mock_post.return_value.status_code = 200
  70. self.channel.notify(self.check)
  71. self.assertTrue(mock_post.called)
  72. @patch("hc.api.transports.requests.request")
  73. def test_it_does_not_escape_special_characters(self, mock_post):
  74. self._setup_data("+1234567890")
  75. self.check.name = "Foo > Bar & Co"
  76. self.check.last_ping = now() - td(hours=2)
  77. mock_post.return_value.status_code = 200
  78. self.channel.notify(self.check)
  79. args, kwargs = mock_post.call_args
  80. payload = kwargs["data"]
  81. self.assertIn("Foo > Bar & Co", payload["Body"])