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.

55 lines
2.0 KiB

  1. from django.test.utils import override_settings
  2. from django.utils.timezone import now
  3. from hc.api.models import Channel, Check, Flip, Notification, Ping
  4. from hc.test import BaseTestCase
  5. @override_settings(METRICS_KEY="foo")
  6. class MetricsTestCase(BaseTestCase):
  7. url = "/api/v1/metrics/"
  8. def test_it_returns_num_unprocessed_flips(self):
  9. check = Check.objects.create(project=self.project, status="down")
  10. flip = Flip(owner=check)
  11. flip.created = now()
  12. flip.old_status = "up"
  13. flip.new_status = "down"
  14. flip.save()
  15. r = self.client.get(self.url, HTTP_X_METRICS_KEY="foo")
  16. self.assertEqual(r.status_code, 200)
  17. doc = r.json()
  18. self.assertEqual(doc["num_unprocessed_flips"], 1)
  19. def test_it_returns_max_ping_id(self):
  20. check = Check.objects.create(project=self.project, status="down")
  21. Ping.objects.create(owner=check, n=1)
  22. last_ping = Ping.objects.last()
  23. r = self.client.get(self.url, HTTP_X_METRICS_KEY="foo")
  24. self.assertEqual(r.status_code, 200)
  25. doc = r.json()
  26. self.assertEqual(doc["max_ping_id"], last_ping.id)
  27. def test_it_returns_max_notification_id(self):
  28. check = Check.objects.create(project=self.project, status="down")
  29. channel = Channel.objects.create(project=self.project, kind="email")
  30. Notification.objects.create(owner=check, channel=channel, check_status="down")
  31. last_notification = Notification.objects.last()
  32. r = self.client.get(self.url, HTTP_X_METRICS_KEY="foo")
  33. self.assertEqual(r.status_code, 200)
  34. doc = r.json()
  35. self.assertEqual(doc["max_notification_id"], last_notification.id)
  36. @override_settings(METRICS_KEY=None)
  37. def test_it_handles_unset_metrics_key(self):
  38. r = self.client.get(self.url, HTTP_X_METRICS_KEY="foo")
  39. self.assertEqual(r.status_code, 403)
  40. def test_it_handles_incorrect_metrics_key(self):
  41. r = self.client.get(self.url, HTTP_X_METRICS_KEY="bar")
  42. self.assertEqual(r.status_code, 403)