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); }
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)); }
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; }
private Mock<IMemoryCache> CreateEmptyMockedCache(TestCacheEntry cacheEntry = null) { return CreateMockedCache("", null, cacheEntry); }