-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
330 lines (271 loc) · 6.3 KB
/
utils_test.go
File metadata and controls
330 lines (271 loc) · 6.3 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
package main
import (
"bytes"
"encoding/json"
"os"
"testing"
"time"
)
func TestPrintTableUtil(t *testing.T) {
headers := []string{"ID", "Name", "Status"}
rows := [][]string{
{"1", "Item One", "Active"},
{"2", "Item Two", "Inactive"},
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
PrintTableUtil(headers, rows)
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
if output == "" {
t.Error("expected table output, got empty string")
}
if !containsStr(output, "ID") {
t.Error("expected header 'ID' in output")
}
if !containsStr(output, "Item One") {
t.Error("expected 'Item One' in output")
}
}
func TestPrintJSONUtil(t *testing.T) {
data := map[string]interface{}{
"name": "Test",
"value": 123,
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
PrintJSONUtil(data)
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
var result map[string]interface{}
err := json.Unmarshal([]byte(output), &result)
if err != nil {
t.Errorf("output is not valid JSON: %v", err)
}
if result["name"] != "Test" {
t.Errorf("expected name 'Test', got %v", result["name"])
}
}
func TestStringSliceToTabSeparated(t *testing.T) {
tests := []struct {
name string
input []string
expected string
}{
{
name: "empty slice",
input: []string{},
expected: "",
},
{
name: "single element",
input: []string{"one"},
expected: "one",
},
{
name: "multiple elements",
input: []string{"one", "two", "three"},
expected: "one\ttwo\tthree",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := stringSliceToTabSeparated(tt.input)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestPromptForInput(t *testing.T) {
oldStdin := os.Stdin
r, w, _ := os.Pipe()
os.Stdin = r
input := "test input\n"
w.Write([]byte(input))
w.Close()
result := promptForInput("Prompt", "default")
os.Stdin = oldStdin
if result != "test input" {
t.Errorf("expected 'test input', got %q", result)
}
}
func TestPromptForInput_EmptyWithDefault(t *testing.T) {
oldStdin := os.Stdin
r, w, _ := os.Pipe()
os.Stdin = r
input := "\n"
w.Write([]byte(input))
w.Close()
result := promptForInput("Prompt", "default value")
os.Stdin = oldStdin
if result != "default value" {
t.Errorf("expected 'default value', got %q", result)
}
}
func TestPromptConfirm(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{"empty default", "\n", true},
{"yes uppercase", "Y\n", true},
{"yes lowercase", "y\n", true},
{"no", "n\n", false},
{"no uppercase", "N\n", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
oldStdin := os.Stdin
r, w, _ := os.Pipe()
os.Stdin = r
w.Write([]byte(tt.input))
w.Close()
result := promptConfirm("Confirm?")
os.Stdin = oldStdin
if result != tt.expected {
t.Errorf("input %q: expected %v, got %v", tt.input, tt.expected, result)
}
})
}
}
func TestMaskString(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"empty", "", "(not set)"},
{"short", "abc", "****"},
{"exactly 4", "abcd", "****"},
{"long", "abcdefghij", "****ghij"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := maskString(tt.input)
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
func TestValidatePort(t *testing.T) {
tests := []struct {
name string
port string
expectError bool
}{
{"valid", "3456", false},
{"minimum", "3000", false},
{"maximum", "9000", false},
{"too low", "2999", true},
{"too high", "9001", true},
{"not a number", "abc", true},
{"empty", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validatePort(tt.port)
if tt.expectError && err == nil {
t.Error("expected error, got nil")
}
if !tt.expectError && err != nil {
t.Errorf("expected no error, got %v", err)
}
})
}
}
func TestPrintStep(t *testing.T) {
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
printStep("1", "5", "Test Step")
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
if !containsStr(output, "Step 1/5") {
t.Error("expected step indicator in output")
}
if !containsStr(output, "Test Step") {
t.Error("expected step title in output")
}
}
func TestPrintConfigSummary(t *testing.T) {
config := &Config{
ClientID: "test-client",
RedirectURI: "http://localhost:3456/callback",
Scopes: "openid accounting.transactions",
Port: "3456",
ClientSecret: "secret123",
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
printConfigSummary(config)
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
if !containsStr(output, "Configuration Summary") {
t.Error("expected summary header")
}
if !containsStr(output, "test-client") {
t.Error("expected client ID in output")
}
}
func containsStr(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || containsAtLocal(s, substr))
}
func containsAtLocal(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
func TestSaveToJSON(t *testing.T) {
tmpFile := "test_token_save.json"
defer os.Remove(tmpFile)
token := &TokenStore{
AccessToken: "test-token",
RefreshToken: "test-refresh",
Expiry: time.Now().Add(30 * time.Minute),
TenantID: "tenant-123",
TenantName: "Test Org",
}
err := SaveToJSON(tmpFile, token)
if err != nil {
t.Fatalf("SaveToJSON failed: %v", err)
}
var loaded TokenStore
err = LoadFromJSON(tmpFile, &loaded)
if err != nil {
t.Fatalf("LoadFromJSON failed: %v", err)
}
if loaded.AccessToken != token.AccessToken {
t.Errorf("access token mismatch")
}
}
func TestLoadFromJSON_NotExist(t *testing.T) {
var target interface{}
err := LoadFromJSON("non_existent_file.json", &target)
if err == nil {
t.Error("expected error for non-existent file, got nil")
}
if !os.IsNotExist(err) {
t.Errorf("expected IsNotExist error, got %v", err)
}
}