Fix UnicodeError with anonymous student IDs

This commit is contained in:
Braden MacDonald
2014-09-24 15:07:04 -07:00
parent ed68b0ab8a
commit 8e32672228
2 changed files with 10 additions and 1 deletions

View File

@@ -100,7 +100,7 @@ def anonymous_id_for_user(user, course_id, save=True):
hasher.update(settings.SECRET_KEY)
hasher.update(unicode(user.id))
if course_id:
hasher.update(course_id.to_deprecated_string())
hasher.update(course_id.to_deprecated_string().encode('utf-8'))
digest = hasher.hexdigest()
if not hasattr(user, '_anonymous_id'):

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
@@ -736,3 +737,11 @@ class AnonymousLookupTable(TestCase):
real_user = user_by_anonymous_id(anonymous_id)
self.assertEqual(self.user, real_user)
self.assertEqual(anonymous_id, anonymous_id_for_user(self.user, self.course.id, save=False))
def test_roundtrip_with_unicode_course_id(self):
course2 = CourseFactory.create(org=self.COURSE_ORG, display_name=u"Omega Course Ω", number=self.COURSE_SLUG)
CourseEnrollment.enroll(self.user, course2.id)
anonymous_id = anonymous_id_for_user(self.user, course2.id)
real_user = user_by_anonymous_id(anonymous_id)
self.assertEqual(self.user, real_user)
self.assertEqual(anonymous_id, anonymous_id_for_user(self.user, course2.id, save=False))