1
1
import unittest
2
2
import os
3
+ import sys
4
+ import json
5
+ import io
3
6
from truffleHog import truffleHog
4
7
from mock import patch
5
8
from mock import MagicMock
@@ -24,6 +27,37 @@ def test_unicode_expection(self):
24
27
except UnicodeEncodeError :
25
28
self .fail ("Unicode print error" )
26
29
30
+ def test_return_correct_commit_hash (self ):
31
+ # Start at commit d15627104d07846ac2914a976e8e347a663bbd9b, which
32
+ # is immediately followed by a secret inserting commit:
33
+ # https://github.com/dxa4481/truffleHog/commit/9ed54617547cfca783e0f81f8dc5c927e3d1e345
34
+ since_commit = 'd15627104d07846ac2914a976e8e347a663bbd9b'
35
+ commit_w_secret = '9ed54617547cfca783e0f81f8dc5c927e3d1e345'
36
+ cross_valdiating_commit_w_secret_comment = 'OH no a secret'
37
+
38
+ json_result = ''
39
+ if sys .version_info >= (3 ,):
40
+ tmp_stdout = io .StringIO ()
41
+ else :
42
+ tmp_stdout = io .BytesIO ()
43
+ bak_stdout = sys .stdout
44
+
45
+ # Redirect STDOUT, run scan and re-establish STDOUT
46
+ sys .stdout = tmp_stdout
47
+ try :
48
+ truffleHog .find_strings ("https://github.com/dxa4481/truffleHog.git" ,
49
+ since_commit = since_commit , printJson = True , surpress_output = False )
50
+ finally :
51
+ sys .stdout = bak_stdout
52
+
53
+ json_result_list = tmp_stdout .getvalue ().split ('\n ' )
54
+ results = [json .loads (r ) for r in json_result_list if bool (r .strip ())]
55
+ filtered_results = list (filter (lambda r : r ['commitHash' ] == commit_w_secret , results ))
56
+ self .assertEqual (1 , len (filtered_results ))
57
+ self .assertEqual (commit_w_secret , filtered_results [0 ]['commitHash' ])
58
+ # Additionally, we cross-validate the commit comment matches the expected comment
59
+ self .assertEqual (cross_valdiating_commit_w_secret_comment , filtered_results [0 ]['commit' ].strip ())
60
+
27
61
@patch ('truffleHog.truffleHog.clone_git_repo' )
28
62
@patch ('truffleHog.truffleHog.Repo' )
29
63
def test_branch (self , repo_const_mock , clone_git_repo ):
0 commit comments