Skip to content

Commit 1ddfb01

Browse files
authored
azidentity test cleanup (#20947)
1 parent 827c7b5 commit 1ddfb01

16 files changed

+77
-85
lines changed

sdk/azidentity/azidentity_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
accessTokenRespSuccess = []byte(fmt.Sprintf(`{"access_token": "%s", "expires_in": %d}`, tokenValue, tokenExpiresIn))
3939
instanceDiscoveryResponse = getInstanceDiscoveryResponse(fakeTenantID)
4040
tenantDiscoveryResponse = getTenantDiscoveryResponse(fakeTenantID)
41+
testTRO = policy.TokenRequestOptions{Scopes: []string{liveTestScope}}
4142
)
4243

4344
// constants for this file

sdk/azidentity/azure_cli_credential_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"errors"
1212
"testing"
1313
"time"
14-
15-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1614
)
1715

1816
var (
@@ -38,7 +36,7 @@ func TestAzureCLICredential_GetTokenSuccess(t *testing.T) {
3836
if err != nil {
3937
t.Fatal(err)
4038
}
41-
at, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
39+
at, err := cred.GetToken(context.Background(), testTRO)
4240
if err != nil {
4341
t.Fatal(err)
4442
}
@@ -58,7 +56,7 @@ func TestAzureCLICredential_GetTokenInvalidToken(t *testing.T) {
5856
if err != nil {
5957
t.Fatalf("Unable to create credential. Received: %v", err)
6058
}
61-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
59+
_, err = cred.GetToken(context.Background(), testTRO)
6260
if err == nil {
6361
t.Fatalf("Expected an error but did not receive one.")
6462
}
@@ -81,7 +79,7 @@ func TestAzureCLICredential_TenantID(t *testing.T) {
8179
if err != nil {
8280
t.Fatalf("Unable to create credential. Received: %v", err)
8381
}
84-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
82+
_, err = cred.GetToken(context.Background(), testTRO)
8583
if err != nil {
8684
t.Fatalf("Unexpected error: %v", err)
8785
}

sdk/azidentity/chained_token_credential_test.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestChainedTokenCredential_GetTokenSuccess(t *testing.T) {
9393
if err != nil {
9494
t.Fatal(err)
9595
}
96-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
96+
tk, err := cred.GetToken(context.Background(), testTRO)
9797
if err != nil {
9898
t.Fatal(err)
9999
}
@@ -118,7 +118,7 @@ func TestChainedTokenCredential_GetTokenFail(t *testing.T) {
118118
if err != nil {
119119
t.Fatal(err)
120120
}
121-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
121+
_, err = cred.GetToken(context.Background(), testTRO)
122122
if _, ok := err.(*AuthenticationFailedError); !ok {
123123
t.Fatalf("expected AuthenticationFailedError, received %T", err)
124124
}
@@ -138,7 +138,7 @@ func TestChainedTokenCredential_MultipleCredentialsGetTokenUnavailable(t *testin
138138
if err != nil {
139139
t.Fatal(err)
140140
}
141-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
141+
_, err = cred.GetToken(context.Background(), testTRO)
142142
if _, ok := err.(*credentialUnavailableError); !ok {
143143
t.Fatalf("expected credentialUnavailableError, received %T", err)
144144
}
@@ -163,7 +163,7 @@ func TestChainedTokenCredential_MultipleCredentialsGetTokenAuthenticationFailed(
163163
if err != nil {
164164
t.Fatal(err)
165165
}
166-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
166+
_, err = cred.GetToken(context.Background(), testTRO)
167167
if _, ok := err.(*AuthenticationFailedError); !ok {
168168
t.Fatalf("expected AuthenticationFailedError, received %T", err)
169169
}
@@ -185,7 +185,7 @@ func TestChainedTokenCredential_MultipleCredentialsGetTokenCustomName(t *testing
185185
t.Fatal(err)
186186
}
187187
cred.name = "CustomNameCredential"
188-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
188+
_, err = cred.GetToken(context.Background(), testTRO)
189189
if _, ok := err.(*credentialUnavailableError); !ok {
190190
t.Fatalf("expected credentialUnavailableError, received %T", err)
191191
}
@@ -208,17 +208,15 @@ func TestChainedTokenCredential_RepeatedGetTokenWithSuccessfulCredential(t *test
208208
t.Fatal(err)
209209
}
210210

211-
getTokenOptions := policy.TokenRequestOptions{Scopes: []string{liveTestScope}}
212-
213-
tk, err := cred.GetToken(context.Background(), getTokenOptions)
211+
tk, err := cred.GetToken(context.Background(), testTRO)
214212
testGoodGetTokenResponse(t, tk, err)
215213
if failedCredential.getTokenCalls != 1 {
216214
t.Fatal("The failed credential getToken should have been called once")
217215
}
218216
if successfulCredential.getTokenCalls != 1 {
219217
t.Fatalf("The successful credential getToken should have been called once")
220218
}
221-
tk2, err2 := cred.GetToken(context.Background(), getTokenOptions)
219+
tk2, err2 := cred.GetToken(context.Background(), testTRO)
222220
testGoodGetTokenResponse(t, tk2, err2)
223221
if failedCredential.getTokenCalls != 1 {
224222
t.Fatalf("The failed credential getToken should not have been called again")
@@ -239,17 +237,15 @@ func TestChainedTokenCredential_RepeatedGetTokenWithSuccessfulCredentialWithRetr
239237
t.Fatal(err)
240238
}
241239

242-
getTokenOptions := policy.TokenRequestOptions{Scopes: []string{liveTestScope}}
243-
244-
tk, err := cred.GetToken(context.Background(), getTokenOptions)
240+
tk, err := cred.GetToken(context.Background(), testTRO)
245241
testGoodGetTokenResponse(t, tk, err)
246242
if failedCredential.getTokenCalls != 1 {
247243
t.Fatalf("The failed credential getToken should have been called once")
248244
}
249245
if successfulCredential.getTokenCalls != 1 {
250246
t.Fatalf("The successful credential getToken should have been called once")
251247
}
252-
tk2, err2 := cred.GetToken(context.Background(), getTokenOptions)
248+
tk2, err2 := cred.GetToken(context.Background(), testTRO)
253249
testGoodGetTokenResponse(t, tk2, err2)
254250
if failedCredential.getTokenCalls != 2 {
255251
t.Fatalf("The failed credential getToken should have been called twice")
@@ -266,7 +262,6 @@ func TestChainedTokenCredential_Race(t *testing.T) {
266262
authFailFake.SetResponse(azcore.AccessToken{}, newAuthenticationFailedError("", "", nil, nil))
267263
unavailableFake := NewFakeCredential()
268264
unavailableFake.SetResponse(azcore.AccessToken{}, newCredentialUnavailableError("", ""))
269-
tro := policy.TokenRequestOptions{Scopes: []string{liveTestScope}}
270265

271266
for _, b := range []bool{true, false} {
272267
t.Run(fmt.Sprintf("RetrySources_%v", b), func(t *testing.T) {
@@ -281,9 +276,9 @@ func TestChainedTokenCredential_Race(t *testing.T) {
281276
)
282277
for i := 0; i < 5; i++ {
283278
go func() {
284-
_, _ = success.GetToken(context.Background(), tro)
285-
_, _ = failure.GetToken(context.Background(), tro)
286-
_, _ = unavailable.GetToken(context.Background(), tro)
279+
_, _ = success.GetToken(context.Background(), testTRO)
280+
_, _ = failure.GetToken(context.Background(), testTRO)
281+
_, _ = unavailable.GetToken(context.Background(), testTRO)
287282
}()
288283
}
289284
})

sdk/azidentity/client_assertion_credential_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"testing"
1515

1616
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
17-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1817
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
1918
)
2019

@@ -41,15 +40,15 @@ func TestClientAssertionCredential(t *testing.T) {
4140
t.Fatal(err)
4241
}
4342
ctx := context.WithValue(context.Background(), key, true)
44-
_, err = cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
43+
_, err = cred.GetToken(ctx, testTRO)
4544
if err != nil {
4645
t.Fatal(err)
4746
}
4847
if calls != 1 {
4948
t.Fatalf("expected 1 call, got %d", calls)
5049
}
5150
// silent authentication should now succeed
52-
_, err = cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
51+
_, err = cred.GetToken(ctx, testTRO)
5352
if err != nil {
5453
t.Fatal(err)
5554
}
@@ -73,7 +72,7 @@ func TestClientAssertionCredentialCallbackError(t *testing.T) {
7372
if err != nil {
7473
t.Fatal(err)
7574
}
76-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
75+
_, err = cred.GetToken(context.Background(), testTRO)
7776
if err == nil || !strings.Contains(err.Error(), expectedError.Error()) {
7877
t.Fatalf(`unexpected error: "%v"`, err)
7978
}

sdk/azidentity/client_certificate_credential_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"testing"
1818

1919
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
20-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
2120
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
2221
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
2322
)
@@ -84,7 +83,7 @@ func TestClientCertificateCredential_GetTokenSuccess(t *testing.T) {
8483
t.Fatalf("Expected an empty error but received: %s", err.Error())
8584
}
8685
cred.client = fakeConfidentialClient{}
87-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
86+
_, err = cred.GetToken(context.Background(), testTRO)
8887
if err != nil {
8988
t.Fatalf("Expected an empty error but received: %s", err.Error())
9089
}
@@ -101,7 +100,7 @@ func TestClientCertificateCredential_GetTokenSuccess_withCertificateChain(t *tes
101100
t.Fatalf("Expected an empty error but received: %s", err.Error())
102101
}
103102
cred.client = fakeConfidentialClient{}
104-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
103+
_, err = cred.GetToken(context.Background(), testTRO)
105104
if err != nil {
106105
t.Fatalf("Expected an empty error but received: %s", err.Error())
107106
}
@@ -124,7 +123,7 @@ func TestClientCertificateCredential_SendCertificateChain(t *testing.T) {
124123
if err != nil {
125124
t.Fatal(err)
126125
}
127-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
126+
tk, err := cred.GetToken(context.Background(), testTRO)
128127
if err != nil {
129128
t.Fatal(err)
130129
}
@@ -142,7 +141,7 @@ func TestClientCertificateCredential_GetTokenCheckPrivateKeyBlocks(t *testing.T)
142141
t.Fatalf("Expected an empty error but received: %s", err.Error())
143142
}
144143
cred.client = fakeConfidentialClient{}
145-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
144+
_, err = cred.GetToken(context.Background(), testTRO)
146145
if err != nil {
147146
t.Fatalf("Expected an empty error but received: %s", err.Error())
148147
}
@@ -283,7 +282,7 @@ func TestClientCertificateCredential_InvalidCertLive(t *testing.T) {
283282
t.Fatalf("failed to construct credential: %v", err)
284283
}
285284

286-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
285+
tk, err := cred.GetToken(context.Background(), testTRO)
287286
if !reflect.ValueOf(tk).IsZero() {
288287
t.Fatal("expected a zero value AccessToken")
289288
}

sdk/azidentity/client_secret_credential_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"strings"
1313
"testing"
1414

15-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1615
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
1716
)
1817

@@ -34,7 +33,7 @@ func TestClientSecretCredential_GetTokenSuccess(t *testing.T) {
3433
t.Fatalf("Unable to create credential. Received: %v", err)
3534
}
3635
cred.client = fakeConfidentialClient{}
37-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
36+
_, err = cred.GetToken(context.Background(), testTRO)
3837
if err != nil {
3938
t.Fatalf("Expected an empty error but received: %v", err)
4039
}
@@ -84,7 +83,7 @@ func TestClientSecretCredential_InvalidSecretLive(t *testing.T) {
8483
if err != nil {
8584
t.Fatalf("failed to construct credential: %v", err)
8685
}
87-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
86+
tk, err := cred.GetToken(context.Background(), testTRO)
8887
if !reflect.ValueOf(tk).IsZero() {
8988
t.Fatal("expected a zero value AccessToken")
9089
}

sdk/azidentity/default_azure_credential_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,16 @@ func TestDefaultAzureCredential_ConstructorErrorHandler(t *testing.T) {
7777
}
7878

7979
func TestDefaultAzureCredential_ConstructorErrors(t *testing.T) {
80+
// ensure NewEnvironmentCredential returns an error
81+
t.Setenv(azureTenantID, "")
8082
cred, err := NewDefaultAzureCredential(nil)
8183
if err != nil {
8284
t.Fatal(err)
8385
}
84-
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
85-
defer cancel()
86-
_, err = cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
86+
// make GetToken return an error in any runtime environment
87+
ctx, cancel := context.WithCancel(context.Background())
88+
cancel()
89+
_, err = cred.GetToken(ctx, testTRO)
8790
if err == nil {
8891
t.Fatal("expected an error")
8992
}
@@ -206,15 +209,15 @@ func TestDefaultAzureCredential_timeoutWrapper(t *testing.T) {
206209
}
207210
for i := 0; i < 2; i++ {
208211
// expecting credentialUnavailableError because delay exceeds the wrapper's timeout
209-
_, err = chain.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
212+
_, err = chain.GetToken(context.Background(), testTRO)
210213
if _, ok := err.(*credentialUnavailableError); !ok {
211214
t.Fatalf("expected credentialUnavailableError, got %T: %v", err, err)
212215
}
213216
}
214217

215218
// remove the delay so the credential can authenticate
216219
dp.delay = 0
217-
tk, err := chain.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
220+
tk, err := chain.GetToken(context.Background(), testTRO)
218221
if err != nil {
219222
t.Fatal(err)
220223
}

sdk/azidentity/device_code_credential_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"fmt"
1313
"testing"
1414

15-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1615
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
1716
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
1817
)
@@ -35,7 +34,7 @@ func TestDeviceCodeCredential_GetTokenInvalidCredentials(t *testing.T) {
3534
t.Fatalf("Unable to create credential. Received: %v", err)
3635
}
3736
cred.client = fakePublicClient{err: errors.New("invalid credentials")}
38-
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
37+
_, err = cred.GetToken(context.Background(), testTRO)
3938
if err == nil {
4039
t.Fatalf("Expected an error but did not receive one.")
4140
}
@@ -77,7 +76,7 @@ func TestDeviceCodeCredential_UserPromptError(t *testing.T) {
7776
},
7877
},
7978
}
80-
_, err = cred.GetToken(expectedCtx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
79+
_, err = cred.GetToken(expectedCtx, testTRO)
8180
if err == nil {
8281
t.Fatal("expected an error")
8382
}

sdk/azidentity/environment_credential_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"testing"
1616

1717
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
18-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1918
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
2019
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
2120
)
@@ -223,7 +222,7 @@ func TestEnvironmentCredential_SendCertificateChain(t *testing.T) {
223222
if err != nil {
224223
t.Fatal(err)
225224
}
226-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
225+
tk, err := cred.GetToken(context.Background(), testTRO)
227226
if err != nil {
228227
t.Fatal(err)
229228
}
@@ -297,7 +296,7 @@ func TestEnvironmentCredential_InvalidClientSecretLive(t *testing.T) {
297296
if err != nil {
298297
t.Fatalf("failed to construct credential: %v", err)
299298
}
300-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
299+
tk, err := cred.GetToken(context.Background(), testTRO)
301300
if !reflect.ValueOf(tk).IsZero() {
302301
t.Fatal("expected a zero value AccessToken")
303302
}
@@ -381,7 +380,7 @@ func TestEnvironmentCredential_InvalidPasswordLive(t *testing.T) {
381380
if err != nil {
382381
t.Fatalf("failed to construct credential: %v", err)
383382
}
384-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
383+
tk, err := cred.GetToken(context.Background(), testTRO)
385384
if !reflect.ValueOf(tk).IsZero() {
386385
t.Fatal("expected a zero value AccessToken")
387386
}

sdk/azidentity/interactive_browser_credential_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestInteractiveBrowserCredential_GetTokenSuccess(t *testing.T) {
4141
ExpiresOn: time.Now().Add(1 * time.Hour),
4242
},
4343
}
44-
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
44+
tk, err := cred.GetToken(context.Background(), testTRO)
4545
if err != nil {
4646
t.Fatalf("Expected an empty error but received: %v", err)
4747
}
@@ -127,7 +127,6 @@ func TestInteractiveBrowserCredentialADFS_Live(t *testing.T) {
127127
if adfsLiveUser.clientID == fakeClientID {
128128
t.Skip("set ADFS_IDENTITY_TEST_CLIENT_ID environment variables to run this test live")
129129
}
130-
//Redirect URL is necessary
131130
url := adfsLiveSP.redirectURL
132131

133132
cloudConfig := cloud.Configuration{ActiveDirectoryAuthorityHost: adfsAuthority}

0 commit comments

Comments
 (0)