-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathexpiry_warning_test.go
More file actions
405 lines (379 loc) · 10.6 KB
/
expiry_warning_test.go
File metadata and controls
405 lines (379 loc) · 10.6 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
package app
import (
"bytes"
"os"
"strings"
"testing"
"time"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/manifest"
)
// expiringTokenData returns a global.Data configured with a stored token that
// expires soon. Callers can override Flags and commandName to test suppression.
func expiringTokenData(out *bytes.Buffer) *global.Data {
soon := time.Now().Add(3 * 24 * time.Hour).Format(time.RFC3339)
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Manifest: &manifest.Data{},
Config: config.File{
Auth: config.Auth{
Default: "mytoken",
Tokens: config.AuthTokens{
"mytoken": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: "tok_abc123",
APITokenExpiresAt: soon,
},
},
},
},
}
}
func TestCheckTokenExpirationWarning(t *testing.T) {
soon := time.Now().Add(3 * 24 * time.Hour).Format(time.RFC3339)
farFuture := time.Now().Add(60 * 24 * time.Hour).Format(time.RFC3339)
tests := []struct {
name string
commandName string
data func(out *bytes.Buffer) *global.Data
wantWarn bool
wantSubstr string
}{
{
name: "SourceAuth expiring soon shows warning",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Config: config.File{
Auth: config.Auth{
Default: "mytoken",
Tokens: config.AuthTokens{
"mytoken": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: "tok_abc123",
APITokenExpiresAt: soon,
},
},
},
},
}
},
wantWarn: true,
wantSubstr: "expires in",
},
{
name: "SourceAuth not expiring soon no warning",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Config: config.File{
Auth: config.Auth{
Default: "mytoken",
Tokens: config.AuthTokens{
"mytoken": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: "tok_abc123",
APITokenExpiresAt: farFuture,
},
},
},
},
}
},
wantWarn: false,
},
{
name: "SourceEnvironment JWT-like token no warning",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Env: config.Environment{APIToken: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.fake"},
Config: config.File{
Auth: config.Auth{
Default: "mytoken",
Tokens: config.AuthTokens{
"mytoken": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: "tok_abc123",
APITokenExpiresAt: soon,
},
},
},
},
}
},
wantWarn: false,
},
{
name: "SourceFlag raw token no warning",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Flags: global.Flags{Token: "some-raw-token"},
Config: config.File{
Auth: config.Auth{
Default: "mytoken",
Tokens: config.AuthTokens{
"mytoken": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: "tok_abc123",
APITokenExpiresAt: soon,
},
},
},
},
}
},
wantWarn: false,
},
{
name: "stale default name nil token no panic",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Config: config.File{
Auth: config.Auth{
Default: "deleted-token",
Tokens: config.AuthTokens{},
},
},
}
},
wantWarn: false,
},
{
name: "malformed expiry logs error no visible warning",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Config: config.File{
Auth: config.Auth{
Default: "mytoken",
Tokens: config.AuthTokens{
"mytoken": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: "tok_abc123",
APITokenExpiresAt: "not-a-date",
},
},
},
},
}
},
wantWarn: false,
},
{
name: "nil ErrLog does not panic",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
Config: config.File{
Auth: config.Auth{
Default: "mytoken",
Tokens: config.AuthTokens{
"mytoken": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: "tok_abc123",
APITokenExpiresAt: "not-a-date",
},
},
},
},
}
},
wantWarn: false,
},
{
name: "SSO token expiring soon shows remediation",
commandName: "service list",
data: func(out *bytes.Buffer) *global.Data {
return &global.Data{
Output: out,
ErrOutput: out,
ErrLog: fsterr.Log,
Config: config.File{
Auth: config.Auth{
Default: "sso-tok",
Tokens: config.AuthTokens{
"sso-tok": &config.AuthToken{
Type: config.AuthTokenTypeSSO,
Token: "tok_sso",
RefreshExpiresAt: soon,
},
},
},
},
}
},
wantWarn: true,
wantSubstr: "fastly auth login --sso",
},
}
// Ensure FASTLY_DISABLE_AUTH_COMMAND is not set.
originalEnv := os.Getenv("FASTLY_DISABLE_AUTH_COMMAND")
os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", "")
defer os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", originalEnv)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
data := tt.data(&buf)
// Ensure Manifest is initialized to avoid nil panics in Token().
if data.Manifest == nil {
data.Manifest = &manifest.Data{}
}
checkTokenExpirationWarning(data, tt.commandName)
output := buf.String()
if tt.wantWarn && output == "" {
t.Error("expected warning output but got none")
}
if !tt.wantWarn && output != "" {
t.Errorf("expected no warning but got: %s", output)
}
if tt.wantSubstr != "" && !strings.Contains(output, tt.wantSubstr) {
t.Errorf("expected output to contain %q, got: %s", tt.wantSubstr, output)
}
})
}
}
func TestCheckTokenExpirationWarningDisabledAuth(t *testing.T) {
originalEnv := os.Getenv("FASTLY_DISABLE_AUTH_COMMAND")
os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", "1")
defer os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", originalEnv)
var buf bytes.Buffer
data := expiringTokenData(&buf)
checkTokenExpirationWarning(data, "service list")
output := buf.String()
if !strings.Contains(output, "FASTLY_API_TOKEN") {
t.Errorf("expected FASTLY_API_TOKEN remediation in disabled-auth mode, got: %s", output)
}
if strings.Contains(output, "fastly auth") {
t.Errorf("should not mention fastly auth in disabled mode, got: %s", output)
}
}
// TestCheckTokenExpirationWarningSuppression tests that the warning is
// suppressed for all auth-related commands, --quiet, and --json (which sets
// Quiet=true). The auth-related set matches FASTLY_DISABLE_AUTH_COMMAND
// (pkg/env/env.go): auth, auth-token, sso, profile, whoami.
func TestCheckTokenExpirationWarningSuppression(t *testing.T) {
tests := []struct {
name string
commandName string
flags global.Flags
}{
// auth family.
{
name: "suppressed for auth list",
commandName: "auth list",
},
{
name: "suppressed for auth show",
commandName: "auth show",
},
{
name: "suppressed for auth login",
commandName: "auth login",
},
{
name: "suppressed for bare auth",
commandName: "auth",
},
// Other auth-related families.
{
name: "suppressed for sso",
commandName: "sso",
},
{
name: "suppressed for auth-token create",
commandName: "auth-token create",
},
{
name: "suppressed for bare auth-token",
commandName: "auth-token",
},
{
name: "suppressed for profile switch",
commandName: "profile switch",
},
{
name: "suppressed for bare profile",
commandName: "profile",
},
{
name: "suppressed for whoami",
commandName: "whoami",
},
// Flag-based suppression.
{
name: "suppressed with --quiet flag",
commandName: "service list",
flags: global.Flags{Quiet: true},
},
}
originalEnv := os.Getenv("FASTLY_DISABLE_AUTH_COMMAND")
os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", "")
defer os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", originalEnv)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
data := expiringTokenData(&buf)
data.Flags = tt.flags
checkTokenExpirationWarning(data, tt.commandName)
output := buf.String()
if output != "" {
t.Errorf("expected no output for %q with flags %+v, got: %s", tt.commandName, tt.flags, output)
}
})
}
}
// TestCheckTokenExpirationWarningShownForJSON verifies that --json mode still
// emits the warning (to stderr) rather than suppressing it entirely.
func TestCheckTokenExpirationWarningShownForJSON(t *testing.T) {
var buf bytes.Buffer
data := expiringTokenData(&buf)
data.Flags = global.Flags{JSON: true}
checkTokenExpirationWarning(data, "service list")
output := buf.String()
if !strings.Contains(output, "expires in") {
t.Errorf("expected expiry warning in --json mode (written to stderr), got: %s", output)
}
}
// TestCheckTokenExpirationWarningNotSuppressedForNonAuth ensures that commands
// starting with "auth" as a prefix of another word (e.g. "authtoken") are not
// incorrectly suppressed.
func TestCheckTokenExpirationWarningNotSuppressedForNonAuth(t *testing.T) {
originalEnv := os.Getenv("FASTLY_DISABLE_AUTH_COMMAND")
os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", "")
defer os.Setenv("FASTLY_DISABLE_AUTH_COMMAND", originalEnv)
var buf bytes.Buffer
data := expiringTokenData(&buf)
// "authtoken" is not "auth" or "auth <sub>", so warning should fire.
checkTokenExpirationWarning(data, "authtoken list")
output := buf.String()
if output == "" {
t.Error("expected warning for non-auth command 'authtoken list' but got none")
}
}