Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error for expired oauth2 token in unit test #17004

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -179,6 +180,20 @@ class AuthorizationHeaderUtilTest {
}

private OAuth2AuthorizedClient getTestOAuth2AuthorizedClient(boolean accessTokenExpired) {
Instant issuedAt = Instant.now();
Instant expiresAt;
if (accessTokenExpired) {
expiresAt = issuedAt.plus(Duration.ofNanos(1));
try {
Thread.sleep(1);
} catch (Exception e) {
fail("Error in Thread.sleep(1) : " + e.getMessage());
}
} else {
expiresAt = issuedAt.plus(Duration.ofMinutes(3));
}
OAuth2AccessToken token = new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "tokenVal", issuedAt, expiresAt);

return new OAuth2AuthorizedClient(
ClientRegistration.withRegistrationId(VALID_REGISTRATION_ID)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
Expand All @@ -189,8 +204,7 @@ class AuthorizationHeaderUtilTest {
.tokenUri("https://localhost:8080/auth/realms/master/protocol/openid-connect/token")
.build(),
"sub",
new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "tokenVal", Instant.now(),
accessTokenExpired ? Instant.now() : Instant.now().plus(Duration.ofMinutes(3))),
token,
new OAuth2RefreshToken("refreshVal", Instant.now()));
}

Expand Down