def wrapper(*args, **kwars): token = request.headers.get("Authorization") role = Utils.request_role() try: if not token or token.split(" ")[0] != "Bearer": raise InvalidTokenError(token, "Token must be a bearer token", "Invalid token") token_decode = Auth.token_decode(token.split(" ")[1]) session_id = token_decode["token"] exists = Auth.session_exists(session_id, role) if not exists: raise InvalidTokenError( token, "User does not have an active session", "Invalid token") except InvalidTokenError as e: return Utils.response(status=e.status, message="Invalid token", data=e.errors) return f(*args, **kwars)
def test_check_fail_role(self): with patch('app.src.common.utils.request') as mock_request: mock_request.path = "/hola/*" role = Utils.request_role() self.assertNotIn(role, self.roles) self.assertIsNone(role)
def test_check_role_user(self): with patch("app.src.common.utils.request") as mock_request: mock_request.path = "/user/secure/*" role = Utils.request_role() self.assertIn(role, self.roles) self.assertEqual(role, self.roles[1])