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.

103 lines
3.4 KiB

  1. from datetime import timedelta as td
  2. import json
  3. from django.utils.timezone import now
  4. from hc.api.models import Check
  5. from hc.test import BaseTestCase
  6. class PauseTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.check = Check.objects.create(project=self.project, status="up")
  10. self.url = f"/api/v1/checks/{self.check.code}/pause"
  11. def test_it_works(self):
  12. r = self.csrf_client.post(
  13. self.url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
  14. )
  15. self.assertEqual(r.status_code, 200)
  16. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  17. self.check.refresh_from_db()
  18. self.assertEqual(self.check.status, "paused")
  19. def test_it_accepts_api_key_in_post_body(self):
  20. payload = json.dumps({"api_key": "X" * 32})
  21. r = self.csrf_client.post(self.url, payload, content_type="application/json")
  22. self.assertEqual(r.status_code, 200)
  23. self.check.refresh_from_db()
  24. self.assertEqual(self.check.status, "paused")
  25. def test_it_handles_options(self):
  26. r = self.client.options(self.url)
  27. self.assertEqual(r.status_code, 204)
  28. self.assertIn("POST", r["Access-Control-Allow-Methods"])
  29. def test_it_only_allows_post(self):
  30. r = self.client.get(self.url, HTTP_X_API_KEY="X" * 32)
  31. self.assertEqual(r.status_code, 405)
  32. def test_it_validates_ownership(self):
  33. check = Check.objects.create(project=self.bobs_project, status="up")
  34. url = "/api/v1/checks/%s/pause" % check.code
  35. r = self.client.post(
  36. url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
  37. )
  38. self.assertEqual(r.status_code, 403)
  39. def test_it_validates_uuid(self):
  40. url = "/api/v1/checks/not-uuid/pause"
  41. r = self.client.post(
  42. url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
  43. )
  44. self.assertEqual(r.status_code, 404)
  45. def test_it_handles_missing_check(self):
  46. url = "/api/v1/checks/07c2f548-9850-4b27-af5d-6c9dc157ec02/pause"
  47. r = self.client.post(
  48. url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
  49. )
  50. self.assertEqual(r.status_code, 404)
  51. def test_it_clears_last_start_alert_after(self):
  52. self.check.last_start = now()
  53. self.check.alert_after = self.check.last_start + td(hours=1)
  54. self.check.save()
  55. r = self.client.post(
  56. self.url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
  57. )
  58. self.assertEqual(r.status_code, 200)
  59. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  60. self.check.refresh_from_db()
  61. self.assertEqual(self.check.last_start, None)
  62. self.assertEqual(self.check.alert_after, None)
  63. def test_it_clears_next_nag_date(self):
  64. self.profile.nag_period = td(hours=1)
  65. self.profile.next_nag_date = now() + td(minutes=30)
  66. self.profile.save()
  67. self.client.post(
  68. self.url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
  69. )
  70. self.profile.refresh_from_db()
  71. self.assertIsNone(self.profile.next_nag_date)
  72. def test_it_rejects_non_dict_post_body(self):
  73. r = self.csrf_client.post(self.url, "123", content_type="application/json")
  74. self.assertEqual(r.status_code, 400)
  75. self.assertEqual(
  76. r.json()["error"], "json validation error: value is not an object"
  77. )