Commit 70c6ce8d by Will Daly

ECOM-1678: Fix broken footer image URLs when using a CDN.

parent a7c934c1
...@@ -277,7 +277,7 @@ def _absolute_url(is_secure, url_path): ...@@ -277,7 +277,7 @@ def _absolute_url(is_secure, url_path):
def _absolute_url_staticfile(is_secure, name): 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: Arguments:
is_secure (bool): If true, use HTTPS as the protocol. is_secure (bool): If true, use HTTPS as the protocol.
...@@ -288,4 +288,13 @@ def _absolute_url_staticfile(is_secure, name): ...@@ -288,4 +288,13 @@ def _absolute_url_staticfile(is_secure, name):
""" """
url_path = staticfiles_storage.url(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) return _absolute_url(is_secure, url_path)
...@@ -102,6 +102,26 @@ class TestFooter(TestCase): ...@@ -102,6 +102,26 @@ class TestFooter(TestCase):
# Copyright # Copyright
self.assertIn("copyright", json_data) 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( @ddt.data(
("en", "registered trademarks"), ("en", "registered trademarks"),
("eo", u"régïstéréd trädémärks"), # Dummy language string ("eo", u"régïstéréd trädémärks"), # Dummy language string
......
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