def test_call_compress(self, m_aiohttp_response_class_init):
        JsonResponse({}, compress=True)

        m_aiohttp_response_class_init.assert_called_once_with(
            body=gzip.compress(b'{}', compresslevel=5),
            status=200,
            headers={'Content-Encoding': 'gzip'},
            content_type='application/json')
    def test_call_custom_content_type(self, m_aiohttp_response_class_init):
        JsonResponse({'abc': '123'}, content_type='text/x-json; charset=utf-8')

        m_aiohttp_response_class_init.assert_called_once_with(
            body=b'{"abc":"123"}',
            status=200,
            headers={},
            content_type='text/x-json; charset=utf-8')
    def test_call_custom_status(self, m_aiohttp_response_class_init):
        JsonResponse({'error': 'validation_error'}, status=400)

        m_aiohttp_response_class_init.assert_called_once_with(
            body=b'{"error":"validation_error"}',
            status=400,
            headers={},
            content_type='application/json')
    def test_default_params_call_list(self, m_aiohttp_response_class_init):
        JsonResponse([])

        m_aiohttp_response_class_init.assert_called_once_with(
            body=b'[]',
            status=200,
            headers={},
            content_type='application/json')
Example #5
0
def verify_token():
    # 检测参数 
    if not s_check.check_values("token"):
        return jsonify(s_json.make_parameter_miss()) 
    # 获取token
    token = request.values.get("token")

    # 验证token
    try:
        data = User.verify_token(token)
    except TokenTimeOutException:
        return jsonify(s_json.make_fail(message="token timeout!"))
    else:
        user_id = data.get("id")
        if user_id:
            user = User.query.get(user_id)
            if user:    # 验证成功
                return jsonify(s_json.make_success(data=user.to_dict(has_token=False)))
    return jsonify(s_json.make_fail())
Example #6
0
def login():
    # 检测参数
    if not s_check.check_values("username", "password"):
        return(jsonify(s_json.make_parameter_miss()))

    values = request.values
    current_app.logger.debug(values)
    
    # 获取用户
    user = User.query.filter_by(username=values.get("username")).first()
    if user:
        ret = user.verify_password(values.get("password"))
        if ret: # 成功
            return jsonify(s_json.make_success(data=user.to_dict()))
        else:
            current_app.logger.debug("{} password error!".format(user))
            return jsonify(s_json.make_fail(message="password error!"))
    else:
        current_app.logger.debug("user not found!")

    return jsonify(s_json.make_fail())
Example #7
0
def logout():
    return jsonify(s_json.make_success())