Commit 8592db92 by Calen Pennington

Add more tests of static_replace

parent 5eae9c3a
...@@ -48,7 +48,6 @@ def replace_course_urls(text, course_id): ...@@ -48,7 +48,6 @@ def replace_course_urls(text, course_id):
def replace_course_url(match): def replace_course_url(match):
log.warning("Course match: %s", match.groupdict())
quote = match.group('quote') quote = match.group('quote')
rest = match.group('rest') rest = match.group('rest')
return "".join([quote, '/courses/' + course_id + '/', rest, quote]) return "".join([quote, '/courses/' + course_id + '/', rest, quote])
...@@ -69,31 +68,26 @@ def replace_static_urls(text, data_directory, course_namespace=None): ...@@ -69,31 +68,26 @@ def replace_static_urls(text, data_directory, course_namespace=None):
""" """
def replace_static_url(match): def replace_static_url(match):
log.warning(match.groupdict()) original = match.group(0)
quote = match.group('quote') quote = match.group('quote')
rest = match.group('rest') rest = match.group('rest')
# course_namespace is not None, then use studio style urls # course_namespace is not None, then use studio style urls
if course_namespace is not None and not isinstance(modulestore(), XMLModuleStore): if course_namespace is not None and not isinstance(modulestore(), XMLModuleStore):
url = StaticContent.convert_legacy_static_url(rest, course_namespace) url = StaticContent.convert_legacy_static_url(rest, course_namespace)
log.warning("From modulestore: %s", url)
# If we're in debug mode, and the file as requested exists, then don't change the links # If we're in debug mode, and the file as requested exists, then don't change the links
elif (settings.DEBUG and finders.find(rest, True)): elif (settings.DEBUG and finders.find(rest, True)):
url = match.group('prefix') + rest return original
log.warning("From finder: %s", url)
# Otherwise, look the file up in staticfiles_storage # Otherwise, look the file up in staticfiles_storage
else: else:
path = data_directory + '/' + rest path = data_directory + '/' + rest
try: try:
url = staticfiles_storage.url(path) url = staticfiles_storage.url(path)
log.warning("From staticfiles_storage: %s", url)
# And if that fails, return the path unmodified # And if that fails, return the path unmodified
except Exception as err: except Exception as err:
log.warning("staticfiles_storage couldn't find path {0}: {1}".format( log.warning("staticfiles_storage couldn't find path {0}: {1}".format(
path, str(err))) path, str(err)))
url = path return original
log.warning("Fallback: %s", url)
log.warning("".join([quote, url, quote]))
return "".join([quote, url, quote]) return "".join([quote, url, quote])
return re.sub( return re.sub(
......
from nose.tools import assert_equals from nose.tools import assert_equals
from static_replace import replace_static_urls, replace_course_urls from static_replace import replace_static_urls, replace_course_urls
from mock import patch, Mock
from xmodule.modulestore import Location
from xmodule.modulestore.mongo import MongoModuleStore
DATA_DIRECTORY = 'data_dir' DATA_DIRECTORY = 'data_dir'
COURSE_ID = 'org/course/run' COURSE_ID = 'org/course/run'
NAMESPACE = Location('org', 'course', 'run', None, None)
def test_multi_replace(): def test_multi_replace():
...@@ -17,4 +21,36 @@ def test_multi_replace(): ...@@ -17,4 +21,36 @@ def test_multi_replace():
replace_course_urls(course_source, COURSE_ID), replace_course_urls(course_source, COURSE_ID),
replace_course_urls(replace_course_urls(course_source, COURSE_ID), COURSE_ID) replace_course_urls(replace_course_urls(course_source, COURSE_ID), COURSE_ID)
) )
assert False
@patch('static_replace.finders')
@patch('static_replace.settings')
def test_debug_no_modify(mock_settings, mock_finders):
mock_settings.DEBUG = True
mock_finders.find.return_value = True
static_source = '"/static/file.png"'
assert_equals(static_source, replace_static_urls(static_source, DATA_DIRECTORY))
mock_finders.find.assert_called_once_with('file.png', True)
@patch('static_replace.StaticContent')
@patch('static_replace.modulestore')
def test_mongo_filestore(mock_modulestore, mock_static_content):
mock_modulestore.return_value = Mock(MongoModuleStore)
mock_static_content.convert_legacy_static_url.return_value = "c4x://mock_url"
static_source = '"/static/file.png"'
# No namespace => no change to path
assert_equals(static_source, replace_static_urls(static_source, DATA_DIRECTORY))
# Namespace => content url
assert_equals(
'"' + mock_static_content.convert_legacy_static_url.return_value + '"',
replace_static_urls(static_source, DATA_DIRECTORY, NAMESPACE)
)
mock_static_content.convert_legacy_static_url.assert_called_once_with('file.png', NAMESPACE)
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