public async Task SetResponseToCache() { _options.PdpCacheDuration = 60; var cacheEntry = new TestCacheEntry(); var mockedCache = CreateEmptyMockedCache(); mockedCache.Setup(c => c.CreateEntry(BuildCacheKey(_userId))) .Returns(cacheEntry); var pdpResponse = new PdpResponse { applicationId = _application, userId = _userId, permissions = new List <String>(new string[] { requestedresource }) }; var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse); var provider = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger); var result = await provider.GetPermissionsAsync(_userId, _application); Assert.Equal(pdpResponse.applicationId, ((PdpResponse)cacheEntry.Value).applicationId); Assert.Equal(pdpResponse.userId, ((PdpResponse)cacheEntry.Value).userId); Assert.Equal(pdpResponse.permissions, ((PdpResponse)cacheEntry.Value).permissions); }
public async Task CacheDurationFromOptionsIsUsed() { _options.PdpCacheDuration = 60; var cacheEntry = new TestCacheEntry(); var mockedCache = CreateEmptyMockedCache(); mockedCache.Setup(c => c.CreateEntry(BuildCacheKey(_userId))) .Returns(cacheEntry); var pdpResponse = new PdpResponse { applicationId = _application, userId = _userId, permissions = new List <String>(new string[] { _requestedresource }) }; var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse); var client = new HttpClient(mockHandler); var uri = _options.PdpUrl.EndsWith("/") ? _options.PdpUrl : $"{_options.PdpUrl}/"; client.BaseAddress = new Uri(uri); var provider = new PolicyDecisionProvider(client, mockedCache.Object, Options.Create(_options), _logger); var result = await provider.GetPermissionsAsync(_userId, _application); Assert.True(cacheEntry.AbsoluteExpirationRelativeToNow.Value == new TimeSpan(0, _options.PdpCacheDuration, 0)); }
public async Task ShouldCacheResponseWithoutPermissions() { _options.PdpCacheDuration = 60; var cacheEntry = new TestCacheEntry(); var mockedCache = CreateEmptyMockedCache(); mockedCache.Setup(c => c.CreateEntry(BuildCacheKey(_userId))) .Returns(cacheEntry); var pdpResponse = new PdpResponse { applicationId = _application, userId = _userId, permissions = new List <String>(new string[] { }) }; var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse); var client = new HttpClient(mockHandler); var uri = _options.PdpUrl.EndsWith("/") ? _options.PdpUrl : $"{_options.PdpUrl}/"; client.BaseAddress = new Uri(uri); var provider = new PolicyDecisionProvider(client, mockedCache.Object, Options.Create(_options), _logger); var result = await provider.GetPermissionsAsync(_userId, _application); mockedCache.Verify(c => c.CreateEntry(It.IsAny <object>()), Times.Once); Assert.NotNull(cacheEntry.Value); Assert.Equal(new List <string>(), ((PdpResponse)cacheEntry.Value).permissions); }
public async Task ThrowsExceptionWhenCallFailed() { var cacheEntry = new TestCacheEntry(); var mockedCache = CreateEmptyMockedCache(cacheEntry); var tokenHelper = new TokenHelper(mockedCache.Object); tokenHelper._client = CreateClient(); _settings.OAuthPathAddition = ""; await Assert.ThrowsAsync <Exception>(async() => await tokenHelper.ReadOrRetrieveToken(_settings)); }
public async Task SetTokenInCache() { var cacheEntry = new TestCacheEntry(); var mockedCache = CreateEmptyMockedCache(cacheEntry); var tokenHelper = new TokenHelper(mockedCache.Object); tokenHelper._client = CreateClient(); var token = await tokenHelper.ReadOrRetrieveToken(_settings); Assert.Equal("accessToken", ((TokenReply)cacheEntry.Value).access_token); }
private Mock <IMemoryCache> CreateMockedCache(string key, TokenReply value, TestCacheEntry cacheEntry = null) { var mockCache = new Mock <IMemoryCache>(); var cachedObject = value as object; mockCache.Setup(c => c.TryGetValue("", out cachedObject)) .Returns(false); mockCache.Setup(c => c.TryGetValue(key, out cachedObject)) .Returns(true); mockCache.Setup(c => c.CreateEntry(_cacheKey)) .Returns(cacheEntry); return(mockCache); }
public ICacheEntry CreateEntry(object key) { if (Entry != null) { // We're being invoked in the inner "CreateEntry" call where the TCS is replaced by the GetChildContentAsync // Task. Wait for the other concurrent Task to grab the TCS before we proceed. Assert.True(ManualResetEvent1.Wait(WaitTimeout)); } var cacheKey = Assert.IsType <CacheTagKey>(key); Assert.Equal(Key1, cacheKey.Key); Entry = new TestCacheEntry { Key = key, DisposeCallback = ManualResetEvent2.Set, }; return(Entry); }
public void CacheDurationFromOptionsIsUsed() { _options.JwtSigningKeyCacheDuration = 5; var cacheEntry = new TestCacheEntry(); var mockedCache = CreateEmptyMockedCache(); mockedCache.Setup(c => c.CreateEntry("JwtSigningKey")) .Returns(cacheEntry); var cert = "{\"x5u\": \"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQ0lqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FnOEFNSUlDQ2dLQ0FnRUFqbXJnN3NGeFJkb2JTWkhJMlpqawpucnBGVC9RclhEcFl6VVU4SU1hNFRPa2dFUnRaM09CbFpkbWNieXVmcEJuNTJmWDlYRWVIOVR1QjkxOWNQeEJFCnpKN0NzUmVTK1dxcHk5UFN3K3BtQ2lDZmpIZmx1ZDJ1dzUwbmVYOGVKeFl0ekhDN1VOOCt1QThvQ0tqdzBJM1AKK0VDYTdhVy9EbU1jSS81T3NyaXhlN2ZQenY4Q0V6aFRidzdBOTZuSzIrVkkvVXFGV2Yxb0Rzd2xYOFBPaHpMRQppdWo3eEJpdWJqbDZONERablF5YW84UzJFZ2ZQT05KNG1ySW42VEQwNzEvdE9NaDFHWUF3SnBWQ3YzYWdSUVdHCjhNaWxhYXlyQzRaNTNrNmRLV1FTNklmVTd3NWJnQjEraGdJenBoK05NbzdWWTROYkpYOTZ1b0Q3QW9pQjRvNjYKclMxakNLS3lEcUwwTTkwQzFIaDcrUit5TWhJa0ZkRUdDS0ZHaDNmbDlVREdKNEZEVG1vNGR1MENxbm13bWpvVgpmUmR0eW4rNjFBRHhQNnpkN24xTEFxeVBCNEVreHVrUTc3Sy9PTkxwUnYydHJmdDlvU1VSMWpXTXE3dzEyV1lyCmhZVnhPR1NvNU40RUdCSmpIQXlRZ01DUzhQWGdtN045WGFOdWtlczBZQ3lBTDlYU0JDRTNuNFQ0Qk9XRzlEMkIKQ0QvelV2bjVDRnl3Smh1ZzlSdzRMV3QwbzJHYXlpTjN5SDBwZFhBc2pTRlRiN1ZpdnBPc1cwL3k2aUdmMEJqSwpUOHlYSkVvOG9QcDRIMkl1TDR4TDQ4bW50Qm5WalBzSXRuemlHQ2pxZ0hCN2xxYjdxeXUvNit4dEhnTGxGb2MyCjBLQnZTYURGWWJiRXRPNE5GVnJNdUlFQ0F3RUFBUT09Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ==\"}"; var mockHandler = new MockMessageHandler <string>(HttpStatusCode.OK, cert); var resolver = new JwtSigningKeyResolver(mockedCache.Object, Options.Create(_options), mockHandler, _logger); var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsIng1dSI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC94NXUifQ.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE0NzI1NDk1NDgsImV4cCI6MTUwNDA4NTU0OCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.jKg9l0cuTapEFcx9v1pLtBiigK_7EXlCqvKZBoS24XE"; SecurityToken securityToken = new JwtSecurityToken(token); var tokenValidationParameters = new TokenValidationParameters(); var keys = resolver.IssuerSigningKeyResolver(token, securityToken, "", tokenValidationParameters); var cackedkey = cacheEntry.Value as RsaSecurityKey; Assert.True(cacheEntry.AbsoluteExpirationRelativeToNow.Value == new TimeSpan(0, _options.JwtSigningKeyCacheDuration, 0)); }
public async Task ShouldNotCacheResponseWithoutPermissions() { _options.PdpCacheDuration = 60; var cacheEntry = new TestCacheEntry(); var mockedCache = CreateEmptyMockedCache(); mockedCache.Setup(c => c.CreateEntry(BuildCacheKey(_userId))) .Returns(cacheEntry); var pdpResponse = new PdpResponse { applicationId = _application, userId = _userId, permissions = new List <String>(new string[] { }) }; var mockHandler = new MockMessageHandler <PdpResponse>(HttpStatusCode.OK, pdpResponse); var provider = new PolicyDescisionProvider(mockedCache.Object, Options.Create(_options), mockHandler, _logger); var result = await provider.GetPermissionsAsync(_userId, _application); mockedCache.Verify(c => c.CreateEntry(It.IsAny <object>()), Times.Never); Assert.Null(cacheEntry.Value); }
private Mock <IMemoryCache> CreateEmptyMockedCache(TestCacheEntry cacheEntry = null) { return(CreateMockedCache("", null, cacheEntry)); }