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.

90 lines
3.0 KiB

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