From d9ccaa650af8a5455f3b159f89d6d486c7b48f0a Mon Sep 17 00:00:00 2001 From: Waheed Ahmed Date: Wed, 13 Aug 2014 20:00:40 +0500 Subject: [PATCH] Check REQUEST_CONTEXT.context exists before accessing it. LMS-11226 --- common/djangoapps/edxmako/shortcuts.py | 2 +- common/djangoapps/edxmako/template.py | 2 +- common/djangoapps/edxmako/tests.py | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/common/djangoapps/edxmako/shortcuts.py b/common/djangoapps/edxmako/shortcuts.py index 01b7cc0e55..487c2fbcaf 100644 --- a/common/djangoapps/edxmako/shortcuts.py +++ b/common/djangoapps/edxmako/shortcuts.py @@ -92,7 +92,7 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'): context_instance['marketing_link'] = marketing_link # In various testing contexts, there might not be a current request context. - if edxmako.middleware.REQUEST_CONTEXT.context is not None: + if getattr(edxmako.middleware.REQUEST_CONTEXT, "context", None): for d in edxmako.middleware.REQUEST_CONTEXT.context: context_dictionary.update(d) for d in context_instance: diff --git a/common/djangoapps/edxmako/template.py b/common/djangoapps/edxmako/template.py index 712d5184e1..677e88bcbe 100644 --- a/common/djangoapps/edxmako/template.py +++ b/common/djangoapps/edxmako/template.py @@ -48,7 +48,7 @@ class Template(MakoTemplate): context_dictionary = {} # In various testing contexts, there might not be a current request context. - if edxmako.middleware.REQUEST_CONTEXT.context is not None: + if getattr(edxmako.middleware.REQUEST_CONTEXT, "context", None): for d in edxmako.middleware.REQUEST_CONTEXT.context: context_dictionary.update(d) for d in context_instance: diff --git a/common/djangoapps/edxmako/tests.py b/common/djangoapps/edxmako/tests.py index b9aafa2d66..5bde841d53 100644 --- a/common/djangoapps/edxmako/tests.py +++ b/common/djangoapps/edxmako/tests.py @@ -1,5 +1,8 @@ from mock import patch, Mock +import unittest + +from django.conf import settings from django.http import HttpResponse from django.test import TestCase from django.test.utils import override_settings @@ -7,7 +10,7 @@ from django.test.client import RequestFactory from django.core.urlresolvers import reverse import edxmako.middleware from edxmako import add_lookup, LOOKUP -from edxmako.shortcuts import marketing_link +from edxmako.shortcuts import marketing_link, render_to_string from student.tests.factories import UserFactory from util.testing import UrlResetMixin @@ -71,6 +74,26 @@ class MakoMiddlewareTest(TestCase): # requestcontext should be None. self.assertIsNone(edxmako.middleware.REQUEST_CONTEXT.context) + @unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms') + @patch("edxmako.middleware.REQUEST_CONTEXT") + def test_render_to_string_when_no_global_context_lms(self, context_mock): + """ + Test render_to_string() when makomiddleware has not initialized + the threadlocal REQUEST_CONTEXT.context. This is meant to run in LMS. + """ + del context_mock.context + 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') + @patch("edxmako.middleware.REQUEST_CONTEXT") + def test_render_to_string_when_no_global_context_cms(self, context_mock): + """ + Test render_to_string() when makomiddleware has not initialized + the threadlocal REQUEST_CONTEXT.context. This is meant to run in CMS. + """ + del context_mock.context + self.assertIn("We're having trouble rendering your component", render_to_string("html_error.html", None)) + def mako_middleware_process_request(request): """