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.

77 lines
2.7 KiB

6 years ago
  1. from datetime import timedelta as td
  2. from django.utils.timezone import now
  3. from hc.api.models import Check
  4. from hc.test import BaseTestCase
  5. class PauseTestCase(BaseTestCase):
  6. def test_it_works(self):
  7. check = Check(user=self.alice, status="up", project=self.project)
  8. check.save()
  9. url = "/api/v1/checks/%s/pause" % check.code
  10. r = self.client.post(url, "", content_type="application/json",
  11. HTTP_X_API_KEY="X" * 32)
  12. self.assertEqual(r.status_code, 200)
  13. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  14. check.refresh_from_db()
  15. self.assertEqual(check.status, "paused")
  16. def test_it_handles_options(self):
  17. check = Check(user=self.alice, status="up", project=self.project)
  18. check.save()
  19. r = self.client.options("/api/v1/checks/%s/pause" % check.code)
  20. self.assertEqual(r.status_code, 204)
  21. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  22. def test_it_only_allows_post(self):
  23. url = "/api/v1/checks/1659718b-21ad-4ed1-8740-43afc6c41524/pause"
  24. r = self.client.get(url, HTTP_X_API_KEY="X" * 32)
  25. self.assertEqual(r.status_code, 405)
  26. def test_it_validates_ownership(self):
  27. check = Check(user=self.bob, status="up", project=self.bobs_project)
  28. check.save()
  29. url = "/api/v1/checks/%s/pause" % check.code
  30. r = self.client.post(url, "", content_type="application/json",
  31. HTTP_X_API_KEY="X" * 32)
  32. self.assertEqual(r.status_code, 403)
  33. def test_it_validates_uuid(self):
  34. url = "/api/v1/checks/not-uuid/pause"
  35. r = self.client.post(url, "", content_type="application/json",
  36. HTTP_X_API_KEY="X" * 32)
  37. self.assertEqual(r.status_code, 404)
  38. def test_it_handles_missing_check(self):
  39. url = "/api/v1/checks/07c2f548-9850-4b27-af5d-6c9dc157ec02/pause"
  40. r = self.client.post(url, "", content_type="application/json",
  41. HTTP_X_API_KEY="X" * 32)
  42. self.assertEqual(r.status_code, 404)
  43. def test_it_clears_last_start_alert_after(self):
  44. check = Check(user=self.alice, status="up", project=self.project)
  45. check.last_start = now()
  46. check.alert_after = check.last_start + td(hours=1)
  47. check.save()
  48. url = "/api/v1/checks/%s/pause" % check.code
  49. r = self.client.post(url, "", content_type="application/json",
  50. HTTP_X_API_KEY="X" * 32)
  51. self.assertEqual(r.status_code, 200)
  52. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  53. check.refresh_from_db()
  54. self.assertEqual(check.last_start, None)
  55. self.assertEqual(check.alert_after, None)