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