The logic used to has a special case for edge in the hostname which didn't really make sense. So instead we just check to see if the given url starts with http and if it does we return it directly. If it doesn't, then we try to convert it to a valid url and return that. If that fails we return . Add a new custom metric `unresolved_marketing_link` to track when we run into this scenario.
204 lines
8.9 KiB
Python
204 lines
8.9 KiB
Python
|
|
|
|
import unittest
|
|
|
|
import ddt
|
|
from django.conf import settings
|
|
from django.http import HttpResponse
|
|
from django.test import TestCase
|
|
from django.test.client import RequestFactory
|
|
from django.test.utils import override_settings
|
|
from django.urls import reverse
|
|
from edx_django_utils.cache import RequestCache
|
|
from mock import Mock, patch
|
|
|
|
from edxmako import LOOKUP, add_lookup
|
|
from edxmako.request_context import get_template_request_context
|
|
from edxmako.shortcuts import is_any_marketing_link_set, is_marketing_link_set, marketing_link, render_to_string
|
|
from student.tests.factories import UserFactory
|
|
from util.testing import UrlResetMixin
|
|
|
|
|
|
@ddt.ddt
|
|
class ShortcutsTests(UrlResetMixin, TestCase):
|
|
"""
|
|
Test the edxmako shortcuts file
|
|
"""
|
|
@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
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': True}):
|
|
expected_link = 'https://dummy-root/about-us'
|
|
link = marketing_link('ABOUT')
|
|
self.assertEqual(link, expected_link)
|
|
# test marketing site off
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': False}):
|
|
expected_link = reverse(self._get_test_url_name())
|
|
link = marketing_link('ABOUT')
|
|
self.assertEqual(link, expected_link)
|
|
|
|
@override_settings(MKTG_URLS={'ROOT': 'https://dummy-root', 'ABOUT': '/about-us'})
|
|
def test_is_marketing_link_set(self):
|
|
with override_settings(MKTG_URL_LINK_MAP={'ABOUT': self._get_test_url_name()}):
|
|
# test marketing site on
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': True}):
|
|
self.assertTrue(is_marketing_link_set('ABOUT'))
|
|
self.assertFalse(is_marketing_link_set('NOT_CONFIGURED'))
|
|
# test marketing site off
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': False}):
|
|
self.assertTrue(is_marketing_link_set('ABOUT'))
|
|
self.assertFalse(is_marketing_link_set('NOT_CONFIGURED'))
|
|
|
|
@override_settings(MKTG_URLS={'ROOT': 'https://dummy-root', 'ABOUT': '/about-us'})
|
|
def test_is_any_marketing_link_set(self):
|
|
with override_settings(MKTG_URL_LINK_MAP={'ABOUT': self._get_test_url_name()}):
|
|
# test marketing site on
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': True}):
|
|
self.assertTrue(is_any_marketing_link_set(['ABOUT']))
|
|
self.assertTrue(is_any_marketing_link_set(['ABOUT', 'NOT_CONFIGURED']))
|
|
self.assertFalse(is_any_marketing_link_set(['NOT_CONFIGURED']))
|
|
# test marketing site off
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': False}):
|
|
self.assertTrue(is_any_marketing_link_set(['ABOUT']))
|
|
self.assertTrue(is_any_marketing_link_set(['ABOUT', 'NOT_CONFIGURED']))
|
|
self.assertFalse(is_any_marketing_link_set(['NOT_CONFIGURED']))
|
|
|
|
def _get_test_url_name(self):
|
|
if settings.ROOT_URLCONF == 'lms.urls':
|
|
# return any lms url name
|
|
return 'dashboard'
|
|
else:
|
|
# return any cms url name
|
|
return 'organizations'
|
|
|
|
@override_settings(MKTG_URLS={'ROOT': 'https://dummy-root', 'TOS': '/tos'})
|
|
@override_settings(MKTG_URL_OVERRIDES={'TOS': 'https://edx.org'})
|
|
def test_override_marketing_link_valid(self):
|
|
expected_link = 'https://edx.org'
|
|
# test marketing site on
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': True}):
|
|
link = marketing_link('TOS')
|
|
self.assertEqual(link, expected_link)
|
|
# test marketing site off
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': False}):
|
|
link = marketing_link('TOS')
|
|
self.assertEqual(link, expected_link)
|
|
|
|
@override_settings(MKTG_URLS={'ROOT': 'https://dummy-root', 'TOS': '/tos'})
|
|
@override_settings(MKTG_URL_OVERRIDES={'TOS': '123456'})
|
|
def test_override_marketing_link_invalid(self):
|
|
expected_link = '#'
|
|
# test marketing site on
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': True}):
|
|
link = marketing_link('TOS')
|
|
self.assertEqual(link, expected_link)
|
|
# test marketing site off
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': False}):
|
|
link = marketing_link('TOS')
|
|
self.assertEqual(link, expected_link)
|
|
|
|
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
|
|
def test_link_map_url_reverse(self):
|
|
url_link_map = {
|
|
'ABOUT': 'dashboard',
|
|
'BAD_URL': 'foobarbaz',
|
|
}
|
|
|
|
with patch.dict('django.conf.settings.FEATURES', {'ENABLE_MKTG_SITE': False}):
|
|
with override_settings(MKTG_URL_LINK_MAP=url_link_map):
|
|
link = marketing_link('ABOUT')
|
|
assert link == '/dashboard'
|
|
|
|
link = marketing_link('BAD_URL')
|
|
assert link == '#'
|
|
|
|
|
|
class AddLookupTests(TestCase):
|
|
"""
|
|
Test the `add_lookup` function.
|
|
"""
|
|
@patch('edxmako.LOOKUP', {})
|
|
def test_with_package(self):
|
|
add_lookup('test', 'management', __name__)
|
|
dirs = LOOKUP['test'].directories
|
|
self.assertEqual(len(dirs), 1)
|
|
self.assertTrue(dirs[0].endswith('management'))
|
|
|
|
|
|
class MakoRequestContextTest(TestCase):
|
|
"""
|
|
Test MakoMiddleware.
|
|
"""
|
|
|
|
def setUp(self):
|
|
super(MakoRequestContextTest, self).setUp()
|
|
self.user = UserFactory.create()
|
|
self.url = "/"
|
|
self.request = RequestFactory().get(self.url)
|
|
self.request.user = self.user
|
|
self.response = Mock(spec=HttpResponse)
|
|
|
|
self.addCleanup(RequestCache.clear_all_namespaces)
|
|
|
|
def test_with_current_request(self):
|
|
"""
|
|
Test that if get_current_request returns a request, then get_template_request_context
|
|
returns a RequestContext.
|
|
"""
|
|
|
|
with patch('edxmako.request_context.get_current_request', return_value=self.request):
|
|
# requestcontext should not be None.
|
|
self.assertIsNotNone(get_template_request_context())
|
|
|
|
def test_without_current_request(self):
|
|
"""
|
|
Test that if get_current_request returns None, then get_template_request_context
|
|
returns None.
|
|
"""
|
|
with patch('edxmako.request_context.get_current_request', return_value=None):
|
|
# requestcontext should be None.
|
|
self.assertIsNone(get_template_request_context())
|
|
|
|
def test_request_context_caching(self):
|
|
"""
|
|
Test that the RequestContext is cached in the RequestCache.
|
|
"""
|
|
with patch('edxmako.request_context.get_current_request', return_value=None):
|
|
# requestcontext should be None, because the cache isn't filled
|
|
self.assertIsNone(get_template_request_context())
|
|
|
|
with patch('edxmako.request_context.get_current_request', return_value=self.request):
|
|
# requestcontext should not be None, and should fill the cache
|
|
self.assertIsNotNone(get_template_request_context())
|
|
|
|
mock_get_current_request = Mock()
|
|
with patch('edxmako.request_context.get_current_request'):
|
|
with patch('edxmako.request_context.RequestContext.__init__') as mock_context_init:
|
|
# requestcontext should not be None, because the cache is filled
|
|
self.assertIsNotNone(get_template_request_context())
|
|
mock_context_init.assert_not_called()
|
|
mock_get_current_request.assert_not_called()
|
|
|
|
RequestCache.clear_all_namespaces()
|
|
|
|
with patch('edxmako.request_context.get_current_request', return_value=None):
|
|
# requestcontext should be None, because the cache isn't filled
|
|
self.assertIsNone(get_template_request_context())
|
|
|
|
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
|
|
def test_render_to_string_when_no_global_context_lms(self):
|
|
"""
|
|
Test render_to_string() when makomiddleware has not initialized
|
|
the threadlocal REQUEST_CONTEXT.context. This is meant to run in LMS.
|
|
"""
|
|
self.assertIn("this module is temporarily unavailable", render_to_string("courseware/error-message.html", None))
|
|
|
|
@unittest.skipUnless(settings.ROOT_URLCONF == 'cms.urls', 'Test only valid in cms')
|
|
def test_render_to_string_when_no_global_context_cms(self):
|
|
"""
|
|
Test render_to_string() when makomiddleware has not initialized
|
|
the threadlocal REQUEST_CONTEXT.context. This is meant to run in CMS.
|
|
"""
|
|
self.assertIn("We're having trouble rendering your component", render_to_string("html_error.html", None))
|