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.

91 lines
3.1 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.conf import settings
  2. from django.contrib.auth.models import User
  3. from django.test import TestCase
  4. from hc.api.models import Channel
  5. class AddChannelTestCase(TestCase):
  6. def setUp(self):
  7. super(AddChannelTestCase, self).setUp()
  8. self.alice = User(username="alice", email="[email protected]")
  9. self.alice.set_password("password")
  10. self.alice.save()
  11. settings.PUSHOVER_API_TOKEN = "bogus_token"
  12. settings.PUSHOVER_SUBSCRIPTION_URL = "bogus_url"
  13. def test_it_works(self):
  14. url = "/integrations/add/"
  15. form = {"kind": "email", "value": "[email protected]"}
  16. self.client.login(username="[email protected]", password="password")
  17. r = self.client.post(url, form)
  18. self.assertRedirects(r, "/integrations/")
  19. assert Channel.objects.count() == 1
  20. def test_it_trims_whitespace(self):
  21. """ Leading and trailing whitespace should get trimmed. """
  22. url = "/integrations/add/"
  23. form = {"kind": "email", "value": " [email protected] "}
  24. self.client.login(username="[email protected]", password="password")
  25. self.client.post(url, form)
  26. q = Channel.objects.filter(value="[email protected]")
  27. self.assertEqual(q.count(), 1)
  28. def test_it_rejects_bad_kind(self):
  29. url = "/integrations/add/"
  30. form = {"kind": "dog", "value": "Lassie"}
  31. self.client.login(username="[email protected]", password="password")
  32. r = self.client.post(url, form)
  33. assert r.status_code == 400, r.status_code
  34. def test_instructions_work(self):
  35. self.client.login(username="[email protected]", password="password")
  36. for frag in ("email", "webhook", "pd", "pushover", "slack", "hipchat"):
  37. url = "/integrations/add_%s/" % frag
  38. r = self.client.get(url)
  39. self.assertContains(r, "Integration Settings", status_code=200)
  40. def test_it_adds_pushover_channel(self):
  41. self.client.login(username="[email protected]", password="password")
  42. session = self.client.session
  43. session["po_nonce"] = "n"
  44. session.save()
  45. params = "pushover_user_key=a&nonce=n&prio=0"
  46. r = self.client.get("/integrations/add_pushover/?%s" % params)
  47. assert r.status_code == 302
  48. channels = list(Channel.objects.all())
  49. assert len(channels) == 1
  50. assert channels[0].value == "a|0"
  51. def test_it_validates_pushover_priority(self):
  52. self.client.login(username="[email protected]", password="password")
  53. session = self.client.session
  54. session["po_nonce"] = "n"
  55. session.save()
  56. params = "pushover_user_key=a&nonce=n&prio=abc"
  57. r = self.client.get("/integrations/add_pushover/?%s" % params)
  58. assert r.status_code == 400
  59. def test_it_validates_pushover_nonce(self):
  60. self.client.login(username="[email protected]", password="password")
  61. session = self.client.session
  62. session["po_nonce"] = "n"
  63. session.save()
  64. params = "pushover_user_key=a&nonce=INVALID&prio=0"
  65. r = self.client.get("/integrations/add_pushover/?%s" % params)
  66. assert r.status_code == 403