-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathauth_console_test.go
More file actions
1021 lines (900 loc) · 28.5 KB
/
Copy pathauth_console_test.go
File metadata and controls
1021 lines (900 loc) · 28.5 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cmd
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/pkg/auth/types"
"github.com/cloudposse/atmos/pkg/schema"
)
func TestAuthConsoleCommand_Registration(t *testing.T) {
_ = NewTestKit(t)
t.Run("command is registered", func(t *testing.T) {
cmd := RootCmd.Commands()
var authCmd *cobra.Command
for _, c := range cmd {
if c.Name() == "auth" {
authCmd = c
break
}
}
require.NotNil(t, authCmd, "auth command should be registered")
var consoleCmd *cobra.Command
for _, c := range authCmd.Commands() {
if c.Name() == "console" {
consoleCmd = c
break
}
}
require.NotNil(t, consoleCmd, "console subcommand should be registered under auth")
})
t.Run("command has correct metadata", func(t *testing.T) {
assert.Equal(t, "console", authConsoleCmd.Name())
assert.Contains(t, authConsoleCmd.Short, "web console")
assert.NotEmpty(t, authConsoleCmd.Long)
assert.NotEmpty(t, authConsoleCmd.Example)
})
t.Run("command has required flags", func(t *testing.T) {
destFlag := authConsoleCmd.Flags().Lookup("destination")
assert.NotNil(t, destFlag)
durationFlag := authConsoleCmd.Flags().Lookup("duration")
assert.NotNil(t, durationFlag)
printOnlyFlag := authConsoleCmd.Flags().Lookup("print-only")
assert.NotNil(t, printOnlyFlag)
noOpenFlag := authConsoleCmd.Flags().Lookup("no-open")
assert.NotNil(t, noOpenFlag)
issuerFlag := authConsoleCmd.Flags().Lookup("issuer")
assert.NotNil(t, issuerFlag)
})
}
func TestRetrieveCredentials(t *testing.T) {
tests := []struct {
name string
whoami *types.WhoamiInfo
wantErr bool
errType error
errMsg string
}{
{
name: "uses in-memory credentials when available",
whoami: &types.WhoamiInfo{
Credentials: &types.AWSCredentials{
AccessKeyID: "AKIATEST",
SecretAccessKey: "secret",
SessionToken: "token",
},
},
wantErr: false,
},
{
name: "returns error when no credentials available",
whoami: &types.WhoamiInfo{
Credentials: nil,
CredentialsRef: "",
},
wantErr: true,
errType: errUtils.ErrAuthConsole,
errMsg: "no credentials available",
},
{
name: "handles OIDC credentials",
whoami: &types.WhoamiInfo{
Credentials: &types.OIDCCredentials{
Token: "oidc-token",
Provider: "github",
},
},
wantErr: false,
},
{
name: "handles AWS credentials with different fields",
whoami: &types.WhoamiInfo{
Credentials: &types.AWSCredentials{
AccessKeyID: "AKIA123",
SecretAccessKey: "secret123",
SessionToken: "session123",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
creds, err := retrieveCredentials(tt.whoami)
if tt.wantErr {
assert.Error(t, err)
if tt.errType != nil {
assert.True(t, errors.Is(err, tt.errType), "expected error type %v, got %v", tt.errType, err)
}
if tt.errMsg != "" {
assert.Contains(t, err.Error(), tt.errMsg)
}
} else {
assert.NoError(t, err)
assert.NotNil(t, creds)
}
})
}
}
func TestHandleBrowserOpen(t *testing.T) {
tests := []struct {
name string
consoleURL string
}{
{
name: "handles valid URL",
consoleURL: "https://console.aws.amazon.com",
},
{
name: "handles empty URL",
consoleURL: "",
},
{
name: "handles URL with query parameters",
consoleURL: "https://console.aws.amazon.com?Action=login&Destination=s3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set CI env to prevent browser from opening during tests.
t.Setenv("CI", "true")
// This function doesn't return an error, just verify it doesn't panic.
assert.NotPanics(t, func() {
handleBrowserOpen(tt.consoleURL)
})
})
}
}
func TestAuthConsoleCommand_Flags(t *testing.T) {
tests := []struct {
name string
args []string
expectedDest string
expectedDuration time.Duration
expectedPrintOnly bool
expectedNoOpen bool
wantErr bool
}{
{
name: "default values",
args: []string{},
expectedDest: "",
expectedDuration: 1 * time.Hour,
expectedPrintOnly: false,
expectedNoOpen: false,
wantErr: false,
},
{
name: "with destination flag",
args: []string{"--destination", "s3"},
expectedDest: "s3",
expectedDuration: 1 * time.Hour,
wantErr: false,
},
{
name: "with destination as ec2",
args: []string{"--destination", "ec2"},
expectedDest: "ec2",
expectedDuration: 1 * time.Hour,
wantErr: false,
},
{
name: "with duration flag",
args: []string{"--duration", "2h"},
expectedDuration: 2 * time.Hour,
wantErr: false,
},
{
name: "with duration in minutes",
args: []string{"--duration", "30m"},
expectedDuration: 30 * time.Minute,
wantErr: false,
},
{
name: "with print-only flag",
args: []string{"--print-only"},
expectedPrintOnly: true,
expectedDuration: 1 * time.Hour,
wantErr: false,
},
{
name: "with no-open flag",
args: []string{"--no-open"},
expectedNoOpen: true,
expectedDuration: 1 * time.Hour,
wantErr: false,
},
{
name: "with all flags",
args: []string{"--destination", "cloudformation", "--duration", "3h", "--print-only", "--no-open"},
expectedDest: "cloudformation",
expectedDuration: 3 * time.Hour,
expectedPrintOnly: true,
expectedNoOpen: true,
wantErr: false,
},
{
name: "invalid duration format",
args: []string{"--duration", "invalid"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := &cobra.Command{}
cmd.Flags().String("destination", "", "")
cmd.Flags().Duration("duration", 1*time.Hour, "")
cmd.Flags().Bool("print-only", false, "")
cmd.Flags().Bool("no-open", false, "")
err := cmd.ParseFlags(tt.args)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
if tt.expectedDest != "" {
dest, _ := cmd.Flags().GetString("destination")
assert.Equal(t, tt.expectedDest, dest)
}
duration, _ := cmd.Flags().GetDuration("duration")
assert.Equal(t, tt.expectedDuration, duration)
printOnly, _ := cmd.Flags().GetBool("print-only")
assert.Equal(t, tt.expectedPrintOnly, printOnly)
noOpen, _ := cmd.Flags().GetBool("no-open")
assert.Equal(t, tt.expectedNoOpen, noOpen)
})
}
}
func TestAuthConsoleCommand_ErrorHandling(t *testing.T) {
tests := []struct {
name string
setup func() error
errType error
errMsg string
}{
{
name: "authentication errors wrapped with sentinel",
setup: func() error {
return fmt.Errorf("%w: authentication failed: %w", errUtils.ErrAuthConsole, context.DeadlineExceeded)
},
errType: errUtils.ErrAuthConsole,
errMsg: "authentication failed",
},
{
name: "credential errors wrapped with sentinel",
setup: func() error {
return fmt.Errorf("%w: no credentials available", errUtils.ErrAuthConsole)
},
errType: errUtils.ErrAuthConsole,
errMsg: "no credentials",
},
{
name: "config loading errors wrapped with sentinel",
setup: func() error {
return fmt.Errorf("%w: failed to load atmos config: %w", errUtils.ErrAuthConsole, fmt.Errorf("file not found"))
},
errType: errUtils.ErrAuthConsole,
errMsg: "failed to load",
},
{
name: "provider not supported errors use correct sentinel",
setup: func() error {
return fmt.Errorf("%w: Azure console access not yet implemented", errUtils.ErrProviderNotSupported)
},
errType: errUtils.ErrProviderNotSupported,
errMsg: "not yet implemented",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.setup()
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.errType), "expected error type %v, got %v", tt.errType, err)
if tt.errMsg != "" {
assert.Contains(t, err.Error(), tt.errMsg)
}
})
}
}
func TestAuthConsoleCommand_Constants(t *testing.T) {
t.Run("consoleLabelWidth has valid value", func(t *testing.T) {
assert.Equal(t, 18, consoleLabelWidth)
})
t.Run("consoleOutputFormat has valid format string", func(t *testing.T) {
assert.Equal(t, "%s %s\n", consoleOutputFormat)
})
}
func TestAuthConsoleCommand_UsageMarkdown(t *testing.T) {
t.Run("usage markdown is not empty", func(t *testing.T) {
assert.NotEmpty(t, authConsoleUsageMarkdown)
})
t.Run("usage markdown contains examples", func(t *testing.T) {
assert.Contains(t, authConsoleUsageMarkdown, "atmos auth console")
})
}
func TestPrintConsoleInfo(t *testing.T) {
tests := []struct {
name string
whoami *types.WhoamiInfo
duration time.Duration
showURL bool
consoleURL string
}{
{
name: "prints basic info without URL",
whoami: &types.WhoamiInfo{
Provider: "aws",
Identity: "test-user",
},
duration: 1 * time.Hour,
showURL: false,
consoleURL: "",
},
{
name: "prints info with account",
whoami: &types.WhoamiInfo{
Provider: "aws",
Identity: "test-user",
Account: "123456789012",
},
duration: 2 * time.Hour,
showURL: false,
consoleURL: "",
},
{
name: "prints info with URL",
whoami: &types.WhoamiInfo{
Provider: "aws",
Identity: "test-user",
Account: "123456789012",
},
duration: 1 * time.Hour,
showURL: true,
consoleURL: "https://console.aws.amazon.com",
},
{
name: "handles zero duration",
whoami: &types.WhoamiInfo{
Provider: "azure",
Identity: "user@example.com",
},
duration: 0,
showURL: false,
consoleURL: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// This function prints to stderr, just verify it doesn't panic.
assert.NotPanics(t, func() {
printConsoleInfo(tt.whoami, tt.duration, tt.showURL, tt.consoleURL)
})
})
}
}
func TestPrintConsoleURL(t *testing.T) {
tests := []struct {
name string
consoleURL string
}{
{
name: "prints valid URL",
consoleURL: "https://console.aws.amazon.com",
},
{
name: "prints empty URL",
consoleURL: "",
},
{
name: "prints URL with parameters",
consoleURL: "https://console.aws.amazon.com?Action=login&Destination=s3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// This function prints to stderr, just verify it doesn't panic.
assert.NotPanics(t, func() {
printConsoleURL(tt.consoleURL)
})
})
}
}
func TestGetConsoleProvider(t *testing.T) {
tests := []struct {
name string
providerKind string
identityName string
wantErr bool
errContains string
}{
{
name: "AWS IAM Identity Center returns provider",
providerKind: types.ProviderKindAWSIAMIdentityCenter,
identityName: "test-identity",
wantErr: false,
},
{
name: "AWS SAML returns provider",
providerKind: types.ProviderKindAWSSAML,
identityName: "test-identity",
wantErr: false,
},
{
name: "Azure OIDC returns console provider successfully",
providerKind: types.ProviderKindAzureOIDC,
identityName: "test-identity",
wantErr: false,
},
{
name: "GCP OIDC returns not implemented error",
providerKind: types.ProviderKindGCPOIDC,
identityName: "test-identity",
wantErr: true,
errContains: "GCP console access not yet implemented",
},
{
name: "unknown provider returns error",
providerKind: "unknown",
identityName: "test-identity",
wantErr: true,
errContains: "does not support web console access",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a mock auth manager.
mockManager := &mockAuthManagerForProvider{
providerKind: tt.providerKind,
}
provider, err := getConsoleProvider(mockManager, tt.identityName)
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errContains)
assert.Nil(t, provider)
} else {
assert.NoError(t, err)
assert.NotNil(t, provider)
}
})
}
}
func TestResolveIdentityName(t *testing.T) {
tests := []struct {
name string
flagValue string
defaultIdentity string
defaultErr error
wantIdentity string
wantErr bool
errContains string
}{
{
name: "uses flag value when provided",
flagValue: "prod-admin",
defaultIdentity: "dev-user",
wantIdentity: "prod-admin",
wantErr: false,
},
{
name: "uses default identity when flag not provided",
flagValue: "",
defaultIdentity: "dev-user",
wantIdentity: "dev-user",
wantErr: false,
},
{
name: "returns error when no default identity",
flagValue: "",
defaultIdentity: "",
wantErr: true,
errContains: "no default identity configured",
},
{
name: "returns error when GetDefaultIdentity fails",
flagValue: "",
defaultErr: fmt.Errorf("auth manager error"),
wantErr: true,
errContains: "failed to get default identity",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = NewTestKit(t)
// Ensure Viper doesn't have stale identity value from previous tests.
// resolveIdentityName() reads from Viper when flag is not changed.
viper.GetViper().Set("identity", "")
// Create a mock command.
cmd := &cobra.Command{}
cmd.Flags().String("identity", "", "identity name")
if tt.flagValue != "" {
_ = cmd.Flags().Set("identity", tt.flagValue)
}
// Create a mock auth manager.
mockManager := &mockAuthManagerForIdentity{
defaultIdentity: tt.defaultIdentity,
defaultErr: tt.defaultErr,
}
identity, err := resolveIdentityName(cmd, mockManager)
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errContains)
assert.Empty(t, identity)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.wantIdentity, identity)
}
})
}
}
// TestResolveIdentityName_PersistentFlag_WithNoOptDefVal tests that GetIdentityFromFlags
// correctly handles the --identity flag when it has NoOptDefVal set.
// This reproduces GitHub issue #1915 where `atmos auth console --identity {identity}`
// ignores the flag and prompts for interactive selection.
//
// The bug occurs because Cobra's NoOptDefVal causes `--identity value` (space-separated)
// to be interpreted as `--identity` (with NoOptDefVal) plus `value` as a positional arg.
// Only `--identity=value` works correctly with NoOptDefVal.
//
// The fix is to use GetIdentityFromFlags which parses os.Args directly to work around
// this Cobra quirk.
func TestResolveIdentityName_PersistentFlag_WithNoOptDefVal(t *testing.T) {
tests := []struct {
name string
osArgs []string // Simulated os.Args
defaultIdentity string
wantIdentity string
wantErr bool
errContains string
}{
{
// This is the bug from issue #1915!
// With NoOptDefVal, "--identity prod-admin" is misinterpreted as:
// - --identity -> gets NoOptDefVal ("__SELECT__")
// - prod-admin -> becomes positional arg
// GetIdentityFromFlags works around this by parsing os.Args directly.
name: "reads persistent flag with space-separated value (issue #1915)",
osArgs: []string{"atmos", "auth", "console", "--identity", "prod-admin"},
defaultIdentity: "dev-user",
wantIdentity: "prod-admin",
wantErr: false,
},
{
name: "reads persistent flag with equals syntax",
osArgs: []string{"atmos", "auth", "console", "--identity=staging-admin"},
defaultIdentity: "dev-user",
wantIdentity: "staging-admin",
wantErr: false,
},
{
// Same bug as above but with short flag
name: "reads persistent short flag with space-separated value",
osArgs: []string{"atmos", "auth", "console", "-i", "test-identity"},
defaultIdentity: "dev-user",
wantIdentity: "test-identity",
wantErr: false,
},
{
name: "falls back to default when flag not provided",
osArgs: []string{"atmos", "auth", "console"},
defaultIdentity: "dev-user",
wantIdentity: "dev-user",
wantErr: false,
},
{
// When --identity is used without value, it should trigger interactive selection
name: "triggers interactive selection when flag used without value",
osArgs: []string{"atmos", "auth", "console", "--identity"},
defaultIdentity: "dev-user",
wantIdentity: "dev-user", // GetDefaultIdentity is called with forceSelect=true
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = NewTestKit(t)
// Ensure Viper doesn't have stale identity value from previous tests.
viper.GetViper().Set("identity", "")
// Create a command with the identity flag and NoOptDefVal (simulates authCmd setup).
cmd := &cobra.Command{Use: "console"}
cmd.Flags().StringP("identity", "i", "", "identity name")
// Set NoOptDefVal like the real authCmd does.
identityFlag := cmd.Flags().Lookup("identity")
if identityFlag != nil {
identityFlag.NoOptDefVal = IdentityFlagSelectValue
}
// Parse the command args (this will exhibit the NoOptDefVal bug for space-separated values).
_ = cmd.ParseFlags(tt.osArgs[3:]) // Skip "atmos", "auth", "console"
// Test GetIdentityFromFlags directly - this is what resolveIdentityName now uses.
identityName := GetIdentityFromFlags(cmd, tt.osArgs)
// Check if user wants to interactively select identity.
forceSelect := identityName == IdentityFlagSelectValue
// Create a mock auth manager.
mockManager := &mockAuthManagerForIdentity{
defaultIdentity: tt.defaultIdentity,
}
// Apply the same logic as resolveIdentityName.
var identity string
var err error
if identityName != "" && !forceSelect {
identity = identityName
} else {
identity, err = mockManager.GetDefaultIdentity(forceSelect)
if err != nil {
err = fmt.Errorf("failed to get default identity: %w", err)
} else if identity == "" {
err = fmt.Errorf("no default identity configured")
}
}
if tt.wantErr {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errContains)
assert.Empty(t, identity)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.wantIdentity, identity)
}
})
}
}
// mockAuthManagerForProvider implements minimal AuthManager for testing getConsoleProvider.
// Only GetProviderKindForIdentity is implemented - other methods return ErrNotImplemented
// because they are not needed by TestGetConsoleProvider.
type mockAuthManagerForProvider struct {
providerKind string
}
func (m *mockAuthManagerForProvider) GetProviderKindForIdentity(identityName string) (string, error) {
return m.providerKind, nil
}
func (m *mockAuthManagerForProvider) GetCachedCredentials(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) Authenticate(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) GetDefaultIdentity(_ bool) (string, error) {
return "", errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) ListIdentities() []string {
return nil
}
func (m *mockAuthManagerForProvider) Logout(ctx context.Context, identityName string, deleteKeychain bool) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) GetIdentity(identityName string) (types.Identity, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) GetFilesDisplayPath(providerName string) string {
return ""
}
func (m *mockAuthManagerForProvider) GetChain() []string {
return nil
}
func (m *mockAuthManagerForProvider) GetIdentities() map[string]schema.Identity {
return nil
}
func (m *mockAuthManagerForProvider) Whoami(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) Validate() error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) GetProviderForIdentity(identityName string) string {
return ""
}
func (m *mockAuthManagerForProvider) GetStackInfo() *schema.ConfigAndStacksInfo {
return nil
}
func (m *mockAuthManagerForProvider) ListProviders() []string {
return nil
}
func (m *mockAuthManagerForProvider) GetProviders() map[string]schema.Provider {
return nil
}
func (m *mockAuthManagerForProvider) LogoutProvider(ctx context.Context, providerName string, deleteKeychain bool) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) LogoutAll(ctx context.Context, deleteKeychain bool) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) GetEnvironmentVariables(identityName string) (map[string]string, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) PrepareShellEnvironment(ctx context.Context, identityName string, currentEnv []string) ([]string, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) AuthenticateProvider(ctx context.Context, providerName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) ExecuteIntegration(ctx context.Context, integrationName string) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) ExecuteIdentityIntegrations(ctx context.Context, identityName string) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) GetIntegration(integrationName string) (*schema.Integration, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForProvider) ResolvePrincipalSetting(identityName, key string) (interface{}, bool) {
return nil, false
}
func (m *mockAuthManagerForProvider) ResolveProviderConfig(identityName string) (*schema.Provider, bool) {
return nil, false
}
// mockAuthManagerForIdentity implements minimal AuthManager for testing resolveIdentityName.
// Only GetDefaultIdentity is implemented - other methods return ErrNotImplemented
// because they are not needed by TestResolveIdentityName.
type mockAuthManagerForIdentity struct {
defaultIdentity string
defaultErr error
}
func (m *mockAuthManagerForIdentity) GetProviderKindForIdentity(identityName string) (string, error) {
return "", errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) GetCachedCredentials(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) Authenticate(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) GetDefaultIdentity(_ bool) (string, error) {
if m.defaultErr != nil {
return "", m.defaultErr
}
return m.defaultIdentity, nil
}
func (m *mockAuthManagerForIdentity) ListIdentities() []string {
return nil
}
func (m *mockAuthManagerForIdentity) Logout(ctx context.Context, identityName string, deleteKeychain bool) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) GetIdentity(identityName string) (types.Identity, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) GetFilesDisplayPath(providerName string) string {
return ""
}
func (m *mockAuthManagerForIdentity) GetChain() []string {
return nil
}
func (m *mockAuthManagerForIdentity) GetIdentities() map[string]schema.Identity {
return nil
}
func (m *mockAuthManagerForIdentity) Whoami(ctx context.Context, identityName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) Validate() error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) GetProviderForIdentity(identityName string) string {
return ""
}
func (m *mockAuthManagerForIdentity) GetStackInfo() *schema.ConfigAndStacksInfo {
return nil
}
func (m *mockAuthManagerForIdentity) ListProviders() []string {
return nil
}
func (m *mockAuthManagerForIdentity) GetProviders() map[string]schema.Provider {
return nil
}
func (m *mockAuthManagerForIdentity) LogoutProvider(ctx context.Context, providerName string, deleteKeychain bool) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) LogoutAll(ctx context.Context, deleteKeychain bool) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) GetEnvironmentVariables(identityName string) (map[string]string, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) PrepareShellEnvironment(ctx context.Context, identityName string, currentEnv []string) ([]string, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) AuthenticateProvider(ctx context.Context, providerName string) (*types.WhoamiInfo, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) ExecuteIntegration(ctx context.Context, integrationName string) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) ExecuteIdentityIntegrations(ctx context.Context, identityName string) error {
return errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) GetIntegration(integrationName string) (*schema.Integration, error) {
return nil, errUtils.ErrNotImplemented
}
func (m *mockAuthManagerForIdentity) ResolvePrincipalSetting(identityName, key string) (interface{}, bool) {
return nil, false
}
func (m *mockAuthManagerForIdentity) ResolveProviderConfig(identityName string) (*schema.Provider, bool) {
return nil, false
}
func TestResolveConsoleDuration(t *testing.T) {
_ = NewTestKit(t)
tests := []struct {
name string
flagSet bool
flagValue time.Duration
providerConfig *schema.ConsoleConfig
expectedDuration time.Duration
expectError bool
}{
{
name: "flag explicitly set takes precedence",
flagSet: true,
flagValue: 4 * time.Hour,
providerConfig: &schema.ConsoleConfig{SessionDuration: "12h"},
expectedDuration: 4 * time.Hour,
expectError: false,
},
{
name: "provider config used when flag not set",
flagSet: false,
flagValue: 1 * time.Hour, // default flag value
providerConfig: &schema.ConsoleConfig{SessionDuration: "8h"},
expectedDuration: 8 * time.Hour,
expectError: false,
},
{
name: "default flag value when no provider config",
flagSet: false,
flagValue: 1 * time.Hour,
providerConfig: nil,
expectedDuration: 1 * time.Hour,
expectError: false,
},
{
name: "default flag value when provider config empty",
flagSet: false,
flagValue: 1 * time.Hour,
providerConfig: &schema.ConsoleConfig{SessionDuration: ""},
expectedDuration: 1 * time.Hour,
expectError: false,
},
{
name: "invalid provider config duration",
flagSet: false,
flagValue: 1 * time.Hour,
providerConfig: &schema.ConsoleConfig{SessionDuration: "invalid"},
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = NewTestKit(t)
// Create test command with duration flag.
cmd := &cobra.Command{}
cmd.Flags().DurationVar(&consoleDuration, "duration", 1*time.Hour, "duration flag")
// Set flag value and simulate whether user explicitly set it.
consoleDuration = tt.flagValue
if tt.flagSet {
require.NoError(t, cmd.Flags().Set("duration", tt.flagValue.String()))
}
// Create mock auth manager using gomock.
ctrl := gomock.NewController(t)
mockManager := types.NewMockAuthManager(ctrl)
// Setup expectation for GetProviders.