Merge pull request #8299 from edx/will/ECOM-1678
ECOM-1678: Fix broken footer image URLs when using a CDN.
This commit is contained in:
@@ -277,7 +277,7 @@ def _absolute_url(is_secure, url_path):
|
||||
|
||||
|
||||
def _absolute_url_staticfile(is_secure, name):
|
||||
"""Construct an absolute URL back to a static resource on the site.
|
||||
"""Construct an absolute URL to a static resource on the site.
|
||||
|
||||
Arguments:
|
||||
is_secure (bool): If true, use HTTPS as the protocol.
|
||||
@@ -288,4 +288,13 @@ def _absolute_url_staticfile(is_secure, name):
|
||||
|
||||
"""
|
||||
url_path = staticfiles_storage.url(name)
|
||||
|
||||
# In production, the static files URL will be an absolute
|
||||
# URL pointing to a CDN. If this happens, we can just
|
||||
# return the URL.
|
||||
if urlparse.urlparse(url_path).netloc:
|
||||
return url_path
|
||||
|
||||
# For local development, the returned URL will be relative,
|
||||
# so we need to make it absolute.
|
||||
return _absolute_url(is_secure, url_path)
|
||||
|
||||
@@ -102,6 +102,26 @@ class TestFooter(TestCase):
|
||||
# Copyright
|
||||
self.assertIn("copyright", json_data)
|
||||
|
||||
def test_absolute_urls_with_cdn(self):
|
||||
self._set_feature_flag(True)
|
||||
|
||||
# Ordinarily, we'd use `override_settings()` to override STATIC_URL,
|
||||
# which is what the staticfiles storage backend is using to construct the URL.
|
||||
# Unfortunately, other parts of the system are caching this value on module
|
||||
# load, which can cause other tests to fail. To ensure that this change
|
||||
# doesn't affect other tests, we patch the `url()` method directly instead.
|
||||
cdn_url = "http://cdn.example.com/static/image.png"
|
||||
with mock.patch('branding.api.staticfiles_storage.url', return_value=cdn_url):
|
||||
resp = self._get_footer()
|
||||
|
||||
self.assertEqual(resp.status_code, 200)
|
||||
json_data = json.loads(resp.content)
|
||||
|
||||
self.assertEqual(json_data["logo_image"], cdn_url)
|
||||
|
||||
for link in json_data["mobile_links"]:
|
||||
self.assertEqual(link["url"], cdn_url)
|
||||
|
||||
@ddt.data(
|
||||
("en", "registered trademarks"),
|
||||
("eo", u"régïstéréd trädémärks"), # Dummy language string
|
||||
|
||||
Reference in New Issue
Block a user