def test_should_call_reporter_write_with_open_browser_true_call_console_write_summary_and_exit( self, mocker, response): mocker.patch( "scanapi.scan.settings", { "spec_path": "", "no_report": False, "open_browser": True, "output_path": "", "template": None, }, ) mock_load_config_file = mocker.patch("scanapi.scan.load_config_file") mock_load_config_file.return_value = {"endpoints": []} mock_endpoint_init = mocker.patch("scanapi.scan.EndpointNode.__init__") mock_endpoint_init.return_value = None mock_endpoint_run = mocker.patch("scanapi.scan.EndpointNode.run") mock_endpoint_run.return_value = [response] mock_reporter_write = mocker.patch("scanapi.scan.Reporter.write") mock_console_write_summary = mocker.patch("scanapi.scan.write_summary") with raises(SystemExit) as excinfo: scan() mock_reporter_write.assert_called_once_with([response], True) mock_console_write_summary.assert_called_once_with() assert excinfo.type == SystemExit assert excinfo.value.code == 0
def run( spec_path, output_path, no_report, config_path, template, log_level, open_browser, ): """ Automated Testing and Documentation for your REST API. SPEC_PATH argument is the API specification file path. """ logging.basicConfig(level=log_level, format="%(message)s") logger = logging.getLogger(__name__) click_preferences = { "spec_path": spec_path, "output_path": output_path, "no_report": no_report, "config_path": config_path, "template": template, "open_browser": open_browser, } try: settings.save_preferences(**click_preferences) except yaml.YAMLError as e: error_message = "Error loading configuration file." error_message = "{}\nPyYAML: {}".format(error_message, str(e)) logger.error(error_message) raise SystemExit(ExitCode.USAGE_ERROR) scan()
def test_should_call_reporter_and_open_results(self, mocker, response): mock_load_config_file = mocker.patch("scanapi.scan.load_config_file") mock_load_config_file.return_value = {"endpoints": []} mock_endpoint_init = mocker.patch("scanapi.scan.EndpointNode.__init__") mock_endpoint_init.return_value = None mock_endpoint_run = mocker.patch("scanapi.scan.EndpointNode.run") mock_endpoint_run.return_value = [response] mock_write_report = mocker.patch("scanapi.scan.write_report") mock_write_results = mocker.patch("scanapi.scan.write_results") mocker.patch( "scanapi.scan.settings", { "spec_path": None, "output_path": "out/my-report.md", "no_report": False, "open_browser": True, "reporter": "markdown", "template": "my-template.jinja", }, ) with raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 0 mock_endpoint_init.assert_called_once_with({"endpoints": []}) assert mock_endpoint_run.called assert mock_write_results.called mock_write_report.assert_called_once_with([response], True)
def test_should_log_error(self, mocker, caplog): mocker.patch("scanapi.scan.load_config_file", side_effect=yaml_error) with caplog.at_level(logging.INFO): with pytest.raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert "error foo" in caplog.text
def test_should_log_error_3(self, mocker, caplog): mocker.patch("scanapi.scan.load_config_file", side_effect=yaml_error) with caplog.at_level(logging.ERROR): with raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert ("Error loading specification file.\nPyYAML: error foo" in caplog.text)
def test_should_log_error_2(self, mocker, caplog): mocker.patch("scanapi.scan.load_config_file", side_effect=empty_config_file) with caplog.at_level(logging.ERROR): with raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert ( "API spec file is empty. File 'valid_path/scanapi.yaml' is empty." in caplog.text)
def test_should_log_error(self, mocker, caplog): mock_load_config_file = mocker.patch( "scanapi.scan.load_config_file") with caplog.at_level(logging.INFO): with pytest.raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert ( "Error loading API spec. Missing 'api' key(s) at 'root' scope" in caplog.text)
def test_should_log_error_4(self, mocker, caplog): mock_load_config_file = mocker.patch("scanapi.scan.load_config_file") mock_load_config_file.return_value = {"blah": "blah"} mocker.patch("scanapi.scan.EndpointNode.__init__", side_effect=invalid_key) with caplog.at_level(logging.ERROR): with raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert ( "Error loading API spec. Invalid key 'foo' at 'endpoint' scope. Available keys " "are: ['bar', 'other']" in caplog.text)
def test_should_log_error(self, mocker, caplog): mocker.patch( "scanapi.scan.load_config_file", side_effect=file_format_not_supported, ) with caplog.at_level(logging.INFO): with pytest.raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert ( "The format .txt is not supported. Supported formats: '.yaml', '.yml', " "'.json'. File path: 'foo/api.txt'." in caplog.text )
def test_should_log_error(self, mocker, caplog): mocker.patch("scanapi.scan.settings", {"spec_path": "invalid_path/scanapi.yaml"}) mocker.patch("scanapi.scan.load_config_file", side_effect=file_not_found) with caplog.at_level(logging.ERROR): with pytest.raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert ( "Could not find API spec file: invalid_path/scanapi.yaml. [Errno 2] No such file " "or directory: 'invalid_path/scanapi.yaml" in caplog.text)
def run(spec_path, output_path, config_path, template, log_level): """ Automated Testing and Documentation for your REST API. SPEC_PATH argument is the API specification file path. """ logging.basicConfig(level=log_level, format="%(message)s") logger = logging.getLogger(__name__) click_preferences = { "spec_path": spec_path, "output_path": output_path, "config_path": config_path, "template": template, } settings.save_preferences(**click_preferences) scan()
def test_should_call_reporter(self, mocker, response): mock_load_config_file = mocker.patch("scanapi.scan.load_config_file") mock_load_config_file.return_value = {"endpoints": []} mock_endpoint_init = mocker.patch("scanapi.scan.EndpointNode.__init__") mock_endpoint_init.return_value = None mock_endpoint_run = mocker.patch("scanapi.scan.EndpointNode.run") mock_endpoint_run.return_value = [response] mock_write_report = mocker.patch("scanapi.scan.write_report") with raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 0 mock_endpoint_init.assert_called_once_with({"endpoints": []}) assert mock_endpoint_run.called mock_write_report.assert_called_once_with([response])
def test_should_log_error(self, mocker, caplog): mock_load_config_file = mocker.patch("scanapi.scan.load_config_file") mock_load_config_file.return_value = {"api": "blah"} mocker.patch( "scanapi.scan.EndpointNode.__init__", side_effect=missing_mandatory_key, ) with caplog.at_level(logging.INFO): with pytest.raises(SystemExit) as excinfo: scan() assert excinfo.type == SystemExit assert excinfo.value.code == 4 assert ( "Error loading API spec. Missing 'bar', 'foo' key(s) at 'endpoint' scope" in caplog.text )