Skip to content

Commit b77ceec

Browse files
author
Aaron Loo
committed
fixing tests
1 parent afe4725 commit b77ceec

File tree

2 files changed

+50
-32
lines changed

2 files changed

+50
-32
lines changed

detect_secrets/core/audit.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,19 @@ def _save_baseline_to_file(filename, data): # pragma: no cover
154154
def _secret_generator(baseline):
155155
"""Generates secrets to audit, from the baseline"""
156156
current_secret_index = 1
157-
num_secrets_to_parse = sum(map(
158-
lambda filename: len(list(filter(
159-
lambda secret: not hasattr(secret, 'is_secret'),
160-
baseline['results'][filename],
161-
))),
162-
baseline['results'],
163-
))
157+
num_secrets_to_parse = sum(
158+
map(
159+
lambda filename: len(
160+
list(
161+
filter(
162+
lambda secret: not hasattr(secret, 'is_secret'),
163+
baseline['results'][filename],
164+
),
165+
),
166+
),
167+
baseline['results'],
168+
),
169+
)
164170

165171
for filename, secrets in baseline['results'].items():
166172
for secret in secrets:
@@ -171,8 +177,6 @@ def _secret_generator(baseline):
171177

172178
current_secret_index += 1
173179

174-
break
175-
176180

177181
def _get_secret_with_context(
178182
filename,
@@ -271,6 +275,13 @@ def _highlight_secret(secret_line, secret, filename, plugin_settings):
271275
# We only want to highlight the right one.
272276
if secret_obj.secret_hash == secret['hashed_secret']:
273277
break
278+
else:
279+
raise SecretNotFoundOnSpecifiedLineError(
280+
textwrap.dedent("""
281+
ERROR: Secret not found on specified line number!
282+
Try recreating your baseline to fix this issue.
283+
""")[1:-1],
284+
)
274285

275286
index_of_secret = secret_line.index(raw_secret)
276287
return '{}{}{}'.format(
@@ -293,15 +304,6 @@ def _raw_secret_generator(plugin, secret_line):
293304
for raw_secret in plugin.secret_generator(secret_line):
294305
yield raw_secret
295306

296-
# It hits here if the secret has been moved, from the original
297-
# line number listed in the baseline.
298-
raise SecretNotFoundOnSpecifiedLineError(
299-
textwrap.dedent("""
300-
ERROR: Secret not found on specified line number!
301-
Try recreating your baseline to fix this issue.
302-
""")[1:-1],
303-
)
304-
305307

306308
def _get_user_decision(prompt_secret_decision=True):
307309
"""

tests/core/audit_test.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import string
44
import textwrap
55
from contextlib import contextmanager
6+
from copy import deepcopy
67

78
import mock
89
import pytest
@@ -33,29 +34,39 @@ def test_quit_before_making_decision(self, mock_printer):
3334
)
3435

3536
def test_nothing_to_audit(self, mock_printer):
36-
modified_baseline = self.baseline.copy()
37+
modified_baseline = deepcopy(self.baseline)
3738
modified_baseline['results']['filenameA'][0]['is_secret'] = True
3839
modified_baseline['results']['filenameA'][1]['is_secret'] = False
40+
modified_baseline['results']['filenameB'][0]['is_secret'] = False
3941

4042
with self.mock_env(baseline=modified_baseline):
4143
audit.audit_baseline('will_be_mocked')
4244

4345
assert mock_printer.message == 'Nothing to audit!\n'
4446

4547
def test_making_decisions(self, mock_printer):
46-
modified_baseline = self.baseline.copy()
47-
modified_baseline['results']['filenameA'][0]['is_secret'] = True
48-
modified_baseline['results']['filenameA'][1]['is_secret'] = False
48+
modified_baseline = deepcopy(self.baseline)
49+
50+
# Need to do it this way, because dictionaries are not ordered:
51+
# meaning, that if we hard-code results to certain filenames, it's
52+
# going to be a flakey test.
53+
values_to_inject = [True, False, False]
54+
for secrets in modified_baseline['results'].values():
55+
for secret in secrets:
56+
secret['is_secret'] = values_to_inject.pop(0)
4957

50-
self.run_logic(['y', 'n'], modified_baseline)
58+
self.run_logic(['y', 'n', 'n'], modified_baseline)
5159

5260
assert mock_printer.message == (
5361
'Saving progress...\n'
5462
)
5563

5664
def test_quit_half_way(self, mock_printer):
57-
modified_baseline = self.baseline.copy()
58-
modified_baseline['results']['filenameA'][0]['is_secret'] = False
65+
modified_baseline = deepcopy(self.baseline)
66+
67+
for secrets in modified_baseline['results'].values():
68+
secrets[0]['is_secret'] = False
69+
break
5970

6071
self.run_logic(['n', 'q'], modified_baseline)
6172

@@ -65,10 +76,16 @@ def test_quit_half_way(self, mock_printer):
6576
)
6677

6778
def test_skip_decision(self, mock_printer):
68-
modified_baseline = self.baseline.copy()
69-
modified_baseline['results']['filenameA'][1]['is_secret'] = True
79+
modified_baseline = deepcopy(self.baseline)
80+
81+
values_to_inject = [None, True, True]
82+
for secrets in modified_baseline['results'].values():
83+
for secret in secrets:
84+
value = values_to_inject.pop(0)
85+
if value:
86+
secret['is_secret'] = value
7087

71-
self.run_logic(['s', 'y'], modified_baseline)
88+
self.run_logic(['s', 'y', 'y'], modified_baseline)
7289

7390
assert mock_printer.message == (
7491
'Saving progress...\n'
@@ -439,17 +456,16 @@ def mock_user_input(inputs):
439456
:type inputs: list
440457
:param inputs: list of user choices
441458
"""
442-
current_case = {'index': 0} # needed, because py2 doesn't have nonlocal
443-
444459
class InputShim(object):
445460
def __init__(self):
446461
self.message = ''
462+
self.index = 0
447463

448464
def get_user_input(self, *args, **kwargs):
449465
self.message += args[0]
450466

451-
output = inputs[current_case['index']]
452-
current_case['index'] += 1
467+
output = inputs[self.index]
468+
self.index += 1
453469

454470
return output
455471

0 commit comments

Comments
 (0)