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.

96 lines
3.3 KiB

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