Skip to content

Commit

Permalink
Make the AzureServiceTokenProvider mockable (Azure#8464)
Browse files Browse the repository at this point in the history
  • Loading branch information
christothes authored and pakrym committed Dec 5, 2019
1 parent 24a8f2e commit a8607ef
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Reflection;
using Xunit;

namespace Microsoft.Azure.Services.AppAuthentication.TestCommon
{
public class MockUtil
{
public static void AssertPublicMethodsAreVirtual<T>()
{
foreach (MethodInfo methodInfo in typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
{
Assert.True(methodInfo.IsVirtual, $"Method {methodInfo.Name} is not virtual");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,5 +304,14 @@ public void DiscoveryTestBothFail()
Environment.SetEnvironmentVariable(Constants.MsiAppServiceEndpointEnv,null);
Environment.SetEnvironmentVariable(Constants.MsiAppServiceSecretEnv, null);
}

/// <summary>
/// Ensure that all public methods are marked virtual to maintain mockability
/// </summary>
[Fact]
public void RemainMockable()
{
MockUtil.AssertPublicMethodsAreVirtual<AzureServiceTokenProvider>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class AzureServiceTokenProvider
/// KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
/// </code>
/// </example>
public TokenCallback KeyVaultTokenCallback => async (authority, resource, scope) =>
public virtual TokenCallback KeyVaultTokenCallback => async (authority, resource, scope) =>
{
var authResult = await GetAuthResultAsyncImpl(resource, authority).ConfigureAwait(false);
return authResult.AccessToken;
Expand All @@ -53,7 +53,7 @@ public class AzureServiceTokenProvider
/// <summary>
/// The principal used to acquire token. This will be of type "User" for local development scenarios, and "App" when client credentials flow is used.
/// </summary>
public Principal PrincipalUsed => _principalUsed;
public virtual Principal PrincipalUsed => _principalUsed;

/// <summary>
/// Creates an instance of the AzureServiceTokenProvider class.
Expand Down Expand Up @@ -240,15 +240,15 @@ private List<NonInteractiveAzureServiceTokenProviderBase> GetTokenProviders()
/// <returns>Access token</returns>
/// <exception cref="ArgumentNullException">Thrown if resource is null or empty.</exception>
/// <exception cref="AzureServiceTokenProviderException">Thrown if access token cannot be acquired.</exception>
public async Task<string> GetAccessTokenAsync(string resource, string tenantId = default(string),
public virtual async Task<string> GetAccessTokenAsync(string resource, string tenantId = default(string),
CancellationToken cancellationToken = default(CancellationToken))
{
var authResult = await GetAuthenticationResultAsync(resource, tenantId, cancellationToken).ConfigureAwait(false);

return authResult.AccessToken;
}

public Task<string> GetAccessTokenAsync(string resource, string tenantId)
public virtual Task<string> GetAccessTokenAsync(string resource, string tenantId)
{
return GetAccessTokenAsync(resource, tenantId, default(CancellationToken));
}
Expand All @@ -267,7 +267,7 @@ public Task<string> GetAccessTokenAsync(string resource, string tenantId)
/// <returns>Access token</returns>
/// <exception cref="ArgumentNullException">Thrown if resource is null or empty.</exception>
/// <exception cref="AzureServiceTokenProviderException">Thrown if access token cannot be acquired.</exception>
public Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId = default(string),
public virtual Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId = default(string),
CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrWhiteSpace(resource))
Expand All @@ -280,7 +280,7 @@ public Task<string> GetAccessTokenAsync(string resource, string tenantId)
return GetAuthResultAsyncImpl(resource, authority, cancellationToken);
}

public Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId)
public virtual Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId)
{
return GetAuthenticationResultAsync(resource, tenantId, default(CancellationToken));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<PackageId>Microsoft.Azure.Services.AppAuthentication</PackageId>
<Description>Enables a service to authenticate to Azure services using the developer's Azure Active Directory/ Microsoft account during development, and authenticate as itself (using OAuth 2.0 Client Credentials flow) when deployed to Azure.</Description>
<Version>1.3.1</Version>
<Version>1.4.0</Version>
<AssemblyName>Microsoft.Azure.Services.AppAuthentication</AssemblyName>
<PackageTags>Azure Authentication AppAuthentication</PackageTags>
<PackageReleaseNotes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[assembly: AssemblyTitle("Microsoft.Azure.Services.AppAuthentication")]
[assembly: AssemblyDescription("Enables a service to authenticate to Azure services using the developer's Azure Active Directory/ Microsoft account during development, and authenticate as itself (using OAuth 2.0 Client Credentials flow) when deployed to Azure.")]

[assembly: AssemblyVersion("1.3.1.0")]
[assembly: AssemblyFileVersion("1.3.1.0")]
[assembly: AssemblyVersion("1.4.0.0")]
[assembly: AssemblyFileVersion("1.4.0.0")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyProduct("Microsoft Azure")]
[assembly: AssemblyCopyright("Copyright (c) Microsoft Corporation. All rights reserved.")]
Expand Down

0 comments on commit a8607ef

Please sign in to comment.