Browse Source

Tests for badges

pull/72/head
Pēteris Caune 8 years ago
parent
commit
44163c3758
4 changed files with 57 additions and 0 deletions
  1. +15
    -0
      hc/accounts/tests/test_profile.py
  2. +24
    -0
      hc/api/tests/test_badge.py
  3. +0
    -0
      hc/lib/tests/__init__.py
  4. +18
    -0
      hc/lib/tests/test_badges.py

+ 15
- 0
hc/accounts/tests/test_profile.py View File

@ -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="[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")

+ 24
- 0
hc/api/tests/test_badge.py View File

@ -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
hc/lib/tests/__init__.py View File


+ 18
- 0
hc/lib/tests/test_badges.py View File

@ -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)

Loading…
Cancel
Save