0

I have a login function with user and pass parameters that returns a token. I'am using flask. Now i need a test case to test my function, i am lost here. Neve done testing and i can't come up with the solution. I need a test case that verifies a token was created when the login is made. Any help?

def login():

    user = request.form['user']
    passwd = request.form['passwd']

    test = ldap.bind_user(user, passwd)
    if test is None or passwd == '':
        response = jsonify(message='Invalid Credentials')
        return response ,401

    access_token = create_access_token(identity=user)
    return jsonify(access_token=access_token), 200```
Patrick José
  • 395
  • 3
  • 13
  • See this https://stackoverflow.com/a/20760055/8375783 answer how to post a form via `requests`. With that you can try login attempts with various data set then check the response. – Trapli Sep 25 '19 at 20:45

1 Answers1

0

After 2 days i finally manage to come up with the solution. I checked if access_token was in the response.data


    def test_token_sent(self):
        tester = app.test_client(self)
        response = tester.post(
            '/login',
            data =dict(user="hermes", passwd ="hermes"),
            follow_redirects=True)
        self.assertIn(b'access_token', response.data)```



Patrick José
  • 395
  • 3
  • 13