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

Refactor TestEnvVar #18364

Merged
2 commits merged into from
Feb 3, 2021
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
52 changes: 28 additions & 24 deletions sdk/identity/Azure.Identity/tests/DefaultAzureCredentialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void ValidateCtorNoOptions()
}

[Test]
public void ValidateCtorIncludedInteractiveParam([Values(true, false)]bool includeInteractive)
public void ValidateCtorIncludedInteractiveParam([Values(true, false)] bool includeInteractive)
{
var cred = new DefaultAzureCredential(includeInteractive);

Expand Down Expand Up @@ -110,7 +110,7 @@ public void ValidateCtorOptionsPassedToCredentials()

[Test]
[NonParallelizable]
public void ValidateEnvironmentBasedOptionsPassedToCredentials([Values]bool clientIdSpecified, [Values]bool usernameSpecified, [Values]bool tenantIdSpecified)
public void ValidateEnvironmentBasedOptionsPassedToCredentials([Values] bool clientIdSpecified, [Values] bool usernameSpecified, [Values] bool tenantIdSpecified)
{
var expClientId = clientIdSpecified ? Guid.NewGuid().ToString() : null;
var expUsername = usernameSpecified ? Guid.NewGuid().ToString() : null;
Expand All @@ -121,9 +121,11 @@ public void ValidateEnvironmentBasedOptionsPassedToCredentials([Values]bool clie
bool onCreateVsCalled = false;
bool onCreateVsCodeCalled = false;

using (new TestEnvVar("AZURE_CLIENT_ID", expClientId))
using (new TestEnvVar("AZURE_USERNAME", expUsername))
using (new TestEnvVar("AZURE_TENANT_ID", expTenantId))
using (new TestEnvVar(new ()
{
{ "AZURE_CLIENT_ID", expClientId },
{ "AZURE_USERNAME", expUsername },
{ "AZURE_TENANT_ID", expTenantId } }))
{
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));

Expand Down Expand Up @@ -191,9 +193,11 @@ public void ValidateEmptyEnvironmentBasedOptionsNotPassedToCredentials([Values]
bool onCreateVsCalled = false;
bool onCreateVsCodeCalled = false;

using (new TestEnvVar("AZURE_CLIENT_ID", expClientId))
using (new TestEnvVar("AZURE_USERNAME", expUsername))
using (new TestEnvVar("AZURE_TENANT_ID", expTenantId))
using (new TestEnvVar(new ()
{
{ "AZURE_CLIENT_ID", expClientId },
{ "AZURE_USERNAME", expUsername },
{ "AZURE_TENANT_ID", expTenantId } }))
{
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));

Expand Down Expand Up @@ -249,13 +253,13 @@ public void ValidateEmptyEnvironmentBasedOptionsNotPassedToCredentials([Values]
}

[Test]
public void ValidateCtorWithExcludeOptions([Values(true, false)]bool excludeEnvironmentCredential,
[Values(true, false)]bool excludeManagedIdentityCredential,
[Values(true, false)]bool excludeSharedTokenCacheCredential,
[Values(true, false)]bool excludeVisualStudioCredential,
[Values(true, false)]bool excludeVisualStudioCodeCredential,
[Values(true, false)]bool excludeCliCredential,
[Values(true, false)]bool excludeInteractiveBrowserCredential)
public void ValidateCtorWithExcludeOptions([Values(true, false)] bool excludeEnvironmentCredential,
[Values(true, false)] bool excludeManagedIdentityCredential,
[Values(true, false)] bool excludeSharedTokenCacheCredential,
[Values(true, false)] bool excludeVisualStudioCredential,
[Values(true, false)] bool excludeVisualStudioCodeCredential,
[Values(true, false)] bool excludeCliCredential,
[Values(true, false)] bool excludeInteractiveBrowserCredential)
{
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));

Expand Down Expand Up @@ -311,13 +315,13 @@ public void ValidateCtorWithExcludeOptions([Values(true, false)]bool excludeEnvi
}

[Test]
public void ValidateAllUnavailable([Values(true, false)]bool excludeEnvironmentCredential,
[Values(true, false)]bool excludeManagedIdentityCredential,
[Values(true, false)]bool excludeSharedTokenCacheCredential,
[Values(true, false)]bool excludeVisualStudioCredential,
[Values(true, false)]bool excludeVisualStudioCodeCredential,
[Values(true, false)]bool excludeCliCredential,
[Values(true, false)]bool excludeInteractiveBrowserCredential)
public void ValidateAllUnavailable([Values(true, false)] bool excludeEnvironmentCredential,
[Values(true, false)] bool excludeManagedIdentityCredential,
[Values(true, false)] bool excludeSharedTokenCacheCredential,
[Values(true, false)] bool excludeVisualStudioCredential,
[Values(true, false)] bool excludeVisualStudioCodeCredential,
[Values(true, false)] bool excludeCliCredential,
[Values(true, false)] bool excludeInteractiveBrowserCredential)
{
if (excludeEnvironmentCredential && excludeManagedIdentityCredential && excludeSharedTokenCacheCredential && excludeVisualStudioCredential && excludeVisualStudioCodeCredential && excludeCliCredential && excludeInteractiveBrowserCredential)
{
Expand Down Expand Up @@ -401,7 +405,7 @@ public void ValidateAllUnavailable([Values(true, false)]bool excludeEnvironmentC
}

[Test]
public void ValidateUnhandledException([Values(0, 1, 2, 3, 4, 5, 6)]int exPossition)
public void ValidateUnhandledException([Values(0, 1, 2, 3, 4, 5, 6)] int exPossition)
{
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));

Expand Down Expand Up @@ -541,7 +545,7 @@ public void ValidateUnhandledException([Values(0, 1, 2, 3, 4, 5, 6)]int exPossit
}

[Test]
public async Task ValidateSelectedCredentialCaching([Values(typeof(EnvironmentCredential), typeof(ManagedIdentityCredential), typeof(SharedTokenCacheCredential), typeof(VisualStudioCredential), typeof(VisualStudioCodeCredential), typeof(AzureCliCredential), typeof(InteractiveBrowserCredential))]Type availableCredential)
public async Task ValidateSelectedCredentialCaching([Values(typeof(EnvironmentCredential), typeof(ManagedIdentityCredential), typeof(SharedTokenCacheCredential), typeof(VisualStudioCredential), typeof(VisualStudioCodeCredential), typeof(AzureCliCredential), typeof(InteractiveBrowserCredential))] Type availableCredential)
{
var expToken = new AccessToken(Guid.NewGuid().ToString(), DateTimeOffset.MaxValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public async Task AuthenticateWithDeviceCodeMockVerifyCallbackCancellationAsync(

var options = new TokenCredentialOptions() { Transport = mockTransport };

var cancelSource = new CancellationTokenSource(1000);
var cancelSource = new CancellationTokenSource(100);

var cred = InstrumentClient(new DeviceCodeCredential(VerifyDeviceCodeCallbackCancellationToken, ClientId, options: options));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Azure.Core.TestFramework;
using Azure.Identity.Tests.Mock;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace Azure.Identity.Tests
{
Expand Down Expand Up @@ -57,16 +58,13 @@ public void CredentialConstructionClientSecret()
[Test]
public void CredentialConstructionClientCertificate()
{
string clientIdBackup = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
string tenantIdBackup = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
string clientCertificateLocationBackup = Environment.GetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH");

try
using (new TestEnvVar(new ()
{
{ "AZURE_CLIENT_ID", "mockclientid" },
{ "AZURE_TENANT_ID", "mocktenantid" },
{ "AZURE_CLIENT_CERTIFICATE_PATH", "mockcertificatepath" }
}))
{
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", "mockclientid");
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", "mocktenantid");
Environment.SetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH", "mockcertificatepath");

var provider = new EnvironmentCredential();
var cred = provider.Credential as ClientCertificateCredential;
Assert.NotNull(cred);
Expand All @@ -78,12 +76,6 @@ public void CredentialConstructionClientCertificate()
Assert.NotNull(certProvider);
Assert.AreEqual("mockcertificatepath", certProvider.CertificatePath);
}
finally
{
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", clientIdBackup);
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", tenantIdBackup);
Environment.SetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH", clientCertificateLocationBackup);
}
}

[Test]
Expand Down Expand Up @@ -113,52 +105,26 @@ public async Task EnvironmentCredentialAuthenticationFailedException()
await Task.CompletedTask;
}

[NonParallelizable]
[Test]
public void AssertCredentialUnavailableWhenEmptyString()
public static IEnumerable<object[]> AssertCredentialUnavailableWhenEmptyStringEnvironmentSettings()
{
// ensure no env vars are set before starting the test
using (new TestEnvVar("AZURE_CLIENT_ID", null))
using (new TestEnvVar("AZURE_TENANT_ID", null))
using (new TestEnvVar("AZURE_CLIENT_SECRET", null))
using (new TestEnvVar("AZURE_CLIENT_CERTIFICATE_PATH", null))
using (new TestEnvVar("AZURE_USERNAME", null))
using (new TestEnvVar("AZURE_PASSWORD", null))
{
using (new TestEnvVar("AZURE_CLIENT_ID", "mockclientid"))
using (new TestEnvVar("AZURE_CLIENT_SECRET", "mockclientsecret"))
using (new TestEnvVar("AZURE_TENANT_ID", "mocktenantid"))
{
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_ID");
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_SECRET");
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_TENANT_ID");
}

using (new TestEnvVar("AZURE_CLIENT_ID", "mockclientid"))
using (new TestEnvVar("AZURE_CLIENT_CERTIFICATE_PATH", "mockcertpath"))
using (new TestEnvVar("AZURE_TENANT_ID", "mocktenantid"))
{
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_ID");
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_CERTIFICATE_PATH");
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_TENANT_ID");
}

using (new TestEnvVar("AZURE_USERNAME", "mockusername"))
using (new TestEnvVar("AZURE_PASSWORD", "mockpassword"))
using (new TestEnvVar("AZURE_TENANT_ID", "mocktenantid"))
using (new TestEnvVar("AZURE_CLIENT_ID", "mockclientid"))
{
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_ID");
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_TENANT_ID");
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_USERNAME");
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_PASSWORD");
}
}
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", string.Empty }, { "AZURE_CLIENT_SECRET", "mockclientsecret" }, { "AZURE_TENANT_ID", "mocktenantid" } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_SECRET", string.Empty }, { "AZURE_TENANT_ID", "mocktenantid" } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_SECRET", "mockclientsecret" }, { "AZURE_TENANT_ID", string.Empty } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", string.Empty }, { "AZURE_CLIENT_CERTIFICATE_PATH", "mockcertpath" }, { "AZURE_TENANT_ID", "mocktenantid" } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_CERTIFICATE_PATH", string.Empty }, { "AZURE_TENANT_ID", "mocktenantid" } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_CERTIFICATE_PATH", "mockcertpath" }, { "AZURE_TENANT_ID", string.Empty } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", string.Empty }, { "AZURE_PASSWORD", "mockpassword" }, { "AZURE_TENANT_ID", "mocktenantid" }, { "AZURE_CLIENT_ID", "mockclientid" } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", "mockusername" }, { "AZURE_PASSWORD", string.Empty }, { "AZURE_TENANT_ID", "mocktenantid" }, { "AZURE_CLIENT_ID", "mockclientid" } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", "mockusername" }, { "AZURE_PASSWORD", "mockpassword" }, { "AZURE_TENANT_ID", string.Empty }, { "AZURE_CLIENT_ID", "mockclientid" } } };
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", "mockusername" }, { "AZURE_PASSWORD", "mockpassword" }, { "AZURE_TENANT_ID", "mocktenantid" }, { "AZURE_CLIENT_ID", string.Empty } } };
}

private void SetEnvVarToEmptyStringAndAssertCredentialUnavailable(string envVar)
[NonParallelizable]
[Test]
[TestCaseSource(nameof(AssertCredentialUnavailableWhenEmptyStringEnvironmentSettings))]
public void AssertCredentialUnavailableWhenEmptyString(Dictionary<string, string> environmentVars)
{
using (new TestEnvVar(envVar, string.Empty))
using (new TestEnvVar(environmentVars))
{
var credential = InstrumentClient(new EnvironmentCredential());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Reflection;
using Azure.Core.TestFramework;
using Azure.Security.KeyVault.Secrets;
Expand Down Expand Up @@ -38,27 +39,24 @@ public void ClearChallengeCacheforRecord()

private class ManagedIdenityEnvironment : IDisposable
{
private readonly TestEnvVar[] _envVars;
private readonly TestEnvVar _envVar;

public ManagedIdenityEnvironment(IdentityTestEnvironment env)
{
_envVars = new TestEnvVar[]
{
new TestEnvVar("IDENTITY_ENDPOINT", env.IdentityEndpoint),
new TestEnvVar("IMDS_ENDPOINT", env.ImdsEndpoint),
new TestEnvVar("MSI_ENDPOINT", env.MsiEndpoint),
new TestEnvVar("MSI_SECRET", env.MsiSecret),
new TestEnvVar("IDENTITY_HEADER", env.IdentityHeader),
new TestEnvVar("IDENTITY_SERVER_THUMBPRINT", env.IdentityServerThumbprint)
};
_envVar =
new TestEnvVar(
new Dictionary<string, string>
{
{ "IDENTITY_ENDPOINT", env.IdentityEndpoint },
{ "IMDS_ENDPOINT", env.ImdsEndpoint },
{ "MSI_ENDPOINT", env.MsiEndpoint },
{ "MSI_SECRET", env.MsiSecret },{ "IDENTITY_HEADER", env.IdentityHeader },
{ "IDENTITY_SERVER_THUMBPRINT", env.IdentityServerThumbprint } });
}

public void Dispose()
{
foreach (TestEnvVar envVar in _envVars)
{
envVar.Dispose();
}
_envVar.Dispose();
}
}
}
Expand Down
Loading