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.

65 lines
2.2 KiB

4 years ago
  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. class AddOpsgenieTestCase(BaseTestCase):
  6. def setUp(self):
  7. super().setUp()
  8. self.url = "/projects/%s/add_opsgenie/" % self.project.code
  9. def test_instructions_work(self):
  10. self.client.login(username="[email protected]", password="password")
  11. r = self.client.get(self.url)
  12. self.assertContains(r, "escalation policies, and incident tracking")
  13. def test_it_works(self):
  14. form = {"key": "123456", "region": "us"}
  15. self.client.login(username="[email protected]", password="password")
  16. r = self.client.post(self.url, form)
  17. self.assertRedirects(r, self.channels_url)
  18. c = Channel.objects.get()
  19. self.assertEqual(c.kind, "opsgenie")
  20. payload = json.loads(c.value)
  21. self.assertEqual(payload["key"], "123456")
  22. self.assertEqual(payload["region"], "us")
  23. self.assertEqual(c.project, self.project)
  24. def test_it_trims_whitespace(self):
  25. form = {"key": " 123456 ", "region": "us"}
  26. self.client.login(username="[email protected]", password="password")
  27. self.client.post(self.url, form)
  28. c = Channel.objects.get()
  29. payload = json.loads(c.value)
  30. self.assertEqual(payload["key"], "123456")
  31. def test_it_saves_eu_region(self):
  32. form = {"key": "123456", "region": "eu"}
  33. self.client.login(username="[email protected]", password="password")
  34. self.client.post(self.url, form)
  35. c = Channel.objects.get()
  36. payload = json.loads(c.value)
  37. self.assertEqual(payload["region"], "eu")
  38. def test_it_requires_rw_access(self):
  39. self.bobs_membership.rw = False
  40. self.bobs_membership.save()
  41. self.client.login(username="[email protected]", password="password")
  42. r = self.client.get(self.url)
  43. self.assertEqual(r.status_code, 403)
  44. @override_settings(OPSGENIE_ENABLED=False)
  45. def test_it_handles_disabled_integration(self):
  46. self.client.login(username="[email protected]", password="password")
  47. r = self.client.get(self.url)
  48. self.assertEqual(r.status_code, 404)