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.

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