def test_should_raise_exception(self): with pytest.raises(FileNotFoundError) as excinfo: load_config_file("invalid/path.yaml") assert (str(excinfo.value) == "[Errno 2] No such file or directory: 'invalid/path.yaml'")
def test_should_raise_exception_3(self): with raises(FileNotFoundError) as excinfo: load_config_file("tests/data/api_invalid_path_include.yaml") assert "[Errno 2] No such file or directory: " in str(excinfo.value) assert "tests/data/invalid_path/include.yaml'" in str(excinfo.value)
def test_should_raise_exception(self): with pytest.raises(FileFormatNotSupportedError) as excinfo: load_config_file("tests/data/api_wrong_format_include.yaml") assert ( str(excinfo.value) == "The format .txt is not supported. Supported formats: '.yaml', '.yml', '.json'. " "File path: 'tests/data/include.txt'.")
def save_config_file_preferences(self, config_path=None): """ Saves the Settings object config file preferences. """ if config_path: self["config_path"] = config_path self.update(**load_config_file(config_path)) elif self.has_local_config_file: self["config_path"] = LOCAL_CONFIG_PATH self.update(**load_config_file(LOCAL_CONFIG_PATH)) elif self.has_global_config_file: self["config_path"] = GLOBAL_CONFIG_PATH self.update(**load_config_file(GLOBAL_CONFIG_PATH))
def save_config_file_preferences(self, config_path=None): if not config_path and not self.has_default_config_file: return if not config_path: user_config = load_config_file(DEFAULT_CONFIG_PATH) self["config_path"] = DEFAULT_CONFIG_PATH self.update(**user_config) return user_config = load_config_file(config_path) self.update(**user_config)
def test_should_load(self): data = load_config_file("tests/data/scanapi.yaml") assert data == { "endpoints": [{ "name": "scanapi-demo", "path": "${BASE_URL}", "requests": [{ "name": "health", "path": "/health/" }], }] }
def scan(): """Caller function that tries to scans the file and write the report.""" spec_path = settings["spec_path"] no_report = settings["no_report"] open_browser = settings["open_browser"] try: api_spec = load_config_file(spec_path) except FileNotFoundError as e: error_message = f"Could not find API spec file: {spec_path}. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except EmptyConfigFileError as e: error_message = f"API spec file is empty. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except yaml.YAMLError as e: error_message = "Error loading specification file." error_message = "{}\nPyYAML: {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) try: root_node = EndpointNode(api_spec) results = root_node.run() except ( InvalidKeyError, KeyError, InvalidPythonCodeError, ) as e: error_message = "Error loading API spec." error_message = "{} {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) if no_report: write_without_generating_report(results) else: try: write_report(results) if open_browser: open_report_in_browser() except (BadConfigurationError, InvalidPythonCodeError) as e: logger.error(e) raise SystemExit(ExitCode.USAGE_ERROR) session.exit()
def scan(): spec_path = settings["spec_path"] try: api_spec = load_config_file(spec_path) except FileNotFoundError as e: error_message = f"Could not find API spec file: {spec_path}. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except EmptyConfigFileError as e: error_message = f"API spec file is empty. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except (yaml.YAMLError, FileFormatNotSupportedError) as e: logger.error(e) raise SystemExit(ExitCode.USAGE_ERROR) try: if API_KEY not in api_spec: raise MissingMandatoryKeyError({API_KEY}, ROOT_SCOPE) root_node = EndpointNode(api_spec[API_KEY]) results = root_node.run() except ( InvalidKeyError, MissingMandatoryKeyError, KeyError, InvalidPythonCodeError, ) as e: error_message = "Error loading API spec." error_message = "{} {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) try: write_report(results) except (BadConfigurationError, InvalidPythonCodeError) as e: logger.error(e) raise SystemExit(ExitCode.USAGE_ERROR) session.exit()
def scan(spec_path, output_path, config_path, reporter, template, log_level): """Automated Testing and Documentation for your REST API.""" logging.basicConfig(level=log_level, format="%(message)s") logger = logging.getLogger(__name__) settings.save_preferences( **{ "spec_path": spec_path, "output_path": output_path, "config_path": config_path, "reporter": reporter, "template": template, }) spec_path = settings["spec_path"] try: api_spec = load_config_file(spec_path) except FileNotFoundError as e: error_message = f"Could not find API spec file: {spec_path}. {str(e)}" logger.error(error_message) return except EmptyConfigFileError as e: error_message = f"API spec file is empty. {str(e)}" logger.error(error_message) return except (yaml.YAMLError, FileFormatNotSupportedError) as e: logger.error(e) return try: if API_KEY not in api_spec: raise MissingMandatoryKeyError({API_KEY}, ROOT_SCOPE) root_node = EndpointNode(api_spec[API_KEY]) Reporter(settings["output_path"], settings["reporter"], settings["template"]).write(root_node.run()) except (InvalidKeyError, MissingMandatoryKeyError, KeyError) as e: error_message = "Error loading API spec." error_message = "{} {}".format(error_message, str(e)) logger.error(error_message) return
def scan(): """Caller function that tries to scans the file and write the report.""" spec_path = settings["spec_path"] try: api_spec = load_config_file(spec_path) except FileNotFoundError as e: error_message = f"Could not find API spec file: {spec_path}. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except EmptyConfigFileError as e: error_message = f"API spec file is empty. {str(e)}" logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) except yaml.YAMLError as e: error_message = "Error loading specification file." error_message = "{}\nPyYAML: {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) try: root_node = EndpointNode(api_spec) results = root_node.run() except ( InvalidKeyError, KeyError, InvalidPythonCodeError, ) as e: error_message = "Error loading API spec." error_message = "{} {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) _write(results) write_summary() session.exit()
def test_should_raise_exception(self): with pytest.raises(EmptyConfigFileError) as excinfo: load_config_file("tests/data/empty.yaml") assert str( excinfo.value) == "File 'tests/data/empty.yaml' is empty."
def test_should_raise_exception_4(self): with raises(BadConfigIncludeError) as excinfo: load_config_file("tests/data/api_non_scalar_include.yaml") assert "Include tag value is not a scalar" in str(excinfo.value)