title: How to fake JWT Authorization outside of request context and get current identity?
body: <p>I'm creating a Flask app with JWT Authorization and trying to test services with PyTest.</p>
  
  <p>I successfully added tests to endpoints, but when I trying to add unit tests for certain function I can't access current user, because <code>flask_jwt_extended.get_current_user()</code> returns None.</p>
  
  <p>Here is simple example:</p>
  
  <pre><code>@api.route('/listings', methods=['POST'])
  @jwt_required
  def create_listing():
      payload = request.json
      listing = listing_svc.create(payload)
      return listing
  
  
  def create(payload):
      listing = ListingSchema().load(payload, db.session).data
  
  
  class ListingSchema(ModelSchema):
      id = field_for(Project, 'id', dump_only=True)
      creator_user_id = field_for(Project, 'creator_user_id')
      # ...
  
      @pre_load
      def set_creator_id(self, data):
          current_user = flask_jwt_extended.get_current_user()
          data['creator_user_id'] = current_user.id
  </code></pre>
  
  <p>It works when I authorize and send a request using app_context:</p>
  
  <pre><code>with client.application.app_context():
      rv = client.post('/listings',
          # ...
      )
  </code></pre>
  
  <p>But what I need is to test <code>create</code> function without sending a request to client. In this case <code>flask_jwt_extended.get_current_user()</code> returns None, so I think I should set request context some way before running this function.</p>
  
  <p>I tried to do this...</p>
  
  <pre><code>fake_payload = {}
  with client.application.test_request_context('/listings', headers={'Authorization': 'Bearer ' + access_token}):
      create(fake_payload)
  </code></pre>
  
  <p>but still getting <code>current_user</code> is <code>None</code></p>
  
  <p>This is how I get token:</p>
  
  <pre><code>def login(email, password):
      user = get_by_email(email)
      if user and check_password_hash(user.password, password):
          return access_token = flask_jwt_extended.create_access_token(identity=email)
  </code></pre>