Skip to content

Commit

Permalink
Cleanup audit --display-results code and tests
Browse files Browse the repository at this point in the history
* Rename a variable to be clearer per review comment
* Change test fixure signature
* Remove unneeded `if` from test
  • Loading branch information
Victor Zhou committed Jul 8, 2019
1 parent 733a600 commit 69a109e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions detect_secrets/core/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def determine_audit_results(baseline, baseline_path):
'results': defaultdict(lambda: deepcopy(EMPTY_PLUGIN_AUDIT_RESULT)),
}

secret_type_mapping = get_mapping_from_secret_type_to_class_name()
secret_type_to_plugin_name = get_mapping_from_secret_type_to_class_name()

for filename, secret in all_secrets:
plaintext_line = _get_file_line(filename, secret['line_number'])
Expand All @@ -239,7 +239,7 @@ def determine_audit_results(baseline, baseline_path):
except SecretNotFoundOnSpecifiedLineError:
secret_plaintext = plaintext_line

plugin_name = secret_type_mapping[secret['type']]
plugin_name = secret_type_to_plugin_name[secret['type']]
audit_result = AUDIT_RESULT_TO_STRING[secret.get('is_secret')]
audit_results['results'][plugin_name]['results'][audit_result].append(secret_plaintext)

Expand Down
15 changes: 7 additions & 8 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ def mock_get_git_remotes(self):
) as _mock:
yield _mock

def get_audited_baseline(self, plugin_config={}, is_secret=None):
def get_audited_baseline(self, plugin_config, is_secret):
"""
Returns a baseline in dict form with 1 plugin and 1 secret.
:param plugin_config: An optional dict for the plugin's config.
Expand Down Expand Up @@ -548,13 +548,12 @@ def test_determine_audit_results_plugin_config(
):
plaintext_secret = 'some_plaintext_secret'
mock_get_raw_secret_value.return_value = plaintext_secret
baseline = self.get_audited_baseline(plugin_config, None)
baseline = self.get_audited_baseline(plugin_config=plugin_config, is_secret=None)

results = audit.determine_audit_results(baseline, '.secrets.baseline')

if plugin_config:
assert results['results']['HexHighEntropyString']['config'].items() \
>= plugin_config.items()
assert results['results']['HexHighEntropyString']['config'].items() \
>= plugin_config.items()

@pytest.mark.parametrize(
'is_secret, expected_audited_result',
Expand All @@ -574,7 +573,7 @@ def test_determine_audit_results_is_secret(
):
plaintext_secret = 'some_plaintext_secret'
mock_get_raw_secret_value.return_value = plaintext_secret
baseline = self.get_audited_baseline({}, is_secret)
baseline = self.get_audited_baseline(plugin_config={}, is_secret=is_secret)

results = audit.determine_audit_results(baseline, '.secrets.baseline')

Expand Down Expand Up @@ -617,7 +616,7 @@ def test_determine_audit_results_git_info(
mock_get_git_remotes.return_value = git_remotes
mock_get_git_sha.return_value = git_sha

baseline = self.get_audited_baseline({}, True)
baseline = self.get_audited_baseline(plugin_config={}, is_secret=True)

results = audit.determine_audit_results(baseline, '.secrets.baseline')

Expand All @@ -633,7 +632,7 @@ def test_determine_audit_results_secret_not_found(
mock_get_git_sha,
):
mock_get_raw_secret_value.side_effect = audit.SecretNotFoundOnSpecifiedLineError(1)
baseline = self.get_audited_baseline({}, True)
baseline = self.get_audited_baseline(plugin_config={}, is_secret=True)

whole_plaintext_line = 'a plaintext line'

Expand Down

0 comments on commit 69a109e

Please sign in to comment.