flask:单元测试时,request.authorization 总是 None

flask: When unittesting, request.authorization is always None

我希望有人能帮助我。

我必须在烧瓶 api 中用 Python 的单元测试编写单元测试。我有一个登录路由,当通过带有 React 前端的应用程序访问它时,它工作得很好,但每当我尝试从测试中 post 时,request.authorization 是 None... 它驱动我疯了

我浏览了整个互联网并尝试了很多不同的方法,但无论我做什么,request.authorization 在进行测试时总是 None

测试:

import unittest
import base64

from backend.peace_api import app


class TestLogin(unittest.TestCase):

    # Assert login() with correct authentication
    def test_login(self):
        with app.app_context():
            tester = app.test_client(self)

            auth = 'seo@hotmail.com:password'

            authheader = base64.b64encode(bytes(auth, 'UTF-8'))
            headers = {"HTTP_AUTHORIZATION": "Bearer " + str(authheader), "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"}

            response = tester.post('/api/login/', headers=dict(headers))
            print(response.json)

            self.assertEqual(response.status_code, 200)


if __name__ == '__main__':
    unittest.main()

路线:

import jwt
import datetime
from flask import Blueprint, request, jsonify
from backend.peace_api import database, secret_key
from backend.peace_api.flat.models.flat import Flat


login_blueprint = Blueprint("login", __name__)


@login_blueprint.route("/", methods=["POST"])
def login():
    auth = request.authorization # This here is always None
    print("Hello World")
    print(request)
    print(request.authorization)
    if auth is None:
        return jsonify({"success": False}, 401)

    email = auth.username
    password = auth.password

    if email is None or email is None or password is None:
        return jsonify({"success": False}, 500)

    mongo_flat = database.flats.find_one({"email": email})
    if mongo_flat is not None:
        flat = Flat(
            mongo_flat["_id"],
            mongo_flat["name"],
            mongo_flat["email"],
            mongo_flat["password"],
            mongo_flat["tasks"],
            mongo_flat["members"],
        )

        if password == flat.password and email == flat.email:
            token = jwt.encode(
                {
                    "id": str(flat.id),
                    "exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=30),
                },
                secret_key,
            )
            return jsonify({"token": token.decode("UTF-8")})

        else:
            return jsonify({"success": False}, 401)
    else:
        return jsonify({"success": False}, 401)

打印的消息:

Testing started at 19:15 ...
Launching unittests with arguments python -m unittest test_login.TestLogin in [...]\tests
Hello World
<Request 'http://localhost/api/login/' [POST]>
None


Ran 1 test in 0.017s

OK
[{'success': False}, 401]

老实说,我不知道我应该做什么...谢谢你的帮助

因此您的设置存在一些问题,导致 header 未发送或发送但格式错误。

  1. header 的名称是 "Authorization",而不是 "HTTP_AUTHORIZATION"。
  2. 授权 header 的凭据值需要根据 the spec.
  3. 进行 base64 编码
  4. 默认 authorization middleware for Werkzeug only supports Basic auth,因此您的 Bearer 令牌将不起作用,除非您使用的扩展程序为 Werkzeug 添加 Bearer 支持(如果不了解您的设置,很难知道那里发生了什么)。

这是一个非常简化的 Flask 应用程序,它演示了一个可以正常工作的测试客户端 Authorization header:

import flask
import base64

app = flask.Flask("app")

@app.route("/")
def test():
    print(flask.request.authorization)
    return "Worked"

with app.test_client() as c:
    c.get("/", headers={"Authorization": "Basic {}".format(base64.b64encode(b"useo@hotmail.com:pass").decode("utf8"))})

打印:

{'password': 'pass', 'username': 'seo@hotmail.com'}
<Response streamed [200 OK]>

这里问了一个类似的问题:

Flask werkzeug request.authorization is none but Authorization headers present