diff --git a/hc/accounts/tests/test_profile.py b/hc/accounts/tests/test_profile.py index 4318f079..ba7528cc 100644 --- a/hc/accounts/tests/test_profile.py +++ b/hc/accounts/tests/test_profile.py @@ -122,3 +122,18 @@ class ProfileTestCase(BaseTestCase): # to user's default team. self.bobs_profile.refresh_from_db() self.assertEqual(self.bobs_profile.current_team, self.bobs_profile) + + def test_it_shows_badges(self): + self.client.login(username="alice@example.org", password="password") + Check.objects.create(user=self.alice, tags="foo a-B_1 baz@") + Check.objects.create(user=self.bob, tags="bobs-tag") + + r = self.client.get("/accounts/profile/") + self.assertContains(r, "foo.svg") + self.assertContains(r, "a-B_1.svg") + + # Expect badge URLs only for tags that match \w+ + self.assertNotContains(r, "baz@.svg") + + # Expect only Alice's tags + self.assertNotContains(r, "bobs-tag.svg") diff --git a/hc/api/tests/test_badge.py b/hc/api/tests/test_badge.py new file mode 100644 index 00000000..31042e52 --- /dev/null +++ b/hc/api/tests/test_badge.py @@ -0,0 +1,24 @@ +from django.conf import settings +from django.core.signing import base64_hmac + +from hc.api.models import Check +from hc.test import BaseTestCase + + +class BadgeTestCase(BaseTestCase): + + def setUp(self): + super(BadgeTestCase, self).setUp() + self.check = Check.objects.create(user=self.alice, tags="foo bar") + + def test_it_rejects_bad_signature(self): + r = self.client.get("/badge/%s/12345678/foo.svg" % self.alice.username) + assert r.status_code == 400 + + def test_it_returns_svg(self): + sig = base64_hmac(str(self.alice.username), "foo", settings.SECRET_KEY) + sig = sig[:8].decode("utf-8") + url = "/badge/%s/%s/foo.svg" % (self.alice.username, sig) + + r = self.client.get(url) + self.assertContains(r, "#4c1") diff --git a/hc/lib/tests/__init__.py b/hc/lib/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/hc/lib/tests/test_badges.py b/hc/lib/tests/test_badges.py new file mode 100644 index 00000000..d5390e1e --- /dev/null +++ b/hc/lib/tests/test_badges.py @@ -0,0 +1,18 @@ +from django.test import TestCase + +from hc.lib.badges import get_width, get_badge_svg + + +class BadgesTestCase(TestCase): + + def test_get_width_works(self): + self.assertEqual(get_width("mm"), 20) + # Default width for unknown characters is 7 + self.assertEqual(get_width("@"), 7) + + def test_it_makes_svg(self): + svg = get_badge_svg("foo", "up") + self.assertTrue("#4c1" in svg) + + svg = get_badge_svg("bar", "down") + self.assertTrue("#e05d44" in svg)