title: flask_login - current_user
body: <p>I am using the <strong>flask_login</strong> extension in my flask app to login users. As you must be knowing, this extension has a variable that stores a <strong>current_user</strong>. The code is working perfectly, except when it comes to testing it.</p>
  
  <p>When I am testing the code (using <strong>unittest</strong>), I register a "test user" and log it in. But the <strong>current_user</strong> variable does not keep the user that logged in.</p>
  
  <p>Here is my app code; the part that adds in a category (<strong>current_user</strong> gets set when a user logs in) :</p>
  
  <pre><code>def post(self):
      # Get the access token from the header
      auth_header = request.headers.get('Authorization')
      access_token = auth_header.split(" ")[1]
  
      if access_token:
          # Attempt to decode the token and get the User ID
          user_id = User.decode_token(access_token)
          if not isinstance(user_id, str):
              # Go ahead and handle the request, the user is authenticated
              data = request.get_json()
              if data['name']:
                  category = Category(name = data['name'], user_id = user_id)
  
                  db.session.add(category)
                  db.session.commit()
  
                  response = {'id' : category.id,
                      'category_name' : category.name,
                      'created_by' : current_user.first_name
                  }
  
                  return response
  
          else:
              # user is not legit, so the payload is an error message
              message = user_id
              response = {
                  'message': message
              }
              return response
  </code></pre>
  
  <p>Here is my code that tests the app:</p>
  
  <pre><code>import unittest
  import os
  import json
  import app
  from app import create_app, db
  
  
  class CategoryTestCase(unittest.TestCase):
      """This class represents the Category test case"""
  
      def setUp(self):
          """setup test variables"""
          self.app = create_app(config_name="testing")
          self.client = self.app.test_client
          self.category_data = {'name' : 'Yummy'}
  
          # binds the app with the current context
          with self.app.app_context():
              #create all tables
              db.session.close()
              db.drop_all()
              db.create_all()
  
      def register_user(self, first_name='Tester', last_name='Api', username='apitester', email='tester@api.com', password='abc'):
          """This helper method helps register a test user"""
          user_data = {
              'first_name' : first_name,
              'last_name' : last_name,
              'username' : username,
              'email' : email,
              'password' : password
          }
  
          return self.client().post('/api/v1.0/register', data=json.dumps(user_data), content_type='application/json')
  
      def login_user(self, email='tester@api.com', password='abc'):
          """this helper method helps log in a test user"""
          user_data = {
              'email' : email,
              'password' : password
          }
  
          return self.client().post('/api/v1.0/login', data=json.dumps(user_data), content_type='application/json')
  
      def test_category_creation(self):
          """Test that the Api can create a category"""
          self.register_user()
          login_result = self.login_user()
  
          token = json.loads(login_result.data)
          token = token['access_token']
  
          # Create a category by going to that link
          response = self.client().post('/api/v1.0/category', headers=dict(Authorization="Bearer " + token), data=json.dumps(self.category_data), content_type='application/json')
          self.assertEquals(response.status_code, 201)
  </code></pre>