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.

146 lines
4.8 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, "Executes an HTTP request")
  9. def test_it_adds_two_webhook_urls_and_redirects(self):
  10. form = {
  11. "method_down": "GET",
  12. "url_down": "http://foo.com",
  13. "method_up": "GET",
  14. "url_up": "https://bar.com",
  15. }
  16. self.client.login(username="[email protected]", password="password")
  17. r = self.client.post(self.url, form)
  18. self.assertRedirects(r, "/integrations/")
  19. c = Channel.objects.get()
  20. self.assertEqual(c.project, self.project)
  21. self.assertEqual(c.down_webhook_spec["url"], "http://foo.com")
  22. self.assertEqual(c.up_webhook_spec["url"], "https://bar.com")
  23. def test_it_adds_webhook_using_team_access(self):
  24. form = {
  25. "method_down": "GET",
  26. "url_down": "http://foo.com",
  27. "method_up": "GET",
  28. "url_up": "https://bar.com",
  29. }
  30. # Logging in as bob, not alice. Bob has team access so this
  31. # should work.
  32. self.client.login(username="[email protected]", password="password")
  33. self.client.post(self.url, form)
  34. c = Channel.objects.get()
  35. self.assertEqual(c.project, self.project)
  36. self.assertEqual(c.down_webhook_spec["url"], "http://foo.com")
  37. self.assertEqual(c.up_webhook_spec["url"], "https://bar.com")
  38. def test_it_rejects_bad_urls(self):
  39. urls = [
  40. # clearly not an URL
  41. "foo",
  42. # FTP addresses not allowed
  43. "ftp://example.org",
  44. # no loopback
  45. "http://localhost:1234/endpoint",
  46. "http://127.0.0.1/endpoint",
  47. ]
  48. self.client.login(username="[email protected]", password="password")
  49. for url in urls:
  50. form = {
  51. "method_down": "GET",
  52. "url_down": url,
  53. "method_up": "GET",
  54. "url_up": "",
  55. }
  56. r = self.client.post(self.url, form)
  57. self.assertContains(r, "Enter a valid URL.", msg_prefix=url)
  58. self.assertEqual(Channel.objects.count(), 0)
  59. def test_it_handles_empty_down_url(self):
  60. form = {
  61. "method_down": "GET",
  62. "url_down": "",
  63. "method_up": "GET",
  64. "url_up": "http://foo.com",
  65. }
  66. self.client.login(username="[email protected]", password="password")
  67. self.client.post(self.url, form)
  68. c = Channel.objects.get()
  69. self.assertEqual(c.down_webhook_spec["url"], "")
  70. self.assertEqual(c.up_webhook_spec["url"], "http://foo.com")
  71. def test_it_adds_request_body(self):
  72. form = {
  73. "method_down": "POST",
  74. "url_down": "http://foo.com",
  75. "body_down": "hello",
  76. "method_up": "GET",
  77. }
  78. self.client.login(username="[email protected]", password="password")
  79. r = self.client.post(self.url, form)
  80. self.assertRedirects(r, "/integrations/")
  81. c = Channel.objects.get()
  82. self.assertEqual(c.down_webhook_spec["body"], "hello")
  83. def test_it_adds_headers(self):
  84. form = {
  85. "method_down": "GET",
  86. "url_down": "http://foo.com",
  87. "headers_down": "test:123\ntest2:abc",
  88. "method_up": "GET",
  89. }
  90. self.client.login(username="[email protected]", password="password")
  91. r = self.client.post(self.url, form)
  92. self.assertRedirects(r, "/integrations/")
  93. c = Channel.objects.get()
  94. self.assertEqual(
  95. c.down_webhook_spec["headers"], {"test": "123", "test2": "abc"}
  96. )
  97. def test_it_rejects_bad_headers(self):
  98. self.client.login(username="[email protected]", password="password")
  99. form = {
  100. "method_down": "GET",
  101. "url_down": "http://example.org",
  102. "headers_down": "invalid-headers",
  103. "method_up": "GET",
  104. }
  105. r = self.client.post(self.url, form)
  106. self.assertContains(r, """invalid-headers""")
  107. self.assertEqual(Channel.objects.count(), 0)
  108. def test_it_strips_headers(self):
  109. form = {
  110. "method_down": "GET",
  111. "url_down": "http://foo.com",
  112. "headers_down": " test : 123 ",
  113. "method_up": "GET",
  114. }
  115. self.client.login(username="[email protected]", password="password")
  116. r = self.client.post(self.url, form)
  117. self.assertRedirects(r, "/integrations/")
  118. c = Channel.objects.get()
  119. self.assertEqual(c.down_webhook_spec["headers"], {"test": "123"})