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

Add test for refresh token flow #604

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
a8ec45c8ea4ba559247b654d01b0d35b21a68865
33f3224cb40e3fa8c56ddb88962e3a4e9319685d
430a1a0a5dd4efe78e21526c37bec9dbce036401
c4d3b5f96f8dca23235dfd3f75882e01a849b54a
7c713fb498eba102edbd8eb50d5072e16f1b188d
d0129c1095216d5c830900c8a6223ef5d4274de1
4bc5c823b8ebf5a00491c7e63e1ea49d29bf5ee7
4bc5c823b8ebf5a00491c7e63e1ea49d29bf5ee7
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@

import static commercetools.utils.CommercetoolsTestUtils.*;

import java.security.MessageDigest;
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;

import com.commercetools.api.client.ApiRoot;
import com.commercetools.api.defaultconfig.ApiRootBuilder;
import com.commercetools.api.defaultconfig.ServiceRegion;
import com.commercetools.api.models.customer.Customer;
import com.commercetools.api.models.customer.CustomerDraft;
import com.commercetools.api.models.customer.CustomerDraftBuilder;
import com.commercetools.api.models.project.Project;
import commercetools.customer.CustomerFixtures;
import commercetools.utils.CommercetoolsTestUtils;

import io.vrap.rmf.base.client.ApiHttpResponse;
import io.vrap.rmf.base.client.AuthenticationToken;
import io.vrap.rmf.base.client.HttpClientSupplier;
import io.vrap.rmf.base.client.VrapHttpClient;
import io.vrap.rmf.base.client.http.HttpStatusCode;
import io.vrap.rmf.base.client.http.OAuthHandler;
import io.vrap.rmf.base.client.http.OAuthMiddleware;
import io.vrap.rmf.base.client.oauth2.GlobalCustomerPasswordTokenSupplier;
import io.vrap.rmf.base.client.oauth2.InMemoryTokenStorage;
import io.vrap.rmf.base.client.oauth2.RefreshFlowTokenSupplier;
import io.vrap.rmf.base.client.utils.ClientUtils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -59,6 +74,70 @@ public void execute() {
}
}

@Test
public void refreshTokenFlow() {
CustomerFixtures.withCustomer(customer -> {
GlobalCustomerPasswordTokenSupplier globalCustomerPasswordTokenSupplier = new GlobalCustomerPasswordTokenSupplier(
getClientId(), getClientSecret(), customer.getEmail(), CustomerFixtures.TEST_CUSTOMER_PASSWORD, null,
ServiceRegion.GCP_EUROPE_WEST1.getPasswordFlowTokenURL(CommercetoolsTestUtils.getProjectKey()),
vrapHttpClient);

final AuthenticationToken authenticationToken = ClientUtils
.blockingWait(globalCustomerPasswordTokenSupplier.getToken(), Duration.ofSeconds(10));

InMemoryTokenStorage tokenStorage = new InMemoryTokenStorage(authenticationToken);

RefreshFlowTokenSupplier refreshFlowTokenSupplier = new RefreshFlowTokenSupplier(getClientId(),
getClientSecret(), ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), tokenStorage, vrapHttpClient);
AuthenticationToken newAuthenticationToken = ClientUtils
.blockingWait(refreshFlowTokenSupplier.refreshToken(), Duration.ofSeconds(10));

Assertions.assertNotEquals(calculateSha256(authenticationToken.getAccessToken()),
calculateSha256(newAuthenticationToken.getAccessToken()));
Assertions.assertTrue(authenticationToken.getExpiresInZonedDateTime()
.isBefore(newAuthenticationToken.getExpiresInZonedDateTime()));
});
}

@Test
public void getNewAccessTokenFromApiRoot() {
CustomerFixtures.withCustomer(customer -> {
GlobalCustomerPasswordTokenSupplier globalCustomerPasswordTokenSupplier = new GlobalCustomerPasswordTokenSupplier(
getClientId(), getClientSecret(), customer.getEmail(), CustomerFixtures.TEST_CUSTOMER_PASSWORD, null,
ServiceRegion.GCP_EUROPE_WEST1.getPasswordFlowTokenURL(CommercetoolsTestUtils.getProjectKey()),
vrapHttpClient);

AuthenticationToken authenticationToken = ClientUtils
.blockingWait(globalCustomerPasswordTokenSupplier.getToken(), Duration.ofSeconds(10));
String oldAccessToken = authenticationToken.getAccessToken();
authenticationToken.setExpiresIn(0L);

InMemoryTokenStorage tokenStorage = new InMemoryTokenStorage(authenticationToken);

RefreshFlowTokenSupplier refreshFlowTokenSupplier = new RefreshFlowTokenSupplier(getClientId(),
getClientSecret(), ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), tokenStorage, vrapHttpClient);

OAuthHandler oAuthHandler = new OAuthHandler(refreshFlowTokenSupplier, Duration.ofSeconds(60));

ApiRoot apiRoot = ApiRootBuilder.ofEnvironmentVariables()
.withOAuthMiddleware(OAuthMiddleware.of(oAuthHandler))
.withPolicies(policyBuilder -> policyBuilder.withRetry(builder -> builder.maxRetries(5)
.statusCodes(Arrays.asList(HttpStatusCode.BAD_GATEWAY_502,
HttpStatusCode.SERVICE_UNAVAILABLE_503, HttpStatusCode.GATEWAY_TIMEOUT_504))))
.build();

apiRoot.withProjectKey(getProjectKey())
.get()
.execute()
.thenApply(ApiHttpResponse::getBody)
.thenApply(Project::getName)
.join();

Assertions.assertNotEquals(calculateSha256(oldAccessToken),
calculateSha256(tokenStorage.getToken().getAccessToken()));
});
}

@Test
public void throwExceptionWrongCredentials() {
Assertions.assertThrows(ExecutionException.class, () -> {
Expand All @@ -70,4 +149,23 @@ public void throwExceptionWrongCredentials() {
globalCustomerPasswordTokenSupplier.getToken().get();
});
}

public static String calculateSha256(final String base) {
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-256");
final byte[] hash = digest.digest(base.getBytes("UTF-8"));
final StringBuilder hexString = new StringBuilder();
for (int i = 0; i < hash.length; i++) {
final String hex = Integer.toHexString(0xff & hash[i]);
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}

}
Loading