-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathauth_connections_test.go
More file actions
1162 lines (1050 loc) · 44.9 KB
/
Copy pathauth_connections_test.go
File metadata and controls
1162 lines (1050 loc) · 44.9 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 (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"os"
"testing"
"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
"github.com/kernel/kernel-go-sdk/packages/ssestream"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type FakeAuthConnectionService struct {
NewFunc func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error)
GetFunc func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error)
UpdateFunc func(ctx context.Context, id string, body kernel.AuthConnectionUpdateParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error)
ListFunc func(ctx context.Context, query kernel.AuthConnectionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ManagedAuth], error)
DeleteFunc func(ctx context.Context, id string, opts ...option.RequestOption) error
LoginFunc func(ctx context.Context, id string, body kernel.AuthConnectionLoginParams, opts ...option.RequestOption) (*kernel.LoginResponse, error)
SubmitFunc func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error)
TimelineFunc func(ctx context.Context, id string, query kernel.AuthConnectionTimelineParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ManagedAuthTimelineEvent], error)
FollowStreamingFunc func(ctx context.Context, id string, opts ...option.RequestOption) *ssestream.Stream[kernel.AuthConnectionFollowResponseUnion]
}
func (f *FakeAuthConnectionService) Timeline(ctx context.Context, id string, query kernel.AuthConnectionTimelineParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ManagedAuthTimelineEvent], error) {
if f.TimelineFunc != nil {
return f.TimelineFunc(ctx, id, query, opts...)
}
return &pagination.OffsetPagination[kernel.ManagedAuthTimelineEvent]{}, nil
}
func (f *FakeAuthConnectionService) New(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
if f.NewFunc != nil {
return f.NewFunc(ctx, body, opts...)
}
return &kernel.ManagedAuth{}, nil
}
func (f *FakeAuthConnectionService) Get(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
if f.GetFunc != nil {
return f.GetFunc(ctx, id, opts...)
}
return nil, errors.New("not found")
}
func (f *FakeAuthConnectionService) Update(ctx context.Context, id string, body kernel.AuthConnectionUpdateParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
if f.UpdateFunc != nil {
return f.UpdateFunc(ctx, id, body, opts...)
}
return &kernel.ManagedAuth{ID: id}, nil
}
func (f *FakeAuthConnectionService) List(ctx context.Context, query kernel.AuthConnectionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ManagedAuth], error) {
if f.ListFunc != nil {
return f.ListFunc(ctx, query, opts...)
}
return &pagination.OffsetPagination[kernel.ManagedAuth]{Items: []kernel.ManagedAuth{}}, nil
}
func (f *FakeAuthConnectionService) Delete(ctx context.Context, id string, opts ...option.RequestOption) error {
if f.DeleteFunc != nil {
return f.DeleteFunc(ctx, id, opts...)
}
return nil
}
func (f *FakeAuthConnectionService) Login(ctx context.Context, id string, body kernel.AuthConnectionLoginParams, opts ...option.RequestOption) (*kernel.LoginResponse, error) {
if f.LoginFunc != nil {
return f.LoginFunc(ctx, id, body, opts...)
}
return &kernel.LoginResponse{}, nil
}
func (f *FakeAuthConnectionService) Submit(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
if f.SubmitFunc != nil {
return f.SubmitFunc(ctx, id, body, opts...)
}
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
}
func (f *FakeAuthConnectionService) FollowStreaming(ctx context.Context, id string, opts ...option.RequestOption) *ssestream.Stream[kernel.AuthConnectionFollowResponseUnion] {
if f.FollowStreamingFunc != nil {
return f.FollowStreamingFunc(ctx, id, opts...)
}
return nil
}
func TestAuthConnectionsGet_PrintsSubmissionHints(t *testing.T) {
setupStdoutCapture(t)
fake := &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
return &kernel.ManagedAuth{
ID: id,
Domain: "auth.leaseweb.com",
ProfileName: "raf-leaseweb",
Status: kernel.ManagedAuthStatusNeedsAuth,
FlowStatus: kernel.ManagedAuthFlowStatusInProgress,
FlowStep: kernel.ManagedAuthFlowStepAwaitingInput,
DiscoveredFields: []kernel.ManagedAuthDiscoveredField{
{Name: "username", Type: "text", Required: true},
{Name: "password", Type: "password", Required: true},
},
MfaOptions: []kernel.ManagedAuthMfaOption{
{Label: "Text message", Type: "sms"},
},
PendingSSOButtons: []kernel.ManagedAuthPendingSSOButton{
{Label: "Continue with Google", Provider: "google"},
},
}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Get(context.Background(), AuthConnectionGetInput{ID: "e0x3vbw4z66kpwny3k5k46tj"})
require.NoError(t, err)
out := outBuf.String()
assert.Contains(t, out, "Discovered Fields")
assert.Contains(t, out, "username")
assert.Contains(t, out, "password")
assert.Contains(t, out, "MFA Options")
assert.Contains(t, out, "Text message")
assert.Contains(t, out, "Pending SSO Buttons")
assert.Contains(t, out, "Continue with Google")
}
// TestAuthConnectionsGet_PrintsCanonicalInputMetadata covers the metadata the
// API preserves on canonical fields and choices: the field hint naming a masked
// code destination, and the MFA type and masked destination that distinguish
// otherwise identical-looking choices.
func TestAuthConnectionsGet_PrintsCanonicalInputMetadata(t *testing.T) {
setupStdoutCapture(t)
fake := &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
return &kernel.ManagedAuth{
ID: id,
Domain: "auth.example.com",
Status: kernel.ManagedAuthStatusNeedsAuth,
FlowStatus: kernel.ManagedAuthFlowStatusInProgress,
FlowStep: kernel.ManagedAuthFlowStepAwaitingInput,
// Canonical fields and choices always arrive with the interaction
// they belong to, which `submit` needs.
InteractionID: "mai_abc123xyz",
Fields: []kernel.ManagedAuthField{
{
ID: "otp",
Label: "One-time code",
Type: "code",
Ref: "totp_code",
Hint: "Enter the code sent to +1 ••• ••• 1234",
Reason: "rejected",
Required: true,
},
},
Choices: []kernel.ManagedAuthChoice{
{
ID: "mfa_sms",
Label: "Text message",
Type: "mfa_method",
MfaType: "sms",
MaskedDestination: "+1 ••• ••• 1234",
},
{
// No label, so the captured display text stands in for it.
ID: "mfa_app",
DisplayText: "Use your authenticator app",
Type: "mfa_method",
MfaType: "totp",
},
},
}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Get(context.Background(), AuthConnectionGetInput{ID: "e0x3vbw4z66kpwny3k5k46tj"}))
out := outBuf.String()
assert.Contains(t, out, `mai_abc123xyz`)
assert.Contains(t, out, `otp (One-time code)`)
// The reason tells the user why the field is being asked for: "rejected"
// means a stored credential was refused, so a new value has to replace it.
assert.Contains(t, out, `code, ref=totp_code, required, reason=rejected`)
assert.Contains(t, out, `hint="Enter the code sent to +1 ••• ••• 1234"`)
assert.Contains(t, out, `mfa_sms (Text message)`)
assert.Contains(t, out, `mfa_method, sms, to=+1 ••• ••• 1234`)
assert.Contains(t, out, `mfa_app (Use your authenticator app)`)
assert.Contains(t, out, `mfa_method, totp`)
}
func TestAuthConnectionsGet_JSONOutputIncludesDiscoveredFields(t *testing.T) {
setupStdoutCapture(t)
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
t.Cleanup(func() {
os.Stdout = oldStdout
})
fake := &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
jsonData := `{
"id":"e0x3vbw4z66kpwny3k5k46tj",
"domain":"auth.leaseweb.com",
"profile_name":"raf-leaseweb",
"save_credentials":true,
"status":"NEEDS_AUTH",
"flow_status":"IN_PROGRESS",
"flow_step":"AWAITING_INPUT",
"discovered_fields":[
{"label":"Email","name":"email","selector":"#email","type":"email","required":true}
]
}`
var auth kernel.ManagedAuth
require.NoError(t, json.Unmarshal([]byte(jsonData), &auth))
return &auth, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Get(context.Background(), AuthConnectionGetInput{
ID: "e0x3vbw4z66kpwny3k5k46tj",
Output: "json",
})
require.NoError(t, err)
w.Close()
var stdoutBuf bytes.Buffer
_, _ = io.Copy(&stdoutBuf, r)
out := stdoutBuf.String()
assert.Contains(t, out, "\"discovered_fields\"")
assert.Contains(t, out, "\"selector\"")
assert.Contains(t, out, "\"email\"")
}
func TestAuthConnectionsList_JSONOutput_PrintsRawResponse(t *testing.T) {
setupStdoutCapture(t)
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
t.Cleanup(func() {
os.Stdout = oldStdout
})
fake := &FakeAuthConnectionService{
ListFunc: func(ctx context.Context, query kernel.AuthConnectionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ManagedAuth], error) {
jsonData := `[{
"id":"e0x3vbw4z66kpwny3k5k46tj",
"domain":"auth.leaseweb.com",
"profile_name":"raf-leaseweb",
"save_credentials":true,
"status":"NEEDS_AUTH"
}]`
var page pagination.OffsetPagination[kernel.ManagedAuth]
require.NoError(t, json.Unmarshal([]byte(jsonData), &page))
return &page, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.List(context.Background(), AuthConnectionListInput{Output: "json"})
require.NoError(t, err)
w.Close()
var stdoutBuf bytes.Buffer
_, _ = io.Copy(&stdoutBuf, r)
out := stdoutBuf.String()
assert.Contains(t, out, "\"profile_name\"")
assert.Contains(t, out, "\"raf-leaseweb\"")
}
// Regression test for the 1Password auto-lookup UX gap: before this fix,
// `kernel auth connections create --credential-provider foo` sent a
// CredentialReference of { provider } with no auto flag, which the API accepted
// as valid-but-inert — the managed auth session would never fetch credentials
// and would prompt the user for manual input. The dashboard already defaults
// to auto: true for this case (see packages/dashboard/src/components/create-managed-auth-dialog.tsx);
// the CLI now matches that UX.
func TestAuthConnectionsCreate_ProviderWithoutPath_DefaultsAutoTrue(t *testing.T) {
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "conn-new"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "google.com",
ProfileName: "my-profile",
CredentialProvider: "my-1p",
Output: "json",
})
require.NoError(t, err)
cred := captured.ManagedAuthCreateRequest.Credential
require.True(t, cred.Provider.Valid())
assert.Equal(t, "my-1p", cred.Provider.Value)
assert.False(t, cred.Path.Valid(), "path should not be set when only --credential-provider is given")
require.True(t, cred.Auto.Valid(), "auto should default to true when provider is set without path")
assert.True(t, cred.Auto.Value)
}
// Explicit --credential-path should keep the credential reference as a pinned
// path lookup (no implicit auto).
func TestAuthConnectionsCreate_ProviderWithPath_DoesNotSetAuto(t *testing.T) {
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "conn-new"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "google.com",
ProfileName: "my-profile",
CredentialProvider: "my-1p",
CredentialPath: "Employees/Google Workspace",
Output: "json",
})
require.NoError(t, err)
cred := captured.ManagedAuthCreateRequest.Credential
require.True(t, cred.Provider.Valid())
assert.Equal(t, "my-1p", cred.Provider.Value)
require.True(t, cred.Path.Valid())
assert.Equal(t, "Employees/Google Workspace", cred.Path.Value)
assert.False(t, cred.Auto.Valid(), "auto should remain unset when --credential-path is explicit")
}
// --credential-auto should still be honored (it was a no-op redundant flag
// before the default changed, but callers may pass it for clarity).
func TestAuthConnectionsCreate_ProviderWithExplicitAuto_SetsAuto(t *testing.T) {
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "conn-new"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "google.com",
ProfileName: "my-profile",
CredentialProvider: "my-1p",
CredentialAuto: true,
Output: "json",
})
require.NoError(t, err)
cred := captured.ManagedAuthCreateRequest.Credential
require.True(t, cred.Auto.Valid())
assert.True(t, cred.Auto.Value)
}
// --credential-name references a Kernel-managed credential and should never
// carry a provider/auto/path — the default-auto logic must not kick in here.
func TestAuthConnectionsCreate_CredentialName_UnaffectedByAutoDefault(t *testing.T) {
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "conn-new"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "google.com",
ProfileName: "my-profile",
CredentialName: "my-google-creds",
Output: "json",
})
require.NoError(t, err)
cred := captured.ManagedAuthCreateRequest.Credential
require.True(t, cred.Name.Valid())
assert.Equal(t, "my-google-creds", cred.Name.Value)
assert.False(t, cred.Provider.Valid())
assert.False(t, cred.Auto.Valid())
assert.False(t, cred.Path.Valid())
}
func TestAuthConnectionsCreate_RecordSession(t *testing.T) {
tests := []struct {
name string
flag BoolFlag
}{
{name: "omitted"},
{name: "enabled", flag: BoolFlag{Set: true, Value: true}},
{name: "disabled", flag: BoolFlag{Set: true, Value: false}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "conn-new"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "example.com",
ProfileName: "profile-1",
RecordSession: tt.flag,
Output: "json",
}))
assert.Equal(t, tt.flag.Set, captured.ManagedAuthCreateRequest.RecordSession.Valid())
if tt.flag.Set {
assert.Equal(t, tt.flag.Value, captured.ManagedAuthCreateRequest.RecordSession.Value)
}
})
}
}
func TestAuthConnectionsUpdate_MapsParams(t *testing.T) {
var captured kernel.AuthConnectionUpdateParams
fake := &FakeAuthConnectionService{
UpdateFunc: func(ctx context.Context, id string, body kernel.AuthConnectionUpdateParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{
ID: id,
Domain: "example.com",
ProfileName: "profile-1",
Status: kernel.ManagedAuthStatusAuthenticated,
}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Update(context.Background(), AuthConnectionUpdateInput{
ID: "conn-1",
LoginURL: "https://login.example.com",
LoginURLSet: true,
AllowedDomains: []string{"example.com", "login.example.com"},
AllowedDomainsSet: true,
CredentialProvider: "vault-provider",
CredentialProviderSet: true,
CredentialPath: "Vault/Item",
CredentialPathSet: true,
CredentialAuto: BoolFlag{Set: true, Value: true},
ProxyID: "proxy-123",
ProxyIDSet: true,
SaveCredentials: BoolFlag{Set: true, Value: false},
RecordSession: BoolFlag{Set: true, Value: false},
HealthCheckInterval: 900,
HealthCheckIntervalSet: true,
})
require.NoError(t, err)
require.True(t, captured.ManagedAuthUpdateRequest.LoginURL.Valid())
assert.Equal(t, "https://login.example.com", captured.ManagedAuthUpdateRequest.LoginURL.Value)
assert.Equal(t, []string{"example.com", "login.example.com"}, captured.ManagedAuthUpdateRequest.AllowedDomains)
require.True(t, captured.ManagedAuthUpdateRequest.Credential.Provider.Valid())
assert.Equal(t, "vault-provider", captured.ManagedAuthUpdateRequest.Credential.Provider.Value)
require.True(t, captured.ManagedAuthUpdateRequest.Credential.Path.Valid())
assert.Equal(t, "Vault/Item", captured.ManagedAuthUpdateRequest.Credential.Path.Value)
require.True(t, captured.ManagedAuthUpdateRequest.Credential.Auto.Valid())
assert.True(t, captured.ManagedAuthUpdateRequest.Credential.Auto.Value)
require.True(t, captured.ManagedAuthUpdateRequest.Browser.Proxy.ID.Valid())
assert.Equal(t, "proxy-123", captured.ManagedAuthUpdateRequest.Browser.Proxy.ID.Value)
require.True(t, captured.ManagedAuthUpdateRequest.SaveCredentials.Valid())
assert.False(t, captured.ManagedAuthUpdateRequest.SaveCredentials.Value)
require.True(t, captured.ManagedAuthUpdateRequest.RecordSession.Valid())
assert.False(t, captured.ManagedAuthUpdateRequest.RecordSession.Value)
require.True(t, captured.ManagedAuthUpdateRequest.HealthCheckInterval.Valid())
assert.Equal(t, int64(900), captured.ManagedAuthUpdateRequest.HealthCheckInterval.Value)
}
func TestAuthConnectionsLogin_RecordSession(t *testing.T) {
tests := []struct {
name string
flag BoolFlag
}{
{name: "inherits connection default"},
{name: "enabled", flag: BoolFlag{Set: true, Value: true}},
{name: "disabled", flag: BoolFlag{Set: true, Value: false}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var captured kernel.AuthConnectionLoginParams
fake := &FakeAuthConnectionService{
LoginFunc: func(ctx context.Context, id string, body kernel.AuthConnectionLoginParams, opts ...option.RequestOption) (*kernel.LoginResponse, error) {
captured = body
return &kernel.LoginResponse{}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Login(context.Background(), AuthConnectionLoginInput{
ID: "conn-1",
RecordSession: tt.flag,
Output: "json",
}))
assert.Equal(t, tt.flag.Set, captured.RecordSession.Valid())
if tt.flag.Set {
assert.Equal(t, tt.flag.Value, captured.RecordSession.Value)
}
})
}
}
func newFakeWithMfaOptions(options []kernel.ManagedAuthMfaOption) *FakeAuthConnectionService {
return &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
return &kernel.ManagedAuth{
ID: id,
MfaOptions: options,
}, nil
},
SubmitFunc: func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
},
}
}
func TestSubmit_MfaOptionResolvesType(t *testing.T) {
fake := newFakeWithMfaOptions([]kernel.ManagedAuthMfaOption{
{Label: "Get a text", Type: "sms"},
{Label: "Have us call you", Type: "call"},
})
var submittedID string
fake.SubmitFunc = func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
submittedID = body.SubmitFieldsRequest.MfaOptionID.Value
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
MfaOptionID: "sms",
Output: "json",
})
require.NoError(t, err)
assert.Equal(t, "sms", submittedID)
}
func TestSubmit_SSOProviderMapped(t *testing.T) {
var provider string
fake := &FakeAuthConnectionService{
SubmitFunc: func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
provider = body.SubmitFieldsRequest.SSOProvider.Value
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
SSOProvider: "google",
Output: "json",
})
require.NoError(t, err)
assert.Equal(t, "google", provider)
}
func TestSubmit_SignInOptionMapped(t *testing.T) {
var signInOption string
fake := &FakeAuthConnectionService{
SubmitFunc: func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
signInOption = body.SubmitFieldsRequest.SignInOptionID.Value
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
SignInOptionID: "pick-account",
Output: "json",
})
require.NoError(t, err)
assert.Equal(t, "pick-account", signInOption)
}
func TestSubmit_MfaOptionResolvesLabel(t *testing.T) {
fake := newFakeWithMfaOptions([]kernel.ManagedAuthMfaOption{
{Label: "Get a text", Type: "sms"},
{Label: "Have us call you", Type: "call"},
})
var submittedID string
fake.SubmitFunc = func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
submittedID = body.SubmitFieldsRequest.MfaOptionID.Value
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
MfaOptionID: "Get a text",
Output: "json",
})
require.NoError(t, err)
assert.Equal(t, "sms", submittedID)
}
func TestSubmit_MfaOptionResolvesDisplayString(t *testing.T) {
fake := newFakeWithMfaOptions([]kernel.ManagedAuthMfaOption{
{Label: "Get a text", Type: "sms"},
})
var submittedID string
fake.SubmitFunc = func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
submittedID = body.SubmitFieldsRequest.MfaOptionID.Value
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
MfaOptionID: "Get a text (sms)",
Output: "json",
})
require.NoError(t, err)
assert.Equal(t, "sms", submittedID)
}
func TestSubmit_MfaOptionResolvesLabelCaseInsensitive(t *testing.T) {
fake := newFakeWithMfaOptions([]kernel.ManagedAuthMfaOption{
{Label: "Get a text", Type: "sms"},
})
var submittedID string
fake.SubmitFunc = func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
submittedID = body.SubmitFieldsRequest.MfaOptionID.Value
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
MfaOptionID: "get a TEXT",
Output: "json",
})
require.NoError(t, err)
assert.Equal(t, "sms", submittedID)
}
func TestSubmit_MfaOptionGetErrorSurfaced(t *testing.T) {
fake := &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
return nil, errors.New("connection not found")
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
MfaOptionID: "sms",
Output: "json",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "connection not found")
}
func TestSubmit_MfaOptionRejectsUnknown(t *testing.T) {
fake := newFakeWithMfaOptions([]kernel.ManagedAuthMfaOption{
{Label: "Get a text", Type: "sms"},
{Label: "Have us call you", Type: "call"},
})
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "conn-1",
MfaOptionID: "carrier pigeon",
Output: "json",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "unknown MFA option")
assert.Contains(t, err.Error(), "carrier pigeon")
assert.Contains(t, err.Error(), "Get a text (sms)")
}
func TestCreate_TelemetryCategoriesOptIn(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "auth_1"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "example.com",
ProfileName: "prof",
Telemetry: "console,network",
})
require.NoError(t, err)
tel := captured.ManagedAuthCreateRequest.Browser.Telemetry
assert.False(t, tel.Enabled.Valid())
assert.True(t, tel.Browser.Console.Enabled.Value)
assert.True(t, tel.Browser.Network.Enabled.Value)
assert.False(t, tel.Browser.Page.Enabled.Valid())
}
func TestCreate_TelemetryOff(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "auth_1"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "example.com", ProfileName: "prof", Telemetry: "off",
}))
tel := captured.ManagedAuthCreateRequest.Browser.Telemetry
require.True(t, tel.Enabled.Valid())
assert.False(t, tel.Enabled.Value)
}
// Proxy and stealth are carried by the connection's browser config, which the
// API applies to its login, reauth, and health-check sessions.
func TestCreate_BrowserConfig(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionNewParams
fake := &FakeAuthConnectionService{
NewFunc: func(ctx context.Context, body kernel.AuthConnectionNewParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: "auth_1"}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "example.com",
ProfileName: "prof",
ProxyName: "my-proxy",
Stealth: BoolFlag{Set: true, Value: false},
}))
browser := captured.ManagedAuthCreateRequest.Browser
assert.Equal(t, "my-proxy", browser.Proxy.Name.Value)
require.True(t, browser.Stealth.Valid())
assert.False(t, browser.Stealth.Value)
}
func TestLogin_BrowserProxyMode(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionLoginParams
fake := &FakeAuthConnectionService{
LoginFunc: func(ctx context.Context, id string, body kernel.AuthConnectionLoginParams, opts ...option.RequestOption) (*kernel.LoginResponse, error) {
captured = body
return &kernel.LoginResponse{ID: id}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Login(context.Background(), AuthConnectionLoginInput{ID: "auth_1", ProxyMode: "direct"}))
assert.Equal(t, kernel.BrowserProxyModeDirect, captured.Browser.Proxy.Mode)
}
// A proxy is selected by ID or name, so an empty value is no longer a way to
// clear it: that is what --proxy-mode=default is for.
func TestUpdate_EmptyProxySelection_Errors(t *testing.T) {
capturePtermOutput(t)
c := AuthConnectionCmd{svc: &FakeAuthConnectionService{}}
err := c.Update(context.Background(), AuthConnectionUpdateInput{ID: "auth_1", ProxyIDSet: true})
require.Error(t, err)
assert.Contains(t, err.Error(), "--proxy-mode=default")
}
func TestCreate_TelemetryUnknownCategoryErrors(t *testing.T) {
capturePtermOutput(t)
c := AuthConnectionCmd{svc: &FakeAuthConnectionService{}}
err := c.Create(context.Background(), AuthConnectionCreateInput{
Domain: "example.com", ProfileName: "prof", Telemetry: "bogus",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "unknown category")
}
func TestUpdate_TelemetryCountsAsChange(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionUpdateParams
fake := &FakeAuthConnectionService{
UpdateFunc: func(ctx context.Context, id string, body kernel.AuthConnectionUpdateParams, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
captured = body
return &kernel.ManagedAuth{ID: id}, nil
},
}
c := AuthConnectionCmd{svc: fake}
// --telemetry alone must satisfy the "at least one field" guard.
require.NoError(t, c.Update(context.Background(), AuthConnectionUpdateInput{ID: "auth_1", Telemetry: "all"}))
tel := captured.ManagedAuthUpdateRequest.Browser.Telemetry
require.True(t, tel.Enabled.Valid())
assert.True(t, tel.Enabled.Value)
}
func TestLogin_TelemetryOverride(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionLoginParams
fake := &FakeAuthConnectionService{
LoginFunc: func(ctx context.Context, id string, body kernel.AuthConnectionLoginParams, opts ...option.RequestOption) (*kernel.LoginResponse, error) {
captured = body
return &kernel.LoginResponse{ID: id}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Login(context.Background(), AuthConnectionLoginInput{ID: "auth_1", Telemetry: "screenshot"}))
assert.True(t, captured.Browser.Telemetry.Browser.Screenshot.Enabled.Value)
}
// canonicalSubmitFake serves the current interaction ID from `get` and captures
// what `submit` sends, which is what every canonical submission needs.
func canonicalSubmitFake(interactionID string, captured *kernel.AuthConnectionSubmitParams) *FakeAuthConnectionService {
return &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
return &kernel.ManagedAuth{ID: id, InteractionID: interactionID}, nil
},
SubmitFunc: func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
*captured = body
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
},
}
}
func TestSubmit_CanonicalChoiceID(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionSubmitParams
c := AuthConnectionCmd{svc: canonicalSubmitFake("mai_current", &captured)}
require.NoError(t, c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
SelectedChoiceID: "choice_sms",
}))
require.True(t, captured.SubmitFieldsRequest.SelectedChoiceID.Valid())
assert.Equal(t, "choice_sms", captured.SubmitFieldsRequest.SelectedChoiceID.Value)
// Legacy fields must stay absent so the API sees exactly one submit mode.
assert.Nil(t, captured.SubmitFieldsRequest.Fields)
}
func TestSubmit_CanonicalFieldValues(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionSubmitParams
c := AuthConnectionCmd{svc: canonicalSubmitFake("mai_current", &captured)}
require.NoError(t, c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
CanonicalFieldValues: map[string]string{"field_email": "me@example.com"},
}))
assert.Equal(t, map[string]string{"field_email": "me@example.com"}, captured.SubmitFieldsRequest.FieldValues)
assert.Nil(t, captured.SubmitFieldsRequest.Fields)
}
func TestSubmit_CanonicalResolvesCurrentInteractionID(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionSubmitParams
c := AuthConnectionCmd{svc: canonicalSubmitFake("mai_current", &captured)}
require.NoError(t, c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
CanonicalFieldValues: map[string]string{"field_email": "me@example.com"},
}))
require.True(t, captured.SubmitFieldsRequest.InteractionID.Valid())
assert.Equal(t, "mai_current", captured.SubmitFieldsRequest.InteractionID.Value)
}
func TestSubmit_ExplicitInteractionIDIsNotOverwritten(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionSubmitParams
fake := canonicalSubmitFake("mai_current", &captured)
getCalls := 0
inner := fake.GetFunc
fake.GetFunc = func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
getCalls++
return inner(ctx, id, opts...)
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
SelectedChoiceID: "choice_sms",
// Pinning an older interaction is how a caller detects that the flow moved
// on, so the CLI must forward it untouched.
InteractionID: "mai_pinned",
}))
assert.Equal(t, 0, getCalls)
require.True(t, captured.SubmitFieldsRequest.InteractionID.Valid())
assert.Equal(t, "mai_pinned", captured.SubmitFieldsRequest.InteractionID.Value)
}
func TestSubmit_LegacyModeOmitsInteractionID(t *testing.T) {
capturePtermOutput(t)
var captured kernel.AuthConnectionSubmitParams
fake := &FakeAuthConnectionService{
SubmitFunc: func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
captured = body
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
},
}
c := AuthConnectionCmd{svc: fake}
require.NoError(t, c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
FieldValues: map[string]string{"username": "me"},
}))
// The API rejects an interaction ID paired with a legacy submit mode.
assert.False(t, captured.SubmitFieldsRequest.InteractionID.Valid())
}
func TestSubmit_InteractionIDRequiresCanonicalMode(t *testing.T) {
capturePtermOutput(t)
c := AuthConnectionCmd{svc: &FakeAuthConnectionService{}}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
FieldValues: map[string]string{"username": "me"},
InteractionID: "mai_current",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "the --interaction-id flag is only valid with --field-value or --choice-id")
}
func TestSubmit_CanonicalWithoutPendingInteractionErrors(t *testing.T) {
capturePtermOutput(t)
submitted := false
fake := &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
return &kernel.ManagedAuth{ID: id}, nil
},
SubmitFunc: func(ctx context.Context, id string, body kernel.AuthConnectionSubmitParams, opts ...option.RequestOption) (*kernel.SubmitFieldsResponse, error) {
submitted = true
return &kernel.SubmitFieldsResponse{Accepted: true}, nil
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
SelectedChoiceID: "choice_sms",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "no canonical interaction awaiting input")
assert.False(t, submitted)
}
func TestSubmit_CanonicalGetErrorSurfaced(t *testing.T) {
capturePtermOutput(t)
fake := &FakeAuthConnectionService{
GetFunc: func(ctx context.Context, id string, opts ...option.RequestOption) (*kernel.ManagedAuth, error) {
return nil, errors.New("boom")
},
}
c := AuthConnectionCmd{svc: fake}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
CanonicalFieldValues: map[string]string{"field_email": "me@example.com"},
})
require.Error(t, err)
assert.Contains(t, err.Error(), "interaction ID resolution")
}
func TestSubmit_CanonicalAndLegacyAreMutuallyExclusive(t *testing.T) {
capturePtermOutput(t)
c := AuthConnectionCmd{svc: &FakeAuthConnectionService{}}
err := c.Submit(context.Background(), AuthConnectionSubmitInput{
ID: "auth_1",
FieldValues: map[string]string{"email": "a@b.com"},
CanonicalFieldValues: map[string]string{"field_email": "a@b.com"},
})
require.Error(t, err)
assert.Contains(t, err.Error(), "provide exactly one of")
}
func TestTimeline_RendersEventsAndPagination(t *testing.T) {
buf := capturePtermOutput(t)
var captured kernel.AuthConnectionTimelineParams
// Decoded from JSON so the telemetry_captured field registers as present;
// the CLI distinguishes "reported false" from "not reported at all".
var loginEvent kernel.ManagedAuthTimelineEvent
require.NoError(t, json.Unmarshal([]byte(`{
"id": "e1",
"type": "login",
"status": "SUCCESS",
"browser_session_id": "browser_1",
"telemetry_captured": true
}`), &loginEvent))
fake := &FakeAuthConnectionService{
TimelineFunc: func(ctx context.Context, id string, query kernel.AuthConnectionTimelineParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ManagedAuthTimelineEvent], error) {
captured = query