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.

49 lines
1.6 KiB

  1. from django.test.utils import override_settings
  2. from hc.api.models import Check
  3. from hc.test import BaseTestCase
  4. class MetricsTestCase(BaseTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.project.api_key_readonly = "R" * 32
  8. self.project.save()
  9. self.check = Check(project=self.project, name="Alice Was Here")
  10. self.check.tags = "foo"
  11. self.check.save()
  12. key = "R" * 32
  13. self.url = "/projects/%s/checks/metrics/%s" % (self.project.code, key)
  14. def test_it_works(self):
  15. r = self.client.get(self.url)
  16. self.assertEqual(r.status_code, 200)
  17. self.assertContains(r, 'name="Alice Was Here"')
  18. self.assertContains(r, 'tags="foo"')
  19. self.assertContains(r, 'tag="foo"')
  20. self.assertContains(r, "hc_checks_total 1")
  21. def test_it_escapes_newline(self):
  22. self.check.name = "Line 1\nLine2"
  23. self.check.tags = "A\\C"
  24. self.check.save()
  25. r = self.client.get(self.url)
  26. self.assertEqual(r.status_code, 200)
  27. self.assertContains(r, "Line 1\\nLine2")
  28. self.assertContains(r, "A\\\\C")
  29. def test_it_checks_api_key_length(self):
  30. r = self.client.get(self.url + "R")
  31. self.assertEqual(r.status_code, 400)
  32. def test_it_checks_api_key(self):
  33. url = "/projects/%s/checks/metrics/%s" % (self.project.code, "X" * 32)
  34. r = self.client.get(url)
  35. self.assertEqual(r.status_code, 403)
  36. @override_settings(PROMETHEUS_ENABLED=False)
  37. def test_it_requires_prometheus_enabled(self):
  38. r = self.client.get(self.url)
  39. self.assertEqual(r.status_code, 404)