public async Task CommunicationTokenCredential_DecodesToken(string token, long expectedExpiryUnixTimeSeconds) { using var tokenCredential = new CommunicationTokenCredential(token); AccessToken accessToken = await tokenCredential.GetTokenAsync(); Assert.AreEqual(token, accessToken.Token); Assert.AreEqual(expectedExpiryUnixTimeSeconds, accessToken.ExpiresOn.ToUnixTimeSeconds()); }
public async Task CommunicationTokenCredential_StaticToken_ReturnsExpiredToken(bool async) { using var tokenCredential = new CommunicationTokenCredential(ExpiredToken); var token = async ? await tokenCredential.GetTokenAsync() : tokenCredential.GetToken(); Assert.AreEqual(ExpiredToken, token.Token); }
public async Task CommunicationTokenCredential_CreateRefreshableWithoutInitialToken() { #region Snippet:CommunicationTokenCredential_CreateRefreshableWithoutInitialToken using var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("*****@*****.**", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("*****@*****.**", cancellationToken)); #endregion await tokenCredential.GetTokenAsync(); }
public async Task CommunicationTokenCredential_CreateStaticToken() { var token = ExpiredToken; #region Snippet:CommunicationTokenCredential_CreateWithStaticToken //@@string token = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); using var tokenCredential = new CommunicationTokenCredential(token); #endregion await tokenCredential.GetTokenAsync(); }
public async Task CommunicationTokenCredential_CreateRefreshableWithInitialToken() { var initialToken = ExpiredToken; #region Snippet:CommunicationTokenCredential_CreateRefreshableWithInitialToken //@@string initialToken = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_USER_TOKEN"); using var tokenCredential = new CommunicationTokenCredential( refreshProactively: true, // Indicates if the token should be proactively refreshed in the background or only on-demand tokenRefresher: cancellationToken => FetchTokenForUserFromMyServer("*****@*****.**", cancellationToken), asyncTokenRefresher: cancellationToken => FetchTokenForUserFromMyServerAsync("*****@*****.**", cancellationToken), initialToken); #endregion await tokenCredential.GetTokenAsync(); }
public void GetTokenSeries_StaticToken_Throws_IfTokenRequestedWhileDisposed(bool async) { using var tokenCredential = new CommunicationTokenCredential(SampleToken); tokenCredential.Dispose(); if (async) { Assert.ThrowsAsync <ObjectDisposedException>(async() => await tokenCredential.GetTokenAsync()); } else { Assert.Throws <ObjectDisposedException>(() => tokenCredential.GetToken()); } }
public void GetTokenSeries_Throws_IfTokenRequestedWhileDisposed(string token, bool refreshProactively, bool async) { using var tokenCredential = new CommunicationTokenCredential( new CommunicationTokenRefreshOptions( refreshProactively, _ => token) { AsyncTokenRefresher = _ => new ValueTask <string>(token) }); tokenCredential.Dispose(); if (async) { Assert.ThrowsAsync <ObjectDisposedException>(async() => await tokenCredential.GetTokenAsync()); } else { Assert.Throws <ObjectDisposedException>(() => tokenCredential.GetToken()); } }
public async Task CommunicationTokenCredential_PassesCancellationToken(bool refreshProactively, bool async) { var cancellationToken = new CancellationToken(); CancellationToken?actualCancellationToken = null; using var tokenCredential = new CommunicationTokenCredential( new CommunicationTokenRefreshOptions( refreshProactively, RefreshToken, c => new ValueTask <string>(RefreshToken(c)), ExpiredToken) ); var token = async ? await tokenCredential.GetTokenAsync(cancellationToken) : tokenCredential.GetToken(cancellationToken); Assert.AreEqual(cancellationToken, actualCancellationToken); string RefreshToken(CancellationToken token) { actualCancellationToken = token; return(SampleToken); } }