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 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.");
        }
        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 TestGetAccessTokenForRequestAsync()
        {
            AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor();
            string accessToken = activeUserCred.GetAccessTokenForRequestAsync().Result;

            TokenResponse activeToken = ActiveUserConfig.GetActiveUserToken(_cancelToken).Result;

            // The access token returned by GetAccessTokenForRequestAsync should be the same as that of active user config.
            Assert.IsTrue(
                Equals(activeToken.AccessToken, accessToken),
                "GetAccessTokenForRefreshAsync returns the wrong access token.");

            // The next call to GetAccessTokenForRequestAsync should returns the same token.
            accessToken = activeUserCred.GetAccessTokenForRequestAsync().Result;
            Assert.IsTrue(
                Equals(activeToken.AccessToken, accessToken),
                "GetAccessTokenForRefreshAsync returns the wrong access token.");
        }
        public void TestGetAccessTokenForRequestAsync()
        {
            AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor();
            // We have to call GetAccessTokenForRequestAsync first for the s_token to be generated.
            string          accessToken     = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result;
            ActiveUserToken activeUserToken = tokenProperty.GetValue(null) as ActiveUserToken;

            // The access token returned by GetAccessTokenForRequestAsync should come from token.
            Assert.IsTrue(
                Equals(activeUserToken.AccessToken, accessToken),
                "GetAccessTokenForRefreshAsync returns the wrong access token.");

            // The next call to GetAccessTokenForRequestAsync should returns the same token.
            accessToken = activeUserCred.GetAccessTokenForRequestAsync(null, cancelToken).Result;
            Assert.IsTrue(
                Equals(activeUserToken.AccessToken, accessToken),
                "GetAccessTokenForRefreshAsync returns the wrong access token.");
        }
        public void TestGetAccessTokenExpiredByTime()
        {
            AuthenticateWithSdkCredentialsExecutor activeUserCred = new AuthenticateWithSdkCredentialsExecutor();
            TokenResponse activeToken = ActiveUserConfig.GetActiveUserToken(_cancelToken).Result;

            // Force the access token to expire.
            activeToken.ExpiredTime = DateTime.UtcNow.AddSeconds(-100);
            Assert.IsTrue(activeToken.IsExpired, "TokenResponse should be expired");

            string newAccessToken = activeUserCred.GetAccessTokenForRequestAsync().Result;

            Assert.IsFalse(
                Equals(activeToken.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().Result;

            Assert.IsTrue(
                Equals(anotherNewAccessToken, newAccessToken),
                "GetAccessTokenForRefreshAsync should returns a new access token when the old one expired due to time.");
        }