Fix error in toggle state endpoint in the absence of module_name

When module_name is None, the call to edx-django-utils'
get_code_owner_from_module crashes. So we avoid making that call when
the module_name is None, which sometimes happens (for good reasons or
not, but it's valid behaviour).
This commit is contained in:
Régis Behmo
2020-10-29 20:08:10 +01:00
parent 373ee5f320
commit 312f0cd749
2 changed files with 18 additions and 5 deletions

View File

@@ -2,14 +2,16 @@
Tests for waffle utils views.
"""
from django.test import TestCase
from edx_django_utils.monitoring.code_owner import utils as code_owner_utils
from mock import patch
from rest_framework.test import APIRequestFactory
from waffle.testutils import override_switch
from student.tests.factories import UserFactory
from .. import WaffleFlag, WaffleFlagNamespace, WaffleSwitch, WaffleSwitchNamespace
from ..views import ToggleStateView
from ..testutils import override_waffle_flag
from ..views import ToggleStateView
TEST_WAFFLE_FLAG_NAMESPACE = WaffleFlagNamespace('test')
TEST_WAFFLE_FLAG = WaffleFlag(TEST_WAFFLE_FLAG_NAMESPACE, 'flag', __name__)
@@ -45,6 +47,17 @@ class ToggleStateViewTests(TestCase):
# This is no longer the first switch
#self.assertEqual(response.data['waffle_switches'][0]['name'], 'test.switch')
def test_code_owners_without_module_information(self):
# Create a waffle flag without any associated module_name
waffle_flag = WaffleFlag(TEST_WAFFLE_FLAG_NAMESPACE, "flag2", module_name=None)
with patch.object(code_owner_utils, "get_code_owner_mappings", return_value={}):
response = self._get_toggle_state_response(is_staff=True)
result = [
flag for flag in response.data["waffle_flags"] if flag["name"] == waffle_flag.name
][0]
self.assertNotIn("code_owner", result)
def _get_toggle_state_response(self, is_staff=True):
request = APIRequestFactory().get('/api/toggles/state/')
user = UserFactory()

View File

@@ -2,13 +2,13 @@
Views that we will use to view toggle state in edx-platform.
"""
from collections import OrderedDict
from django.conf import settings
from edx_django_utils.monitoring.code_owner.utils import get_code_owner_from_module, is_code_owner_mappings_configured
from django.conf import settings
from edx_django_utils.monitoring import get_code_owner_from_module
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
from edx_rest_framework_extensions.permissions import IsStaff
from rest_framework.authentication import SessionAuthentication
from rest_framework import permissions, views
from rest_framework.authentication import SessionAuthentication
from rest_framework.response import Response
from waffle.models import Flag, Switch
@@ -107,7 +107,7 @@ class ToggleStateView(views.APIView):
"""
toggle['class'] = toggle_instance.__class__.__name__
toggle['module'] = toggle_instance.module_name
if is_code_owner_mappings_configured():
if toggle_instance.module_name:
code_owner = get_code_owner_from_module(toggle_instance.module_name)
if code_owner:
toggle['code_owner'] = code_owner