Skip to content

Commit 083557d

Browse files
committed
feedback: add test for universedomain exception with mock.
1 parent a8d466f commit 083557d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import static org.junit.Assert.assertThrows;
4141
import static org.junit.Assert.assertTrue;
4242
import static org.junit.Assert.fail;
43+
import static org.mockito.Mockito.mock;
44+
import static org.mockito.Mockito.when;
4345

4446
import com.google.api.client.http.HttpStatusCodes;
4547
import com.google.api.client.json.GenericJson;
@@ -940,6 +942,32 @@ public void sign_sameAs_nonGDU() {
940942
assertArrayEquals(expectedSignature, targetCredentials.sign(expectedSignature));
941943
}
942944

945+
@Test
946+
public void sign_universeDomainException() throws IOException {
947+
// Currently, no credentials allowed as source credentials throws exception for
948+
// getUniverseDomain(), mock this behavior for test only. ServiceAccountCredentials
949+
// should not throw for getUniverseDomain() calls.
950+
ServiceAccountCredentials sourceCredentialsMock = mock(ServiceAccountCredentials.class);
951+
when(sourceCredentialsMock.getUniverseDomain()).thenThrow(IOException.class);
952+
953+
MockIAMCredentialsServiceTransportFactory transportFactory =
954+
new MockIAMCredentialsServiceTransportFactory();
955+
ImpersonatedCredentials targetCredentials =
956+
ImpersonatedCredentials.create(
957+
sourceCredentialsMock,
958+
IMPERSONATED_CLIENT_EMAIL,
959+
null,
960+
IMMUTABLE_SCOPES_LIST,
961+
VALID_LIFETIME,
962+
transportFactory);
963+
964+
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
965+
966+
IllegalStateException exception =
967+
assertThrows(IllegalStateException.class, () -> targetCredentials.sign(expectedSignature));
968+
assertEquals("Failed to sign: Error obtaining universe domain", exception.getMessage());
969+
}
970+
943971
@Test
944972
public void idTokenWithAudience_sameAs() throws IOException {
945973
mockTransportFactory.getTransport().setTargetPrincipal(IMPERSONATED_CLIENT_EMAIL);

0 commit comments

Comments
 (0)