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.

110 lines
3.8 KiB

  1. import json
  2. from unittest.mock import patch
  3. from django.core import mail
  4. from hc.api.models import Channel, Notification
  5. from hc.test import BaseTestCase
  6. class SendTestNotificationTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.channel = Channel(kind="email", project=self.project)
  10. self.channel.email_verified = True
  11. self.channel.value = "[email protected]"
  12. self.channel.save()
  13. self.url = "/integrations/%s/test/" % self.channel.code
  14. def test_it_sends_test_email(self):
  15. self.client.login(username="[email protected]", password="password")
  16. r = self.client.post(self.url, {}, follow=True)
  17. self.assertRedirects(r, self.channels_url)
  18. self.assertContains(r, "Test notification sent!")
  19. # And email should have been sent
  20. self.assertEqual(len(mail.outbox), 1)
  21. email = mail.outbox[0]
  22. self.assertEqual(email.to[0], "[email protected]")
  23. self.assertTrue("X-Status-Url" in email.extra_headers)
  24. self.assertTrue("List-Unsubscribe" in email.extra_headers)
  25. # It should create a notification
  26. n = Notification.objects.get()
  27. self.assertEqual(n.channel, self.channel)
  28. self.assertEqual(n.error, "")
  29. def test_it_clears_channel_last_error(self):
  30. self.channel.last_error = "Something went wrong"
  31. self.channel.save()
  32. self.client.login(username="[email protected]", password="password")
  33. self.client.post(self.url, {})
  34. self.channel.refresh_from_db()
  35. self.assertEqual(self.channel.last_error, "")
  36. def test_it_sets_channel_last_error(self):
  37. self.channel.email_verified = False
  38. self.channel.save()
  39. self.client.login(username="[email protected]", password="password")
  40. r = self.client.post(self.url, {}, follow=True)
  41. self.assertContains(r, "Could not send a test notification")
  42. self.assertContains(r, "Email not verified")
  43. self.channel.refresh_from_db()
  44. self.assertEqual(self.channel.last_error, "Email not verified")
  45. @patch("hc.api.transports.requests.request")
  46. def test_it_handles_webhooks_with_no_down_url(self, mock_get):
  47. mock_get.return_value.status_code = 200
  48. self.channel.kind = "webhook"
  49. self.channel.value = json.dumps(
  50. {
  51. "method_down": "GET",
  52. "url_down": "",
  53. "body_down": "",
  54. "headers_down": {},
  55. "method_up": "GET",
  56. "url_up": "http://example-url",
  57. "body_up": "",
  58. "headers_up": {},
  59. }
  60. )
  61. self.channel.save()
  62. self.client.login(username="[email protected]", password="password")
  63. r = self.client.post(self.url, {}, follow=True)
  64. self.assertRedirects(r, self.channels_url)
  65. self.assertContains(r, "Test notification sent!")
  66. def test_it_handles_webhooks_with_no_urls(self):
  67. self.channel.kind = "webhook"
  68. self.channel.value = json.dumps(
  69. {
  70. "method_down": "GET",
  71. "url_down": "",
  72. "body_down": "",
  73. "headers_down": {},
  74. "method_up": "GET",
  75. "url_up": "",
  76. "body_up": "",
  77. "headers_up": {},
  78. }
  79. )
  80. self.channel.save()
  81. self.client.login(username="[email protected]", password="password")
  82. r = self.client.post(self.url, {}, follow=True)
  83. self.assertRedirects(r, self.channels_url)
  84. self.assertContains(r, "Could not send a test notification")
  85. def test_it_checks_channel_ownership(self):
  86. self.client.login(username="[email protected]", password="password")
  87. r = self.client.post(self.url, {}, follow=True)
  88. self.assertEqual(r.status_code, 404)