def add(application): """Register a new application allowed to work with the API""" a = Auth.query.filter_by(application=application).first() if not a: a = Auth() a.application = application db.session.add(a) db.session.commit() print( '>>> Application {} registered. Please use the following token to connect: {}' .format(a.application, a.get_token()))
def test_model_auth(self): # create a = Auth() a.application = 'test' db.session.add(a) db.session.commit() self.assertTrue(a.id > 0, 'id must be defined, but it is not.') # find a = Auth.query.filter_by(application='test').first() self.assertIsNotNone(a, 'we did not find the test application') # get token token = a.get_token() self.assertIsNot(token, '', 'token is not provided') # get another object b = Auth.query.get(a.id) self.assertIsNotNone( b, 'we did not retrieve the test application by its id') # verify objects equals self.assertEqual(str(a), str(b), 'error: a is not b') # check token of a self.assertIsNotNone(a.verify_token(token), 'invalid token provided (a)') self.assertIsNotNone(b.verify_token(token), 'invalid token provided (b)') self.assertEqual( a.verify_token(token).application, 'test', 'a check token did not send the proper application') # check bad token self.assertIsNone(a.verify_token('i am a bad token'), 'check invalid token failed') # must not find undefined application c = Auth.query.filter_by(application='totolastiko').first() self.assertIsNone( c, 'search for undefined application did not fails as expected') d = Auth.query.get(123456) self.assertIsNone( d, 'get with value 123456 retrieve something but it should not') # delete e = Auth.query.get(a.id) self.assertEqual(e.application, 'test', 'cannot retrieve application test') db.session.delete(e) db.session.commit() f = Auth.query.get(a.id) self.assertIsNone(f, 'test application was not deleted')