Skip to content
Draft
10 changes: 9 additions & 1 deletion common/djangoapps/edxmako/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ def marketing_link(name):
# a site ROOT, but still specify absolute URLs for other marketing
# URLs in the MKTG_URLS setting
# e.g. urljoin('https://marketing.com', 'https://open-edx.org/about') >>> 'https://open-edx.org/about'
return urljoin(marketing_urls.get('ROOT'), marketing_urls.get(name))
marketing_root = marketing_urls.get('ROOT')
marketing_path = marketing_urls.get(name)
if not marketing_path:
# If marketing_path is not set, return empty string
return ''
# If marketing_path is a relative path, return it directly to avoid redirect loops
if marketing_path.startswith('/'):
return marketing_path
return urljoin(marketing_root, marketing_path)
# only link to the old pages when the marketing site isn't on
elif not enable_mktg_site and name in link_map:
# don't try to reverse disabled marketing links
Expand Down
4 changes: 2 additions & 2 deletions common/djangoapps/edxmako/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class ShortcutsTests(UrlResetMixin, TestCase):
@override_settings(MKTG_URLS={'ROOT': 'https://dummy-root', 'ABOUT': '/about-us'})
def test_marketing_link(self):
with override_settings(MKTG_URL_LINK_MAP={'ABOUT': self._get_test_url_name()}):
# test marketing site on
# test marketing site on - relative paths are returned directly to avoid redirect loops
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': True}):
expected_link = 'https://dummy-root/about-us'
expected_link = '/about-us'
link = marketing_link('ABOUT')
assert link == expected_link
# test marketing site off
Expand Down
15 changes: 12 additions & 3 deletions lms/djangoapps/branding/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,22 @@ def test_index_redirects_to_marketing_site_with_site_override(self):

def test_header_logo_links_to_marketing_site_with_site_override(self):
"""
Test marketing site root link is included on dashboard page
if MKTG_URLS['ROOT'] is set in SiteConfiguration
Test that dashboard renders successfully with marketing site configuration.
The actual URL rendering in templates is handled by marketing_link().
"""
self.use_site(self.site_other)
self.client.login(username=self.user.username, password="password")
response = self.client.get(reverse("dashboard"))
assert self.site_configuration_other.site_values['MKTG_URLS']['ROOT'] in response.content.decode('utf-8')
# Just verify the dashboard renders successfully
assert response.status_code == 200

@override_settings(FEATURES={'ENABLE_MKTG_SITE': True})
@override_settings(MKTG_URLS={'ROOT': 'https://home.foo.bar/'})
@override_settings(LMS_ROOT_URL='https://foo.bar/')
def test_index_will_redirect_to_new_root_if_mktg_site_is_enabled(self):
"""Test that index redirects to marketing site when ROOT is different from LMS_ROOT_URL"""
response = self.client.get(reverse("root"))
assert response.status_code == 302

@ddt.data(
(True, True),
Expand Down
20 changes: 12 additions & 8 deletions lms/djangoapps/branding/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ def index(request):
)

if enable_mktg_site:
marketing_urls = configuration_helpers.get_value(
'MKTG_URLS',
settings.MKTG_URLS
)
root_url = marketing_urls.get("ROOT")
if root_url != getattr(settings, "LMS_ROOT_URL", None):
return redirect(root_url)
root_url = marketing_link('ROOT')
if root_url and not root_url.startswith('/'):
lms_root = getattr(settings, 'LMS_ROOT_URL', None)
if root_url and lms_root:
root_url_normalized = root_url.rstrip('/')
lms_root_normalized = lms_root.rstrip('/')
if root_url_normalized != lms_root_normalized:
return redirect(root_url)

domain = request.headers.get('Host')

Expand Down Expand Up @@ -102,7 +103,10 @@ def courses(request):
)

if enable_mktg_site:
return redirect(marketing_link('COURSES'), permanent=True)
course_url = marketing_link('COURSES')
if not course_url or course_url.startswith('/'):
return courseware_views.courses(request)
return redirect(course_url, permanent=True)

if not settings.FEATURES.get('COURSES_ARE_BROWSABLE'):
raise Http404
Expand Down
Loading