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.

96 lines
3.5 KiB

  1. from datetime import timedelta as td
  2. from django.conf import settings
  3. from django.core.signing import base64_hmac
  4. from django.utils.timezone import now
  5. from hc.api.models import Check
  6. from hc.test import BaseTestCase
  7. class BadgeTestCase(BaseTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.check = Check.objects.create(project=self.project, tags="foo bar")
  11. sig = base64_hmac(str(self.project.badge_key), "foo", settings.SECRET_KEY)
  12. sig = sig[:8]
  13. self.svg_url = "/badge/%s/%s-2/foo.svg" % (self.project.badge_key, sig)
  14. self.json_url = "/badge/%s/%s-2/foo.json" % (self.project.badge_key, sig)
  15. self.with_late_url = "/badge/%s/%s/foo.json" % (self.project.badge_key, sig)
  16. self.shields_url = "/badge/%s/%s-2/foo.shields" % (self.project.badge_key, sig)
  17. def test_it_rejects_bad_signature(self):
  18. r = self.client.get("/badge/%s/12345678/foo.svg" % self.project.badge_key)
  19. self.assertEqual(r.status_code, 404)
  20. def test_it_returns_svg(self):
  21. r = self.client.get(self.svg_url)
  22. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  23. self.assertIn("no-cache", r["Cache-Control"])
  24. self.assertContains(r, "#4c1")
  25. def test_it_rejects_bad_format(self):
  26. r = self.client.get(self.json_url + "foo")
  27. self.assertEqual(r.status_code, 404)
  28. def test_it_handles_options(self):
  29. r = self.client.options(self.svg_url)
  30. self.assertEqual(r.status_code, 204)
  31. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  32. def test_it_handles_new(self):
  33. doc = self.client.get(self.json_url).json()
  34. self.assertEqual(doc, {"status": "up", "total": 1, "grace": 0, "down": 0})
  35. def test_it_ignores_started_when_down(self):
  36. self.check.last_start = now()
  37. self.check.status = "down"
  38. self.check.save()
  39. doc = self.client.get(self.json_url).json()
  40. self.assertEqual(doc, {"status": "down", "total": 1, "grace": 0, "down": 1})
  41. def test_it_treats_late_as_up(self):
  42. self.check.last_ping = now() - td(days=1, minutes=10)
  43. self.check.status = "up"
  44. self.check.save()
  45. doc = self.client.get(self.json_url).json()
  46. self.assertEqual(doc, {"status": "up", "total": 1, "grace": 1, "down": 0})
  47. def test_it_handles_special_characters(self):
  48. self.check.tags = "db@dc1"
  49. self.check.save()
  50. sig = base64_hmac(str(self.project.badge_key), "db@dc1", settings.SECRET_KEY)
  51. sig = sig[:8]
  52. url = "/badge/%s/%s/db%%2540dc1.svg" % (self.project.badge_key, sig)
  53. r = self.client.get(url)
  54. self.assertEqual(r.status_code, 200)
  55. def test_late_mode_returns_late_status(self):
  56. self.check.last_ping = now() - td(days=1, minutes=10)
  57. self.check.status = "up"
  58. self.check.save()
  59. doc = self.client.get(self.with_late_url).json()
  60. self.assertEqual(doc, {"status": "late", "total": 1, "grace": 1, "down": 0})
  61. def test_late_mode_ignores_started_when_late(self):
  62. self.check.last_start = now()
  63. self.check.last_ping = now() - td(days=1, minutes=10)
  64. self.check.status = "up"
  65. self.check.save()
  66. doc = self.client.get(self.with_late_url).json()
  67. self.assertEqual(doc, {"status": "late", "total": 1, "grace": 1, "down": 0})
  68. def test_it_returns_shields_json(self):
  69. doc = self.client.get(self.shields_url).json()
  70. self.assertEqual(
  71. doc,
  72. {"schemaVersion": 1, "label": "foo", "message": "up", "color": "success"},
  73. )