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.

89 lines
3.1 KiB

  1. from django.test.utils import override_settings
  2. from hc.api.models import Channel
  3. from hc.test import BaseTestCase
  4. @override_settings(
  5. PUSHOVER_API_TOKEN="token", PUSHOVER_SUBSCRIPTION_URL="http://example.org"
  6. )
  7. class AddPushoverTestCase(BaseTestCase):
  8. def setUp(self):
  9. super(AddPushoverTestCase, self).setUp()
  10. self.url = "/projects/%s/add_pushover/" % self.project.code
  11. @override_settings(PUSHOVER_API_TOKEN=None)
  12. def test_it_requires_api_token(self):
  13. self.client.login(username="[email protected]", password="password")
  14. r = self.client.get(self.url)
  15. self.assertEqual(r.status_code, 404)
  16. def test_it_shows_form(self):
  17. self.client.login(username="[email protected]", password="password")
  18. r = self.client.get(self.url)
  19. self.assertContains(r, "Subscribe with Pushover")
  20. def test_post_redirects(self):
  21. self.client.login(username="[email protected]", password="password")
  22. payload = {"po_priority": 2}
  23. r = self.client.post(self.url, form=payload)
  24. self.assertEqual(r.status_code, 302)
  25. def test_it_requires_authenticated_user(self):
  26. r = self.client.get(self.url)
  27. self.assertRedirects(r, "/accounts/login/?next=" + self.url)
  28. def test_it_adds_channel(self):
  29. self.client.login(username="[email protected]", password="password")
  30. session = self.client.session
  31. session["pushover"] = "foo"
  32. session.save()
  33. params = "?pushover_user_key=a&state=foo&prio=0&prio_up=-1"
  34. r = self.client.get(self.url + params, follow=True)
  35. self.assertRedirects(r, self.channels_url)
  36. channel = Channel.objects.get()
  37. self.assertEqual(channel.value, "a|0|-1")
  38. self.assertEqual(channel.project, self.project)
  39. def test_it_validates_priority(self):
  40. self.client.login(username="[email protected]", password="password")
  41. session = self.client.session
  42. session["pushover"] = "foo"
  43. session.save()
  44. params = "?pushover_user_key=a&state=foo&prio=abc"
  45. r = self.client.get(self.url + params)
  46. self.assertEqual(r.status_code, 400)
  47. def test_it_validates_priority_up(self):
  48. self.client.login(username="[email protected]", password="password")
  49. session = self.client.session
  50. session["pushover"] = "foo"
  51. session.save()
  52. params = "?pushover_user_key=a&state=foo&prio_up=abc"
  53. r = self.client.get(self.url + params)
  54. self.assertEqual(r.status_code, 400)
  55. def test_it_validates_state(self):
  56. self.client.login(username="[email protected]", password="password")
  57. session = self.client.session
  58. session["pushover"] = "foo"
  59. session.save()
  60. params = "?pushover_user_key=a&state=INVALID&prio=0"
  61. r = self.client.get(self.url + params)
  62. self.assertEqual(r.status_code, 403)
  63. def test_it_requires_rw_access(self):
  64. self.bobs_membership.rw = False
  65. self.bobs_membership.save()
  66. self.client.login(username="[email protected]", password="password")
  67. r = self.client.get(self.url)
  68. self.assertEqual(r.status_code, 403)