@ -122,3 +122,18 @@ class ProfileTestCase(BaseTestCase): | |||||
# to user's default team. | # to user's default team. | ||||
self.bobs_profile.refresh_from_db() | self.bobs_profile.refresh_from_db() | ||||
self.assertEqual(self.bobs_profile.current_team, self.bobs_profile) | self.assertEqual(self.bobs_profile.current_team, self.bobs_profile) | ||||
def test_it_shows_badges(self): | |||||
self.client.login(username="[email protected]", 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, "[email protected]") | |||||
# Expect only Alice's tags | |||||
self.assertNotContains(r, "bobs-tag.svg") |
@ -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") |
@ -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) |