Skip to content

Commit 3748bcd

Browse files
author
elchananarb
committed
add test to check secret detection functionality
1 parent def0e44 commit 3748bcd

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

test/integration/data/mock-secret

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`# This file contains sensitive information
2+
API_KEY=sk_live_1234567890abcdefghijklmnopqrstuvwxyz
3+
SECRET_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
4+
PASSWORD=mySuperSecretPassword123!`

test/integration/pre_commit_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ func TestHooksPreCommitLicenseValidation(t *testing.T) {
5858
assert.Error(t, err, "Error validating scan types: Token decoding error: token is malformed: token contains an invalid number of segments")
5959
}
6060

61+
func TestHooksPreCommitSecretDetection(t *testing.T) {
62+
// Create a temporary directory and initialize git repository
63+
tmpDir, cleanup := setupTempDir(t)
64+
defer cleanup()
65+
66+
// Initialize Git repository
67+
execCmd(t, tmpDir, "git", "init")
68+
69+
// Install pre-commit hook
70+
_ = executeCmdNilAssertion(t, "Installing pre-commit hook", "hooks", "pre-commit", "secrets-install-git-hook")
71+
72+
// Copy the mock secret file to the temporary directory
73+
mockSecretPath := filepath.Join("data", "mock-secret")
74+
targetPath := filepath.Join(tmpDir, "mock-secret")
75+
76+
// Read the mock secret file
77+
secretContent, err := os.ReadFile(mockSecretPath)
78+
assert.NoError(t, err, "Failed to read mock secret file")
79+
80+
// Write the content to the target file
81+
err = os.WriteFile(targetPath, secretContent, 0644)
82+
assert.NoError(t, err, "Failed to write secret file")
83+
84+
// Add the file to git
85+
execCmd(t, tmpDir, "git", "add", "mock-secret")
86+
87+
// Try to commit the file - should fail due to secret detection
88+
cmd := exec.Command("git", "commit", "-m", "Add secret file")
89+
cmd.Dir = tmpDir
90+
output, err := cmd.CombinedOutput()
91+
assert.Error(t, err, "Commit should fail due to secret detection")
92+
assert.Contains(t, string(output), "Secret detection failed", "Error message should indicate secret detection failure")
93+
}
94+
6195
// Helper functions
6296
func execCmd(t *testing.T, dir string, name string, args ...string) {
6397
cmd := exec.Command(name, args...)

0 commit comments

Comments
 (0)