-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
343 lines (311 loc) · 8.08 KB
/
config_test.go
File metadata and controls
343 lines (311 loc) · 8.08 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
package inspect
import (
"os"
"path/filepath"
"testing"
"time"
)
func TestLoadConfig_NoConfigFile(t *testing.T) {
dir := t.TempDir()
opts, err := LoadConfig(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if opts != nil {
t.Errorf("expected nil options when no config file, got %v", opts)
}
}
func TestLoadConfig_WithConfigFile(t *testing.T) {
dir := t.TempDir()
content := `
# Inspect configuration
depth = 3
checks = ["links", "security"]
exclude = ["/admin", "/api"]
fail_on = "high"
concurrency = 5
rate_limit = 10
timeout = "30s"
page_timeout = "10s"
user_agent = "my-bot/1.0"
auth_header = "Authorization"
auth_value = "Bearer abc123"
accepted_status_codes = [200, 201, 204]
`
err := os.WriteFile(filepath.Join(dir, ".inspect.toml"), []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
}
opts, err := LoadConfig(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if opts == nil {
t.Fatal("expected non-nil options")
}
// Apply the options and check the resulting config
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
if cfg.depth != 3 {
t.Errorf("expected depth=3, got %d", cfg.depth)
}
if len(cfg.checks) != 2 || cfg.checks[0] != "links" || cfg.checks[1] != "security" {
t.Errorf("expected checks=[links,security], got %v", cfg.checks)
}
if len(cfg.exclude) != 2 || cfg.exclude[0] != "/admin" || cfg.exclude[1] != "/api" {
t.Errorf("expected exclude=[/admin,/api], got %v", cfg.exclude)
}
if cfg.failOn != SeverityHigh {
t.Errorf("expected failOn=high, got %v", cfg.failOn)
}
if cfg.concurrency != 5 {
t.Errorf("expected concurrency=5, got %d", cfg.concurrency)
}
if cfg.rateLimit != 10 {
t.Errorf("expected rateLimit=10, got %d", cfg.rateLimit)
}
if cfg.timeout != 30*time.Second {
t.Errorf("expected timeout=30s, got %v", cfg.timeout)
}
if cfg.pageTimeout != 10*time.Second {
t.Errorf("expected pageTimeout=10s, got %v", cfg.pageTimeout)
}
if cfg.userAgent != "my-bot/1.0" {
t.Errorf("expected userAgent=my-bot/1.0, got %q", cfg.userAgent)
}
if cfg.authHeader != "Authorization" {
t.Errorf("expected authHeader=Authorization, got %q", cfg.authHeader)
}
if cfg.authValue != "Bearer abc123" {
t.Errorf("expected authValue=Bearer abc123, got %q", cfg.authValue)
}
if len(cfg.acceptedStatusCodes) != 3 {
t.Errorf("expected 3 accepted status codes, got %d", len(cfg.acceptedStatusCodes))
}
}
func TestLoadConfig_SearchesParentDirs(t *testing.T) {
parent := t.TempDir()
child := filepath.Join(parent, "sub", "dir")
err := os.MkdirAll(child, 0755)
if err != nil {
t.Fatalf("failed to create child dir: %v", err)
}
content := `depth = 7
`
err = os.WriteFile(filepath.Join(parent, ".inspect.toml"), []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
}
opts, err := LoadConfig(child)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if opts == nil {
t.Fatal("expected to find config from parent directory")
}
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
if cfg.depth != 7 {
t.Errorf("expected depth=7, got %d", cfg.depth)
}
}
func TestLoadConfig_SkipsCommentsAndSections(t *testing.T) {
dir := t.TempDir()
content := `# This is a comment
[section]
depth = 4
# Another comment
`
err := os.WriteFile(filepath.Join(dir, ".inspect.toml"), []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
}
opts, err := LoadConfig(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if opts == nil {
t.Fatal("expected non-nil options")
}
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
if cfg.depth != 4 {
t.Errorf("expected depth=4, got %d", cfg.depth)
}
}
func TestLoadConfig_FailOnVariants(t *testing.T) {
tests := []struct {
failOn string
expected Severity
}{
{"critical", SeverityCritical},
{"high", SeverityHigh},
{"medium", SeverityMedium},
{"low", SeverityLow},
{"info", SeverityInfo},
{"unknown", SeverityInfo},
}
for _, tt := range tests {
t.Run(tt.failOn, func(t *testing.T) {
dir := t.TempDir()
content := `fail_on = "` + tt.failOn + `"
`
err := os.WriteFile(filepath.Join(dir, ".inspect.toml"), []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
}
opts, err := LoadConfig(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
if cfg.failOn != tt.expected {
t.Errorf("expected failOn=%v, got %v", tt.expected, cfg.failOn)
}
})
}
}
func TestLoadConfig_EmptyValues(t *testing.T) {
dir := t.TempDir()
content := `depth = 0
concurrency = 0
rate_limit = 0
`
err := os.WriteFile(filepath.Join(dir, ".inspect.toml"), []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
}
opts, err := LoadConfig(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Zero values should not produce options (no changes to defaults)
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
// Defaults should be preserved since zero-value ints are skipped
if cfg.depth != 5 {
t.Errorf("expected default depth=5, got %d", cfg.depth)
}
}
func TestLoadConfig_InvalidTimeout(t *testing.T) {
dir := t.TempDir()
content := `timeout = "not-a-duration"
`
err := os.WriteFile(filepath.Join(dir, ".inspect.toml"), []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
}
opts, err := LoadConfig(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
// Invalid duration should be ignored, default preserved
if cfg.timeout != 60*time.Second {
t.Errorf("expected default timeout, got %v", cfg.timeout)
}
}
func TestLoadConfig_InspectTomlAlternateNames(t *testing.T) {
// Test that inspect.toml (without dot prefix) is also found
dir := t.TempDir()
content := `depth = 9
`
err := os.WriteFile(filepath.Join(dir, "inspect.toml"), []byte(content), 0644)
if err != nil {
t.Fatalf("failed to write config file: %v", err)
}
opts, err := LoadConfig(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if opts == nil {
t.Fatal("expected to find inspect.toml config")
}
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
if cfg.depth != 9 {
t.Errorf("expected depth=9, got %d", cfg.depth)
}
}
func TestParseTOMLArrayVal(t *testing.T) {
tests := []struct {
input string
expected []string
}{
{`["a", "b", "c"]`, []string{"a", "b", "c"}},
{`["single"]`, []string{"single"}},
{`[]`, nil},
{`["a","b"]`, []string{"a", "b"}},
}
for _, tt := range tests {
got := parseTOMLArrayVal(tt.input)
if len(got) != len(tt.expected) {
t.Errorf("parseTOMLArrayVal(%q) = %v, want %v", tt.input, got, tt.expected)
continue
}
for i := range got {
if got[i] != tt.expected[i] {
t.Errorf("parseTOMLArrayVal(%q)[%d] = %q, want %q", tt.input, i, got[i], tt.expected[i])
}
}
}
}
func TestParseTOMLIntArray(t *testing.T) {
tests := []struct {
input string
expected []int
}{
{`[200, 201, 204]`, []int{200, 201, 204}},
{`[404]`, []int{404}},
{`[]`, nil},
}
for _, tt := range tests {
got := parseTOMLIntArray(tt.input)
if len(got) != len(tt.expected) {
t.Errorf("parseTOMLIntArray(%q) = %v, want %v", tt.input, got, tt.expected)
continue
}
for i := range got {
if got[i] != tt.expected[i] {
t.Errorf("parseTOMLIntArray(%q)[%d] = %d, want %d", tt.input, i, got[i], tt.expected[i])
}
}
}
}
func TestApplyFileConfig_Nil(t *testing.T) {
opts := applyFileConfig(nil)
if opts != nil {
t.Errorf("expected nil opts for nil config, got %v", opts)
}
}
func TestApplyFileConfig_AuthRequiresBoth(t *testing.T) {
// Auth should only apply when both header and value are present
fc := &FileConfig{
AuthHeader: "Authorization",
AuthValue: "",
}
opts := applyFileConfig(fc)
cfg := defaultConfig()
for _, o := range opts {
o.apply(cfg)
}
if cfg.authHeader != "" {
t.Errorf("expected no auth when auth_value is empty, got header=%q", cfg.authHeader)
}
}