@@ -23,6 +23,18 @@ import (
2323 "github.com/stretchr/testify/require"
2424)
2525
26+ // authRejectionStrategy reads the one AuthStrategy detail a callback rejection
27+ // carries.
28+ func authRejectionStrategy (t * testing.T , connectErr * connect.Error ) * frontierv1beta1.AuthStrategy {
29+ t .Helper ()
30+ require .Len (t , connectErr .Details (), 1 )
31+ msg , err := connectErr .Details ()[0 ].Value ()
32+ require .NoError (t , err )
33+ strategy , ok := msg .(* frontierv1beta1.AuthStrategy )
34+ require .True (t , ok , "detail is %T, want AuthStrategy" , msg )
35+ return strategy
36+ }
37+
2638func TestConnectHandler_AuthToken_ServiceUser (t * testing.T ) {
2739 tests := []struct {
2840 name string
@@ -529,6 +541,9 @@ func TestConnectHandler_Authenticate_Rejections(t *testing.T) {
529541 assert .Equal (t , tt .wantCode , connectErr .Code ())
530542 assert .Equal (t , tt .wantMsg , connectErr .Message ())
531543 assert .NotContains (t , connectErr .Message (), "terms_of_service" )
544+ // at flow start the client named the strategy itself, so nothing is
545+ // echoed back
546+ assert .Empty (t , connectErr .Details ())
532547 })
533548 }
534549}
@@ -593,6 +608,66 @@ func TestConnectHandler_AuthCallback_Rejections(t *testing.T) {
593608 }
594609}
595610
611+ // TestConnectHandler_AuthCallback_RejectionNamesStrategy covers the
612+ // AuthStrategy detail the callback attaches so a client can put the message
613+ // under the button that caused it. A rejection with no flow behind it carries
614+ // no detail.
615+ func TestConnectHandler_AuthCallback_RejectionNamesStrategy (t * testing.T ) {
616+ tests := []struct {
617+ name string
618+ err error
619+
620+ wantCode connect.Code
621+ wantStrategy string
622+ }{
623+ {
624+ name : "an oidc rejection names its provider" ,
625+ err : & authenticate.FlowRejection {Err : authenticate .ErrLoginUserNotFound , Strategy : "google" },
626+ wantCode : connect .CodeNotFound ,
627+ wantStrategy : "google" ,
628+ },
629+ {
630+ name : "a wrapped consent rejection keeps its strategy" ,
631+ err : & authenticate.FlowRejection {
632+ Err : fmt .Errorf ("%w: %w: terms_of_service" , authenticate .ErrConsentRequired , consent .ErrMissingDocuments ),
633+ Strategy : authenticate .MailOTPAuthMethod .String (),
634+ },
635+ wantCode : connect .CodeFailedPrecondition ,
636+ wantStrategy : authenticate .MailOTPAuthMethod .String (),
637+ },
638+ {
639+ name : "a bare sentinel carries no detail" ,
640+ err : authenticate .ErrSignupUserExists ,
641+ wantCode : connect .CodeAlreadyExists ,
642+ },
643+ }
644+
645+ for _ , tt := range tests {
646+ t .Run (tt .name , func (t * testing.T ) {
647+ ctx := context .Background ()
648+
649+ mockAuthnSrv := mocks .NewAuthnService (t )
650+ mockAuthnSrv .EXPECT ().FinishFlow (ctx , mock .Anything ).Return (nil , tt .err )
651+
652+ handler := & ConnectHandler {authnService : mockAuthnSrv }
653+ resp , err := handler .AuthCallback (ctx , connect .NewRequest (& frontierv1beta1.AuthCallbackRequest {
654+ State : "state" ,
655+ Code : "code" ,
656+ }))
657+
658+ assert .Nil (t , resp )
659+ connectErr := err .(* connect.Error )
660+ assert .Equal (t , tt .wantCode , connectErr .Code ())
661+ assert .NotContains (t , connectErr .Message (), "terms_of_service" )
662+ if tt .wantStrategy == "" {
663+ assert .Empty (t , connectErr .Details ())
664+ return
665+ }
666+ assert .Equal (t , tt .wantStrategy , authRejectionStrategy (t , connectErr ).GetName ())
667+ })
668+ }
669+ }
670+
596671// TestConnectHandler_AuthCallback_UnmappedErrorStaysInternal pins the boundary of
597672// the closed code set: an error with no code of its own stays a 500.
598673func TestConnectHandler_AuthCallback_UnmappedErrorStaysInternal (t * testing.T ) {
0 commit comments