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.

72 lines
2.6 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, "Runs a HTTP GET or HTTP POST")
  9. def test_it_adds_two_webhook_urls_and_redirects(self):
  10. form = {"value_down": "http://foo.com", "value_up": "https://bar.com"}
  11. self.client.login(username="[email protected]", password="password")
  12. r = self.client.post(self.url, form)
  13. self.assertRedirects(r, "/integrations/")
  14. c = Channel.objects.get()
  15. self.assertEqual(c.value, "http://foo.com\nhttps://bar.com\n")
  16. def test_it_adds_webhook_using_team_access(self):
  17. form = {"value_down": "http://foo.com", "value_up": "https://bar.com"}
  18. # Logging in as bob, not alice. Bob has team access so this
  19. # should work.
  20. self.client.login(username="[email protected]", password="password")
  21. self.client.post(self.url, form)
  22. c = Channel.objects.get()
  23. self.assertEqual(c.user, self.alice)
  24. self.assertEqual(c.value, "http://foo.com\nhttps://bar.com\n")
  25. def test_it_rejects_bad_urls(self):
  26. urls = [
  27. # clearly not an URL
  28. "foo",
  29. # FTP addresses not allowed
  30. "ftp://example.org",
  31. # no loopback
  32. "http://localhost:1234/endpoint",
  33. "http://127.0.0.1/endpoint"
  34. ]
  35. self.client.login(username="[email protected]", password="password")
  36. for url in urls:
  37. form = {"value_down": url, "value_up": ""}
  38. r = self.client.post(self.url, form)
  39. self.assertContains(r, "Enter a valid URL.", msg_prefix=url)
  40. self.assertEqual(Channel.objects.count(), 0)
  41. def test_it_handles_empty_down_url(self):
  42. form = {"value_down": "", "value_up": "http://foo.com"}
  43. self.client.login(username="[email protected]", password="password")
  44. self.client.post(self.url, form)
  45. c = Channel.objects.get()
  46. self.assertEqual(c.value, "\nhttp://foo.com\n")
  47. def test_it_adds_post_data(self):
  48. form = {"value_down": "http://foo.com", "post_data": "hello"}
  49. self.client.login(username="[email protected]", password="password")
  50. r = self.client.post(self.url, form)
  51. self.assertRedirects(r, "/integrations/")
  52. c = Channel.objects.get()
  53. self.assertEqual(c.value, "http://foo.com\n\nhello")