def test_config_clear_api_key(): config.config_file_clear_section(SECTION) random_key = ''.join( random.choices(string.ascii_uppercase + string.digits, k=12)) config.config_file_save_api_key(random_key, USERNAME, SECTION) assert config.config_file_read_api_key(SECTION) == random_key config.config_file_clear_api_key(SECTION) assert config.config_file_read_api_key(SECTION) is None
def test_login_valid_parameters_without_confirmation(): config.config_file_clear_api_key('test') assert config.config_file_read_api_key('test') is None runner = CliRunner(echo_stdin=True) prepare_successful_login() result = runner.invoke(cli.login, ['--debug', '--test'], input=TEST_LOGIN_USERNAME + '\n' + TEST_LOGIN_PASSWORD + '\nN') assert result.exit_code == 0 and not result.exception assert config.config_file_read_api_key('test') is None
def test_login_basic(): clear_test_credentials() assert config.config_file_read_api_key('test') is None prepare_successful_login() ArcsecondAPI.login(TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, debug=True, test=True) assert config.config_file_read_api_key('test') is None assert config.config_file_read_upload_key('test') is None
def test_login_both_apikey_uploadkey(): clear_test_credentials() assert config.config_file_read_api_key('test') is None prepare_successful_login() ArcsecondAPI.login(TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD, api_key=True, upload_key=True, debug=True, test=True) assert config.config_file_read_api_key('test') == TEST_API_KEY assert config.config_file_read_upload_key('test') == TEST_UPLOAD_KEY
def _check_and_set_auth_key(self, headers, url): if API_AUTH_PATH_REGISTER in url or API_AUTH_PATH_LOGIN in url or 'Authorization' in headers.keys(): return headers # Choose the strongest key first auth_key = self.state.api_key or self.state.upload_key if auth_key is None: if self.state.verbose: click.echo('Checking local API|Upload key... ', nl=False) # Choose the strongest key first auth_key = config_file_read_api_key(self.state.config_section()) if not auth_key: auth_key = config_file_read_upload_key(self.state.config_section()) if not auth_key: raise ArcsecondError('Missing auth keys (API or Upload). You must login first: $ arcsecond login') headers['X-Arcsecond-API-Authorization'] = 'Key ' + auth_key if self.state.verbose: key_str = auth_key[:3] + 9 * '*' click.echo(f'OK (\'X-Arcsecond-API-Authorization\' = \'Key {key_str}\'') return headers
def _check_and_set_api_key(self, headers, url): if 'login' in url or 'register' in url or 'Authorization' in headers.keys( ): return headers if self.state.verbose: click.echo('Checking local API key... ', nl=False) api_key = config_file_read_api_key(self.state.debug) if not api_key: raise ArcsecondError( 'Missing API key. You must login first: $ arcsecond login') headers['X-Arcsecond-API-Authorization'] = 'Key ' + api_key if self.state.verbose: click.echo('OK') return headers
def is_logged_in(cls, state=None): return config_file_read_api_key(debug=state.debug) is not None
def api_key(cls, state: Optional[State] = None, **kwargs) -> str: state = get_api_state(state, **kwargs) return config_file_read_api_key(section=state.config_section()) or ''