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.

134 lines
4.9 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from django.test.utils import override_settings
  2. from hc.api.models import Channel
  3. from hc.test import BaseTestCase
  4. @override_settings(PUSHOVER_API_TOKEN="token", PUSHOVER_SUBSCRIPTION_URL="url")
  5. class AddChannelTestCase(BaseTestCase):
  6. def test_it_works(self):
  7. url = "/integrations/add/"
  8. form = {"kind": "email", "value": "[email protected]"}
  9. self.client.login(username="[email protected]", password="password")
  10. r = self.client.post(url, form)
  11. self.assertRedirects(r, "/integrations/")
  12. assert Channel.objects.count() == 1
  13. def test_team_access_works(self):
  14. url = "/integrations/add/"
  15. form = {"kind": "email", "value": "[email protected]"}
  16. self.client.login(username="[email protected]", password="password")
  17. self.client.post(url, form)
  18. ch = Channel.objects.get()
  19. # Added by bob, but should belong to alice (bob has team access)
  20. self.assertEqual(ch.user, self.alice)
  21. def test_it_trims_whitespace(self):
  22. """ Leading and trailing whitespace should get trimmed. """
  23. url = "/integrations/add/"
  24. form = {"kind": "email", "value": " [email protected] "}
  25. self.client.login(username="[email protected]", password="password")
  26. self.client.post(url, form)
  27. q = Channel.objects.filter(value="[email protected]")
  28. self.assertEqual(q.count(), 1)
  29. def test_it_rejects_bad_kind(self):
  30. url = "/integrations/add/"
  31. form = {"kind": "dog", "value": "Lassie"}
  32. self.client.login(username="[email protected]", password="password")
  33. r = self.client.post(url, form)
  34. assert r.status_code == 400, r.status_code
  35. def test_instructions_work(self):
  36. self.client.login(username="[email protected]", password="password")
  37. for frag in ("email", "webhook", "pd", "pushover", "slack", "hipchat", "victorops"):
  38. url = "/integrations/add_%s/" % frag
  39. r = self.client.get(url)
  40. self.assertContains(r, "Integration Settings", status_code=200)
  41. def test_it_adds_pushover_channel(self):
  42. self.client.login(username="[email protected]", password="password")
  43. session = self.client.session
  44. session["po_nonce"] = "n"
  45. session.save()
  46. params = "pushover_user_key=a&nonce=n&prio=0"
  47. r = self.client.get("/integrations/add_pushover/?%s" % params)
  48. assert r.status_code == 302
  49. channels = list(Channel.objects.all())
  50. assert len(channels) == 1
  51. assert channels[0].value == "a|0"
  52. def test_it_validates_pushover_priority(self):
  53. self.client.login(username="[email protected]", password="password")
  54. session = self.client.session
  55. session["po_nonce"] = "n"
  56. session.save()
  57. params = "pushover_user_key=a&nonce=n&prio=abc"
  58. r = self.client.get("/integrations/add_pushover/?%s" % params)
  59. assert r.status_code == 400
  60. def test_it_validates_pushover_nonce(self):
  61. self.client.login(username="[email protected]", password="password")
  62. session = self.client.session
  63. session["po_nonce"] = "n"
  64. session.save()
  65. params = "pushover_user_key=a&nonce=INVALID&prio=0"
  66. r = self.client.get("/integrations/add_pushover/?%s" % params)
  67. assert r.status_code == 403
  68. def test_it_adds_two_webhook_urls_and_redirects(self):
  69. form = {"value_down": "http://foo.com", "value_up": "https://bar.com"}
  70. self.client.login(username="[email protected]", password="password")
  71. r = self.client.post("/integrations/add_webhook/", form)
  72. self.assertRedirects(r, "/integrations/")
  73. c = Channel.objects.get()
  74. self.assertEqual(c.value, "http://foo.com\nhttps://bar.com")
  75. def test_it_adds_webhook_using_team_access(self):
  76. form = {"value_down": "http://foo.com", "value_up": "https://bar.com"}
  77. # Logging in as bob, not alice. Bob has team access so this
  78. # should work.
  79. self.client.login(username="[email protected]", password="password")
  80. self.client.post("/integrations/add_webhook/", form)
  81. c = Channel.objects.get()
  82. self.assertEqual(c.user, self.alice)
  83. self.assertEqual(c.value, "http://foo.com\nhttps://bar.com")
  84. def test_it_rejects_non_http_webhook_urls(self):
  85. form = {"value_down": "foo", "value_up": "bar"}
  86. self.client.login(username="[email protected]", password="password")
  87. r = self.client.post("/integrations/add_webhook/", form)
  88. self.assertContains(r, "Enter a valid URL.")
  89. self.assertEqual(Channel.objects.count(), 0)
  90. def test_it_handles_empty_down_url(self):
  91. form = {"value_down": "", "value_up": "http://foo.com"}
  92. self.client.login(username="[email protected]", password="password")
  93. self.client.post("/integrations/add_webhook/", form)
  94. c = Channel.objects.get()
  95. self.assertEqual(c.value, "\nhttp://foo.com")