-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmain_test.go
More file actions
487 lines (461 loc) · 10.7 KB
/
main_test.go
File metadata and controls
487 lines (461 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package main
import (
"reflect"
"testing"
)
func TestSplitWithEscapes(t *testing.T) {
testCases := []struct {
name string
input string
delimiter string
expected []string
}{
{
name: "simple comma split",
input: "a,b,c",
delimiter: ",",
expected: []string{"a", "b", "c"},
},
{
name: "escaped comma",
input: "a,b\\,c,d",
delimiter: ",",
expected: []string{"a", "b,c", "d"},
},
{
name: "email with escaped comma",
input: "[email protected]\\,backup,*@example.org",
delimiter: ",",
expected: []string{"[email protected],backup", "*@example.org"},
},
{
name: "glob pattern with escaped comma",
input: "admin.*@company\\,inc.*,*@example.com",
delimiter: ",",
expected: []string{"admin.*@company,inc.*", "*@example.com"},
},
{
name: "empty string",
input: "",
delimiter: ",",
expected: []string{},
},
{
name: "single item",
input: "single",
delimiter: ",",
expected: []string{"single"},
},
{
name: "no escapes needed",
input: "[email protected],[email protected]",
delimiter: ",",
expected: []string{"[email protected]", "[email protected]"},
},
{
name: "multiple escaped commas",
input: "a\\,b\\,c,d,e\\,f",
delimiter: ",",
expected: []string{"a,b,c", "d", "e,f"},
},
{
name: "whitespace trimming",
input: "a , b\\,c , d",
delimiter: ",",
expected: []string{"a", "b,c", "d"},
},
{
name: "different delimiter",
input: "a;b\\;c;d",
delimiter: ";",
expected: []string{"a", "b;c", "d"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := splitWithEscapes(tc.input, tc.delimiter)
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
})
}
}
func TestParseAttributeMap(t *testing.T) {
testCases := []struct {
name string
input string
expected map[string][]string
}{
{
name: "empty string",
input: "",
expected: map[string][]string{},
},
{
name: "single key-value pair",
input: "/groups=admin",
expected: map[string][]string{
"/groups": {"admin"},
},
},
{
name: "multiple values for same key",
input: "/groups=admin,/groups=users",
expected: map[string][]string{
"/groups": {"admin", "users"},
},
},
{
name: "multiple keys",
input: "/groups=admin,/department=engineering",
expected: map[string][]string{
"/groups": {"admin"},
"/department": {"engineering"},
},
},
{
name: "nested key with JSON pointer",
input: "/org/team=platform",
expected: map[string][]string{
"/org/team": {"platform"},
},
},
{
name: "glob pattern value",
input: "/groups=*-admins,/email=*@example.com",
expected: map[string][]string{
"/groups": {"*-admins"},
"/email": {"*@example.com"},
},
},
{
name: "whitespace trimming",
input: " /groups = admin , /role = editor ",
expected: map[string][]string{
"/groups": {"admin"},
"/role": {"editor"},
},
},
{
name: "invalid format - no equals sign",
input: "invalid",
expected: map[string][]string{},
},
{
name: "invalid format - empty key",
input: "=value",
expected: map[string][]string{},
},
{
name: "invalid format - empty value",
input: "/key=",
expected: map[string][]string{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := parseAttributeMap(tc.input)
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
})
}
}
func TestParseHeaderMapping(t *testing.T) {
testCases := []struct {
name string
input string
expected map[string]string
}{
{
name: "empty string",
input: "",
expected: map[string]string{},
},
{
name: "single mapping",
input: "/email:X-Forwarded-Email",
expected: map[string]string{
"/email": "X-Forwarded-Email",
},
},
{
name: "multiple mappings",
input: "/email:X-Forwarded-Email,/preferred_username:X-Forwarded-User",
expected: map[string]string{
"/email": "X-Forwarded-Email",
"/preferred_username": "X-Forwarded-User",
},
},
{
name: "nested JSON pointer",
input: "/org/team:X-Forwarded-Team",
expected: map[string]string{
"/org/team": "X-Forwarded-Team",
},
},
{
name: "whitespace trimming",
input: " /email : X-Forwarded-Email , /sub : X-Forwarded-Sub ",
expected: map[string]string{
"/email": "X-Forwarded-Email",
"/sub": "X-Forwarded-Sub",
},
},
{
name: "no colon - skipped",
input: "invalid",
expected: map[string]string{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := parseHeaderMapping(tc.input)
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
})
}
}
func TestGetEnvWithDefault(t *testing.T) {
testCases := []struct {
name string
key string
def string
expected string
setEnv bool
envValue string
}{
{
name: "env var not set",
key: "TEST_KEY_NOT_SET",
def: "default_value",
expected: "default_value",
setEnv: false,
},
{
name: "env var set",
key: "TEST_KEY_SET",
def: "default_value",
expected: "env_value",
setEnv: true,
envValue: "env_value",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup
if tc.setEnv {
t.Setenv(tc.key, tc.envValue)
}
// Test
result := getEnvWithDefault(tc.key, tc.def)
if result != tc.expected {
t.Errorf("Expected %s, got %s", tc.expected, result)
}
})
}
}
func TestGetEnvBoolWithDefault(t *testing.T) {
testCases := []struct {
name string
key string
def bool
expected bool
setEnv bool
envValue string
}{
{
name: "env var not set - default false",
key: "TEST_BOOL_NOT_SET",
def: false,
expected: false,
setEnv: false,
},
{
name: "env var not set - default true",
key: "TEST_BOOL_NOT_SET2",
def: true,
expected: true,
setEnv: false,
},
{
name: "env var set to 'true'",
key: "TEST_BOOL_TRUE",
def: false,
expected: true,
setEnv: true,
envValue: "true",
},
{
name: "env var set to 'TRUE'",
key: "TEST_BOOL_TRUE_UPPER",
def: false,
expected: true,
setEnv: true,
envValue: "TRUE",
},
{
name: "env var set to '1'",
key: "TEST_BOOL_ONE",
def: false,
expected: true,
setEnv: true,
envValue: "1",
},
{
name: "env var set to 'false'",
key: "TEST_BOOL_FALSE",
def: true,
expected: false,
setEnv: true,
envValue: "false",
},
{
name: "env var set to '0'",
key: "TEST_BOOL_ZERO",
def: true,
expected: false,
setEnv: true,
envValue: "0",
},
{
name: "env var set to other value",
key: "TEST_BOOL_OTHER",
def: true,
expected: false,
setEnv: true,
envValue: "other",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup
if tc.setEnv {
t.Setenv(tc.key, tc.envValue)
}
// Test
result := getEnvBoolWithDefault(tc.key, tc.def)
if result != tc.expected {
t.Errorf("Expected %t, got %t", tc.expected, result)
}
})
}
}
func TestNewRootCommand_HTTPStreamingOnlyFlag(t *testing.T) {
t.Setenv("HTTP_STREAMING_ONLY", "")
var streamingOnly bool
var receivedTargets []string
runner := proxyRunnerFunc(func(listen string,
tlsListen string,
autoTLS bool,
tlsHost string,
tlsDirectoryURL string,
tlsAcceptTOS bool,
tlsCertFile string,
tlsKeyFile string,
dataPath string,
repositoryBackend string,
repositoryDSN string,
externalURL string,
googleClientID string,
googleClientSecret string,
googleAllowedUsers []string,
googleAllowedWorkspaces []string,
githubClientID string,
githubClientSecret string,
githubAllowedUsers []string,
githubAllowedOrgs []string,
oidcConfigurationURL string,
oidcClientID string,
oidcClientSecret string,
oidcScopes []string,
oidcUserIDField string,
oidcProviderName string,
oidcAllowedUsers []string,
oidcAllowedUsersGlob []string,
oidcAllowedAttributes map[string][]string,
oidcAllowedAttributesGlob map[string][]string,
noProviderAutoSelect bool,
password string,
passwordHash string,
trustedProxy []string,
proxyHeaders []string,
proxyBearerToken string,
proxyTarget []string,
httpStreamingOnly bool,
headerMapping map[string]string,
headerMappingBase string,
) error {
streamingOnly = httpStreamingOnly
receivedTargets = proxyTarget
return nil
})
cmd := newRootCommand(runner)
cmd.SetArgs([]string{"--http-streaming-only", "http://backend"})
if err := cmd.Execute(); err != nil {
t.Fatalf("expected command to succeed, got error: %v", err)
}
if !streamingOnly {
t.Fatalf("expected httpStreamingOnly to be true when flag is set")
}
if len(receivedTargets) != 1 || receivedTargets[0] != "http://backend" {
t.Fatalf("expected proxyTarget to receive CLI args, got %v", receivedTargets)
}
}
func TestNewRootCommand_HTTPStreamingOnlyFromEnv(t *testing.T) {
t.Setenv("HTTP_STREAMING_ONLY", "true")
var streamingOnly bool
runner := proxyRunnerFunc(func(listen string,
tlsListen string,
autoTLS bool,
tlsHost string,
tlsDirectoryURL string,
tlsAcceptTOS bool,
tlsCertFile string,
tlsKeyFile string,
dataPath string,
repositoryBackend string,
repositoryDSN string,
externalURL string,
googleClientID string,
googleClientSecret string,
googleAllowedUsers []string,
googleAllowedWorkspaces []string,
githubClientID string,
githubClientSecret string,
githubAllowedUsers []string,
githubAllowedOrgs []string,
oidcConfigurationURL string,
oidcClientID string,
oidcClientSecret string,
oidcScopes []string,
oidcUserIDField string,
oidcProviderName string,
oidcAllowedUsers []string,
oidcAllowedUsersGlob []string,
oidcAllowedAttributes map[string][]string,
oidcAllowedAttributesGlob map[string][]string,
noProviderAutoSelect bool,
password string,
passwordHash string,
trustedProxy []string,
proxyHeaders []string,
proxyBearerToken string,
proxyTarget []string,
httpStreamingOnly bool,
headerMapping map[string]string,
headerMappingBase string,
) error {
streamingOnly = httpStreamingOnly
return nil
})
cmd := newRootCommand(runner)
cmd.SetArgs([]string{"http://backend"})
if err := cmd.Execute(); err != nil {
t.Fatalf("expected command to succeed, got error: %v", err)
}
if !streamingOnly {
t.Fatalf("expected httpStreamingOnly to default to true from env var")
}
}