From bb54ae04dccaa970f606e7d35bbdab968166d4c1 Mon Sep 17 00:00:00 2001 From: abram axel booth Date: Fri, 12 Jul 2024 11:21:33 -0400 Subject: [PATCH] fix: do flask stuff only when flask --- api/waffle/utils.py | 19 +++++++++++-------- tests/test_ember_osf_web.py | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/api/waffle/utils.py b/api/waffle/utils.py index 9b6236e9a2e..573afe93be5 100644 --- a/api/waffle/utils.py +++ b/api/waffle/utils.py @@ -2,7 +2,7 @@ from framework.auth.core import _get_current_user from osf import features -from flask import request +import flask class MockUser(object): @@ -13,18 +13,21 @@ def flag_is_active(request, flag_name): """ This function changes the typical flask request object so it can be used by django-waffle. Other modifications for django-waffle can be found in the __call__ method of OsfWebRenderer. - :param flask request object: - :return flask request object: + + :param request (flask or django): + :param flag name (string): + :return bool: """ - # Waffle does not enjoy NoneTypes as user values. - request.user = _get_current_user() or MockUser() - request.COOKIES = getattr(request, 'cookies', None) + if isinstance(request, flask.Request): + # Waffle does not enjoy NoneTypes as user values. + request.user = _get_current_user() or MockUser() + request.COOKIES = getattr(request, 'cookies', None) return waffle.flag_is_active(request, flag_name) def storage_i18n_flag_active(): - return flag_is_active(request, features.STORAGE_I18N) + return flag_is_active(flask.request, features.STORAGE_I18N) def storage_usage_flag_active(): - return flag_is_active(request, features.STORAGE_USAGE) + return flag_is_active(flask.request, features.STORAGE_USAGE) diff --git a/tests/test_ember_osf_web.py b/tests/test_ember_osf_web.py index 6a3f074968b..14cd1c50fdb 100644 --- a/tests/test_ember_osf_web.py +++ b/tests/test_ember_osf_web.py @@ -32,7 +32,7 @@ def test_dont_use_ember_app(self, mock_use_ember_app): assert not mock_use_ember_app.called @mock.patch('api.waffle.utils._get_current_user') - @mock.patch('website.ember_osf_web.decorators.waffle.flag_is_active') + @mock.patch('website.ember_osf_web.decorators.flag_is_active') @mock.patch('website.ember_osf_web.decorators.use_ember_app') def test_ember_flag_is_active_authenticated_user(self, mock_use_ember_app, mock_flag_is_active, mock__get_current_user): # mock over external module 'waflle.flag_is_active` not ours @@ -46,7 +46,7 @@ def test_ember_flag_is_active_authenticated_user(self, mock_use_ember_app, mock_ mock_use_ember_app.assert_called_with() @mock.patch('api.waffle.utils._get_current_user', return_value=None) - @mock.patch('website.ember_osf_web.decorators.waffle.flag_is_active') + @mock.patch('website.ember_osf_web.decorators.flag_is_active') @mock.patch('website.ember_osf_web.decorators.use_ember_app') def test_ember_flag_is_active_unauthenticated_user(self, mock_use_ember_app, mock_flag_is_active, mock__get_current_user): # mock over external module 'waflle.flag_is_active` not ours