@@ -13,7 +13,6 @@ import (
1313 "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
1414 "github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
1515 "github.com/stretchr/testify/require"
16- "net/http"
1716 "strings"
1817 "testing"
1918 "time"
@@ -25,52 +24,69 @@ func (cf credentialFunc) GetToken(ctx context.Context, options policy.TokenReque
2524 return cf (ctx , options )
2625}
2726
28- func TestChallengePolicy (t * testing.T ) {
27+ func TestChallengePolicyStorage (t * testing.T ) {
2928 accessToken := "***"
30- storageResource := "https://storage.azure.com"
3129 storageScope := "https://storage.azure.com/.default"
32- challenge := `Bearer authorization_uri="https://login.microsoftonline.com/{tenant}", resource_id="{storageResource}"`
30+
31+ srv , close := mock .NewServer (mock .WithTransformAllRequestsToTestServerUrl ())
32+ defer close ()
33+ srv .AppendResponse (
34+ mock .WithStatusCode (200 ),
35+ )
36+ authenticated := false
37+ cred := credentialFunc (func (ctx context.Context , tro policy.TokenRequestOptions ) (azcore.AccessToken , error ) {
38+ authenticated = true
39+ require .Equal (t , []string {storageScope }, tro .Scopes )
40+ return azcore.AccessToken {Token : accessToken , ExpiresOn : time .Now ().Add (time .Hour )}, nil
41+ })
42+ p := NewStorageChallengePolicy (cred )
43+ pl := runtime .NewPipeline ("" , "" ,
44+ runtime.PipelineOptions {PerRetry : []policy.Policy {p }},
45+ & policy.ClientOptions {Transport : srv },
46+ )
47+ req , err := runtime .NewRequest (context .Background (), "GET" , "https://localhost" )
48+ require .NoError (t , err )
49+ _ , err = pl .Do (req )
50+ require .NoError (t , err )
51+ require .True (t , authenticated , "policy should have authenticated" )
52+ }
53+
54+ func TestChallengePolicyDisk (t * testing.T ) {
55+ accessToken := "***"
3356 diskResource := "https://disk.azure.com/"
3457 diskScope := "https://disk.azure.com//.default"
58+ challenge := `Bearer authorization_uri="https://login.microsoftonline.com/{tenant}", resource_id="{storageResource}"`
3559
36- for _ , test := range []struct {
37- expectedScope , format , resource string
38- }{
39- {format : challenge , resource : storageResource , expectedScope : storageScope },
40- {format : challenge , resource : diskResource , expectedScope : diskScope },
41- } {
42- t .Run ("" , func (t * testing.T ) {
43- srv , close := mock .NewServer (mock .WithTransformAllRequestsToTestServerUrl ())
44- defer close ()
45- srv .AppendResponse (
46- mock .WithHeader ("WWW-Authenticate" , strings .ReplaceAll (test .format , "{storageResource}" , test .resource )),
47- mock .WithStatusCode (401 ),
48- )
49- srv .AppendResponse (mock .WithPredicate (func (r * http.Request ) bool {
50- if authz := r .Header .Values ("Authorization" ); len (authz ) != 1 || authz [0 ] != "Bearer " + accessToken {
51- t .Errorf (`unexpected Authorization "%s"` , authz )
52- }
53- return true
54- }))
55- srv .AppendResponse ()
56- authenticated := false
57- cred := credentialFunc (func (ctx context.Context , tro policy.TokenRequestOptions ) (azcore.AccessToken , error ) {
58- authenticated = true
59- require .Equal (t , []string {test .expectedScope }, tro .Scopes )
60- return azcore.AccessToken {Token : accessToken , ExpiresOn : time .Now ().Add (time .Hour )}, nil
61- })
62- p := NewStorageChallengePolicy (cred )
63- pl := runtime .NewPipeline ("" , "" ,
64- runtime.PipelineOptions {PerRetry : []policy.Policy {p }},
65- & policy.ClientOptions {Transport : srv },
66- )
67- req , err := runtime .NewRequest (context .Background (), "GET" , "https://localhost" )
68- require .NoError (t , err )
69- _ , err = pl .Do (req )
70- require .NoError (t , err )
71- require .True (t , authenticated , "policy should have authenticated" )
72- })
73- }
60+ srv , close := mock .NewServer (mock .WithTransformAllRequestsToTestServerUrl ())
61+ defer close ()
62+ srv .AppendResponse (
63+ mock .WithHeader ("WWW-Authenticate" , strings .ReplaceAll (challenge , "{storageResource}" , diskResource )),
64+ mock .WithStatusCode (401 ),
65+ )
66+ srv .AppendResponse (
67+ mock .WithStatusCode (200 ),
68+ )
69+ attemptedAuthentication := false
70+ authenticated := false
71+ cred := credentialFunc (func (ctx context.Context , tro policy.TokenRequestOptions ) (azcore.AccessToken , error ) {
72+ if attemptedAuthentication {
73+ authenticated = true
74+ require .Equal (t , []string {diskScope }, tro .Scopes )
75+ return azcore.AccessToken {Token : accessToken , ExpiresOn : time .Now ().Add (time .Hour )}, nil
76+ }
77+ attemptedAuthentication = true
78+ return azcore.AccessToken {}, nil
79+ })
80+ p := NewStorageChallengePolicy (cred )
81+ pl := runtime .NewPipeline ("" , "" ,
82+ runtime.PipelineOptions {PerRetry : []policy.Policy {p }},
83+ & policy.ClientOptions {Transport : srv },
84+ )
85+ req , err := runtime .NewRequest (context .Background (), "GET" , "https://localhost" )
86+ require .NoError (t , err )
87+ _ , err = pl .Do (req )
88+ require .NoError (t , err )
89+ require .True (t , authenticated , "policy should have authenticated" )
7490}
7591
7692func TestParseTenant (t * testing.T ) {
0 commit comments