public void GetToken_WhenTokenIsNotInCacheAndTokenProxyReturnsNull_ThrowsException() { //Arrange ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <string>(Arg.Is("client-id")) .Returns((string)null); AzureBearerTokenOptions options = CreateOptions(); IAzureBearerTokenProxy proxy = CreateTokenProxy(); proxy .FetchToken(Arg.Any <AzureBearerTokenOptions>()) .Returns((AzureBearerToken)null); AzureBearerTokenProvider tokenProvider = CreateTokenProvider(proxy, cacheProvider, options); //Act Func <Task> test = async() => await tokenProvider.GetToken(); //Assert test .Should() .ThrowExactly <Exception>() .Which .Message .Should() .Be("Failed to refersh access token for url: http://test-token-url/"); }
public async Task <string> GetToken() { string accessToken = await _cacheProvider.GetAsync <string>(_azureBearerTokenOptions.ClientId); if (!string.IsNullOrWhiteSpace(accessToken)) { return(accessToken); } AzureBearerToken token = await _azureBearerTokenProxy.FetchToken(_azureBearerTokenOptions); if (token == null) { throw new Exception($"Failed to refersh access token for url: {_azureBearerTokenOptions.Url}"); } double cacheExpiryLength = (0.9 * token.ExpiryLength); await _cacheProvider.SetAsync <string>(_azureBearerTokenOptions.ClientId, token.AccessToken, TimeSpan.FromSeconds(cacheExpiryLength), false, null); return(token.AccessToken); }
public async Task GetToken_WhenTokenIsNotInCacheButReturnsFromProxy_SetsInCacheReturnsToken() { //Arrange AzureBearerToken azureBearerToken = new AzureBearerToken { AccessToken = "this-is-a-token", ExpiryLength = 3600 }; ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <string>(Arg.Is("client-id")) .Returns((string)null); AzureBearerTokenOptions options = CreateOptions(); IAzureBearerTokenProxy proxy = CreateTokenProxy(); proxy .FetchToken(Arg.Any <AzureBearerTokenOptions>()) .Returns(azureBearerToken); AzureBearerTokenProvider tokenProvider = CreateTokenProvider(proxy, cacheProvider, options); //Act string token = await tokenProvider.GetToken(); //Assert token .Should() .BeEquivalentTo(azureBearerToken.AccessToken); await cacheProvider .Received(1) .SetAsync <string>(Arg.Is("client-id"), Arg.Is("this-is-a-token"), Arg.Is(TimeSpan.FromSeconds(3240)), Arg.Is(false), null); }