Unverified Commit 03e496bd by Andy Armstrong Committed by GitHub

Merge pull request #16370 from edx/andya/waffle-fix

Allow Waffle flags to work when outside a request context
parents c7ed394f c55dbb2c
"""
Unit tests for course tools.
"""
import crum
import datetime import datetime
from mock import patch from mock import patch
...@@ -59,6 +64,7 @@ class VerifiedUpgradeToolTest(SharedModuleStoreTestCase): ...@@ -59,6 +64,7 @@ class VerifiedUpgradeToolTest(SharedModuleStoreTestCase):
) )
self.request = RequestFactory().request() self.request = RequestFactory().request()
self.request.user = self.enrollment.user self.request.user = self.enrollment.user
crum.set_current_request(self.request)
def test_tool_visible(self): def test_tool_visible(self):
self.assertTrue(VerifiedUpgradeTool().is_enabled(self.request, self.course.id)) self.assertTrue(VerifiedUpgradeTool().is_enabled(self.request, self.course.id))
......
...@@ -45,6 +45,7 @@ To test WaffleSwitchNamespace, use the provided context managers. For example: ...@@ -45,6 +45,7 @@ To test WaffleSwitchNamespace, use the provided context managers. For example:
... ...
""" """
import crum
import logging import logging
from abc import ABCMeta from abc import ABCMeta
from contextlib import contextmanager from contextlib import contextmanager
...@@ -54,7 +55,6 @@ from opaque_keys.edx.keys import CourseKey ...@@ -54,7 +55,6 @@ from opaque_keys.edx.keys import CourseKey
from waffle import flag_is_active, switch_is_active from waffle import flag_is_active, switch_is_active
from request_cache import get_cache as get_request_cache from request_cache import get_cache as get_request_cache
from request_cache import get_request
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -256,7 +256,17 @@ class WaffleFlagNamespace(WaffleNamespace): ...@@ -256,7 +256,17 @@ class WaffleFlagNamespace(WaffleNamespace):
value = flag_undefined_default value = flag_undefined_default
if value is None: if value is None:
value = flag_is_active(get_request(), namespaced_flag_name) request = crum.get_current_request()
if request:
value = flag_is_active(request, namespaced_flag_name)
else:
log.warn(u"%sFlag '%s' accessed without a request", self.log_prefix, namespaced_flag_name)
# Return the default value if not in a request context.
# Note: this skips the cache as the value might be different
# in a normal request context. This case seems to occur when
# a page redirects to a 404. In this case, we'll just return
# the default value.
return bool(flag_undefined_default)
self._cached_flags[namespaced_flag_name] = value self._cached_flags[namespaced_flag_name] = value
return value return value
......
""" """
Tests for waffle utils features. Tests for waffle utils features.
""" """
import crum
import ddt import ddt
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory
from mock import patch from mock import patch
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from request_cache.middleware import RequestCache from request_cache.middleware import RequestCache
...@@ -27,6 +29,12 @@ class TestCourseWaffleFlag(TestCase): ...@@ -27,6 +29,12 @@ class TestCourseWaffleFlag(TestCase):
TEST_NAMESPACE = WaffleFlagNamespace(NAMESPACE_NAME) TEST_NAMESPACE = WaffleFlagNamespace(NAMESPACE_NAME)
TEST_COURSE_FLAG = CourseWaffleFlag(TEST_NAMESPACE, FLAG_NAME) TEST_COURSE_FLAG = CourseWaffleFlag(TEST_NAMESPACE, FLAG_NAME)
def setUp(self):
super(TestCourseWaffleFlag, self).setUp()
request = RequestFactory().request()
crum.set_current_request(request)
RequestCache.clear_request_cache()
@ddt.data( @ddt.data(
{'course_override': WaffleFlagCourseOverrideModel.ALL_CHOICES.on, 'waffle_enabled': False, 'result': True}, {'course_override': WaffleFlagCourseOverrideModel.ALL_CHOICES.on, 'waffle_enabled': False, 'result': True},
{'course_override': WaffleFlagCourseOverrideModel.ALL_CHOICES.off, 'waffle_enabled': True, 'result': False}, {'course_override': WaffleFlagCourseOverrideModel.ALL_CHOICES.off, 'waffle_enabled': True, 'result': False},
...@@ -38,8 +46,6 @@ class TestCourseWaffleFlag(TestCase): ...@@ -38,8 +46,6 @@ class TestCourseWaffleFlag(TestCase):
Tests various combinations of a flag being set in waffle and overridden Tests various combinations of a flag being set in waffle and overridden
for a course. for a course.
""" """
RequestCache.clear_request_cache()
with patch.object(WaffleFlagCourseOverrideModel, 'override_value', return_value=data['course_override']): with patch.object(WaffleFlagCourseOverrideModel, 'override_value', return_value=data['course_override']):
with override_flag(self.NAMESPACED_FLAG_NAME, active=data['waffle_enabled']): with override_flag(self.NAMESPACED_FLAG_NAME, active=data['waffle_enabled']):
# check twice to test that the result is properly cached # check twice to test that the result is properly cached
...@@ -70,8 +76,6 @@ class TestCourseWaffleFlag(TestCase): ...@@ -70,8 +76,6 @@ class TestCourseWaffleFlag(TestCase):
""" """
Test flag with various defaults provided for undefined waffle flags. Test flag with various defaults provided for undefined waffle flags.
""" """
RequestCache.clear_request_cache()
test_course_flag = CourseWaffleFlag( test_course_flag = CourseWaffleFlag(
self.TEST_NAMESPACE, self.TEST_NAMESPACE,
self.FLAG_NAME, self.FLAG_NAME,
...@@ -91,3 +95,20 @@ class TestCourseWaffleFlag(TestCase): ...@@ -91,3 +95,20 @@ class TestCourseWaffleFlag(TestCase):
self.NAMESPACED_FLAG_NAME, self.NAMESPACED_FLAG_NAME,
self.TEST_COURSE_KEY self.TEST_COURSE_KEY
) )
@ddt.data(
{'flag_undefined_default': None, 'result': False},
{'flag_undefined_default': False, 'result': False},
{'flag_undefined_default': True, 'result': True},
)
def test_without_request(self, data):
"""
Test the flag behavior when outside a request context.
"""
crum.set_current_request(None)
test_course_flag = CourseWaffleFlag(
self.TEST_NAMESPACE,
self.FLAG_NAME,
flag_undefined_default=data['flag_undefined_default']
)
self.assertEqual(test_course_flag.is_enabled(self.TEST_COURSE_KEY), data['result'])
""" """
Tests for waffle utils test utilities. Tests for waffle utils test utilities.
""" """
import crum
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from request_cache.middleware import RequestCache from request_cache.middleware import RequestCache
...@@ -25,6 +29,8 @@ class OverrideWaffleFlagTests(TestCase): ...@@ -25,6 +29,8 @@ class OverrideWaffleFlagTests(TestCase):
def setUp(self): def setUp(self):
super(OverrideWaffleFlagTests, self).setUp() super(OverrideWaffleFlagTests, self).setUp()
request = RequestFactory().request()
crum.set_current_request(request)
RequestCache.clear_request_cache() RequestCache.clear_request_cache()
@override_waffle_flag(TEST_COURSE_FLAG, True) @override_waffle_flag(TEST_COURSE_FLAG, True)
......
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