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.

66 lines
2.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(BadgeTestCase, self).setUp()
  10. self.check = Check.objects.create(user=self.alice, tags="foo bar")
  11. sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY)
  12. sig = sig[:8]
  13. self.svg_url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)
  14. self.json_url = "/badge/%s/%s/foo.json" % (self.alice.username, sig)
  15. def test_it_rejects_bad_signature(self):
  16. r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username)
  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. sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY)
  24. sig = sig[:8]
  25. url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig)
  26. r = self.client.options(url)
  27. self.assertEqual(r.status_code, 204)
  28. self.assertEqual(r["Access-Control-Allow-Origin"], "*")
  29. def test_it_handles_started_but_down(self):
  30. self.check.last_start = now()
  31. self.check.tags = "foo"
  32. self.check.status = "down"
  33. self.check.save()
  34. r = self.client.get(self.json_url)
  35. self.assertContains(r, "down")
  36. def test_it_shows_grace_badge(self):
  37. self.check.last_ping = now() - td(days=1, minutes=10)
  38. self.check.tags = "foo"
  39. self.check.status = "up"
  40. self.check.save()
  41. r = self.client.get(self.json_url)
  42. self.assertContains(r, "late")
  43. def test_it_shows_started_but_grace_badge(self):
  44. self.check.last_start = now()
  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. self.assertContains(r, "late")