def test_generate_token_errors(foss_server: str): responses.add( responses.POST, f"{foss_server}/api/v1/tokens", body=requests.exceptions.ConnectionError(), ) responses.add( responses.POST, f"{foss_server}/api/v1/tokens", status=404, ) with pytest.raises(SystemExit) as excinfo: fossology_token( foss_server, "fossy", "fossy", secrets.token_urlsafe(8), token_expire=str(date.today() - timedelta(days=1)), ) assert ( f"Server {foss_server} does not seem to be running or is unreachable" in str(excinfo.value)) with pytest.raises(AuthenticationError) as excinfo: fossology_token( foss_server, "fossy", "nofossy", secrets.token_urlsafe(8), token_expire=str(date.today() + timedelta(days=1)), ) assert "Authentication error" in str(excinfo.value)
def test_generate_token_too_long(foss_server: str): with pytest.raises(FossologyApiError) as excinfo: fossology_token( foss_server, "fossy", "fossy", secrets.token_urlsafe(41), token_expire=str(date.today() + timedelta(days=1)), ) assert "Error while generating new token" in str(excinfo.value)
def generate_fossology_token(server): try: return fossology_token( server, "fossy", "fossy", secrets.token_urlsafe(8), TokenScope.WRITE ) except (FossologyApiError, AuthenticationError) as error: exit(error.message)
def config( ctx: click.core.Context, server: str, username: str, password: str, token_scope: str, interactive: bool, ): """Create a foss_cli config file.""" if interactive: print( "Enter the URL to your Fossology server: e.g. http://fossology/repo" ) server = input("Fossology URL: ") print( "Enter Username and Password: e.g. fossy/fossy (in the default environment)" ) username = input("Username: "******"read" while True: try: print( "Enter a scope for your Fossology token: either 'read' or 'write'" ) token_scope = input("Token scope: ") assert token_scope in ["read", "write"] break except Exception: print( "Allowed values are 'read' or 'write' (using 'read' as default)" ) logger.warning( f"Create a new config for {username} on {server} with scope {token_scope}" ) if token_scope == "read": the_token_scope = TokenScope.READ else: the_token_scope = TokenScope.WRITE logger.debug(f"Create token for {username} on {server}") token = fossology_token( server, username, password, secrets.token_urlsafe(8), # TOKEN_NAME the_token_scope, ) logger.debug(f"Token {token} has been created") path_to_cfg_file = pathlib.Path.cwd() / DEFAULT_CONFIG_FILE_NAME if path_to_cfg_file.exists(): logger.info( f"Found existing foss_cli config file {path_to_cfg_file}, updating the values..." ) config = configparser.ConfigParser() config.read(path_to_cfg_file) else: logger.info( f"foss_cli config file {path_to_cfg_file} not found, creating a new one..." ) config = configparser.ConfigParser() config["FOSSOLOGY"] = { "SERVER_URL": server, "USERNAME": username, "TOKEN": token, } with open(path_to_cfg_file, "w") as fp: config.write(fp) logger.warning(f"New config has been generated in {path_to_cfg_file}")
def foss_token(foss_server: str) -> str: return fossology_token(foss_server, "fossy", "fossy", secrets.token_urlsafe(8), TokenScope.WRITE)