From 825aaed67f061ab62c297718d4e1d2d387e2cab1 Mon Sep 17 00:00:00 2001 From: Syed Hassan Raza Date: Tue, 31 Mar 2015 17:40:24 +0500 Subject: [PATCH] Split course's assests can access through '/' in asset url --- .../contentstore/views/tests/test_assets.py | 40 +++++++++++++++++++ common/djangoapps/contentserver/middleware.py | 2 + common/djangoapps/static_replace/__init__.py | 7 +++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cms/djangoapps/contentstore/views/tests/test_assets.py b/cms/djangoapps/contentstore/views/tests/test_assets.py index 8a454d9d06..daa7450f2b 100644 --- a/cms/djangoapps/contentstore/views/tests/test_assets.py +++ b/cms/djangoapps/contentstore/views/tests/test_assets.py @@ -14,9 +14,11 @@ from xmodule.assetstore import AssetMetadata from xmodule.contentstore.content import StaticContent from xmodule.contentstore.django import contentstore from xmodule.modulestore.django import modulestore +from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.xml_importer import import_course_from_xml from django.test.utils import override_settings from opaque_keys.edx.locations import SlashSeparatedCourseKey, AssetLocation +from static_replace import replace_static_urls import mock from ddt import ddt from ddt import data @@ -86,6 +88,44 @@ class BasicAssetsTestCase(AssetsTestCase): # Note: Actual contentType for textbook.pdf in asset.json is 'text/pdf' self.assertEqual(content.content_type, 'application/pdf') + def test_relative_url_for_split_course(self): + """ + Test relative path for split courses assets + """ + with modulestore().default_store(ModuleStoreEnum.Type.split): + module_store = modulestore() + course_id = module_store.make_course_key('edX', 'toy', '2012_Fall') + import_course_from_xml( + module_store, + self.user.id, + TEST_DATA_DIR, + ['toy'], + static_content_store=contentstore(), + target_id=course_id, + create_if_not_present=True + ) + course = module_store.get_course(course_id) + + filename = 'sample_static.txt' + html_src_attribute = '"/static/{}"'.format(filename) + asset_url = replace_static_urls(html_src_attribute, course_id=course.id) + url = asset_url.replace('"', '') + base_url = url.replace(filename, '') + + self.assertTrue("/{}".format(filename) in url) + resp = self.client.get(url) + self.assertEquals(resp.status_code, 200) + + # simulation of html page where base_url is up-to asset's main directory + # and relative_path is dom element with its src + relative_path = 'just_a_test.jpg' + # browser append relative_path with base_url + absolute_path = base_url + relative_path + + self.assertTrue("/{}".format(relative_path) in absolute_path) + resp = self.client.get(absolute_path) + self.assertEquals(resp.status_code, 200) + class PaginationTestCase(AssetsTestCase): """ diff --git a/common/djangoapps/contentserver/middleware.py b/common/djangoapps/contentserver/middleware.py index 987d4383d4..a5fa3ca2c1 100644 --- a/common/djangoapps/contentserver/middleware.py +++ b/common/djangoapps/contentserver/middleware.py @@ -31,6 +31,8 @@ class StaticContentServer(object): request.path.startswith('/' + XASSET_LOCATION_TAG + '/') or request.path.startswith('/' + AssetLocator.CANONICAL_NAMESPACE) ): + if AssetLocator.CANONICAL_NAMESPACE in request.path: + request.path = request.path.replace('block/', 'block@', 1) try: loc = StaticContent.get_location_from_path(request.path) except (InvalidLocationError, InvalidKeyError): diff --git a/common/djangoapps/static_replace/__init__.py b/common/djangoapps/static_replace/__init__.py index d0e617008f..96db5a73b6 100644 --- a/common/djangoapps/static_replace/__init__.py +++ b/common/djangoapps/static_replace/__init__.py @@ -9,6 +9,8 @@ from xmodule.modulestore.django import modulestore from xmodule.modulestore import ModuleStoreEnum from xmodule.contentstore.content import StaticContent +from opaque_keys.edx.locator import AssetLocator + log = logging.getLogger(__name__) @@ -152,7 +154,6 @@ def replace_static_urls(text, data_directory=None, course_id=None, static_asset_ """ Replace a single matched url. """ - # Don't mess with things that end in '?raw' if rest.endswith('?raw'): return original @@ -180,6 +181,10 @@ def replace_static_urls(text, data_directory=None, course_id=None, static_asset_ # if not, then assume it's courseware specific content and then look in the # Mongo-backed database url = StaticContent.convert_legacy_static_url_with_course_id(rest, course_id) + + if AssetLocator.CANONICAL_NAMESPACE in url: + url = url.replace('block@', 'block/', 1) + # Otherwise, look the file up in staticfiles_storage, and append the data directory if needed else: course_path = "/".join((static_asset_path or data_directory, rest))