from datetime import timedelta as td
|
|
import json
|
|
|
|
from django.utils.timezone import now
|
|
from hc.api.models import Check
|
|
from hc.test import BaseTestCase
|
|
|
|
|
|
class PauseTestCase(BaseTestCase):
|
|
def setUp(self):
|
|
super().setUp()
|
|
|
|
self.check = Check.objects.create(project=self.project, status="up")
|
|
self.url = f"/api/v1/checks/{self.check.code}/pause"
|
|
|
|
def test_it_works(self):
|
|
r = self.csrf_client.post(
|
|
self.url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
|
|
)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertEqual(r["Access-Control-Allow-Origin"], "*")
|
|
|
|
self.check.refresh_from_db()
|
|
self.assertEqual(self.check.status, "paused")
|
|
|
|
def test_it_accepts_api_key_in_post_body(self):
|
|
payload = json.dumps({"api_key": "X" * 32})
|
|
r = self.csrf_client.post(self.url, payload, content_type="application/json")
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
self.check.refresh_from_db()
|
|
self.assertEqual(self.check.status, "paused")
|
|
|
|
def test_it_handles_options(self):
|
|
r = self.client.options(self.url)
|
|
self.assertEqual(r.status_code, 204)
|
|
self.assertIn("POST", r["Access-Control-Allow-Methods"])
|
|
|
|
def test_it_only_allows_post(self):
|
|
r = self.client.get(self.url, HTTP_X_API_KEY="X" * 32)
|
|
self.assertEqual(r.status_code, 405)
|
|
|
|
def test_it_validates_ownership(self):
|
|
check = Check.objects.create(project=self.bobs_project, status="up")
|
|
|
|
url = "/api/v1/checks/%s/pause" % check.code
|
|
r = self.client.post(
|
|
url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
|
|
)
|
|
|
|
self.assertEqual(r.status_code, 403)
|
|
|
|
def test_it_validates_uuid(self):
|
|
url = "/api/v1/checks/not-uuid/pause"
|
|
r = self.client.post(
|
|
url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
|
|
)
|
|
|
|
self.assertEqual(r.status_code, 404)
|
|
|
|
def test_it_handles_missing_check(self):
|
|
url = "/api/v1/checks/07c2f548-9850-4b27-af5d-6c9dc157ec02/pause"
|
|
r = self.client.post(
|
|
url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
|
|
)
|
|
|
|
self.assertEqual(r.status_code, 404)
|
|
|
|
def test_it_clears_last_start_alert_after(self):
|
|
self.check.last_start = now()
|
|
self.check.alert_after = self.check.last_start + td(hours=1)
|
|
self.check.save()
|
|
|
|
r = self.client.post(
|
|
self.url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
|
|
)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
self.assertEqual(r["Access-Control-Allow-Origin"], "*")
|
|
|
|
self.check.refresh_from_db()
|
|
self.assertEqual(self.check.last_start, None)
|
|
self.assertEqual(self.check.alert_after, None)
|
|
|
|
def test_it_clears_next_nag_date(self):
|
|
self.profile.nag_period = td(hours=1)
|
|
self.profile.next_nag_date = now() + td(minutes=30)
|
|
self.profile.save()
|
|
|
|
self.client.post(
|
|
self.url, "", content_type="application/json", HTTP_X_API_KEY="X" * 32
|
|
)
|
|
|
|
self.profile.refresh_from_db()
|
|
self.assertIsNone(self.profile.next_nag_date)
|