Skip to content

Commit 01d40fa

Browse files
author
elchananarb
committed
add TestUpdatePreCommitHook
1 parent 476d2ce commit 01d40fa

1 file changed

Lines changed: 27 additions & 83 deletions

File tree

test/integration/pre_commit_test.go

Lines changed: 27 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import (
99
"strings"
1010
"testing"
1111

12-
"github.com/checkmarx/ast-cli/internal/params"
1312
"github.com/stretchr/testify/assert"
1413
)
1514

16-
func TestHooksPreCommitFullIntegration(t *testing.T) {
15+
func TestInstallAndUninstallPreCommitHook(t *testing.T) {
1716
tmpDir, cleanup := setupTempDir(t)
1817
defer cleanup()
1918

@@ -36,81 +35,42 @@ func TestHooksPreCommitFullIntegration(t *testing.T) {
3635
assert.NotContains(t, string(data), "cx-secret-detection", "Hook content should be cleared after uninstall")
3736
}
3837

39-
func TestHooksPreCommitLicenseValidation(t *testing.T) {
40-
// Store original environment variables
41-
originals := getOriginalEnvVars()
42-
43-
// Set only the API key to invalid value
44-
setEnvVars(map[string]string{
45-
params.AstAPIKeyEnv: invalidAPIKey,
46-
})
47-
48-
// Restore original environment variables after test
49-
defer setEnvVars(originals)
50-
51-
// Define command arguments for installing the pre-commit hook
52-
args := []string{
53-
"hooks", "pre-commit", "secrets-install-git-hook",
54-
}
55-
56-
// Execute the command and verify it fails with the expected error
57-
err, _ := executeCommand(t, args...)
58-
assert.Error(t, err, "Error validating scan types: Token decoding error: token is malformed: token contains an invalid number of segments")
59-
}
60-
61-
func TestHooksPreCommitSecretDetection(t *testing.T) {
62-
// Create a temporary directory and initialize git repository
38+
func TestUpdatePreCommitHook(t *testing.T) {
6339
tmpDir, cleanup := setupTempDir(t)
6440
defer cleanup()
6541

6642
// Initialize Git repository
6743
execCmd(t, tmpDir, "git", "init")
6844

69-
// Configure Git user
70-
execCmd(t, tmpDir, "git", "config", "user.email", "test@example.com")
71-
execCmd(t, tmpDir, "git", "config", "user.name", "Test User")
72-
73-
// Install pre-commit hook
45+
// Install pre-commit hook locally
7446
_ = executeCmdNilAssertion(t, "Installing pre-commit hook", "hooks", "pre-commit", "secrets-install-git-hook")
7547

76-
// Create a test file with a secret
77-
testSecret := "password=secret123"
78-
targetPath := filepath.Join(tmpDir, "test-secret.txt")
79-
err := os.WriteFile(targetPath, []byte(testSecret), 0644)
80-
assert.NoError(t, err, "Failed to write secret file")
81-
82-
// Add the file to git
83-
execCmd(t, tmpDir, "git", "add", "test-secret.txt")
84-
85-
// Try to commit the file - should fail due to secret detection
86-
cmd := exec.Command("git", "commit", "-m", "Add secret file")
87-
cmd.Dir = tmpDir
88-
output, err := cmd.CombinedOutput()
89-
assert.Error(t, err, "Commit should fail due to secret detection")
90-
assert.Contains(t, string(output), "Secret detection failed", "Error message should indicate secret detection failure")
48+
// Update pre-commit hook
49+
output := executeCmdNilAssertion(t, "Updating pre-commit hook", "hooks", "pre-commit", "secrets-update-git-hook")
50+
assert.Contains(t, output, "updated successfully", "Hook should update successfully")
9151
}
9252

93-
func TestHooksPreCommitSecretsScan(t *testing.T) {
94-
// Store original environment variables
95-
originals := getOriginalEnvVars()
96-
97-
// Set only the API key to invalid value
98-
setEnvVars(map[string]string{
99-
params.AstAPIKeyEnv: invalidAPIKey,
100-
})
101-
102-
// Restore original environment variables after test
103-
defer setEnvVars(originals)
104-
105-
// Define command arguments for running the secrets scan
106-
args := []string{
107-
"hooks", "pre-commit", "secrets-scan",
108-
}
109-
110-
// Execute the command and verify it fails with the expected error
111-
err, _ := executeCommand(t, args...)
112-
assert.Error(t, err, "Error validating scan types: Token decoding error: token is malformed: token contains an invalid number of segments")
113-
}
53+
//func TestHooksPreCommitLicenseValidation(t *testing.T) {
54+
// // Store original environment variables
55+
// originals := getOriginalEnvVars()
56+
//
57+
// // Set only the API key to invalid value
58+
// setEnvVars(map[string]string{
59+
// params.AstAPIKeyEnv: invalidAPIKey,
60+
// })
61+
//
62+
// // Restore original environment variables after test
63+
// defer setEnvVars(originals)
64+
//
65+
// // Define command arguments for installing the pre-commit hook
66+
// args := []string{
67+
// "hooks", "pre-commit", "secrets-install-git-hook",
68+
// }
69+
//
70+
// // Execute the command and verify it fails with the expected error
71+
// err, _ := executeCommand(t, args...)
72+
// assert.Error(t, err, "Error validating scan types: Token decoding error: token is malformed: token contains an invalid number of segments")
73+
//}
11474

11575
// Helper functions
11676
func execCmd(t *testing.T, dir string, name string, args ...string) {
@@ -120,22 +80,6 @@ func execCmd(t *testing.T, dir string, name string, args ...string) {
12080
assert.NoError(t, err, "Failed command %s: %s", strings.Join(cmd.Args, " "), string(output))
12181
}
12282

123-
func parseResultIDs(output string) []string {
124-
// Mock parsing function: Extract IDs from output.
125-
// Replace with real parsing logic based on actual scan output format
126-
lines := strings.Split(output, "\n")
127-
var ids []string
128-
for _, line := range lines {
129-
if strings.Contains(line, "Result ID:") {
130-
parts := strings.Split(line, ":")
131-
if len(parts) == 2 {
132-
ids = append(ids, strings.TrimSpace(parts[1]))
133-
}
134-
}
135-
}
136-
return ids
137-
}
138-
13983
func setupTempDir(t *testing.T) (string, func()) {
14084
originalWD, err := os.Getwd()
14185
assert.NoError(t, err)

0 commit comments

Comments
 (0)