Skip to content

Commit

Permalink
fix analyzer warnings (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyf19 authored Aug 23, 2021
1 parent 3bf8f43 commit ca9f790
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Net;
using System.Net.Http;
using Microsoft.Identity.Web.Test.Common;
using Microsoft.Identity.Web.Util;

namespace Microsoft.Identity.Web.Test.Common.Mocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ namespace Microsoft.Identity.Web.Test.Common.Mocks
public class MockHttpMessageHandler : HttpMessageHandler
{
public HttpResponseMessage ResponseMessage { get; set; }

public string ExpectedUrl { get; set; }

public HttpMethod ExpectedMethod { get; set; }

public Exception ExceptionToThrow { get; set; }

/// <summary>
/// Once the http message is executed, this property holds the request message
/// Once the http message is executed, this property holds the request message.
/// </summary>
public HttpRequestMessage ActualRequestMessage { get; private set; }

Expand Down
124 changes: 61 additions & 63 deletions tests/Microsoft.Identity.Web.Test/CacheExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,80 +27,24 @@ public void InMemoryCacheExtensionsTests()
}

[Fact]
// bug: https://github.com/AzureAD/microsoft-identity-web/issues/1390
public async Task InMemoryCacheExtensionsAgainTestsAsync()
public async Task CacheExtensions_CcaAlreadyExists_TestsAsync()
{
AuthenticationResult result;
result = await CreateAppAndGetTokenAsync(CacheType.InMemory).ConfigureAwait(false);
// new InMemory serializer and new cca
result = await CreateAppAndGetTokenAsync(CacheType.InMemory, addInstanceMock: true).ConfigureAwait(false);
Assert.Equal(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource);

result = await CreateAppAndGetTokenAsync(CacheType.InMemory, false, false).ConfigureAwait(false);
result = await CreateAppAndGetTokenAsync(CacheType.InMemory, addTokenMock: false).ConfigureAwait(false);
Assert.Equal(TokenSource.Cache, result.AuthenticationResultMetadata.TokenSource);

result = await CreateAppAndGetTokenAsync(CacheType.DistributedInMemory, true, false).ConfigureAwait(false);
// new DistributedInMemory and same cca
result = await CreateAppAndGetTokenAsync(CacheType.DistributedInMemory).ConfigureAwait(false);
Assert.Equal(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource);

result = await CreateAppAndGetTokenAsync(CacheType.DistributedInMemory, false, false).ConfigureAwait(false);
result = await CreateAppAndGetTokenAsync(CacheType.DistributedInMemory, addTokenMock: false).ConfigureAwait(false);
Assert.Equal(TokenSource.Cache, result.AuthenticationResultMetadata.TokenSource);
}

private enum CacheType
{
InMemory,
DistributedInMemory,
}

private static async Task<AuthenticationResult> CreateAppAndGetTokenAsync(
CacheType cacheType,
bool addTokenMock = true,
bool addInstanceMock = true)
{
using (MockHttpClientFactory mockHttp = new MockHttpClientFactory())
using (var discoveryHandler = MockHttpCreator.CreateInstanceDiscoveryMockHandler())
using (var tokenHandler = MockHttpCreator.CreateClientCredentialTokenHandler())
{

if (addInstanceMock)
{
mockHttp.AddMockHandler(discoveryHandler);
}

// for when the token is requested from ESTS
if (addTokenMock)
{
mockHttp.AddMockHandler(tokenHandler);
}

var confidentialApp = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithAuthority(TestConstants.AuthorityCommonTenant)
.WithHttpClientFactory(mockHttp)
.WithClientSecret(TestConstants.ClientSecret)
.Build();

switch (cacheType)
{
case CacheType.InMemory:
confidentialApp.AddInMemoryTokenCache();
break;

case CacheType.DistributedInMemory:
confidentialApp.AddDistributedTokenCache(services =>
{
services.AddDistributedMemoryCache();
services.AddLogging(configure => configure.AddConsole())
.Configure<LoggerFilterOptions>(options => options.MinLevel = Microsoft.Extensions.Logging.LogLevel.Warning);
});
break;
}

var result = await confidentialApp.AcquireTokenForClient(new[] { TestConstants.s_scopeForApp })
.ExecuteAsync().ConfigureAwait(false);

return result;
}
}

[Fact]
public void InMemoryCacheExtensions_NoCca_ThrowsException_Tests()
{
Expand Down Expand Up @@ -143,6 +87,60 @@ public void DistributedCacheExtensions_NoService_ThrowsException_Tests()
Assert.Equal("initializeDistributedCache", ex.ParamName);
}

private enum CacheType
{
InMemory,
DistributedInMemory,
}

private static async Task<AuthenticationResult> CreateAppAndGetTokenAsync(
CacheType cacheType,
bool addTokenMock = true,
bool addInstanceMock = false)
{
using MockHttpClientFactory mockHttp = new MockHttpClientFactory();
using var discoveryHandler = MockHttpCreator.CreateInstanceDiscoveryMockHandler();
using var tokenHandler = MockHttpCreator.CreateClientCredentialTokenHandler();
if (addInstanceMock)
{
mockHttp.AddMockHandler(discoveryHandler);
}

// for when the token is requested from ESTS
if (addTokenMock)
{
mockHttp.AddMockHandler(tokenHandler);
}

var confidentialApp = ConfidentialClientApplicationBuilder
.Create(TestConstants.ClientId)
.WithAuthority(TestConstants.AuthorityCommonTenant)
.WithHttpClientFactory(mockHttp)
.WithClientSecret(TestConstants.ClientSecret)
.Build();

switch (cacheType)
{
case CacheType.InMemory:
confidentialApp.AddInMemoryTokenCache();
break;

case CacheType.DistributedInMemory:
confidentialApp.AddDistributedTokenCache(services =>
{
services.AddDistributedMemoryCache();
services.AddLogging(configure => configure.AddConsole())
.Configure<LoggerFilterOptions>(options => options.MinLevel = Microsoft.Extensions.Logging.LogLevel.Warning);
});
break;
}

var result = await confidentialApp.AcquireTokenForClient(new[] { TestConstants.s_scopeForApp })
.ExecuteAsync().ConfigureAwait(false);

return result;
}

private void CreateCca()
{
if (_confidentialApp == null)
Expand Down

0 comments on commit ca9f790

Please sign in to comment.