-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain_simple_test.go
More file actions
175 lines (160 loc) · 4.65 KB
/
Copy pathmain_simple_test.go
File metadata and controls
175 lines (160 loc) · 4.65 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
package traefikoidc
import (
"os"
"testing"
)
// TestIsTestMode tests the isTestMode function
func TestIsTestMode(t *testing.T) {
// Save original environment
originalSuppressLogs := os.Getenv("SUPPRESS_DIAGNOSTIC_LOGS")
originalGoTest := os.Getenv("GO_TEST")
defer func() {
os.Setenv("SUPPRESS_DIAGNOSTIC_LOGS", originalSuppressLogs)
os.Setenv("GO_TEST", originalGoTest)
}()
tests := []struct {
name string
suppressDiagnostics string
goTestEnv string
description string
}{
{
name: "SUPPRESS_DIAGNOSTIC_LOGS=1",
suppressDiagnostics: "1",
goTestEnv: "",
description: "Should return true when diagnostic logs are suppressed",
},
{
name: "GO_TEST=1",
suppressDiagnostics: "",
goTestEnv: "1",
description: "Should return true when GO_TEST is set",
},
{
name: "Both environment variables set",
suppressDiagnostics: "1",
goTestEnv: "1",
description: "Should return true when both env vars are set",
},
{
name: "No environment variables",
suppressDiagnostics: "",
goTestEnv: "",
description: "Should detect test mode from binary name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set environment variables
os.Setenv("SUPPRESS_DIAGNOSTIC_LOGS", tt.suppressDiagnostics)
os.Setenv("GO_TEST", tt.goTestEnv)
// Call function
result := isTestMode()
// The result should always be true during testing because
// os.Args[0] contains ".test" when running via go test
if !result {
t.Error("Expected isTestMode to return true during testing")
}
})
}
}
// TestIsTestMode_DefaultBehavior tests default detection
func TestIsTestMode_DefaultBehavior(t *testing.T) {
// Clear test-related environment variables
os.Unsetenv("SUPPRESS_DIAGNOSTIC_LOGS")
os.Unsetenv("GO_TEST")
// Function should still detect test mode from os.Args[0] or runtime
result := isTestMode()
if !result {
t.Error("Expected isTestMode to return true when running tests")
}
}
// TestVerifyAudience tests the verifyAudience function
func TestVerifyAudience(t *testing.T) {
tests := []struct {
tokenAudience interface{}
name string
expectedAudience string
description string
expectError bool
}{
{
name: "Audience matches",
tokenAudience: "test-client-id",
expectedAudience: "test-client-id",
expectError: false,
description: "Should pass when audience matches",
},
{
name: "Audience array contains expected",
tokenAudience: []interface{}{"other", "test-client-id", "another"},
expectedAudience: "test-client-id",
expectError: false,
description: "Should pass when audience array contains expected",
},
{
name: "Nil audience",
tokenAudience: nil,
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience is nil",
},
{
name: "Audience doesn't match",
tokenAudience: "different-client-id",
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience doesn't match",
},
{
name: "Audience array doesn't contain expected",
tokenAudience: []interface{}{"other", "another"},
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience array doesn't contain expected",
},
{
name: "Invalid audience type",
tokenAudience: 12345,
expectedAudience: "test-client-id",
expectError: true,
description: "Should fail when audience is not string or array",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := verifyAudience(tt.tokenAudience, tt.expectedAudience)
if tt.expectError {
if err == nil {
t.Errorf("Expected error for test case: %s", tt.description)
}
} else {
if err != nil {
t.Errorf("Unexpected error for test case: %s, error: %v", tt.description, err)
}
}
})
}
}
// Benchmark tests
func BenchmarkIsTestMode(b *testing.B) {
for i := 0; i < b.N; i++ {
isTestMode()
}
}
func BenchmarkVerifyAudience_String(b *testing.B) {
audience := "test-client-id"
expected := "test-client-id"
b.ResetTimer()
for i := 0; i < b.N; i++ {
verifyAudience(audience, expected)
}
}
func BenchmarkVerifyAudience_Array(b *testing.B) {
audience := []interface{}{"other", "test-client-id", "another"}
expected := "test-client-id"
b.ResetTimer()
for i := 0; i < b.N; i++ {
verifyAudience(audience, expected)
}
}