Skip to content

Commit def0e44

Browse files
author
elchananarb
committed
Update pre_commit_test.go
1 parent 7f1dfad commit def0e44

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

test/integration/pre_commit_test.go

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"github.com/checkmarx/ast-cli/internal/params"
1213
"github.com/stretchr/testify/assert"
1314
)
1415

@@ -36,56 +37,25 @@ func TestHooksPreCommitFullIntegration(t *testing.T) {
3637
}
3738

3839
func TestHooksPreCommitLicenseValidation(t *testing.T) {
39-
tmpDir, cleanup := setupTempDir(t)
40-
defer cleanup()
41-
42-
// Initialize Git repository
43-
execCmd(t, tmpDir, "git", "init")
40+
// Store original environment variables
41+
originals := getOriginalEnvVars()
4442

45-
// Test case 1: Without license (should fail)
46-
t.Run("Without Enterprise License", func(t *testing.T) {
47-
// Set environment to simulate no license
48-
originalEnv := os.Getenv("CX_LICENSE")
49-
os.Setenv("CX_LICENSE", "")
50-
defer os.Setenv("CX_LICENSE", originalEnv)
51-
52-
// Try to install pre-commit hook
53-
args := []string{"hooks", "pre-commit", "secrets-install-git-hook"}
54-
err, output := executeCommand(t, args...)
55-
56-
// Verify error message
57-
assert.Error(t, err)
58-
assert.Contains(t, output.String(), "License validation failed")
59-
assert.Contains(t, output.String(), "Please verify your CxOne License includes Enterprise Secrets")
60-
61-
// Verify hook was not installed
62-
hookPath := filepath.Join(tmpDir, ".git", "hooks", "pre-commit")
63-
_, err = os.Stat(hookPath)
64-
assert.True(t, os.IsNotExist(err), "Hook should not be installed without license")
43+
// Set only the API key to invalid value
44+
setEnvVars(map[string]string{
45+
params.AstAPIKeyEnv: invalidAPIKey,
6546
})
6647

67-
// Test case 2: With license (should succeed)
68-
t.Run("With Enterprise License", func(t *testing.T) {
69-
// Set environment to simulate valid license
70-
originalEnv := os.Getenv("CX_LICENSE")
71-
os.Setenv("CX_LICENSE", "valid-license-key") // Replace with actual license
72-
defer os.Setenv("CX_LICENSE", originalEnv)
48+
// Restore original environment variables after test
49+
defer setEnvVars(originals)
7350

74-
// Install pre-commit hook
75-
_ = executeCmdNilAssertion(t, "Installing pre-commit hook", "hooks", "pre-commit", "secrets-install-git-hook")
76-
77-
// Verify hook installation
78-
hookPath := filepath.Join(tmpDir, ".git", "hooks", "pre-commit")
79-
assert.FileExists(t, hookPath, "Hook should be installed with valid license")
80-
81-
// Verify hook content
82-
data, err := os.ReadFile(hookPath)
83-
assert.NoError(t, err)
84-
assert.Contains(t, string(data), "cx-secret-detection", "Hook should contain secret detection code")
51+
// Define command arguments for installing the pre-commit hook
52+
args := []string{
53+
"hooks", "pre-commit", "secrets-install-git-hook",
54+
}
8555

86-
// Clean up - uninstall hook
87-
_ = executeCmdNilAssertion(t, "Uninstalling pre-commit hook", "hooks", "pre-commit", "secrets-uninstall-git-hook")
88-
})
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")
8959
}
9060

9161
// Helper functions

0 commit comments

Comments
 (0)