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.

163 lines
5.3 KiB

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