Skip to content

Commit bdc7e78

Browse files
committed
chore: Add test for non-2xx responses from idTokenWithAudience calls
1 parent d084c80 commit bdc7e78

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.junit.Assert.assertNotNull;
3939
import static org.junit.Assert.assertNull;
4040
import static org.junit.Assert.assertSame;
41+
import static org.junit.Assert.assertThrows;
4142
import static org.junit.Assert.assertTrue;
4243
import static org.junit.Assert.fail;
4344

@@ -975,6 +976,55 @@ public void idTokenWithAudience_iamFlow_targetAudienceDoesNotMatchAudClaim() thr
975976
tokenCredential.getIdToken().getJsonWebSignature().getPayload().getAudience());
976977
}
977978

979+
@Test
980+
public void idTokenWithAudience_oauthEndpoint_non2XXError() throws IOException {
981+
String universeDomain = "test.com";
982+
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
983+
transportFactory.transport.setError(new IOException("404 Not Found"));
984+
ServiceAccountCredentials credentials =
985+
createDefaultBuilder()
986+
.setScopes(SCOPES)
987+
.setHttpTransportFactory(transportFactory)
988+
.setUniverseDomain(universeDomain)
989+
.build();
990+
991+
String targetAudience = "differentAudience";
992+
IdTokenCredentials tokenCredential =
993+
IdTokenCredentials.newBuilder()
994+
.setIdTokenProvider(credentials)
995+
.setTargetAudience(targetAudience)
996+
.build();
997+
998+
assertThrows(IOException.class, tokenCredential::refresh);
999+
}
1000+
1001+
@Test
1002+
public void idTokenWithAudience_iamEndpoint_non2XXError() throws IOException {
1003+
String universeDomain = "test.com";
1004+
MockIAMCredentialsServiceTransportFactory transportFactory =
1005+
new MockIAMCredentialsServiceTransportFactory(universeDomain);
1006+
transportFactory.getTransport().setTargetPrincipal(CLIENT_EMAIL);
1007+
transportFactory.getTransport().setIdToken(DEFAULT_ID_TOKEN);
1008+
transportFactory
1009+
.getTransport()
1010+
.addStatusCodeAndMessage(HttpStatusCodes.STATUS_CODE_NOT_FOUND, "Not Found");
1011+
ServiceAccountCredentials credentials =
1012+
createDefaultBuilder()
1013+
.setScopes(SCOPES)
1014+
.setHttpTransportFactory(transportFactory)
1015+
.setUniverseDomain(universeDomain)
1016+
.build();
1017+
1018+
String targetAudience = "differentAudience";
1019+
IdTokenCredentials tokenCredential =
1020+
IdTokenCredentials.newBuilder()
1021+
.setIdTokenProvider(credentials)
1022+
.setTargetAudience(targetAudience)
1023+
.build();
1024+
1025+
assertThrows(IOException.class, tokenCredential::refresh);
1026+
}
1027+
9781028
@Test
9791029
public void getScopes_nullReturnsEmpty() throws IOException {
9801030
ServiceAccountCredentials credentials = createDefaultBuilder().build();

oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static org.junit.Assert.assertNotNull;
3838
import static org.junit.Assert.assertNull;
3939
import static org.junit.Assert.assertSame;
40+
import static org.junit.Assert.assertThrows;
4041
import static org.junit.Assert.assertTrue;
4142
import static org.junit.Assert.fail;
4243

@@ -813,6 +814,21 @@ public void IdTokenCredentials_NoRetry_RetryableStatus_throws() throws IOExcepti
813814
}
814815
}
815816

817+
@Test
818+
public void idTokenWithAudience_non2xxError() throws IOException {
819+
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
820+
transportFactory.transport.setError(new IOException("404 Not Found"));
821+
String refreshToken = MockTokenServerTransport.REFRESH_TOKEN_WITH_USER_SCOPE;
822+
InputStream userStream = writeUserStream(CLIENT_ID, CLIENT_SECRET, refreshToken, QUOTA_PROJECT);
823+
824+
UserCredentials credentials = UserCredentials.fromStream(userStream, transportFactory);
825+
826+
IdTokenCredentials tokenCredential =
827+
IdTokenCredentials.newBuilder().setIdTokenProvider(credentials).build();
828+
829+
assertThrows(GoogleAuthException.class, tokenCredential::refresh);
830+
}
831+
816832
@Test
817833
public void refreshAccessToken_4xx_5xx_NonRetryableFails() throws IOException {
818834
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();

0 commit comments

Comments
 (0)