|
1 |
| -////go:build integration |
2 |
| -// |
3 |
| -//package integration |
4 |
| -// |
5 |
| -//import ( |
6 |
| -// "gotest.tools/assert" |
7 |
| -// "os" |
8 |
| -// "strings" |
9 |
| -// "testing" |
10 |
| -//) |
11 |
| -// |
12 |
| -//var filePath = "data/config.yaml" |
13 |
| -// |
14 |
| -//func TestIntegration_LoadConfiguration_EnvVarConfigFilePath(t *testing.T) { |
15 |
| -// os.Setenv("CX_CONFIG_FILE_PATH", filePath) |
16 |
| -// defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
17 |
| -// |
18 |
| -// cmd, buffer := createRedirectedTestCommand(t) |
19 |
| -// |
20 |
| -// err := execute(cmd, "configure", "show") |
21 |
| -// assert.NilError(t, err) |
22 |
| -// |
23 |
| -// output := buffer.String() |
24 |
| -// if !strings.Contains(output, "cx_base_uri: https://example.com") { |
25 |
| -// t.Errorf("expected output to contain 'cx_base_uri: https://example.com', but it did not") |
26 |
| -// } |
27 |
| -//} |
28 |
| -// |
29 |
| -//func TestIntegration_LoadConfiguration_FileNotFound(t *testing.T) { |
30 |
| -// // Set an invalid file path |
31 |
| -// os.Setenv("CX_CONFIG_FILE_PATH", "data/nonexistent_config.yaml") |
32 |
| -// defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
33 |
| -// |
34 |
| -// cmd, buffer := createRedirectedTestCommand(t) |
35 |
| -// |
36 |
| -// // Execute the command |
37 |
| -// err := execute(cmd, "configure", "show") |
38 |
| -// |
39 |
| -// // Verify that an error occurred |
40 |
| -// assert.ErrorContains(t, err, "The specified file does not exist") |
41 |
| -// |
42 |
| -// // Verify the output contains the expected error message |
43 |
| -// output := buffer.String() |
44 |
| -// if !strings.Contains(output, "The specified file does not exist") { |
45 |
| -// t.Errorf("expected output to contain 'The specified file does not exist', but it did not") |
46 |
| -// } |
47 |
| -//} |
48 |
| -// |
49 |
| -//func TestIntegration_LoadConfiguration_FileWithoutPermission_UsingConfigFile(t *testing.T) { |
50 |
| -// |
51 |
| -// // Set the file to have no permissions |
52 |
| -// if err := os.Chmod(filePath, 0000); err != nil { |
53 |
| -// t.Fatalf("failed to set file permissions: %v", err) |
54 |
| -// } |
55 |
| -// defer os.Chmod(filePath, 0644) // Restore permissions for cleanup |
56 |
| -// |
57 |
| -// // Set the environment variable to point to the restricted file |
58 |
| -// os.Setenv("CX_CONFIG_FILE_PATH", filePath) |
59 |
| -// defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
60 |
| -// |
61 |
| -// cmd, buffer := createRedirectedTestCommand(t) |
62 |
| -// |
63 |
| -// // Execute the command |
64 |
| -// err := execute(cmd, "configure", "show") |
65 |
| -// |
66 |
| -// // Verify that an error occurred |
67 |
| -// assert.ErrorContains(t, err, "Access to the specified file is restricted") |
68 |
| -// |
69 |
| -// // Verify the output contains the expected error message |
70 |
| -// output := buffer.String() |
71 |
| -// if !strings.Contains(output, "Access to the specified file is restricted") { |
72 |
| -// t.Errorf("expected output to contain 'Access to the specified file is restricted', but it did not") |
73 |
| -// } |
74 |
| -//} |
75 |
| -// |
76 |
| -// |
77 |
| -//func TestIntegration_SetConfigProperty_EnvVarConfigFilePath(t *testing.T) { |
78 |
| -// // Set environment variable |
79 |
| -// os.Setenv("CX_CONFIG_FILE_PATH", filePath) |
80 |
| -// defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
81 |
| -// |
82 |
| -// // Create command |
83 |
| -// cmd, buffer := createRedirectedTestCommand(t) |
84 |
| -// |
85 |
| -// // Execute command |
86 |
| -// err := execute(cmd, "configure", "set", "--prop-name", "new_key", "--prop-value", "new_value") |
87 |
| -// assert.NilError(t, err) |
88 |
| -// |
89 |
| -// // Verify output |
90 |
| -// output := buffer.String() |
91 |
| -// asserts.Contains(t, output, "Setting property [new_key] to value [new_value]") |
92 |
| -// |
93 |
| -// // Verify configuration |
94 |
| -// asserts.Equal(t, viper.GetString("new_key"), "new_value") |
95 |
| -//} |
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/checkmarx/ast-cli/internal/wrappers/configuration" |
| 7 | + "github.com/spf13/viper" |
| 8 | + "gotest.tools/assert" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +const filePath = "data/config.yaml" |
| 15 | + |
| 16 | +func TestLoadConfiguration_EnvVarConfigFilePath(t *testing.T) { |
| 17 | + os.Setenv("CX_CONFIG_FILE_PATH", filePath) |
| 18 | + defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
| 19 | + |
| 20 | + _ = viper.BindEnv("CX_CONFIG_FILE_PATH") |
| 21 | + err := configuration.LoadConfiguration() |
| 22 | + assert.NilError(t, err) |
| 23 | +} |
| 24 | + |
| 25 | +func TestLoadConfiguration_FileNotFound(t *testing.T) { |
| 26 | + os.Setenv("CX_CONFIG_FILE_PATH", "data/nonexistent_config.yaml") |
| 27 | + defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
| 28 | + |
| 29 | + _ = viper.BindEnv("CX_CONFIG_FILE_PATH") |
| 30 | + var err error |
| 31 | + err = configuration.LoadConfiguration() |
| 32 | + assert.ErrorContains(t, err, "The specified file does not exist") |
| 33 | +} |
| 34 | +func TestLoadConfiguration_ValidDirectory(t *testing.T) { |
| 35 | + validDirPath := "data" |
| 36 | + os.Setenv("CX_CONFIG_FILE_PATH", validDirPath) |
| 37 | + defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
| 38 | + |
| 39 | + _ = viper.BindEnv("CX_CONFIG_FILE_PATH") |
| 40 | + err := configuration.LoadConfiguration() |
| 41 | + assert.ErrorContains(t, err, "The specified path points to a directory") |
| 42 | +} |
| 43 | +func TestLoadConfiguration_FileWithoutPermission_UsingConfigFile(t *testing.T) { |
| 44 | + if err := os.Chmod(filePath, 0000); err != nil { |
| 45 | + t.Fatalf("failed to set file permissions: %v", err) |
| 46 | + } |
| 47 | + defer os.Chmod(filePath, 0644) |
| 48 | + |
| 49 | + os.Setenv("CX_CONFIG_FILE_PATH", filePath) |
| 50 | + defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
| 51 | + |
| 52 | + _ = viper.BindEnv("CX_CONFIG_FILE_PATH") |
| 53 | + err := configuration.LoadConfiguration() |
| 54 | + |
| 55 | + assert.ErrorContains(t, err, "Access to the specified file is restricted") |
| 56 | +} |
| 57 | + |
| 58 | +func TestSetConfigProperty_EnvVarConfigFilePath(t *testing.T) { |
| 59 | + os.Setenv("CX_CONFIG_FILE_PATH", filePath) |
| 60 | + defer os.Unsetenv("CX_CONFIG_FILE_PATH") |
| 61 | + |
| 62 | + _ = viper.BindEnv("CX_CONFIG_FILE_PATH") |
| 63 | + err := configuration.LoadConfiguration() |
| 64 | + assert.NilError(t, err) |
| 65 | + |
| 66 | + err, _ = executeCommand(t, "configure", "set", "--prop-name", "cx_client_id", "--prop-value", "dummy-client_id") |
| 67 | + assert.NilError(t, err) |
| 68 | + |
| 69 | + content, err := os.ReadFile(filePath) |
| 70 | + assert.NilError(t, err) |
| 71 | + assert.Assert(t, strings.Contains(string(content), "dummy-client_id")) |
| 72 | + |
| 73 | + err, _ = executeCommand(t, "configure", "set", "--prop-name", "cx_client_id", "--prop-value", "example_client_id") |
| 74 | + assert.NilError(t, err) |
| 75 | +} |
0 commit comments