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

chore: Add test for non-2xx responses from idTokenWithAudience calls #1656

Merged
merged 7 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -38,6 +38,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -975,6 +976,58 @@ public void idTokenWithAudience_iamFlow_targetAudienceDoesNotMatchAudClaim() thr
tokenCredential.getIdToken().getJsonWebSignature().getPayload().getAudience());
}

@Test
public void idTokenWithAudience_oauthEndpoint_non2XXStatusCode() throws IOException {
String universeDomain = "test.com";
lqiu96 marked this conversation as resolved.
Show resolved Hide resolved
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.setError(new IOException("404 Not Found"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TokenServerTransportFactory sets an error response with setError(IOException). This could be refactored in the future to be similar to below .addStatusCodeAndMessage(HttpStatusCodes.STATUS_CODE_NOT_FOUND, "Not Found");.

ServiceAccountCredentials credentials =
createDefaultBuilder()
.setScopes(SCOPES)
.setHttpTransportFactory(transportFactory)
.setUniverseDomain(universeDomain)
.build();

String targetAudience = "audience";
IdTokenCredentials tokenCredential =
IdTokenCredentials.newBuilder()
.setIdTokenProvider(credentials)
.setTargetAudience(targetAudience)
.build();

// Ensure that a non 2xx status code returns an exception and doesn't continue execution
assertThrows(IOException.class, tokenCredential::refresh);
}

@Test
public void idTokenWithAudience_iamEndpoint_non2XXStatusCode() throws IOException {
String universeDomain = "test.com";
zhumin8 marked this conversation as resolved.
Show resolved Hide resolved
MockIAMCredentialsServiceTransportFactory transportFactory =
new MockIAMCredentialsServiceTransportFactory(universeDomain);
transportFactory.getTransport().setTargetPrincipal(CLIENT_EMAIL);
transportFactory.getTransport().setIdToken(DEFAULT_ID_TOKEN);
transportFactory
.getTransport()
.addStatusCodeAndMessage(HttpStatusCodes.STATUS_CODE_NOT_FOUND, "Not Found");
ServiceAccountCredentials credentials =
createDefaultBuilder()
.setScopes(SCOPES)
.setHttpTransportFactory(transportFactory)
.setUniverseDomain(universeDomain)
.build();

String targetAudience = "audience";
IdTokenCredentials tokenCredential =
IdTokenCredentials.newBuilder()
.setIdTokenProvider(credentials)
.setTargetAudience(targetAudience)
.build();

// Ensure that a non 2xx status code returns an exception and doesn't continue execution
// Non 2xx status codes will be returned as HttpResponseException
assertThrows(IOException.class, tokenCredential::refresh);
}

@Test
public void getScopes_nullReturnsEmpty() throws IOException {
ServiceAccountCredentials credentials = createDefaultBuilder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -813,6 +814,21 @@ public void IdTokenCredentials_NoRetry_RetryableStatus_throws() throws IOExcepti
}
}

@Test
public void idTokenWithAudience_non2xxError() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.setError(new IOException("404 Not Found"));
String refreshToken = MockTokenServerTransport.REFRESH_TOKEN_WITH_USER_SCOPE;
InputStream userStream = writeUserStream(CLIENT_ID, CLIENT_SECRET, refreshToken, QUOTA_PROJECT);

UserCredentials credentials = UserCredentials.fromStream(userStream, transportFactory);

IdTokenCredentials tokenCredential =
IdTokenCredentials.newBuilder().setIdTokenProvider(credentials).build();

assertThrows(GoogleAuthException.class, tokenCredential::refresh);
}

@Test
public void refreshAccessToken_4xx_5xx_NonRetryableFails() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
Expand Down
Loading