public void TestRefreshTokenAsync() { AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor(); string currentAccessToken = activeUserCred.GetAccessTokenForRequestAsync().Result; bool refreshed = activeUserCred.RefreshTokenAsync(_cancelToken).Result; Assert.IsTrue(refreshed, "RefreshTokenAsync should return true."); string refreshedAccessToken = activeUserCred.GetAccessTokenForRequestAsync().Result; Assert.IsTrue( !Equals(currentAccessToken, refreshedAccessToken), "A different token should be returned when RefreshTokenAsync is called again."); refreshed = activeUserCred.RefreshTokenAsync(_cancelToken).Result; Assert.IsTrue(refreshed, "RefreshTokenAsync should return true."); string refreshedAccessTokenTwo = activeUserCred.GetAccessTokenForRequestAsync().Result; Assert.IsTrue( !Equals(refreshedAccessToken, refreshedAccessTokenTwo), "A different token should be returned when RefreshTokenAsync is called again."); Assert.IsTrue( !Equals(currentAccessToken, refreshedAccessTokenTwo), "A different token should be returned when RefreshTokenAsync is called again."); }
public void TestRefreshTokenAsync() { AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor(); object activeUserToken = tokenProperty.GetValue(null); Assert.IsNull(activeUserToken, "s_token should be null initially."); bool refreshed = activeUserCred.RefreshTokenAsync(cancelToken).Result; Assert.IsTrue(refreshed, "RefreshTokenAsync should return true."); ActiveUserToken refreshedToken = tokenProperty.GetValue(null) as ActiveUserToken; Assert.IsNotNull(refreshedToken, "RefreshTokenAsync should set s_token to a non-null token."); Assert.IsNotNullOrEmpty(refreshedToken.AccessToken, "s_token should have a valid access token."); // We refresh again to make sure we get a different token. refreshed = activeUserCred.RefreshTokenAsync(cancelToken).Result; Assert.IsTrue(refreshed, "RefreshTokenAsync should return true."); ActiveUserToken secondRefreshedToken = tokenProperty.GetValue(null) as ActiveUserToken; Assert.IsNotNull(secondRefreshedToken, "RefreshTokenAsync should set s_token to a non-null token."); Assert.IsNotNullOrEmpty(secondRefreshedToken.AccessToken, "s_token should have a valid access token."); Assert.IsTrue( !Equals(refreshedToken.AccessToken, secondRefreshedToken.AccessToken), "A different token should be returned when RefreshTokenAsync is called again."); }
public void TestGetAccessTokenExpiredByTime() { AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor(); // Generate the s_token. activeUserCred.RefreshTokenAsync(new CancellationToken()).Wait(); ActiveUserToken activeUserToken = tokenProperty.GetValue(null) as ActiveUserToken; // Force the access token to expire. activeUserToken.ExpiredTime = DateTime.UtcNow.AddSeconds(-100); Assert.IsTrue(activeUserToken.IsExpired, "ActiveUserToken should be expired"); string newAccessToken = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result; Assert.IsFalse( Equals(activeUserToken.AccessToken, newAccessToken), "GetAccessTokenForRefreshAsync should returns a new access token when the old one expired due to time."); // The next call to GetAccessTokenForRequestAsync should returns the same token. string anotherNewAccessToken = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result; Assert.IsTrue( Equals(anotherNewAccessToken, newAccessToken), "GetAccessTokenForRefreshAsync should returns a new access token when the old one expired due to time."); }