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.

99 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. self.assertEqual(c.project, self.project)
  17. def test_it_adds_webhook_using_team_access(self):
  18. form = {"url_down": "http://foo.com", "url_up": "https://bar.com"}
  19. # Logging in as bob, not alice. Bob has team access so this
  20. # should work.
  21. self.client.login(username="[email protected]", password="password")
  22. self.client.post(self.url, form)
  23. c = Channel.objects.get()
  24. self.assertEqual(c.project, self.project)
  25. self.assertEqual(c.value, '{"headers": {}, "post_data": "", "url_down": "http://foo.com", "url_up": "https://bar.com"}')
  26. def test_it_rejects_bad_urls(self):
  27. urls = [
  28. # clearly not an URL
  29. "foo",
  30. # FTP addresses not allowed
  31. "ftp://example.org",
  32. # no loopback
  33. "http://localhost:1234/endpoint",
  34. "http://127.0.0.1/endpoint"
  35. ]
  36. self.client.login(username="[email protected]", password="password")
  37. for url in urls:
  38. form = {"url_down": url, "url_up": ""}
  39. r = self.client.post(self.url, form)
  40. self.assertContains(r, "Enter a valid URL.", msg_prefix=url)
  41. self.assertEqual(Channel.objects.count(), 0)
  42. def test_it_handles_empty_down_url(self):
  43. form = {"url_down": "", "url_up": "http://foo.com"}
  44. self.client.login(username="[email protected]", password="password")
  45. self.client.post(self.url, form)
  46. c = Channel.objects.get()
  47. self.assertEqual(c.value, '{"headers": {}, "post_data": "", "url_down": "", "url_up": "http://foo.com"}')
  48. def test_it_adds_post_data(self):
  49. form = {"url_down": "http://foo.com", "post_data": "hello"}
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.post(self.url, form)
  52. self.assertRedirects(r, "/integrations/")
  53. c = Channel.objects.get()
  54. self.assertEqual(c.value, '{"headers": {}, "post_data": "hello", "url_down": "http://foo.com", "url_up": ""}')
  55. def test_it_adds_headers(self):
  56. form = {
  57. "url_down": "http://foo.com",
  58. "header_key[]": ["test", "test2"],
  59. "header_value[]": ["123", "abc"]
  60. }
  61. self.client.login(username="[email protected]", password="password")
  62. r = self.client.post(self.url, form)
  63. self.assertRedirects(r, "/integrations/")
  64. c = Channel.objects.get()
  65. self.assertEqual(c.headers, {"test": "123", "test2": "abc"})
  66. def test_it_rejects_bad_header_names(self):
  67. self.client.login(username="[email protected]", password="password")
  68. form = {
  69. "url_down": "http://example.org",
  70. "header_key[]": ["ill:egal"],
  71. "header_value[]": ["123"]
  72. }
  73. r = self.client.post(self.url, form)
  74. self.assertContains(r, "Please use valid HTTP header names.")
  75. self.assertEqual(Channel.objects.count(), 0)