title: flask: When unittesting, request.authorization is always None
body: <p>I hope that somebody can help me.</p>
  
  <p>I have to write a unit test with unittest of Python in a flask api. I have a login route that works perfectly fine when accessing it through the app with a React frontend but whenever I tried to post from the test, the request.authorization is None... It drives me crazy</p>
  
  <p>I looked all over the internet and tried a lot of different approach but whatever I do, request.authorization is always None when doing a test</p>
  
  <p>Testing :</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>Route : </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Printed message :</p>
  
  <pre><code>Testing started at 19:15 ...
  Launching unittests with arguments python -m unittest test_login.TestLogin in [...]\tests
  Hello World
  &lt;Request 'http://localhost/api/login/' [POST]&gt;
  None
  
  
  Ran 1 test in 0.017s
  
  OK
  [{'success': False}, 401]
  </code></pre>
  
  <p>I have honestly no clue what I should do... Thanks for the help</p>