Skip to content
Merged
Changes from all commits
Commits
File filter

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 @@ -22,14 +22,16 @@
import com.ibm.cloud.sdk.core.http.HttpHeaders;
import com.ibm.cloud.sdk.core.security.Authenticator;
import com.ibm.cloud.sdk.core.security.ConfigBasedAuthenticatorFactory;
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
import com.ibm.cloud.sdk.core.security.IamToken;

import okhttp3.Request;

//
// This class contains an integration test that uses the live IAM token service.
// This test is normally @Ignored to avoid trying to run this during automated builds.
//
// In order to run these tests, ceate file "cp4dtest.env" in the project root.
// In order to run these tests, ceate file "iamtest.env" in the project root.
// It should look like this:
//
// IAMTEST1_AUTH_URL=<prod iam url> e.g. https://iam.cloud.ibm.com
Expand All @@ -39,6 +41,8 @@
// IAMTEST2_AUTH_URL=<test iam url> e.g. https://iam.test.cloud.ibm.com
// IAMTEST2_AUTH_TYPE=iam
// IAMTEST2_APIKEY=<apikey>
// IAMTEST2_CLIENT_ID=bx
// IAMTEST2_CLIENT_SECRET=bx
//
// Then remove/comment-out the @Ignore annotation below and run the method as a junit test.
//
Expand All @@ -57,15 +61,25 @@ public void testIamLiveTokenServer() {

Request.Builder requestBuilder;

// Test the username/password combination.
// Perform a test using the "production" IAM token server.
requestBuilder = new Request.Builder().url("https://test.com");
auth1.authenticate(requestBuilder);
verifyAuthHeader(requestBuilder, "Bearer ");

// Test the username/apikey combination.
// Perform a test using the "test" IAM token server.
requestBuilder = new Request.Builder().url("https://test.com");
auth2.authenticate(requestBuilder);
verifyAuthHeader(requestBuilder, "Bearer ");

// Test the retrieval of a refresh token from the "test" IAM token server.
IamToken tokenResponse = ((IamAuthenticator) auth2).requestToken();
assertNotNull(tokenResponse);
String refreshToken = tokenResponse.getRefreshToken();
assertNotNull(refreshToken);
System.out.println("Refresh token: " + refreshToken);

// This check is to ensure we get back a valid refresh token rather than something like "not_supported".
assertTrue(refreshToken.length() > 20);
}

// Verify the Authorization header in the specified request builder.
Expand Down