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.

84 lines
2.9 KiB

  1. import json
  2. from hc.api.models import Channel
  3. from hc.test import BaseTestCase
  4. class EditWebhookTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(EditWebhookTestCase, self).setUp()
  7. definition = {
  8. "method_down": "GET",
  9. "url_down": "http://example.org/down",
  10. "body_down": "$NAME is down",
  11. "headers_down": {"User-Agent": "My-Custom-UA"},
  12. "method_up": "GET",
  13. "url_up": "http://example.org/up",
  14. "body_up": "$NAME is up",
  15. "headers_up": {},
  16. }
  17. self.channel = Channel(project=self.project, kind="webhook")
  18. self.channel.name = "Call example.org"
  19. self.channel.value = json.dumps(definition)
  20. self.channel.save()
  21. self.url = "/integrations/%s/edit_webhook/" % self.channel.code
  22. def test_it_shows_form(self):
  23. self.client.login(username="[email protected]", password="password")
  24. r = self.client.get(self.url)
  25. self.assertContains(r, "Webhook Settings")
  26. self.assertContains(r, "Call example.org")
  27. # down
  28. self.assertContains(r, "http://example.org/down")
  29. self.assertContains(r, "My-Custom-UA")
  30. self.assertContains(r, "$NAME is down")
  31. # up
  32. self.assertContains(r, "http://example.org/up")
  33. self.assertContains(r, "$NAME is up")
  34. def test_it_saves_form_and_redirects(self):
  35. form = {
  36. "name": "Call foo.com / bar.com",
  37. "method_down": "POST",
  38. "url_down": "http://foo.com",
  39. "headers_down": "X-Foo: 1\nX-Bar: 2",
  40. "body_down": "going down",
  41. "method_up": "POST",
  42. "url_up": "https://bar.com",
  43. "headers_up": "Content-Type: text/plain",
  44. "body_up": "going up",
  45. }
  46. self.client.login(username="[email protected]", password="password")
  47. r = self.client.post(self.url, form)
  48. self.assertRedirects(r, self.channels_url)
  49. self.channel.refresh_from_db()
  50. self.assertEqual(self.channel.name, "Call foo.com / bar.com")
  51. down_spec = self.channel.down_webhook_spec
  52. self.assertEqual(down_spec["method"], "POST")
  53. self.assertEqual(down_spec["url"], "http://foo.com")
  54. self.assertEqual(down_spec["body"], "going down")
  55. self.assertEqual(down_spec["headers"], {"X-Foo": "1", "X-Bar": "2"})
  56. up_spec = self.channel.up_webhook_spec
  57. self.assertEqual(up_spec["method"], "POST")
  58. self.assertEqual(up_spec["url"], "https://bar.com")
  59. self.assertEqual(up_spec["body"], "going up")
  60. self.assertEqual(up_spec["headers"], {"Content-Type": "text/plain"})
  61. def test_it_requires_kind_webhook(self):
  62. self.channel.kind = "email"
  63. self.channel.value = "[email protected]"
  64. self.channel.save()
  65. self.client.login(username="[email protected]", password="password")
  66. r = self.client.get(self.url)
  67. self.assertEqual(r.status_code, 400)