Split course's assests can access through '/' in asset url
This commit is contained in:
committed by
Adam Palay
parent
08cb8dd541
commit
825aaed67f
@@ -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):
|
||||
"""
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user