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.

96 lines
3.1 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, status="down"):
  11. self.check = Check(project=self.project)
  12. self.check.status = status
  13. self.check.last_ping = now() - td(minutes=61)
  14. self.check.save()
  15. self.channel = Channel(project=self.project, kind="whatsapp")
  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. definition = {"value": "+1234567890", "up": True, "down": True}
  22. self._setup_data(json.dumps(definition))
  23. self.check.last_ping = now() - td(hours=2)
  24. mock_post.return_value.status_code = 200
  25. self.channel.notify(self.check)
  26. args, kwargs = mock_post.call_args
  27. payload = kwargs["data"]
  28. self.assertEqual(payload["To"], "whatsapp:+1234567890")
  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_obeys_up_down_flags(self, mock_post):
  37. definition = {"value": "+1234567890", "up": True, "down": False}
  38. self._setup_data(json.dumps(definition))
  39. self.check.last_ping = now() - td(hours=2)
  40. self.channel.notify(self.check)
  41. self.assertEqual(Notification.objects.count(), 0)
  42. self.assertFalse(mock_post.called)
  43. @patch("hc.api.transports.requests.request")
  44. def test_it_enforces_limit(self, mock_post):
  45. # At limit already:
  46. self.profile.last_sms_date = now()
  47. self.profile.sms_sent = 50
  48. self.profile.save()
  49. definition = {"value": "+1234567890", "up": True, "down": True}
  50. self._setup_data(json.dumps(definition))
  51. self.channel.notify(self.check)
  52. self.assertFalse(mock_post.called)
  53. n = Notification.objects.get()
  54. self.assertTrue("Monthly message limit exceeded" in n.error)
  55. # And email should have been sent
  56. self.assertEqual(len(mail.outbox), 1)
  57. email = mail.outbox[0]
  58. self.assertEqual(email.to[0], "[email protected]")
  59. self.assertEqual(email.subject, "Monthly WhatsApp Limit Reached")
  60. @patch("hc.api.transports.requests.request")
  61. def test_it_does_not_escape_special_characters(self, mock_post):
  62. definition = {"value": "+1234567890", "up": True, "down": True}
  63. self._setup_data(json.dumps(definition))
  64. self.check.name = "Foo > Bar & Co"
  65. mock_post.return_value.status_code = 200
  66. self.channel.notify(self.check)
  67. args, kwargs = mock_post.call_args
  68. payload = kwargs["data"]
  69. self.assertIn("Foo > Bar & Co", payload["Body"])