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.

98 lines
3.7 KiB

  1. from hc.api.models import Channel
  2. from hc.test import BaseTestCase
  3. class AddWebhookTestCase(BaseTestCase):
  4. url = "/integrations/add_webhook/"
  5. def test_instructions_work(self):
  6. self.client.login(username="[email protected]", password="password")
  7. r = self.client.get(self.url)
  8. self.assertContains(r, "Runs a HTTP GET or HTTP POST")
  9. def test_it_adds_two_webhook_urls_and_redirects(self):
  10. form = {"url_down": "http://foo.com", "url_up": "https://bar.com"}
  11. self.client.login(username="[email protected]", password="password")
  12. r = self.client.post(self.url, form)
  13. self.assertRedirects(r, "/integrations/")
  14. c = Channel.objects.get()
  15. self.assertEqual(c.value, '{"headers": {}, "post_data": "", "url_down": "http://foo.com", "url_up": "https://bar.com"}')
  16. def test_it_adds_webhook_using_team_access(self):
  17. form = {"url_down": "http://foo.com", "url_up": "https://bar.com"}
  18. # Logging in as bob, not alice. Bob has team access so this
  19. # should work.
  20. self.client.login(username="[email protected]", password="password")
  21. self.client.post(self.url, form)
  22. c = Channel.objects.get()
  23. self.assertEqual(c.user, self.alice)
  24. self.assertEqual(c.value, '{"headers": {}, "post_data": "", "url_down": "http://foo.com", "url_up": "https://bar.com"}')
  25. def test_it_rejects_bad_urls(self):
  26. urls = [
  27. # clearly not an URL
  28. "foo",
  29. # FTP addresses not allowed
  30. "ftp://example.org",
  31. # no loopback
  32. "http://localhost:1234/endpoint",
  33. "http://127.0.0.1/endpoint"
  34. ]
  35. self.client.login(username="[email protected]", password="password")
  36. for url in urls:
  37. form = {"url_down": url, "url_up": ""}
  38. r = self.client.post(self.url, form)
  39. self.assertContains(r, "Enter a valid URL.", msg_prefix=url)
  40. self.assertEqual(Channel.objects.count(), 0)
  41. def test_it_handles_empty_down_url(self):
  42. form = {"url_down": "", "url_up": "http://foo.com"}
  43. self.client.login(username="[email protected]", password="password")
  44. self.client.post(self.url, form)
  45. c = Channel.objects.get()
  46. self.assertEqual(c.value, '{"headers": {}, "post_data": "", "url_down": "", "url_up": "http://foo.com"}')
  47. def test_it_adds_post_data(self):
  48. form = {"url_down": "http://foo.com", "post_data": "hello"}
  49. self.client.login(username="[email protected]", password="password")
  50. r = self.client.post(self.url, form)
  51. self.assertRedirects(r, "/integrations/")
  52. c = Channel.objects.get()
  53. self.assertEqual(c.value, '{"headers": {}, "post_data": "hello", "url_down": "http://foo.com", "url_up": ""}')
  54. def test_it_adds_headers(self):
  55. form = {
  56. "url_down": "http://foo.com",
  57. "header_key[]": ["test", "test2"],
  58. "header_value[]": ["123", "abc"]
  59. }
  60. self.client.login(username="[email protected]", password="password")
  61. r = self.client.post(self.url, form)
  62. self.assertRedirects(r, "/integrations/")
  63. c = Channel.objects.get()
  64. self.assertEqual(c.headers, {"test": "123", "test2": "abc"})
  65. def test_it_rejects_bad_header_names(self):
  66. self.client.login(username="[email protected]", password="password")
  67. form = {
  68. "url_down": "http://example.org",
  69. "header_key[]": ["ill:egal"],
  70. "header_value[]": ["123"]
  71. }
  72. r = self.client.post(self.url, form)
  73. self.assertContains(r, "Please use valid HTTP header names.")
  74. self.assertEqual(Channel.objects.count(), 0)