Skip to content

Commit a8607ef

Browse files
christothespakrym
authored andcommitted
Make the AzureServiceTokenProvider mockable (#8464)
1 parent 24a8f2e commit a8607ef

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
using Xunit;
8+
9+
namespace Microsoft.Azure.Services.AppAuthentication.TestCommon
10+
{
11+
public class MockUtil
12+
{
13+
public static void AssertPublicMethodsAreVirtual<T>()
14+
{
15+
foreach (MethodInfo methodInfo in typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
16+
{
17+
Assert.True(methodInfo.IsVirtual, $"Method {methodInfo.Name} is not virtual");
18+
}
19+
}
20+
}
21+
}

sdk/mgmtcommon/AppAuthentication/Azure.Services.AppAuthentication.Unit.Tests/AzureServiceTokenProviderTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,5 +304,14 @@ public void DiscoveryTestBothFail()
304304
Environment.SetEnvironmentVariable(Constants.MsiAppServiceEndpointEnv,null);
305305
Environment.SetEnvironmentVariable(Constants.MsiAppServiceSecretEnv, null);
306306
}
307+
308+
/// <summary>
309+
/// Ensure that all public methods are marked virtual to maintain mockability
310+
/// </summary>
311+
[Fact]
312+
public void RemainMockable()
313+
{
314+
MockUtil.AssertPublicMethodsAreVirtual<AzureServiceTokenProvider>();
315+
}
307316
}
308317
}

sdk/mgmtcommon/AppAuthentication/Azure.Services.AppAuthentication/AzureServiceTokenProvider.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class AzureServiceTokenProvider
4444
/// KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
4545
/// </code>
4646
/// </example>
47-
public TokenCallback KeyVaultTokenCallback => async (authority, resource, scope) =>
47+
public virtual TokenCallback KeyVaultTokenCallback => async (authority, resource, scope) =>
4848
{
4949
var authResult = await GetAuthResultAsyncImpl(resource, authority).ConfigureAwait(false);
5050
return authResult.AccessToken;
@@ -53,7 +53,7 @@ public class AzureServiceTokenProvider
5353
/// <summary>
5454
/// The principal used to acquire token. This will be of type "User" for local development scenarios, and "App" when client credentials flow is used.
5555
/// </summary>
56-
public Principal PrincipalUsed => _principalUsed;
56+
public virtual Principal PrincipalUsed => _principalUsed;
5757

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

248248
return authResult.AccessToken;
249249
}
250250

251-
public Task<string> GetAccessTokenAsync(string resource, string tenantId)
251+
public virtual Task<string> GetAccessTokenAsync(string resource, string tenantId)
252252
{
253253
return GetAccessTokenAsync(resource, tenantId, default(CancellationToken));
254254
}
@@ -267,7 +267,7 @@ public Task<string> GetAccessTokenAsync(string resource, string tenantId)
267267
/// <returns>Access token</returns>
268268
/// <exception cref="ArgumentNullException">Thrown if resource is null or empty.</exception>
269269
/// <exception cref="AzureServiceTokenProviderException">Thrown if access token cannot be acquired.</exception>
270-
public Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId = default(string),
270+
public virtual Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId = default(string),
271271
CancellationToken cancellationToken = default(CancellationToken))
272272
{
273273
if (string.IsNullOrWhiteSpace(resource))
@@ -280,7 +280,7 @@ public Task<string> GetAccessTokenAsync(string resource, string tenantId)
280280
return GetAuthResultAsyncImpl(resource, authority, cancellationToken);
281281
}
282282

283-
public Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId)
283+
public virtual Task<AppAuthenticationResult> GetAuthenticationResultAsync(string resource, string tenantId)
284284
{
285285
return GetAuthenticationResultAsync(resource, tenantId, default(CancellationToken));
286286
}

sdk/mgmtcommon/AppAuthentication/Azure.Services.AppAuthentication/Microsoft.Azure.Services.AppAuthentication.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<PackageId>Microsoft.Azure.Services.AppAuthentication</PackageId>
44
<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>
5-
<Version>1.3.1</Version>
5+
<Version>1.4.0</Version>
66
<AssemblyName>Microsoft.Azure.Services.AppAuthentication</AssemblyName>
77
<PackageTags>Azure Authentication AppAuthentication</PackageTags>
88
<PackageReleaseNotes>

sdk/mgmtcommon/AppAuthentication/Azure.Services.AppAuthentication/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
[assembly: AssemblyTitle("Microsoft.Azure.Services.AppAuthentication")]
55
[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.")]
66

7-
[assembly: AssemblyVersion("1.3.1.0")]
8-
[assembly: AssemblyFileVersion("1.3.1.0")]
7+
[assembly: AssemblyVersion("1.4.0.0")]
8+
[assembly: AssemblyFileVersion("1.4.0.0")]
99
[assembly: AssemblyCompany("Microsoft Corporation")]
1010
[assembly: AssemblyProduct("Microsoft Azure")]
1111
[assembly: AssemblyCopyright("Copyright (c) Microsoft Corporation. All rights reserved.")]

0 commit comments

Comments
 (0)