Skip to content

Commit

Permalink
Merge pull request #536 from commercetools/add_static_auth_token_example
Browse files Browse the repository at this point in the history
Add an example test for static auth token
  • Loading branch information
lojzatran authored Dec 20, 2023
2 parents 4b8101c + a64f05b commit d9216d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
a8ec45c8ea4ba559247b654d01b0d35b21a68865
33f3224cb40e3fa8c56ddb88962e3a4e9319685d
430a1a0a5dd4efe78e21526c37bec9dbce036401


Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@

package commercetools;

import static java.lang.String.format;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.concurrent.ExecutionException;

import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.defaultconfig.ApiRootBuilder;
import com.commercetools.api.defaultconfig.ServiceRegion;
import com.commercetools.api.models.product.ProductPagedQueryResponse;
import commercetools.utils.CommercetoolsTestUtils;

import io.vrap.rmf.base.client.*;
Expand Down Expand Up @@ -40,7 +45,7 @@ public void introspection() throws ExecutionException, InterruptedException {
(credentials.getClientId() + ":" + credentials.getClientSecret()).getBytes(StandardCharsets.UTF_8));

final ApiHttpHeaders headers = new ApiHttpHeaders()
.withHeader(ApiHttpHeaders.AUTHORIZATION, String.format("Basic %s", auth))
.withHeader(ApiHttpHeaders.AUTHORIZATION, format("Basic %s", auth))
.withHeader(ApiHttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
final String body = "token=" + token.getAccessToken();

Expand Down Expand Up @@ -76,7 +81,7 @@ public void revoke() throws ExecutionException, InterruptedException {
.encodeToString(
(credentials.getClientId() + ":" + credentials.getClientSecret()).getBytes(StandardCharsets.UTF_8));
final ApiHttpHeaders headers = new ApiHttpHeaders()
.withHeader(ApiHttpHeaders.AUTHORIZATION, String.format("Basic %s", auth))
.withHeader(ApiHttpHeaders.AUTHORIZATION, format("Basic %s", auth))
.withHeader(ApiHttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");

// build the revoke request
Expand All @@ -103,4 +108,28 @@ public void revoke() throws ExecutionException, InterruptedException {

Assertions.assertFalse(tokenIntrospection.getBody().isActive());
}

@Test
public void apiRootWithStaticAuthToken() throws ExecutionException, InterruptedException {
final ClientCredentials credentials = ClientCredentials.of()
.withClientId(CommercetoolsTestUtils.getClientId())
.withClientSecret(CommercetoolsTestUtils.getClientSecret())
.build();
final ClientCredentialsTokenSupplier tokenSupplier = new ClientCredentialsTokenSupplier(
credentials.getClientId(), credentials.getClientSecret(), "",
ServiceRegion.GCP_EUROPE_WEST1.getOAuthTokenUrl(), HttpClientSupplier.of().get());
final AuthenticationToken token = tokenSupplier.getToken().get();

final ApiRootBuilder builder = ApiRootBuilder.of()
.withProjectKey(CommercetoolsTestUtils.getProjectKey())
.withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
.withStaticTokenFlow(token);
final ProjectApiRoot projectApiRoot = builder.buildProjectRoot();
final ApiHttpResponse<ProductPagedQueryResponse> productPagedQueryResponseApiHttpResponse = projectApiRoot
.products()
.get()
.executeBlocking();

Assertions.assertEquals(productPagedQueryResponseApiHttpResponse.getStatusCode(), 200);
}
}

0 comments on commit d9216d2

Please sign in to comment.