Exemple #1
0
def write_without_generating_report(results):
    """Constructs a Reporter object and calls the
    write_without_generating_report method of Reporter to print the results to
    the console output without generating a report.
    """
    reporter = Reporter()
    reporter.write_without_generating_report(results)
Exemple #2
0
def _write_report(results, open_browser):
    """Constructs a Reporter object and calls the write method of Reporter to
    push the results to a file.

    Returns:
        None
    """
    reporter = Reporter(settings["output_path"], settings["template"])
    reporter.write(results, open_browser)
Exemple #3
0
    def test_should_write_to_custom_output(self, mocker,
                                           mocked__render_content,
                                           mocked_open):
        mocked__render_content.return_value = "ScanAPI Report"
        reporter = Reporter("./custom/report-output.html", "html")
        reporter.write(fake_responses)

        mocked_open.assert_called_once_with("./custom/report-output.html",
                                            "w",
                                            newline="\n")
        mocked_open().write.assert_called_once_with("ScanAPI Report")
Exemple #4
0
    def test_should_handle_custom_templates(self, mocker,
                                            mocked__render_content,
                                            mocked_open):
        mocked__render_content.return_value = "ScanAPI Report"
        reporter = Reporter(None, None, "my-template.html")
        reporter.write(fake_responses)

        mocked_open.assert_called_once_with("scanapi-report",
                                            "w",
                                            newline="\n")
        mocked_open().write.assert_called_once_with("ScanAPI Report")
Exemple #5
0
    def test_should_write_to_default_output(self, reporter_type, mocker,
                                            mocked__render_content,
                                            mocked_open):
        mocked__render_content.return_value = "ScanAPI Report"
        reporter = Reporter(None, reporter_type)
        reporter.write(fake_responses)

        file_extension = {"html": "html", "markdown": "md"}[reporter_type]
        mocked_open.assert_called_once_with(f"scanapi-report.{file_extension}",
                                            "w",
                                            newline="\n")
        mocked_open().write.assert_called_once_with("ScanAPI Report")
Exemple #6
0
        def test_should_handle_custom_templates(
            self, mocker, mocked__render, mocked__open, mocked__session, context,
        ):
            mocked__render.return_value = "ScanAPI Report"
            reporter = Reporter(template="my-template.html")
            reporter.write(fake_results)

            mocked__render.assert_called_once_with("my-template.html", context, True)
            mocked__open.assert_called_once_with(
                "scanapi-report.html", "w", newline="\n"
            )
            mocked__open().write.assert_called_once_with("ScanAPI Report")
Exemple #7
0
        def test_should_write_to_custom_output(
            self, mocker, mocked__render, mocked__open, mocked__session, context,
        ):
            mocked__render.return_value = "ScanAPI Report"
            reporter = Reporter("./custom/report-output.html", "html")
            reporter.write(fake_results)

            mocked__render.assert_called_once_with("html", context, True)
            mocked__open.assert_called_once_with(
                "./custom/report-output.html", "w", newline="\n"
            )
            mocked__open().write.assert_called_once_with("ScanAPI Report")
Exemple #8
0
        def test_should_write_to_default_output(
            self, mocker, mocked__render, mocked__open, mocked__session, context,
        ):
            mocked__render.return_value = "ScanAPI Report"
            reporter = Reporter()
            reporter.write(fake_results)

            mocked__render.assert_called_once_with("report.html", context, False)
            mocked__open.assert_called_once_with(
                "scanapi-report.html", "w", newline="\n"
            )
            mocked__open().write.assert_called_once_with("ScanAPI Report")
Exemple #9
0
    def test_should_write_without_generating_report(
        self,
        mocker,
        mocked__render,
        mocked__open,
        mocked__logger,
    ):
        reporter = Reporter()
        reporter.write_without_generating_report(fake_results)

        mocked__render.assert_not_called()
        mocked__open.assert_not_called()
        mocked__logger.info.assert_called_once()
Exemple #10
0
    def test_should_print(self, mocker, mocked_print):
        console_reporter = Reporter(None, "console")
        console_reporter.write(fake_responses)

        expected_content = "\n".join((
            "ScanAPI Report: Console",
            "=======================",
            "",
            "GET http://test.com - 200",
            "",
        ))

        mocked_print.assert_called_once_with(f"\n{expected_content}")
Exemple #11
0
    def test_should_open_report_in_browser(
        self,
        mocker,
        mocked__render,
        mocked__open,
        mocked__session,
        mock_get_distribution,
        context,
        mocked__webbrowser,
    ):

        reporter = Reporter()
        reporter.write(fake_results, True)
        assert mocked__webbrowser.open.call_count == 1
Exemple #12
0
def scan(spec_path, output_path, reporter, template, log_level):
    """Automated Testing and Documentation for your REST API."""

    logging.basicConfig(level=log_level)
    logger = logging.getLogger(__name__)
    SETTINGS.update({"spec_path": spec_path, "output_path": output_path})

    # custom templates to be implemented later
    if template is not None:
        logger.warn(
            "Custom templates are not supported yet. Soon to be. Hang tight.")

    spec_path = SETTINGS["spec_path"]
    try:
        api_spec = load_yaml(spec_path)
    except FileNotFoundError as e:
        error_message = f"Could not find spec file: {spec_path}. {str(e)}"
        logger.error(error_message)
        return

    try:
        api_tree = APITree(api_spec)
    except Exception as e:
        error_message = "Error loading API spec."
        error_message = "{} {}".format(error_message, str(e))
        logger.error(error_message)
        return

    RequestsMaker(api_tree).make_all()
    Reporter(output_path, reporter,
             template).write(api_tree.responses.values())
Exemple #13
0
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
Exemple #14
0
            def test_init_output_path_and_template(self):
                reporter = Reporter(template="my_template.jinja")

                assert reporter.output_path == "scanapi-report.html"
                assert reporter.template == "my_template.jinja"
Exemple #15
0
            def test_init_output_path_and_template(self):
                reporter = Reporter(output_path="my-report.html")

                assert reporter.output_path == "my-report.html"
                assert reporter.template is None
Exemple #16
0
def open_report_in_browser():
    """Open the results file on a browser"""
    reporter = Reporter(settings["output_path"], settings["template"])
    reporter.open_report_in_browser()
Exemple #17
0
    def test_init_output_path_and_template(self):
        reporter = Reporter()

        assert str(reporter.output_path) == "scanapi-report.html"
        assert reporter.template is None
Exemple #18
0
def write_report(results):
    reporter = Reporter(settings["output_path"], settings["template"])
    reporter.write(results)
Exemple #19
0
def write_report(results):
    """ Constructs a Reporter object and calls the write method of Reporter to push
    the results to a file.
    """
    reporter = Reporter(settings["output_path"], settings["template"])
    reporter.write(results)