|
6 | 6 | "testing" |
7 | 7 |
|
8 | 8 | "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | func Test_ConfigOptions_SecretsAndVars(t *testing.T) { |
@@ -46,3 +47,108 @@ func Test_ConfigOptions_SecretsAndVars(t *testing.T) { |
46 | 47 | assert.Equal(t, expectedVariables, variables) |
47 | 48 | assert.Equal(t, expectedSecrets, secrets) |
48 | 49 | } |
| 50 | + |
| 51 | +// Test_ConfigOptions_EscapedValues tests that JSON-escaped string values are preserved |
| 52 | +// when merging project variables and secrets. |
| 53 | +// This addresses the issue where values like `["api://..."]` need to be escaped |
| 54 | +// to `[\"api://...\"]` when sent to remote pipelines to prevent them from being |
| 55 | +// interpreted as JSON arrays instead of strings. |
| 56 | +func Test_ConfigOptions_EscapedValues(t *testing.T) { |
| 57 | + projectVariables := []string{"AzureAd_TokenValidationParameters_ValidAudiences"} |
| 58 | + projectSecrets := []string{} |
| 59 | + |
| 60 | + initialVariables := map[string]string{} |
| 61 | + initialSecrets := map[string]string{} |
| 62 | + |
| 63 | + // This simulates a value that is read from config.json. |
| 64 | + // After JSON unmarshaling, the value `"[\"api://...\"]"` becomes `["api://..."]` (backslashes consumed) |
| 65 | + // We need to re-escape it before sending to the pipeline so it's treated as a string, not an array |
| 66 | + env := map[string]string{ |
| 67 | + "AzureAd_TokenValidationParameters_ValidAudiences": "[\"api://e935a748-8b59-4c26-a59c-9bcc83f5ab57\"]", |
| 68 | + } |
| 69 | + |
| 70 | + variables, secrets, err := mergeProjectVariablesAndSecrets( |
| 71 | + projectVariables, projectSecrets, initialVariables, initialSecrets, nil, env) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + // After escaping, the value should have backslashes to prevent JSON parsing in the pipeline |
| 75 | + // The value becomes: [\"api://e935a748-8b59-4c26-a59c-9bcc83f5ab57\"] |
| 76 | + expectedVariables := map[string]string{ |
| 77 | + "AzureAd_TokenValidationParameters_ValidAudiences": "[\\\"api://e935a748-8b59-4c26-a59c-9bcc83f5ab57\\\"]", |
| 78 | + } |
| 79 | + expectedSecrets := map[string]string{} |
| 80 | + |
| 81 | + assert.Equal(t, expectedVariables, variables) |
| 82 | + assert.Equal(t, expectedSecrets, secrets) |
| 83 | +} |
| 84 | + |
| 85 | +// Test_ConfigOptions_SimpleValues tests that simple string values are properly escaped |
| 86 | +func Test_ConfigOptions_SimpleValues(t *testing.T) { |
| 87 | + projectVariables := []string{"SIMPLE_VAR", "VAR_WITH_QUOTES"} |
| 88 | + projectSecrets := []string{} |
| 89 | + |
| 90 | + initialVariables := map[string]string{} |
| 91 | + initialSecrets := map[string]string{} |
| 92 | + |
| 93 | + env := map[string]string{ |
| 94 | + "SIMPLE_VAR": "simple-value", |
| 95 | + "VAR_WITH_QUOTES": "value with \"quotes\"", |
| 96 | + } |
| 97 | + |
| 98 | + variables, secrets, err := mergeProjectVariablesAndSecrets( |
| 99 | + projectVariables, projectSecrets, initialVariables, initialSecrets, nil, env) |
| 100 | + require.NoError(t, err) |
| 101 | + |
| 102 | + // Simple values remain mostly the same, quotes get escaped |
| 103 | + expectedVariables := map[string]string{ |
| 104 | + "SIMPLE_VAR": "simple-value", |
| 105 | + "VAR_WITH_QUOTES": "value with \\\"quotes\\\"", |
| 106 | + } |
| 107 | + expectedSecrets := map[string]string{} |
| 108 | + |
| 109 | + assert.Equal(t, expectedVariables, variables) |
| 110 | + assert.Equal(t, expectedSecrets, secrets) |
| 111 | +} |
| 112 | + |
| 113 | +// Test_escapeValuesForPipeline tests the escape function directly |
| 114 | +func Test_escapeValuesForPipeline(t *testing.T) { |
| 115 | + tests := []struct { |
| 116 | + name string |
| 117 | + input string |
| 118 | + expected string |
| 119 | + }{ |
| 120 | + { |
| 121 | + name: "JSON array string", |
| 122 | + input: "[\"api://guid\"]", |
| 123 | + expected: "[\\\"api://guid\\\"]", |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "Simple string", |
| 127 | + input: "simple", |
| 128 | + expected: "simple", |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "String with quotes", |
| 132 | + input: "value with \"quotes\"", |
| 133 | + expected: "value with \\\"quotes\\\"", |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "String with backslashes", |
| 137 | + input: "path\\to\\file", |
| 138 | + expected: "path\\\\to\\\\file", |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "Empty string", |
| 142 | + input: "", |
| 143 | + expected: "", |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + for _, tt := range tests { |
| 148 | + t.Run(tt.name, func(t *testing.T) { |
| 149 | + values := map[string]string{"test": tt.input} |
| 150 | + escapeValuesForPipeline(values) |
| 151 | + assert.Equal(t, tt.expected, values["test"]) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments