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.

71 lines
2.5 KiB

  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. from unittest.mock import patch
  4. from django.utils.timezone import now
  5. from hc.api.models import Channel, Check, Notification
  6. from hc.test import BaseTestCase
  7. from django.test.utils import override_settings
  8. class NotifyTestCase(BaseTestCase):
  9. def _setup_data(self, value, status="down", email_verified=True):
  10. self.check = Check(project=self.project)
  11. self.check.status = status
  12. self.check.last_ping = now() - td(minutes=61)
  13. self.check.save()
  14. self.channel = Channel(project=self.project)
  15. self.channel.kind = "msteams"
  16. self.channel.value = value
  17. self.channel.email_verified = email_verified
  18. self.channel.save()
  19. self.channel.checks.add(self.check)
  20. @patch("hc.api.transports.requests.request")
  21. def test_msteams(self, mock_post):
  22. self._setup_data("http://example.com/webhook")
  23. mock_post.return_value.status_code = 200
  24. self.check.name = "_underscores_ & more"
  25. self.channel.notify(self.check)
  26. assert Notification.objects.count() == 1
  27. args, kwargs = mock_post.call_args
  28. payload = kwargs["json"]
  29. self.assertEqual(payload["@type"], "MessageCard")
  30. # summary and title should be the same, except
  31. # title should have any special HTML characters escaped
  32. self.assertEqual(payload["summary"], "“_underscores_ & more” is DOWN.")
  33. self.assertEqual(payload["title"], "“_underscores_ & more” is DOWN.")
  34. @patch("hc.api.transports.requests.request")
  35. def test_msteams_escapes_html_and_markdown_in_desc(self, mock_post):
  36. self._setup_data("http://example.com/webhook")
  37. mock_post.return_value.status_code = 200
  38. self.check.desc = """
  39. TEST _underscore_ `backticks` <u>underline</u> \\backslash\\ "quoted"
  40. """
  41. self.channel.notify(self.check)
  42. args, kwargs = mock_post.call_args
  43. text = kwargs["json"]["sections"][0]["text"]
  44. self.assertIn(r"\_underscore\_", text)
  45. self.assertIn(r"\`backticks\`", text)
  46. self.assertIn("&lt;u&gt;underline&lt;/u&gt;", text)
  47. self.assertIn(r"\\backslash\\ ", text)
  48. self.assertIn("&quot;quoted&quot;", text)
  49. @override_settings(MSTEAMS_ENABLED=False)
  50. def test_it_requires_msteams_enabled(self):
  51. self._setup_data("http://example.com/webhook")
  52. self.channel.notify(self.check)
  53. n = Notification.objects.get()
  54. self.assertEqual(n.error, "MS Teams notifications are not enabled.")