@@ -18,6 +18,7 @@ import (
1818 "github.com/azure/azure-dev/cli/azd/pkg/azdext"
1919 "github.com/azure/azure-dev/cli/azd/pkg/extensions"
2020 "github.com/stretchr/testify/require"
21+ "google.golang.org/genproto/googleapis/rpc/errdetails"
2122 "google.golang.org/grpc/codes"
2223 "google.golang.org/grpc/status"
2324)
@@ -239,6 +240,7 @@ func Test_wrapErrorWithSuggestion(t *testing.T) {
239240 wantContain string
240241 wantSameInstance bool
241242 wantGrpcCode codes.Code
243+ wantAuthReason string
242244 }{
243245 {
244246 name : "nil error returns nil" ,
@@ -268,25 +270,43 @@ func Test_wrapErrorWithSuggestion(t *testing.T) {
268270 wantContain : "azd auth login" ,
269271 },
270272 {
271- name : "ErrNoCurrentUser returns Unauthenticated" ,
272- err : auth .ErrNoCurrentUser ,
273- wantContain : "not logged in" ,
274- wantGrpcCode : codes .Unauthenticated ,
273+ name : "ErrNoCurrentUser returns Unauthenticated" ,
274+ err : auth .ErrNoCurrentUser ,
275+ wantContain : "not logged in" ,
276+ wantGrpcCode : codes .Unauthenticated ,
277+ wantAuthReason : azdext .AuthErrorReasonNotLoggedIn ,
275278 },
276279 {
277- name : "wrapped ErrNoCurrentUser returns Unauthenticated" ,
278- err : fmt .Errorf ("failed to list subscriptions: %w" , auth .ErrNoCurrentUser ),
279- wantContain : "not logged in" ,
280- wantGrpcCode : codes .Unauthenticated ,
280+ name : "wrapped ErrNoCurrentUser returns Unauthenticated" ,
281+ err : fmt .Errorf ("failed to list subscriptions: %w" , auth .ErrNoCurrentUser ),
282+ wantContain : "not logged in" ,
283+ wantGrpcCode : codes .Unauthenticated ,
284+ wantAuthReason : azdext .AuthErrorReasonNotLoggedIn ,
281285 },
282286 {
283287 name : "ReLoginRequiredError with suggestion returns Unauthenticated" ,
284288 err : & internal.ErrorWithSuggestion {
285289 Err : & auth.ReLoginRequiredError {},
286290 Suggestion : "login expired, run `azd auth login` to acquire a new token." ,
287291 },
288- wantContain : "azd auth login" ,
289- wantGrpcCode : codes .Unauthenticated ,
292+ wantContain : "azd auth login" ,
293+ wantGrpcCode : codes .Unauthenticated ,
294+ wantAuthReason : azdext .AuthErrorReasonLoginRequired ,
295+ },
296+ {
297+ name : "TokenProtectionBlockedError with suggestion returns Unauthenticated" ,
298+ err : & internal.ErrorWithSuggestion {
299+ Err : & auth.AuthFailedError {
300+ Parsed : & auth.AadErrorResponse {
301+ Error : "invalid_grant" ,
302+ ErrorCodes : []int {530084 },
303+ },
304+ },
305+ Suggestion : "Contact your IT administrator or request a policy exception." ,
306+ },
307+ wantContain : "policy exception" ,
308+ wantGrpcCode : codes .Unauthenticated ,
309+ wantAuthReason : "AADSTS530084" ,
290310 },
291311 }
292312
@@ -306,6 +326,14 @@ func Test_wrapErrorWithSuggestion(t *testing.T) {
306326 st , ok := status .FromError (result )
307327 require .True (t , ok , "expected gRPC status error" )
308328 require .Equal (t , tt .wantGrpcCode , st .Code ())
329+ if tt .wantAuthReason != "" {
330+ details := st .Details ()
331+ require .Len (t , details , 1 )
332+ info , ok := details [0 ].(* errdetails.ErrorInfo )
333+ require .True (t , ok , "expected ErrorInfo detail" )
334+ require .Equal (t , azdext .AuthErrorDomain , info .Domain )
335+ require .Equal (t , tt .wantAuthReason , info .Reason )
336+ }
309337 }
310338 })
311339 }
0 commit comments