def test_save_token(self): """Test save_token method.""" self.kytos_config.save_token('user', 'token') config = KytosConfig(self.config_file).config has_token = config.has_option('auth', 'token') self.assertTrue(has_token)
def __init__(self, controller=None): """If controller is not informed, the necessary paths must be. If ``controller`` is available, NApps will be (un)loaded at runtime and you don't need to inform the paths. Otherwise, you should inform the required paths for the methods called. Args: controller (kytos.Controller): Controller to (un)load NApps. install_path (str): Folder where NApps should be installed. If None, use the controller's configuration. enabled_path (str): Folder where enabled NApps are stored. If None, use the controller's configuration. """ self._controller = controller self._config = KytosConfig().config self._kytos_api = self._config.get('kytos', 'api') self.user = None self.napp = None self.version = None # Automatically get from kytosd API when needed self.__enabled = None self.__installed = None
def test_clear_token(self): """Test clear_token method.""" self.kytos_config.clear_token() config = KytosConfig(self.config_file).config has_token = config.has_option('auth', 'token') self.assertFalse(has_token)
def test_update(self, mock_post): """Test update method.""" args = {'<version>': 'ABC'} self.web_api.update(args) kytos_api = KytosConfig().config.get('kytos', 'api') url = f"{kytos_api}api/kytos/core/web/update/ABC" mock_post.assert_called_with(url)
def setUp(self): """Execute steps before each tests.""" self.kytos_auth = kytos_auth(MagicMock()) self.kytos_auth.__get__(MagicMock(), MagicMock()) config = KytosConfig('/tmp/.kytosrc').config config.set('auth', 'user', 'username') config.set('auth', 'token', 'hash') self.kytos_auth.config = config
def __init__(self, func): """Init method. Save the function on the func attribute and bootstrap a new config. """ self.func = func self.config = KytosConfig().config self.cls = None self.obj = None
def setUp(self): """Execute steps before each tests.""" with tempfile.TemporaryDirectory() as tmp_dir: config_file = '{}.kytosrc'.format(tmp_dir) kytos_config = KytosConfig(config_file) self.users_client = UsersClient() self.users_client._config = kytos_config.config self.users_client._config.set('napps', 'api', 'endpoint')
def authenticate(self): """Check the user authentication.""" endpoint = os.path.join(self.config.get('napps', 'api'), 'auth', '') username = self.config.get('auth', 'user') password = getpass("Enter the password for {}: ".format(username)) response = requests.get(endpoint, auth=(username, password)) if response.status_code != 201: LOG.error(response.content) LOG.error('ERROR: %s: %s', response.status_code, response.reason) sys.exit(1) else: data = response.json() KytosConfig().save_token(username, data.get('hash')) return data.get('hash')
def update(cls): """Call the method to update the Web UI.""" kytos_api = KytosConfig().config.get('kytos', 'api') url = f"{kytos_api}api/kytos/core/web/update/" try: result = requests.post(url) except (HTTPError, URLError, requests.exceptions.ConnectionError): LOG.error(f"Can't connect to the server: {kytos_api}") return if result.status_code != 200: LOG.info(f"Error while update web ui: {result.content}") else: LOG.info("Web UI updated.")
def __init__(self): """Instance a new NAppsManager. This method do not need parameters. """ self._config = KytosConfig().config self._kytos_api = self._config.get('kytos', 'api') self.user = None self.napp = None self.version = None # Automatically get from kytosd API when needed self.__local_enabled = None self.__local_installed = None
def upload_napp(self, metadata, package): """Upload the napp from the current directory to the napps server.""" endpoint = os.path.join(self._config.get('napps', 'api'), 'napps', '') metadata['token'] = self._config.get('auth', 'token') request = self.make_request(endpoint, json=metadata, package=package, method="POST") if request.status_code != 201: KytosConfig().clear_token() LOG.error("%s: %s", request.status_code, request.reason) sys.exit(1) # WARNING: this will change in future versions, when 'author' will get # removed. username = metadata.get('username', metadata.get('author')) name = metadata.get('name') print("SUCCESS: NApp {}/{} uploaded.".format(username, name))
def update(cls, args): """Call the method to update the Web UI.""" kytos_api = KytosConfig().config.get('kytos', 'api') url = f"{kytos_api}api/kytos/core/web/update" version = args["<version>"] if version: url += f"/{version}" try: result = requests.post(url) except (HTTPError, URLError, requests.exceptions.ConnectionError): LOG.error("Can't connect to server: %s", kytos_api) return if result.status_code != 200: LOG.info("Error while updating web ui: %s", result.content) else: LOG.info("Web UI updated.")
def authenticate(self): """Check the user authentication.""" endpoint = os.path.join(self.config.get('napps', 'api'), 'auth', '') username = self.config.get('auth', 'user') password = getpass('Enter the password for {}: '.format(username)) response = requests.get(endpoint, auth=(username, password)) # Check if it is unauthorized if response.status_code == 401: print(f'Error with status code: {response.status_code}.\n' 'Possible causes: incorrect credentials, the token was ' 'not set or was expired.') if response.status_code != 201: LOG.error(response.content) LOG.error('ERROR: %s: %s', response.status_code, response.reason) print('Press Ctrl+C or CTRL+Z to stop the process.') user = input('Enter the username: '******'auth', 'user', user) self.authenticate() else: data = response.json() KytosConfig().save_token(username, data.get('hash')) return data.get('hash')
def __init__(self, config=None): """Set Kytos config.""" if config is None: config = KytosConfig().config self._config = config
def setUp(self): """Execute steps before each tests.""" with tempfile.TemporaryDirectory() as tmp_dir: self.config_file = '{}.kytosrc'.format(tmp_dir) self.kytos_config = KytosConfig(self.config_file)