From 70c6ce8ddb364a50a18c579e5f6f804f177f87d7 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Mon, 1 Jun 2015 11:24:37 -0400 Subject: [PATCH] ECOM-1678: Fix broken footer image URLs when using a CDN. --- lms/djangoapps/branding/api.py | 11 ++++++++++- lms/djangoapps/branding/tests/test_views.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lms/djangoapps/branding/api.py b/lms/djangoapps/branding/api.py index 119f3aa06b..b040774678 100644 --- a/lms/djangoapps/branding/api.py +++ b/lms/djangoapps/branding/api.py @@ -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) diff --git a/lms/djangoapps/branding/tests/test_views.py b/lms/djangoapps/branding/tests/test_views.py index b11ab62518..41c7db22cb 100644 --- a/lms/djangoapps/branding/tests/test_views.py +++ b/lms/djangoapps/branding/tests/test_views.py @@ -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