Skip to content

Commit

Permalink
Add test for force-printing context of a secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Zhou committed Aug 6, 2019
1 parent fc52867 commit c8b60c7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion detect_secrets/core/code_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_code_snippet(self, file_lines, line_number, lines_of_context=5):
)


class CodeSnippet:
class CodeSnippet(object):

def __init__(self, snippet, start_line, target_index):
"""
Expand Down
48 changes: 46 additions & 2 deletions tests/core/audit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,14 @@ def test_print_audit_results_none(

class TestPrintContext(object):

def run_logic(self, secret=None, secret_lineno=15, settings=None, should_find_secret=True):
def run_logic(
self,
secret=None,
secret_lineno=15,
settings=None,
should_find_secret=True,
force=False,
):
# Setup default arguments
if not secret:
secret = potential_secret_factory(
Expand Down Expand Up @@ -744,6 +751,7 @@ def run_logic(self, secret=None, secret_lineno=15, settings=None, should_find_se
count=1,
total=2,
plugin_settings=settings,
force=force,
)

@contextmanager
Expand Down Expand Up @@ -838,7 +846,7 @@ def test_secret_at_top_of_file(self, mock_printer):
""")[1:-1]

def test_secret_not_found(self, mock_printer):
def test_secret_not_found_no_force(self, mock_printer):
with self.mock_open(), pytest.raises(
audit.SecretNotFoundOnSpecifiedLineError,
):
Expand All @@ -850,6 +858,7 @@ def test_secret_not_found(self, mock_printer):
lineno=15,
),
should_find_secret=False,
force=False,
)

assert uncolor(mock_printer.message) == textwrap.dedent("""
Expand All @@ -863,6 +872,41 @@ def test_secret_not_found(self, mock_printer):
""")[1:-1]

def test_secret_not_found_force(self, mock_printer):
with self.mock_open(
line_containing_secret='THIS IS NOT AN RSA PRIVATE KEY',
):
self.run_logic(
secret=potential_secret_factory(
type_='Private Key',
filename='filenameA',
secret='BEGIN RSA PRIVATE KEY',
lineno=15,
),
should_find_secret=False,
force=True,
)

assert uncolor(mock_printer.message) == textwrap.dedent("""
Secret: 1 of 2
Filename: filenameA
Secret Type: Private Key
----------
10:a
11:b
12:c
13:d
14:e
15:THIS IS NOT AN RSA PRIVATE KEY
16:e
17:d
18:c
19:b
20:a
----------
""")[1:-1]

def test_hex_high_entropy_secret_in_yaml_file(self, mock_printer):
with self.mock_open(
line_containing_secret='api key: 123456789a',
Expand Down

0 comments on commit c8b60c7

Please sign in to comment.