title: Unit testing of OAuth on Flask
body: <p>I'm new to coding and I feel really stuck. I decided to unit test my micro Flask app. I use &quot;Google sign in&quot; to authenticate my users, but I'm not able to get through the Google authentication and assert what is really on the page. I have no idea if I need to mock, play with the session or application context.</p>
  <p>Here is the code for authentication process:</p>
  <pre><code>def login_required(f):
      @wraps(f)
      def decorated_function(*args, **kwargs):
          user = dict(session).get('profile', None)
          if user is not None:
              email = session['profile']['email']
          else:
              email = None
          if user:
              return f(*args, **kwargs)
  
          return render_template('login.html', email=email)
  
      return decorated_function
  
  @app.route('/login/')
  def login():
      google = oauth.create_client('google')  # create the google oauth client
      redirect_uri = url_for('authorize', _external=True)
      return google.authorize_redirect(redirect_uri)
  
  @app.route('/authorize')
      def authorize():
          google = oauth.create_client('google')  # create the google oauth client
          token = google.authorize_access_token()  # Access token from google (needed to get user info)
          resp = google.get('userinfo')  # userinfo specificed in the scrope
          user_info = resp.json()
          # Loading and cleaning all user emails
          auth_users = db.session.query(users.email).all()
          auth_users = [str(i).strip(&quot;(),'&quot;) for i in auth_users]
          # Checking if user is in the list
          if user_info['email'] in auth_users:
              flash(&quot;Boli ste úspešne prihlásený.&quot;, category=&quot;success&quot;)
          else:
              flash(&quot;Nemáte práva na prístup.&quot;, category=&quot;primary&quot;)
              return redirect('/')
          session['profile'] = user_info
          # make the session pernament so it keeps existing after broweser gets closed
          session.permanent = True
          return redirect('/')
  </code></pre>
  <p>The test structure look like this:</p>
  <pre><code>class Uvo_page(unittest.TestCase):
  
      def setUp(self):
          self.client = app.test_client(self)
          self.user_info = {'email': 'xxx'}
          self.auth_users = ['xxx', 'yyy']
  
     # UVO page shows results
      def test_uvo_page_show_results(self):
          response = self.client.get('/show/1',
                                     content_type='html/text',
                                     follow_redirects=True)
          test_string = bytes('Sledované stránky', 'utf-8')
          self.assertTrue(test_string in response.data)
  </code></pre>
  <p>I thought that by defining the &quot;user_info&quot; it will be used for authentication evaluation. Instead of it it does not continue to &quot;/authorize&quot; and authentication, but stays on the login page.</p>