Ejemplo n.º 1
0
def test_leak_message(result_input, snapshot, show_secrets, verbose):
    # The text output includes the version of the secrets engine, but this version is
    # None until we make an API call. Since this test does not make any API call, set
    # the version to a fake value.
    with mock.patch("ggshield.output.text.message.VERSIONS") as VERSIONS:
        VERSIONS.secrets_engine_version = "3.14.159"

        output_handler = TextOutputHandler(show_secrets=show_secrets,
                                           verbose=verbose)

        # _process_scan_impl() modifies its ScanCollection arg(!), so make a copy of it
        new_result = deepcopy(result_input)

        output = output_handler._process_scan_impl(
            ScanCollection(
                id="scan",
                type="test",
                results=[new_result],
                optional_header="> This is an example header",
            ))
    # Make output OS-independent, so that it can be safely compared to snapshots
    # regardless of the current OS:
    # - Remove colors because color codes are not the same on all OSes
    # - Replace any custom decoration with the default one
    output = click.unstyle(output).replace(_file_info_decoration(),
                                           _file_info_default_decoration())

    snapshot.assert_match(output)
Ejemplo n.º 2
0
 def test_docker_scan(self, scan_mock: Mock, save_mock,
                      cli_fs_runner: click.testing.CliRunner):
     scan_mock.return_value = ScanCollection(id="ggshield-non-existant",
                                             type="docker",
                                             results=[])
     result = cli_fs_runner.invoke(
         cli,
         ["-v", "scan", "docker", "ggshield-non-existant"],
     )
     assert result.exit_code == 0
Ejemplo n.º 3
0
def test_leak_message(result_input, snapshot, show_secrets):
    output_handler = TextHandler(show_secrets=show_secrets, verbose=True)
    new_result = deepcopy(result_input)
    output, exit_code = output_handler.process_scan(
        ScanCollection(
            id="scan",
            type="test",
            results=[new_result],
            optional_header="> This is an example header",
        ))

    assert exit_code == 1
    snapshot.assert_match(output)
Ejemplo n.º 4
0
 def test_docker_scan_failed_to_save(
         self, scan_mock: Mock, save_mock: Mock,
         cli_fs_runner: click.testing.CliRunner):
     save_mock.side_effect = click.exceptions.ClickException(
         'Image "ggshield-non-existant" not found')
     scan_mock.return_value = ScanCollection(id="ggshield-non-existant",
                                             type="docker",
                                             results=[])
     result = cli_fs_runner.invoke(
         cli,
         ["-v", "scan", "docker", "ggshield-non-existant"],
     )
     assert 'Error: Image "ggshield-non-existant" not found\n' in result.output
     assert result.exit_code == 1
Ejemplo n.º 5
0
def test_json_output(client, name, input_patch, expected, snapshot):
    c = Commit()
    c._patch = input_patch
    handler = JSONHandler(verbose=True, show_secrets=False)

    with my_vcr.use_cassette(name):
        results = c.scan(
            client=client, matches_ignore={}, all_policies=True, verbose=False
        )

        flat_results, exit_code = handler.process_scan(
            scan=ScanCollection(id="path", type="test", results=results), top=True
        )

        assert exit_code == expected
        json_flat_results = JSONScanCollectionSchema().dumps(flat_results)
        snapshot.assert_match(JSONScanCollectionSchema().loads(json_flat_results))