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.

114 lines
3.9 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 update self.channel.last_notify
  26. self.channel.refresh_from_db()
  27. self.assertIsNotNone(self.channel.last_notify)
  28. # It should create a notification
  29. n = Notification.objects.get()
  30. self.assertEqual(n.channel, self.channel)
  31. self.assertEqual(n.error, "")
  32. def test_it_clears_channel_last_error(self):
  33. self.channel.last_error = "Something went wrong"
  34. self.channel.save()
  35. self.client.login(username="[email protected]", password="password")
  36. self.client.post(self.url, {})
  37. self.channel.refresh_from_db()
  38. self.assertEqual(self.channel.last_error, "")
  39. def test_it_sets_channel_last_error(self):
  40. self.channel.email_verified = False
  41. self.channel.save()
  42. self.client.login(username="[email protected]", password="password")
  43. r = self.client.post(self.url, {}, follow=True)
  44. self.assertContains(r, "Could not send a test notification")
  45. self.assertContains(r, "Email not verified")
  46. self.channel.refresh_from_db()
  47. self.assertEqual(self.channel.last_error, "Email not verified")
  48. @patch("hc.api.transports.requests.request")
  49. def test_it_handles_webhooks_with_no_down_url(self, mock_get):
  50. mock_get.return_value.status_code = 200
  51. self.channel.kind = "webhook"
  52. self.channel.value = json.dumps(
  53. {
  54. "method_down": "GET",
  55. "url_down": "",
  56. "body_down": "",
  57. "headers_down": {},
  58. "method_up": "GET",
  59. "url_up": "http://example-url",
  60. "body_up": "",
  61. "headers_up": {},
  62. }
  63. )
  64. self.channel.save()
  65. self.client.login(username="[email protected]", password="password")
  66. r = self.client.post(self.url, {}, follow=True)
  67. self.assertRedirects(r, self.channels_url)
  68. self.assertContains(r, "Test notification sent!")
  69. def test_it_handles_webhooks_with_no_urls(self):
  70. self.channel.kind = "webhook"
  71. self.channel.value = json.dumps(
  72. {
  73. "method_down": "GET",
  74. "url_down": "",
  75. "body_down": "",
  76. "headers_down": {},
  77. "method_up": "GET",
  78. "url_up": "",
  79. "body_up": "",
  80. "headers_up": {},
  81. }
  82. )
  83. self.channel.save()
  84. self.client.login(username="[email protected]", password="password")
  85. r = self.client.post(self.url, {}, follow=True)
  86. self.assertRedirects(r, self.channels_url)
  87. self.assertContains(r, "Could not send a test notification")
  88. def test_it_checks_channel_ownership(self):
  89. self.client.login(username="[email protected]", password="password")
  90. r = self.client.post(self.url, {}, follow=True)
  91. self.assertEqual(r.status_code, 404)