Skip to content
This repository has been archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Add a regression test for bug #1463
Browse files Browse the repository at this point in the history
  • Loading branch information
bgavrilMS committed Dec 18, 2018
1 parent afd19f1 commit 9157c5e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 7 deletions.
34 changes: 27 additions & 7 deletions tests/Test.ADAL.NET.Unit.net45/TokenCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Threading.Tasks;
using Microsoft.Identity.Core;
using Microsoft.Identity.Core.Cache;
using Microsoft.Identity.Test.Common.Core.Mocks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Cache;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Helpers;
Expand Down Expand Up @@ -335,13 +336,26 @@ internal static void TokenCacheCrossTenantOperationsTest()
AdalResultWrapper value = CreateCacheValue(null, "user1");
}

internal static async Task MRRT_TestAsync()
internal static void MRRTWithBroker_Test()
{
const string Authority = "authority";
const string Resource1 = "R1";
const string Resource2 = "R2";

var testLogger = new TestLogger(); //todo nsubstitute this
var requestContext = new RequestContext(ValidClientId, testLogger);

var tokenCache = new TokenCache();
var cacheDictionary = tokenCache.tokenCacheDictionary;

AdalTokenCacheKey keyR1 = new AdalTokenCacheKey("authority", "R1", "client", TokenSubjectType.User, null, "user");
AdalTokenCacheKey keyR2 = new AdalTokenCacheKey("authority", "R2", "client", TokenSubjectType.User, null, "user");


//AdalTokenCacheKey keyR1 = new AdalTokenCacheKey(Authority, Resource1, ValidClientId, TokenSubjectType.User, null, "user");
//AdalTokenCacheKey keyR2 = new AdalTokenCacheKey(Authority, Resource2, ValidClientId, TokenSubjectType.User, null, "user");

var cacheValue = CreateCacheValue("uniqueId", "displayId", false);

tokenCache.StoreToCacheCommon(cacheValue, Authority, Resource1, ValidClientId, TokenSubjectType.User, requestContext);

}

Expand Down Expand Up @@ -617,20 +631,26 @@ internal static void TokenCacheSerializationTest()
}
}

public static AdalResultWrapper CreateCacheValue(string uniqueId, string displayableId)
public static AdalResultWrapper CreateCacheValue(string uniqueId, string displayableId, bool withRt = true)
{
string refreshToken = string.Format(CultureInfo.CurrentCulture, " RefreshToken{0}", Rand.Next());
var result = new AdalResult(null, ValidAccessToken,
new DateTimeOffset(DateTime.UtcNow + TimeSpan.FromSeconds(ValidExpiresIn)))
{
UserInfo = new AdalUserInfo { UniqueId = uniqueId, DisplayableId = displayableId }
};

return new AdalResultWrapper
var resultWrapper = new AdalResultWrapper
{
Result = result,
RefreshToken = refreshToken
};

if (withRt)
{
resultWrapper.RefreshToken =
string.Format(CultureInfo.CurrentCulture, " RefreshToken{0}", Rand.Next());
}

return resultWrapper;
}

public static void CheckPublicGetSets()
Expand Down
58 changes: 58 additions & 0 deletions tests/Test.ADAL.NET.Unit.net45/TokenCacheUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
using Test.ADAL.NET.Common.Mocks;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Http;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal;
using Microsoft.Identity.Test.Common.Core.Mocks;
using Microsoft.Identity.Core;
using Microsoft.Identity.Core.Cache;
using Microsoft.IdentityModel.Clients.ActiveDirectory.Internal.Cache;

namespace Test.ADAL.NET.Unit
{
Expand All @@ -58,6 +62,60 @@ public void DefaultTokenCacheTest()
TokenCacheTests.DefaultTokenCacheTest();
}


[TestMethod]
[TestCategory("Regression")] // regression for https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/issues/1463
public void MrrtTest()
{
// Arrange
const string ClientId = "ClientId";
string Authority = AdalTestConstants.DefaultAuthorityCommonTenant;
const string Resource1 = "R1";
const string Resource2 = "R2";
const string UniqueId = "uniqueId";
const string DisplayableId = "displayId";

var requestContext = new RequestContext(ClientId, new TestLogger());
var tokenCache = new TokenCache();
var cacheDictionary = tokenCache.tokenCacheDictionary;

AdalResultWrapper entryForResource1 = TokenCacheTests.CreateCacheValue(UniqueId, DisplayableId, false);
CacheQueryData cacheQueryForResource2 = new CacheQueryData()
{
Authority = Authority,
Resource = Resource2,
ClientId = ClientId,
SubjectType = TokenSubjectType.User,
UniqueId = UniqueId,
DisplayableId = DisplayableId
};

CacheQueryData cacheQueryForResource1 = new CacheQueryData()
{
Authority = Authority,
Resource = Resource1,
ClientId = ClientId,
SubjectType = TokenSubjectType.User,
UniqueId = UniqueId,
DisplayableId = DisplayableId
};

// Act

// 1. Store the AT and IdT (but not the RT as it's on the broker)
tokenCache.StoreToCacheCommon(entryForResource1, Authority, Resource1, ClientId, TokenSubjectType.User, requestContext);

// 2. Rquest an AT for Resource2 from the cache -> should fail, because we don't have the MRRT (the broker has it)
AdalResultWrapper resultForResource2Query = tokenCache.LoadFromCacheCommon(cacheQueryForResource2, requestContext);

// 3. Request an AT for Resource1 from the cache -> should succed (it used to fail because step 2 would delete the token)
AdalResultWrapper resultForResource1Query = tokenCache.LoadFromCacheCommon(cacheQueryForResource1, requestContext);

// Assert
Assert.IsNull(resultForResource2Query, "No result should be returned from the cache for Resource2");
Assert.IsNotNull(resultForResource1Query, "An AT is present in the cache for Resource1");
}

#if !NET_CORE // netcore doesn't support interactive
[TestMethod]
[TestCategory("AdalDotNetUnit")]
Expand Down

0 comments on commit 9157c5e

Please sign in to comment.