|
9 | 9 | "strings" |
10 | 10 | "testing" |
11 | 11 |
|
| 12 | + "github.com/checkmarx/ast-cli/internal/params" |
12 | 13 | "github.com/stretchr/testify/assert" |
13 | 14 | ) |
14 | 15 |
|
@@ -36,56 +37,25 @@ func TestHooksPreCommitFullIntegration(t *testing.T) { |
36 | 37 | } |
37 | 38 |
|
38 | 39 | 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() |
44 | 42 |
|
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, |
65 | 46 | }) |
66 | 47 |
|
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) |
73 | 50 |
|
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 | + } |
85 | 55 |
|
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") |
89 | 59 | } |
90 | 60 |
|
91 | 61 | // Helper functions |
|
0 commit comments