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.

62 lines
2.0 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_started_but_down(self):
  27. self.check.last_start = now()
  28. self.check.tags = "foo"
  29. self.check.status = "down"
  30. self.check.save()
  31. r = self.client.get(self.json_url)
  32. self.assertContains(r, "down")
  33. def test_it_shows_grace_badge(self):
  34. self.check.last_ping = now() - td(days=1, minutes=10)
  35. self.check.tags = "foo"
  36. self.check.status = "up"
  37. self.check.save()
  38. r = self.client.get(self.json_url)
  39. self.assertContains(r, "late")
  40. def test_it_shows_started_but_grace_badge(self):
  41. self.check.last_start = now()
  42. self.check.last_ping = now() - td(days=1, minutes=10)
  43. self.check.tags = "foo"
  44. self.check.status = "up"
  45. self.check.save()
  46. r = self.client.get(self.json_url)
  47. self.assertContains(r, "late")