Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azidentity test cleanup #20947

Merged
merged 6 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/azidentity/azidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
accessTokenRespSuccess = []byte(fmt.Sprintf(`{"access_token": "%s", "expires_in": %d}`, tokenValue, tokenExpiresIn))
instanceDiscoveryResponse = getInstanceDiscoveryResponse(fakeTenantID)
tenantDiscoveryResponse = getTenantDiscoveryResponse(fakeTenantID)
testTRO = policy.TokenRequestOptions{Scopes: []string{liveTestScope}}
)

// constants for this file
Expand Down
8 changes: 3 additions & 5 deletions sdk/azidentity/azure_cli_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"errors"
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
)

var (
Expand All @@ -38,7 +36,7 @@ func TestAzureCLICredential_GetTokenSuccess(t *testing.T) {
if err != nil {
t.Fatal(err)
}
at, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
at, err := cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatal(err)
}
Expand All @@ -58,7 +56,7 @@ func TestAzureCLICredential_GetTokenInvalidToken(t *testing.T) {
if err != nil {
t.Fatalf("Unable to create credential. Received: %v", err)
}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err == nil {
t.Fatalf("Expected an error but did not receive one.")
}
Expand All @@ -81,7 +79,7 @@ func TestAzureCLICredential_TenantID(t *testing.T) {
if err != nil {
t.Fatalf("Unable to create credential. Received: %v", err)
}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
Expand Down
29 changes: 12 additions & 17 deletions sdk/azidentity/chained_token_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestChainedTokenCredential_GetTokenSuccess(t *testing.T) {
if err != nil {
t.Fatal(err)
}
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatal(err)
}
Expand All @@ -118,7 +118,7 @@ func TestChainedTokenCredential_GetTokenFail(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if _, ok := err.(*AuthenticationFailedError); !ok {
t.Fatalf("expected AuthenticationFailedError, received %T", err)
}
Expand All @@ -138,7 +138,7 @@ func TestChainedTokenCredential_MultipleCredentialsGetTokenUnavailable(t *testin
if err != nil {
t.Fatal(err)
}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if _, ok := err.(*credentialUnavailableError); !ok {
t.Fatalf("expected credentialUnavailableError, received %T", err)
}
Expand All @@ -163,7 +163,7 @@ func TestChainedTokenCredential_MultipleCredentialsGetTokenAuthenticationFailed(
if err != nil {
t.Fatal(err)
}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if _, ok := err.(*AuthenticationFailedError); !ok {
t.Fatalf("expected AuthenticationFailedError, received %T", err)
}
Expand All @@ -185,7 +185,7 @@ func TestChainedTokenCredential_MultipleCredentialsGetTokenCustomName(t *testing
t.Fatal(err)
}
cred.name = "CustomNameCredential"
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if _, ok := err.(*credentialUnavailableError); !ok {
t.Fatalf("expected credentialUnavailableError, received %T", err)
}
Expand All @@ -208,17 +208,15 @@ func TestChainedTokenCredential_RepeatedGetTokenWithSuccessfulCredential(t *test
t.Fatal(err)
}

getTokenOptions := policy.TokenRequestOptions{Scopes: []string{liveTestScope}}

tk, err := cred.GetToken(context.Background(), getTokenOptions)
tk, err := cred.GetToken(context.Background(), testTRO)
testGoodGetTokenResponse(t, tk, err)
if failedCredential.getTokenCalls != 1 {
t.Fatal("The failed credential getToken should have been called once")
}
if successfulCredential.getTokenCalls != 1 {
t.Fatalf("The successful credential getToken should have been called once")
}
tk2, err2 := cred.GetToken(context.Background(), getTokenOptions)
tk2, err2 := cred.GetToken(context.Background(), testTRO)
testGoodGetTokenResponse(t, tk2, err2)
if failedCredential.getTokenCalls != 1 {
t.Fatalf("The failed credential getToken should not have been called again")
Expand All @@ -239,17 +237,15 @@ func TestChainedTokenCredential_RepeatedGetTokenWithSuccessfulCredentialWithRetr
t.Fatal(err)
}

getTokenOptions := policy.TokenRequestOptions{Scopes: []string{liveTestScope}}

tk, err := cred.GetToken(context.Background(), getTokenOptions)
tk, err := cred.GetToken(context.Background(), testTRO)
testGoodGetTokenResponse(t, tk, err)
if failedCredential.getTokenCalls != 1 {
t.Fatalf("The failed credential getToken should have been called once")
}
if successfulCredential.getTokenCalls != 1 {
t.Fatalf("The successful credential getToken should have been called once")
}
tk2, err2 := cred.GetToken(context.Background(), getTokenOptions)
tk2, err2 := cred.GetToken(context.Background(), testTRO)
testGoodGetTokenResponse(t, tk2, err2)
if failedCredential.getTokenCalls != 2 {
t.Fatalf("The failed credential getToken should have been called twice")
Expand All @@ -266,7 +262,6 @@ func TestChainedTokenCredential_Race(t *testing.T) {
authFailFake.SetResponse(azcore.AccessToken{}, newAuthenticationFailedError("", "", nil, nil))
unavailableFake := NewFakeCredential()
unavailableFake.SetResponse(azcore.AccessToken{}, newCredentialUnavailableError("", ""))
tro := policy.TokenRequestOptions{Scopes: []string{liveTestScope}}

for _, b := range []bool{true, false} {
t.Run(fmt.Sprintf("RetrySources_%v", b), func(t *testing.T) {
Expand All @@ -281,9 +276,9 @@ func TestChainedTokenCredential_Race(t *testing.T) {
)
for i := 0; i < 5; i++ {
go func() {
_, _ = success.GetToken(context.Background(), tro)
_, _ = failure.GetToken(context.Background(), tro)
_, _ = unavailable.GetToken(context.Background(), tro)
_, _ = success.GetToken(context.Background(), testTRO)
_, _ = failure.GetToken(context.Background(), testTRO)
_, _ = unavailable.GetToken(context.Background(), testTRO)
}()
}
})
Expand Down
7 changes: 3 additions & 4 deletions sdk/azidentity/client_assertion_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
)

Expand All @@ -41,15 +40,15 @@ func TestClientAssertionCredential(t *testing.T) {
t.Fatal(err)
}
ctx := context.WithValue(context.Background(), key, true)
_, err = cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(ctx, testTRO)
if err != nil {
t.Fatal(err)
}
if calls != 1 {
t.Fatalf("expected 1 call, got %d", calls)
}
// silent authentication should now succeed
_, err = cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(ctx, testTRO)
if err != nil {
t.Fatal(err)
}
Expand All @@ -73,7 +72,7 @@ func TestClientAssertionCredentialCallbackError(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err == nil || !strings.Contains(err.Error(), expectedError.Error()) {
t.Fatalf(`unexpected error: "%v"`, err)
}
Expand Down
11 changes: 5 additions & 6 deletions sdk/azidentity/client_certificate_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
)
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestClientCertificateCredential_GetTokenSuccess(t *testing.T) {
t.Fatalf("Expected an empty error but received: %s", err.Error())
}
cred.client = fakeConfidentialClient{}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatalf("Expected an empty error but received: %s", err.Error())
}
Expand All @@ -101,7 +100,7 @@ func TestClientCertificateCredential_GetTokenSuccess_withCertificateChain(t *tes
t.Fatalf("Expected an empty error but received: %s", err.Error())
}
cred.client = fakeConfidentialClient{}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatalf("Expected an empty error but received: %s", err.Error())
}
Expand All @@ -124,7 +123,7 @@ func TestClientCertificateCredential_SendCertificateChain(t *testing.T) {
if err != nil {
t.Fatal(err)
}
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatal(err)
}
Expand All @@ -142,7 +141,7 @@ func TestClientCertificateCredential_GetTokenCheckPrivateKeyBlocks(t *testing.T)
t.Fatalf("Expected an empty error but received: %s", err.Error())
}
cred.client = fakeConfidentialClient{}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatalf("Expected an empty error but received: %s", err.Error())
}
Expand Down Expand Up @@ -283,7 +282,7 @@ func TestClientCertificateCredential_InvalidCertLive(t *testing.T) {
t.Fatalf("failed to construct credential: %v", err)
}

tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if !reflect.ValueOf(tk).IsZero() {
t.Fatal("expected a zero value AccessToken")
}
Expand Down
5 changes: 2 additions & 3 deletions sdk/azidentity/client_secret_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"testing"

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

Expand All @@ -34,7 +33,7 @@ func TestClientSecretCredential_GetTokenSuccess(t *testing.T) {
t.Fatalf("Unable to create credential. Received: %v", err)
}
cred.client = fakeConfidentialClient{}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatalf("Expected an empty error but received: %v", err)
}
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestClientSecretCredential_InvalidSecretLive(t *testing.T) {
if err != nil {
t.Fatalf("failed to construct credential: %v", err)
}
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if !reflect.ValueOf(tk).IsZero() {
t.Fatal("expected a zero value AccessToken")
}
Expand Down
13 changes: 8 additions & 5 deletions sdk/azidentity/default_azure_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ func TestDefaultAzureCredential_ConstructorErrorHandler(t *testing.T) {
}

func TestDefaultAzureCredential_ConstructorErrors(t *testing.T) {
// ensure NewEnvironmentCredential returns an error
t.Setenv(azureTenantID, "")
cred, err := NewDefaultAzureCredential(nil)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
_, err = cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
// make GetToken return an error in any runtime environment
ctx, cancel := context.WithCancel(context.Background())
cancel()
jhendrixMSFT marked this conversation as resolved.
Show resolved Hide resolved
_, err = cred.GetToken(ctx, testTRO)
if err == nil {
t.Fatal("expected an error")
}
Expand Down Expand Up @@ -206,15 +209,15 @@ func TestDefaultAzureCredential_timeoutWrapper(t *testing.T) {
}
for i := 0; i < 2; i++ {
// expecting credentialUnavailableError because delay exceeds the wrapper's timeout
_, err = chain.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = chain.GetToken(context.Background(), testTRO)
if _, ok := err.(*credentialUnavailableError); !ok {
t.Fatalf("expected credentialUnavailableError, got %T: %v", err, err)
}
}

// remove the delay so the credential can authenticate
dp.delay = 0
tk, err := chain.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := chain.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions sdk/azidentity/device_code_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
)
Expand All @@ -35,7 +34,7 @@ func TestDeviceCodeCredential_GetTokenInvalidCredentials(t *testing.T) {
t.Fatalf("Unable to create credential. Received: %v", err)
}
cred.client = fakePublicClient{err: errors.New("invalid credentials")}
_, err = cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(context.Background(), testTRO)
if err == nil {
t.Fatalf("Expected an error but did not receive one.")
}
Expand Down Expand Up @@ -77,7 +76,7 @@ func TestDeviceCodeCredential_UserPromptError(t *testing.T) {
},
},
}
_, err = cred.GetToken(expectedCtx, policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
_, err = cred.GetToken(expectedCtx, testTRO)
if err == nil {
t.Fatal("expected an error")
}
Expand Down
7 changes: 3 additions & 4 deletions sdk/azidentity/environment_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
)
Expand Down Expand Up @@ -223,7 +222,7 @@ func TestEnvironmentCredential_SendCertificateChain(t *testing.T) {
if err != nil {
t.Fatal(err)
}
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -297,7 +296,7 @@ func TestEnvironmentCredential_InvalidClientSecretLive(t *testing.T) {
if err != nil {
t.Fatalf("failed to construct credential: %v", err)
}
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if !reflect.ValueOf(tk).IsZero() {
t.Fatal("expected a zero value AccessToken")
}
Expand Down Expand Up @@ -381,7 +380,7 @@ func TestEnvironmentCredential_InvalidPasswordLive(t *testing.T) {
if err != nil {
t.Fatalf("failed to construct credential: %v", err)
}
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if !reflect.ValueOf(tk).IsZero() {
t.Fatal("expected a zero value AccessToken")
}
Expand Down
3 changes: 1 addition & 2 deletions sdk/azidentity/interactive_browser_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestInteractiveBrowserCredential_GetTokenSuccess(t *testing.T) {
ExpiresOn: time.Now().Add(1 * time.Hour),
},
}
tk, err := cred.GetToken(context.Background(), policy.TokenRequestOptions{Scopes: []string{liveTestScope}})
tk, err := cred.GetToken(context.Background(), testTRO)
if err != nil {
t.Fatalf("Expected an empty error but received: %v", err)
}
Expand Down Expand Up @@ -127,7 +127,6 @@ func TestInteractiveBrowserCredentialADFS_Live(t *testing.T) {
if adfsLiveUser.clientID == fakeClientID {
t.Skip("set ADFS_IDENTITY_TEST_CLIENT_ID environment variables to run this test live")
}
//Redirect URL is necessary
url := adfsLiveSP.redirectURL

cloudConfig := cloud.Configuration{ActiveDirectoryAuthorityHost: adfsAuthority}
Expand Down
Loading