Commit 62f015bb by Will Daly

Merge pull request #330 from edx/will/opaque-keys

Handle opaque keys in a backwards-compatible way
parents d96edce9 697cc77a
......@@ -149,13 +149,14 @@ class OpenAssessmentBlock(
(dict): The student item associated with this XBlock instance. This
includes the student id, item id, and course id.
"""
item_id = unicode(self.scope_ids.usage_id)
item_id = self._serialize_opaque_key(self.scope_ids.usage_id)
# This is not the real way course_ids should work, but this is a
# temporary expediency for LMS integration
if hasattr(self, "xmodule_runtime"):
course_id = unicode(self.xmodule_runtime.course_id)
student_id = self.xmodule_runtime.anonymous_student_id
course_id = self._serialize_opaque_key(self.xmodule_runtime.course_id) # pylint:disable=E1101
student_id = self.xmodule_runtime.anonymous_student_id # pylint:disable=E1101
else:
course_id = "edX/Enchantment_101/April_1"
if self.scope_ids.user_id is None:
......@@ -504,3 +505,23 @@ class OpenAssessmentBlock(
for assessment in self.rubric_assessments:
if assessment["name"] == mixin_name:
return assessment
def _serialize_opaque_key(self, key):
"""
Gracefully handle opaque keys, both before and after the transition.
https://github.com/edx/edx-platform/wiki/Opaque-Keys
Currently uses `to_deprecated_string()` to ensure that new keys
are backwards-compatible with keys we store in ORA2 database models.
Args:
key (unicode or OpaqueKey subclass): The key to serialize.
Returns:
unicode
"""
if hasattr(key, 'to_deprecated_string'):
return key.to_deprecated_string()
else:
return unicode(key)
# -*- coding: utf-8 -*-
"""
Tests for opaque key transition in the LMS runtime.
See https://github.com/edx/edx-platform/wiki/Opaque-Keys
"""
import mock
from .base import XBlockHandlerTestCase, scenario
class TestOpaqueKeys(XBlockHandlerTestCase):
"""
Test that the XBlock handles the opaque key transition gracefully.
"""
@scenario('data/basic_scenario.xml', user_id='Bob')
def test_opaque_key_deprecated_string(self, xblock):
# Simulate the opaque key changeover by
# providing a mock `to_deprecated_string()` method.
usage_key = mock.MagicMock()
usage_key.to_deprecated_string.return_value = u"เՇє๓ เ๔"
course_key = mock.MagicMock()
course_key.to_deprecated_string.return_value = u"¢συяѕє ι∂"
xblock.scope_ids = mock.MagicMock()
xblock.scope_ids.usage_id = usage_key
xblock.xmodule_runtime = mock.MagicMock()
xblock.xmodule_runtime.course_id = course_key
student_item = xblock.get_student_item_dict()
# Expect that we correctly serialize the opaque keys
self.assertEqual(student_item['item_id'], u"เՇє๓ เ๔")
self.assertEqual(student_item['course_id'], u"¢συяѕє ι∂")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment