def test_gen_new_auth_token(self): from pillar.api import service with self.app.test_request_context(): tokens_coll = self.app.db('tokens') service_account_id = self.flamenco.manager_manager.find_service_account_id( self.mngr_id) # Create a new more tokens service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 4) token = self.flamenco.manager_manager.gen_new_auth_token( self.mngr_id) # There can be only one, rest should have been deleted. all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 1) self.assertNotEqual(token.token, self.mngr_token) self.assertTrue(token.token.startswith('SRV')) self.assertTrue(token.expire_time > datetime.datetime.now(tz=utc))
def gen_new_auth_token( self, manager_id: bson.ObjectId) -> typing.Optional[AuthTokenInfo]: """Generates a new authentication token for the given Manager. Deletes all pre-existing authentication tokens of the Manager. """ from pillar.api import service service_account_id = self.revoke_auth_token(manager_id) self._log.info( 'Generating new authentication token for Manager %s on behalf of user %s', manager_id, current_user.user_id) token_info = service.generate_auth_token(service_account_id) return AuthTokenInfo( token=token_info['token'], expire_time=token_info['expire_time'], )
def test_revoke_auth_token(self): from pillar.api import service with self.app.test_request_context(): tokens_coll = self.app.db('tokens') service_account_id = self.flamenco.manager_manager.find_service_account_id(self.mngr_id) # Create a new more tokens service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 4) self.flamenco.manager_manager.revoke_auth_token(self.mngr_id) # All should have been deleted. all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 0)
def test_revoke_auth_token(self): from pillar.api import service with self.app.test_request_context(): tokens_coll = self.app.db('tokens') service_account_id = self.flamenco.manager_manager.find_service_account_id( self.mngr_id) # Create a new more tokens service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 4) self.flamenco.manager_manager.revoke_auth_token(self.mngr_id) # All should have been deleted. all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 0)
def test_gen_new_auth_token(self): from pillar.api import service with self.app.test_request_context(): tokens_coll = self.app.db('tokens') service_account_id = self.flamenco.manager_manager.find_service_account_id(self.mngr_id) # Create a new more tokens service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) service.generate_auth_token(service_account_id) all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 4) token = self.flamenco.manager_manager.gen_new_auth_token(self.mngr_id) # There can be only one, rest should have been deleted. all_tokens = tokens_coll.find({'user': service_account_id}) self.assertEqual(all_tokens.count(), 1) self.assertNotEqual(token.token, self.mngr_token) self.assertTrue(token.token.startswith('SRV')) self.assertTrue(token.expire_time > datetime.datetime.now(tz=utc))