From ef8f841ac276eec2609f6960a963a790dcd53f43 Mon Sep 17 00:00:00 2001 From: Jillian Vogel Date: Mon, 17 Jan 2022 09:57:08 +1030 Subject: [PATCH] temp: let XBlock API users optionally use LabXchange block types when fetching block metadata and rendering blocks while maintaining the original usage IDs/OLX. This change is marked temporary because LabXchange need it during the transition to a custom runtime, but it's not really useful to anyone else. We will revert this change with a future PR. --- .../tests/test_content_libraries.py | 167 ++++++++++++++++++ openedx/core/djangoapps/xblock/api.py | 19 +- .../core/djangoapps/xblock/rest_api/views.py | 49 ++++- .../xblock/runtime/blockstore_runtime.py | 9 +- 4 files changed, 232 insertions(+), 12 deletions(-) diff --git a/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py b/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py index c23679cb18..599dfd9a60 100644 --- a/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py +++ b/openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py @@ -3,7 +3,9 @@ Tests for Blockstore-based Content Libraries """ from uuid import UUID from unittest.mock import patch +from urllib.parse import urlparse, parse_qsl +import json import ddt from django.conf import settings from django.contrib.auth.models import Group @@ -11,6 +13,9 @@ from django.test.client import Client from django.test.utils import override_settings from organizations.models import Organization from rest_framework.test import APITestCase +from web_fragments.fragment import Fragment +from webob import Response +from xblock.core import XBlock from openedx.core.djangoapps.content_libraries.libraries_index import LibraryBlockIndexer, ContentLibraryIndexer from openedx.core.djangoapps.content_libraries.tests.base import ( @@ -20,6 +25,7 @@ from openedx.core.djangoapps.content_libraries.tests.base import ( URL_BLOCK_RENDER_VIEW, URL_BLOCK_GET_HANDLER_URL, URL_BLOCK_XBLOCK_HANDLER, + URL_LIB_BLOCK_OLX, ) from openedx.core.djangoapps.content_libraries.constants import VIDEO, COMPLEX, PROBLEM, CC_4_BY, ALL_RIGHTS_RESERVED from openedx.core.djangolib.blockstore_cache import cache @@ -903,3 +909,164 @@ class ContentLibraryXBlockValidationTest(APITestCase): self.assertEqual(response.json(), { 'detail': f"XBlock {valid_not_found_key} does not exist, or you don't have permission to view it.", }) + + +class AltBlock(XBlock): + """Class for testing LabXchange XBlock type overrides.""" + @XBlock.handler + def student_view_user_state(self, request, suffix=""): + """ + Returns a JSON response for testing. + """ + view_state = { + "id": str(self.location), + "block_type": str(self.location.block_type), + "override_type": str(self.__class__), + } + return Response( + json.dumps(view_state), + content_type='application/json', + charset='UTF-8', + ) + + def student_view(self, context=None): + """ + Returns an HTML fragment for testing. + """ + return Fragment(f"
" + "
") + + +@ddt.ddt +@elasticsearch_test +class ContentLibrariesXBlockTypeOverrideTest(ContentLibrariesRestApiTest): + """ + Tests for Blockstore-based Content Libraries XBlock API, + where the expected XBlock type returned is overridden in the request. + """ + BLOCK_DATA = ( + ('block-wo-override', {}, 'video'), + ('block-w-override', {'lx_block_types': '1'}, 'alt-block'), + ) + + def setUp(self): + super().setUp() + if settings.ENABLE_ELASTICSEARCH_FOR_TESTS: + ContentLibraryIndexer.remove_all_items() + LibraryBlockIndexer.remove_all_items() + + self.olx = """ +