【问题标题】:flask: When unittesting, request.authorization is always Noneflask:单元测试时,request.authorization 始终为 None
【发布时间】:2018-06-26 17:25:23
【问题描述】:

我希望有人可以帮助我。

我必须在烧瓶 api 中使用 Python 的 unittest 编写单元测试。我有一个登录路由,当通过带有 React 前端的应用程序访问它时,它工作得非常好,但是每当我尝试从测试中发布时,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]

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

【问题讨论】:

    标签: python flask python-unittest


    【解决方案1】:

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

    1. 标头的名称是“Authorization”,而不是“HTTP_AUTHORIZATION”。
    2. Authorization 标头的凭据值需要按照 the spec 进行 base64 编码。
    3. Werkzeug 的默认 authorization middleware 仅支持 Basic auth,因此您的 Bearer 令牌将不起作用,除非您使用的扩展程序将 Bearer 支持添加到 Werkzeug(如果不了解有关您的设置的更多信息,很难知道那里发生了什么)。

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

    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

    【讨论】:

      猜你喜欢
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多