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.

82 lines
2.8 KiB

  1. import json
  2. from django.core import mail
  3. from hc.api.models import Channel
  4. from hc.test import BaseTestCase
  5. from mock import patch
  6. class SendTestNotificationTestCase(BaseTestCase):
  7. def setUp(self):
  8. super(SendTestNotificationTestCase, self).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-Bounce-Url" in email.extra_headers)
  24. self.assertTrue("List-Unsubscribe" in email.extra_headers)
  25. @patch("hc.api.transports.requests.request")
  26. def test_it_handles_webhooks_with_no_down_url(self, mock_get):
  27. mock_get.return_value.status_code = 200
  28. self.channel.kind = "webhook"
  29. self.channel.value = json.dumps(
  30. {
  31. "method_down": "GET",
  32. "url_down": "",
  33. "body_down": "",
  34. "headers_down": {},
  35. "method_up": "GET",
  36. "url_up": "http://example-url",
  37. "body_up": "",
  38. "headers_up": {},
  39. }
  40. )
  41. self.channel.save()
  42. self.client.login(username="[email protected]", password="password")
  43. r = self.client.post(self.url, {}, follow=True)
  44. self.assertRedirects(r, self.channels_url)
  45. self.assertContains(r, "Test notification sent!")
  46. def test_it_handles_webhooks_with_no_urls(self):
  47. self.channel.kind = "webhook"
  48. self.channel.value = json.dumps(
  49. {
  50. "method_down": "GET",
  51. "url_down": "",
  52. "body_down": "",
  53. "headers_down": {},
  54. "method_up": "GET",
  55. "url_up": "",
  56. "body_up": "",
  57. "headers_up": {},
  58. }
  59. )
  60. self.channel.save()
  61. self.client.login(username="[email protected]", password="password")
  62. r = self.client.post(self.url, {}, follow=True)
  63. self.assertRedirects(r, self.channels_url)
  64. self.assertContains(r, "Could not send a test notification")
  65. def test_it_checks_channel_ownership(self):
  66. self.client.login(username="[email protected]", password="password")
  67. r = self.client.post(self.url, {}, follow=True)
  68. self.assertEqual(r.status_code, 404)