def dispatch(self, request, *args, **kwargs): if self.allowed_roles is None: raise ImproperlyConfigured( _("'LTIRoleRestrictionMixin' requires " "'allowed_roles' attribute to be set.")) if is_allowed(request, self.allowed_roles, self.raise_exception): return super().dispatch(request, *args, **kwargs) return redirect(self.redirect_url)
def _wrapped_view(request, *args, **kwargs): if is_allowed(request, allowed_roles, raise_exception): return view_func(request, *args, **kwargs) return redirect(redirect_url)
def test_is_allowed_failure_one_role(self): request = MagicMock(LTI={"roles": []}) allowed_roles = "admin" user_is_allowed = is_allowed(request, allowed_roles, False) self.assertFalse(user_is_allowed)
def test_is_allowed_failure(self): request = MagicMock(LTI={"roles": []}) allowed_roles = ["admin", "student"] user_is_allowed = is_allowed(request, allowed_roles, False) self.assertFalse(user_is_allowed)
def test_is_allowed_success_one_role(self): request = MagicMock(LTI={"roles": ["admin"]}) allowed_roles = "admin" user_is_allowed = is_allowed(request, allowed_roles, False) self.assertTrue(user_is_allowed)
def test_is_allowed_success(self): request = MagicMock(LTI={"roles": ["admin"]}) allowed_roles = ["admin", "student"] user_is_allowed = is_allowed(request, allowed_roles, False) self.assertTrue(user_is_allowed)