Skip to content

Commit bc41872

Browse files
committed
chore: Add tests for GoogleCredentials
1 parent 2444a19 commit bc41872

File tree

8 files changed

+262
-122
lines changed

8 files changed

+262
-122
lines changed

oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ private final GoogleCredentials getDefaultCredentialsUnsynchronized(
222222
if (credentials != null) {
223223
credentials =
224224
credentials.withSource(
225-
String.format(
226-
"Metadata Server URL set to %s", getEnv(GCE_METADATA_HOST_ENV_VAR)));
225+
String.format("Metadata Server URL set to %s", getEnv(GCE_METADATA_HOST_ENV_VAR)));
227226
}
228227
}
229228

oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)
272272
builder.metricsHandler == null
273273
? new ExternalAccountMetricsHandler(this)
274274
: builder.metricsHandler;
275+
276+
this.type = "External Account Credentials";
275277
}
276278

277279
ImpersonatedCredentials buildImpersonatedCredentials() {

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -743,13 +743,13 @@ public AccessToken refreshAccessToken() throws IOException {
743743
}
744744

745745
@Test
746-
public void getDefaultCredentials_envVarSet_serviceAccountCredentials_correctCredentialInfo() throws IOException {
747-
MockTokenServerTransportFactory transportFactory =
748-
new MockTokenServerTransportFactory();
746+
public void getDefaultCredentials_envVarSet_serviceAccountCredentials_correctCredentialInfo()
747+
throws IOException {
748+
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
749749
transportFactory.transport.addServiceAccount(SA_CLIENT_EMAIL, ACCESS_TOKEN);
750750
InputStream serviceAccountStream =
751-
ServiceAccountCredentialsTest.writeServiceAccountStream(
752-
SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID);
751+
ServiceAccountCredentialsTest.writeServiceAccountStream(
752+
SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID);
753753
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
754754
String serviceAccountPath = tempFilePath("service_account.json");
755755
testProvider.addFile(serviceAccountPath, serviceAccountStream);
@@ -761,68 +761,68 @@ public void getDefaultCredentials_envVarSet_serviceAccountCredentials_correctCre
761761
assertTrue(credentials instanceof ServiceAccountCredentials);
762762
Map<String, String> credentialInfo = credentials.getCredentialInfo();
763763
assertEquals(
764-
String.format(
765-
"Env Var %s set to %s",
766-
DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, serviceAccountPath),
767-
credentialInfo.get("Credential Source"));
764+
String.format(
765+
"Env Var %s set to %s",
766+
DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, serviceAccountPath),
767+
credentialInfo.get("Credential Source"));
768768
assertEquals("Service Account Credentials", credentialInfo.get("Credential Type"));
769769
assertEquals(SA_CLIENT_EMAIL, credentialInfo.get("Principal"));
770770
}
771771

772772
@Test
773-
public void getDefaultCredentials_envVarSet_userCredential_correctCredentialInfo() throws IOException {
773+
public void getDefaultCredentials_envVarSet_userCredential_correctCredentialInfo()
774+
throws IOException {
774775
InputStream userStream =
775-
UserCredentialsTest.writeUserStream(
776-
USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN, QUOTA_PROJECT);
776+
UserCredentialsTest.writeUserStream(
777+
USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN, QUOTA_PROJECT);
777778
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
778779
String userPath = tempFilePath("user.json");
779780
testProvider.addFile(userPath, userStream);
780781
testProvider.setEnv(DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, userPath);
781-
HttpTransportFactory transportFactory =
782-
new MockTokenServerTransportFactory();
782+
HttpTransportFactory transportFactory = new MockTokenServerTransportFactory();
783783

784784
GoogleCredentials credentials = testProvider.getDefaultCredentials(transportFactory);
785785

786786
assertNotNull(credentials);
787787
assertTrue(credentials instanceof UserCredentials);
788788
Map<String, String> credentialInfo = credentials.getCredentialInfo();
789789
assertEquals(
790-
String.format(
791-
"Env Var %s set to %s",
792-
DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, userPath),
793-
credentialInfo.get("Credential Source"));
790+
String.format(
791+
"Env Var %s set to %s", DefaultCredentialsProvider.CREDENTIAL_ENV_VAR, userPath),
792+
credentialInfo.get("Credential Source"));
794793
assertEquals("User Credentials", credentialInfo.get("Credential Type"));
795794
assertNull(credentialInfo.get("Principal"));
796795
}
797796

798797
@Test
799-
public void getDefaultCredentials_wellKnownFile_userCredential_correctCredentialInfo() throws IOException {
798+
public void getDefaultCredentials_wellKnownFile_userCredential_correctCredentialInfo()
799+
throws IOException {
800800
File cloudConfigDir = getTempDirectory();
801801
InputStream userStream =
802-
UserCredentialsTest.writeUserStream(
803-
USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN, QUOTA_PROJECT);
802+
UserCredentialsTest.writeUserStream(
803+
USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN, QUOTA_PROJECT);
804804
File wellKnownFile =
805-
new File(cloudConfigDir, DefaultCredentialsProvider.WELL_KNOWN_CREDENTIALS_FILE);
805+
new File(cloudConfigDir, DefaultCredentialsProvider.WELL_KNOWN_CREDENTIALS_FILE);
806806
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
807807
testProvider.setEnv("CLOUDSDK_CONFIG", cloudConfigDir.getAbsolutePath());
808808
testProvider.addFile(wellKnownFile.getAbsolutePath(), userStream);
809-
HttpTransportFactory transportFactory =
810-
new MockTokenServerTransportFactory();
809+
HttpTransportFactory transportFactory = new MockTokenServerTransportFactory();
811810

812811
GoogleCredentials credentials = testProvider.getDefaultCredentials(transportFactory);
813812

814813
assertNotNull(credentials);
815814
assertTrue(credentials instanceof UserCredentials);
816815
Map<String, String> credentialInfo = credentials.getCredentialInfo();
817816
assertEquals(
818-
String.format("Well Known File at %s", wellKnownFile.getCanonicalPath()),
819-
credentialInfo.get("Credential Source"));
817+
String.format("Well Known File at %s", wellKnownFile.getCanonicalPath()),
818+
credentialInfo.get("Credential Source"));
820819
assertEquals("User Credentials", credentialInfo.get("Credential Type"));
821820
assertNull(credentialInfo.get("Principal"));
822821
}
823822

824823
@Test
825-
public void getDefaultCredentials_computeEngineCredentialscorrectCredentialInfo() throws IOException {
824+
public void getDefaultCredentials_computeEngineCredentialscorrectCredentialInfo()
825+
throws IOException {
826826
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
827827
TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider();
828828
String gceMetadataHost = "192.0.2.0";
@@ -834,8 +834,8 @@ public void getDefaultCredentials_computeEngineCredentialscorrectCredentialInfo(
834834
assertTrue(credentials instanceof ComputeEngineCredentials);
835835
Map<String, String> credentialInfo = credentials.getCredentialInfo();
836836
assertEquals(
837-
String.format("Metadata Server URL set to %s", gceMetadataHost),
838-
credentials.getCredentialInfo().get("Credential Source"));
837+
String.format("Metadata Server URL set to %s", gceMetadataHost),
838+
credentials.getCredentialInfo().get("Credential Source"));
839839
assertEquals("Compute Engine Credentials", credentialInfo.get("Credential Type"));
840840
assertNull(credentialInfo.get("Principal"));
841841
}
@@ -1006,4 +1006,4 @@ public HttpTransport create() {
10061006
return transport;
10071007
}
10081008
}
1009-
}
1009+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import com.google.common.collect.ImmutableMap;
5353
import com.google.common.io.BaseEncoding;
5454
import java.io.IOException;
55+
import java.io.InputStream;
5556
import java.net.URI;
5657
import java.nio.charset.StandardCharsets;
5758
import java.util.Arrays;
@@ -95,6 +96,26 @@ public class ExternalAccountAuthorizedUserCredentialsTest extends BaseSerializat
9596

9697
private MockExternalAccountAuthorizedUserCredentialsTransportFactory transportFactory;
9798

99+
static InputStream writeExternalAccountUserCredentialStream(
100+
String clientId, String clientSecret, String refreshToken, String tokenUrl)
101+
throws IOException {
102+
GenericJson json = new GenericJson();
103+
if (clientId != null) {
104+
json.put("client_id", clientId);
105+
}
106+
if (clientSecret != null) {
107+
json.put("client_secret", clientSecret);
108+
}
109+
if (refreshToken != null) {
110+
json.put("refresh_token", refreshToken);
111+
}
112+
if (tokenUrl != null) {
113+
json.put("token_url", tokenUrl);
114+
}
115+
json.put("type", "external_account_authorized_user");
116+
return TestUtils.jsonToInputStream(json);
117+
}
118+
98119
static class MockExternalAccountAuthorizedUserCredentialsTransportFactory
99120
implements HttpTransportFactory {
100121

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,4 +821,111 @@ private static void testFromStreamException(InputStream stream, String expectedM
821821
assertTrue(expected.getMessage().contains(expectedMessageContent));
822822
}
823823
}
824+
825+
@Test
826+
public void getCredentialInfo_serviceAccountCredentials() throws IOException {
827+
InputStream serviceAccountStream =
828+
ServiceAccountCredentialsTest.writeServiceAccountStream(
829+
SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID);
830+
GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccountStream);
831+
Map<String, String> credentialInfo = credentials.getCredentialInfo();
832+
833+
// No need to test the Credential Source as this is fake data
834+
// Credential Source is tested in DefaultCredentialsProviderTest
835+
assertEquals("Service Account Credentials", credentialInfo.get("Credential Type"));
836+
assertEquals(SA_CLIENT_EMAIL, credentialInfo.get("Principal"));
837+
}
838+
839+
@Test
840+
public void getCredentialInfo_userCredentials() throws IOException {
841+
InputStream userStream =
842+
UserCredentialsTest.writeUserStream(
843+
USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN, null);
844+
GoogleCredentials credentials = GoogleCredentials.fromStream(userStream);
845+
Map<String, String> credentialInfo = credentials.getCredentialInfo();
846+
847+
// No need to test the Credential Source as this is fake data
848+
// Credential Source is tested in DefaultCredentialsProviderTest
849+
assertEquals("User Credentials", credentialInfo.get("Credential Type"));
850+
assertNull(credentialInfo.get("Principal"));
851+
}
852+
853+
@Test
854+
public void getCredentialInfo_gdchCredentials() throws IOException {
855+
InputStream gdchServiceAccountStream =
856+
GdchCredentialsTest.writeGdchServiceAccountStream(
857+
GDCH_SA_FORMAT_VERSION,
858+
GDCH_SA_PROJECT_ID,
859+
GDCH_SA_PRIVATE_KEY_ID,
860+
GDCH_SA_PRIVATE_KEY_PKC8,
861+
GDCH_SA_SERVICE_IDENTITY_NAME,
862+
GDCH_SA_CA_CERT_PATH,
863+
GDCH_SA_TOKEN_SERVER_URI);
864+
GoogleCredentials credentials = GoogleCredentials.fromStream(gdchServiceAccountStream);
865+
Map<String, String> credentialInfo = credentials.getCredentialInfo();
866+
867+
// No need to test the Credential Source as this is fake data
868+
// Credential Source is tested in DefaultCredentialsProviderTest
869+
assertEquals("GDCH Credentials", credentialInfo.get("Credential Type"));
870+
assertNull(credentialInfo.get("Principal"));
871+
}
872+
873+
@Test
874+
public void getCredentialInfo_externalAccount() throws IOException {
875+
MockExternalAccountCredentialsTransportFactory transportFactory =
876+
new MockExternalAccountCredentialsTransportFactory();
877+
InputStream identityPoolCredentialStream =
878+
IdentityPoolCredentialsTest.writeIdentityPoolCredentialsStream(
879+
transportFactory.transport.getStsUrl(),
880+
transportFactory.transport.getMetadataUrl(),
881+
/* serviceAccountImpersonationUrl= */ null,
882+
/* serviceAccountImpersonationOptionsMap= */ null);
883+
884+
GoogleCredentials credentials = GoogleCredentials.fromStream(identityPoolCredentialStream);
885+
Map<String, String> credentialInfo = credentials.getCredentialInfo();
886+
887+
// No need to test the Credential Source as this is fake data
888+
// Credential Source is tested in DefaultCredentialsProviderTest
889+
assertEquals("External Account Credentials", credentialInfo.get("Credential Type"));
890+
assertNull(credentialInfo.get("Principal"));
891+
}
892+
893+
@Test
894+
public void getCredentialInfo_externalAccountUserCredentials() throws IOException {
895+
InputStream externalAccountUserCredentialStream =
896+
ExternalAccountAuthorizedUserCredentialsTest.writeExternalAccountUserCredentialStream(
897+
USER_CLIENT_ID,
898+
USER_CLIENT_SECRET,
899+
REFRESH_TOKEN,
900+
"https://sts.googleapis.com/v1/oauthtoken");
901+
902+
GoogleCredentials credentials =
903+
GoogleCredentials.fromStream(externalAccountUserCredentialStream);
904+
Map<String, String> credentialInfo = credentials.getCredentialInfo();
905+
906+
// No need to test the Credential Source as this is fake data
907+
// Credential Source is tested in DefaultCredentialsProviderTest
908+
assertEquals(
909+
"External Account Authorized User Credentials", credentialInfo.get("Credential Type"));
910+
assertNull(credentialInfo.get("Principal"));
911+
}
912+
913+
@Test
914+
public void getCredentialInfo_impersonatedServiceAccount() throws IOException {
915+
InputStream impersonationCredentialsStream =
916+
ImpersonatedCredentialsTest.writeImpersonationCredentialsStream(
917+
ImpersonatedCredentialsTest.IMPERSONATION_OVERRIDE_URL,
918+
ImpersonatedCredentialsTest.DELEGATES,
919+
null);
920+
921+
ImpersonatedCredentials credentials =
922+
(ImpersonatedCredentials) GoogleCredentials.fromStream(impersonationCredentialsStream);
923+
Map<String, String> credentialInfo = credentials.getCredentialInfo();
924+
925+
// No need to test the Credential Source as this is fake data
926+
// Credential Source is tested in DefaultCredentialsProviderTest
927+
assertEquals("Impersonated Credentials", credentialInfo.get("Credential Type"));
928+
assertEquals(
929+
ImpersonatedCredentialsTest.IMPERSONATED_CLIENT_EMAIL, credentialInfo.get("Principal"));
930+
}
824931
}

0 commit comments

Comments
 (0)