From 8e32672228d8be6ffc271bd6fbde91beb88522ae Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Wed, 24 Sep 2014 15:07:04 -0700 Subject: [PATCH] Fix UnicodeError with anonymous student IDs --- common/djangoapps/student/models.py | 2 +- common/djangoapps/student/tests/tests.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 89e81d6529..28e1e6ed99 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -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'): diff --git a/common/djangoapps/student/tests/tests.py b/common/djangoapps/student/tests/tests.py index 7e899e4ef2..dd060f1186 100644 --- a/common/djangoapps/student/tests/tests.py +++ b/common/djangoapps/student/tests/tests.py @@ -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))