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.2 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/foo.svg" % (self.project.badge_key, sig)
  14. self.json_url = "/badge/%s/%s/foo.json" % (self.project.badge_key, sig)
  15. def test_it_rejects_bad_signature(self):
  16. r = self.client.get("/badge/%s/12345678/foo.svg" % self.project.badge_key)
  17. assert r.status_code == 404
  18. def test_it_returns_svg(self):
  19. r = self.client.get(self.svg_url)
  20. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  21. self.assertContains(r, "#4c1")
  22. def test_it_rejects_bad_format(self):
  23. r = self.client.get(self.json_url + "foo")
  24. self.assertEqual(r.status_code, 404)
  25. def test_it_handles_options(self):
  26. r = self.client.options(self.svg_url)
  27. self.assertEqual(r.status_code, 204)
  28. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  29. def test_it_handles_new(self):
  30. r = self.client.get(self.json_url)
  31. doc = r.json()
  32. self.assertEqual(doc["status"], "up")
  33. self.assertEqual(doc["total"], 1)
  34. self.assertEqual(doc["grace"], 0)
  35. self.assertEqual(doc["down"], 0)
  36. def test_it_handles_started_but_down(self):
  37. self.check.last_start = now()
  38. self.check.tags = "foo"
  39. self.check.status = "down"
  40. self.check.save()
  41. r = self.client.get(self.json_url)
  42. doc = r.json()
  43. self.assertEqual(doc["status"], "down")
  44. self.assertEqual(doc["total"], 1)
  45. self.assertEqual(doc["grace"], 0)
  46. self.assertEqual(doc["down"], 1)
  47. def test_it_shows_grace_badge(self):
  48. self.check.last_ping = now() - td(days=1, minutes=10)
  49. self.check.tags = "foo"
  50. self.check.status = "up"
  51. self.check.save()
  52. r = self.client.get(self.json_url)
  53. doc = r.json()
  54. self.assertEqual(doc["status"], "late")
  55. self.assertEqual(doc["total"], 1)
  56. self.assertEqual(doc["grace"], 1)
  57. self.assertEqual(doc["down"], 0)
  58. def test_it_shows_started_but_grace_badge(self):
  59. self.check.last_start = now()
  60. self.check.last_ping = now() - td(days=1, minutes=10)
  61. self.check.tags = "foo"
  62. self.check.status = "up"
  63. self.check.save()
  64. r = self.client.get(self.json_url)
  65. doc = r.json()
  66. self.assertEqual(doc["status"], "late")
  67. self.assertEqual(doc["total"], 1)
  68. self.assertEqual(doc["grace"], 1)
  69. self.assertEqual(doc["down"], 0)
  70. def test_it_handles_special_characters(self):
  71. self.check.tags = "db@dc1"
  72. self.check.save()
  73. sig = base64_hmac(str(self.project.badge_key), "db@dc1", settings.SECRET_KEY)
  74. sig = sig[:8]
  75. url = "/badge/%s/%s/db%%2540dc1.svg" % (self.project.badge_key, sig)
  76. r = self.client.get(url)
  77. self.assertEqual(r.status_code, 200)