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.

63 lines
2.1 KiB

  1. import json
  2. from django.test.utils import override_settings
  3. from hc.api.models import Channel
  4. from hc.test import BaseTestCase
  5. from urllib.parse import urlencode
  6. @override_settings(PD_APP_ID="FOOBAR")
  7. class AddPagerDutyCompleteTestCase(BaseTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. session = self.client.session
  11. session["pagerduty"] = ("ABC", str(self.project.code))
  12. session.save()
  13. def _url(self, state="ABC"):
  14. config = {
  15. "account": {"name": "Foo"},
  16. "integration_keys": [{"integration_key": "foo", "name": "bar"}],
  17. }
  18. url = "/integrations/add_pagerduty/?"
  19. url += urlencode({"state": state, "config": json.dumps(config)})
  20. return url
  21. def test_it_adds_channel(self):
  22. self.client.login(username="[email protected]", password="password")
  23. r = self.client.get(self._url())
  24. self.assertRedirects(r, self.channels_url)
  25. channel = Channel.objects.get()
  26. self.assertEqual(channel.kind, "pd")
  27. self.assertEqual(channel.pd_service_key, "foo")
  28. self.assertEqual(channel.pd_account, "Foo")
  29. def test_it_validates_state(self):
  30. self.client.login(username="[email protected]", password="password")
  31. r = self.client.get(self._url(state="XYZ"))
  32. self.assertEqual(r.status_code, 403)
  33. @override_settings(PD_APP_ID=None)
  34. def test_it_requires_app_id(self):
  35. self.client.login(username="[email protected]", password="password")
  36. r = self.client.get(self._url())
  37. self.assertEqual(r.status_code, 404)
  38. @override_settings(PD_ENABLED=False)
  39. def test_it_requires_pd_enabled(self):
  40. self.client.login(username="[email protected]", password="password")
  41. r = self.client.get(self._url())
  42. self.assertEqual(r.status_code, 404)
  43. def test_it_requires_rw_access(self):
  44. self.bobs_membership.rw = False
  45. self.bobs_membership.save()
  46. self.client.login(username="[email protected]", password="password")
  47. r = self.client.get(self._url())
  48. self.assertEqual(r.status_code, 403)