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.

92 lines
3.4 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.assertContains(r, "#4c1")
  24. def test_it_rejects_bad_format(self):
  25. r = self.client.get(self.json_url + "foo")
  26. self.assertEqual(r.status_code, 404)
  27. def test_it_handles_options(self):
  28. r = self.client.options(self.svg_url)
  29. self.assertEqual(r.status_code, 204)
  30. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  31. def test_it_handles_new(self):
  32. doc = self.client.get(self.json_url).json()
  33. self.assertEqual(doc, {"status": "up", "total": 1, "grace": 0, "down": 0})
  34. def test_it_ignores_started_when_down(self):
  35. self.check.last_start = now()
  36. self.check.status = "down"
  37. self.check.save()
  38. doc = self.client.get(self.json_url).json()
  39. self.assertEqual(doc, {"status": "down", "total": 1, "grace": 0, "down": 1})
  40. def test_it_treats_late_as_up(self):
  41. self.check.last_ping = now() - td(days=1, minutes=10)
  42. self.check.status = "up"
  43. self.check.save()
  44. doc = self.client.get(self.json_url).json()
  45. self.assertEqual(doc, {"status": "up", "total": 1, "grace": 1, "down": 0})
  46. def test_it_handles_special_characters(self):
  47. self.check.tags = "db@dc1"
  48. self.check.save()
  49. sig = base64_hmac(str(self.project.badge_key), "db@dc1", settings.SECRET_KEY)
  50. sig = sig[:8]
  51. url = "/badge/%s/%s/db%%2540dc1.svg" % (self.project.badge_key, sig)
  52. r = self.client.get(url)
  53. self.assertEqual(r.status_code, 200)
  54. def test_late_mode_returns_late_status(self):
  55. self.check.last_ping = now() - td(days=1, minutes=10)
  56. self.check.status = "up"
  57. self.check.save()
  58. doc = self.client.get(self.with_late_url).json()
  59. self.assertEqual(doc, {"status": "late", "total": 1, "grace": 1, "down": 0})
  60. def test_late_mode_ignores_started_when_late(self):
  61. self.check.last_start = now()
  62. self.check.last_ping = now() - td(days=1, minutes=10)
  63. self.check.status = "up"
  64. self.check.save()
  65. doc = self.client.get(self.with_late_url).json()
  66. self.assertEqual(doc, {"status": "late", "total": 1, "grace": 1, "down": 0})
  67. def test_it_returns_shields_json(self):
  68. doc = self.client.get(self.shields_url).json()
  69. self.assertEqual(doc, {"label": "foo", "message": "up", "color": "success"})