diff --git a/common/djangoapps/util/tests/test_sandboxing.py b/common/djangoapps/util/tests/test_sandboxing.py index a1c06172ed13..e544a07e8585 100644 --- a/common/djangoapps/util/tests/test_sandboxing.py +++ b/common/djangoapps/util/tests/test_sandboxing.py @@ -7,7 +7,7 @@ from django.test import TestCase from django.test.utils import override_settings from opaque_keys.edx.keys import CourseKey -from opaque_keys.edx.locator import CourseLocator, LibraryLocator +from opaque_keys.edx.locator import CourseLocator, LibraryLocator, LibraryLocatorV2 from xmodule.contentstore.django import contentstore from xmodule.modulestore.tests.django_utils import upload_file_to_course @@ -114,3 +114,20 @@ def test_get_python_lib_zip(self): def test_no_python_lib_zip(self): assert self.sandbox_service.get_python_lib_zip() is None + + +class SandboxServiceForLibrariesV2Test(TestCase): + """ + Test SandboxService methods for V2 Content Libraries. + + (Lacks tests for anything other than python_lib_zip) + """ + + @classmethod + def setUpClass(cls): + super().setUpClass() + library_key = LibraryLocatorV2('test', 'sandbox_test') + cls.sandbox_service = SandboxService(course_id=library_key, contentstore=contentstore) + + def test_no_python_lib_zip(self): + assert self.sandbox_service.get_python_lib_zip() is None diff --git a/xmodule/util/sandboxing.py b/xmodule/util/sandboxing.py index 298f421b74da..01e897adeca2 100644 --- a/xmodule/util/sandboxing.py +++ b/xmodule/util/sandboxing.py @@ -3,6 +3,7 @@ import re from django.conf import settings +from opaque_keys.edx.keys import CourseKey, LearningContextKey DEFAULT_PYTHON_LIB_FILENAME = 'python_lib.zip' @@ -39,10 +40,14 @@ def can_execute_unsafe_code(course_id): return False -def get_python_lib_zip(contentstore, course_id): +def get_python_lib_zip(contentstore, context_key: LearningContextKey): """Return the bytes of the course code library file, if it exists.""" + if not isinstance(context_key, CourseKey): + # Although Content Libraries V2 does support python-evaluated capa problems, + # it doesn't yet support supplementary python zip files. + return None python_lib_filename = course_code_library_asset_name() - asset_key = course_id.make_asset_key("asset", python_lib_filename) + asset_key = context_key.make_asset_key("asset", python_lib_filename) zip_lib = contentstore().find(asset_key, throw_on_not_found=False) if zip_lib is not None: return zip_lib.data