Skip to content

Commit 1f156e2

Browse files
committed
include unit testing for fromJson
1 parent e87ed31 commit 1f156e2

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static org.junit.Assert.assertTrue;
4343
import static org.junit.Assert.fail;
4444

45+
import com.google.api.client.json.GenericJson;
4546
import com.google.api.client.json.JsonFactory;
4647
import com.google.api.client.json.gson.GsonFactory;
4748
import com.google.api.client.json.webtoken.JsonWebSignature;
@@ -831,6 +832,48 @@ public void onFailure(Throwable exception) {
831832
assertTrue("Should have run onSuccess() callback", success.get());
832833
}
833834

835+
@Test
836+
public void fromJSON_noUniverseDomain() throws IOException {
837+
GenericJson json =
838+
writeServiceAccountJson(
839+
SA_CLIENT_ID,
840+
SA_CLIENT_EMAIL,
841+
SA_PRIVATE_KEY_PKCS8,
842+
"test-project-id",
843+
SA_PRIVATE_KEY_ID,
844+
QUOTA_PROJECT,
845+
null);
846+
ServiceAccountJwtAccessCredentials credentials =
847+
ServiceAccountJwtAccessCredentials.fromJson(json, URI.create("default-aud"));
848+
assertEquals(SA_CLIENT_ID, credentials.getClientId());
849+
assertEquals(SA_CLIENT_EMAIL, credentials.getClientEmail());
850+
assertEquals(
851+
OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8), credentials.getPrivateKey());
852+
assertEquals(QUOTA_PROJECT, credentials.getQuotaProjectId());
853+
assertEquals(GOOGLE_DEFAULT_UNIVERSE, credentials.getUniverseDomain());
854+
}
855+
856+
@Test
857+
public void fromJSON_UniverseDomain() throws IOException {
858+
GenericJson json =
859+
writeServiceAccountJson(
860+
SA_CLIENT_ID,
861+
SA_CLIENT_EMAIL,
862+
SA_PRIVATE_KEY_PKCS8,
863+
"test-project-id",
864+
SA_PRIVATE_KEY_ID,
865+
QUOTA_PROJECT,
866+
"example.com");
867+
ServiceAccountJwtAccessCredentials credentials =
868+
ServiceAccountJwtAccessCredentials.fromJson(json, URI.create("default-aud"));
869+
assertEquals(SA_CLIENT_ID, credentials.getClientId());
870+
assertEquals(SA_CLIENT_EMAIL, credentials.getClientEmail());
871+
assertEquals(
872+
OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8), credentials.getPrivateKey());
873+
assertEquals(QUOTA_PROJECT, credentials.getQuotaProjectId());
874+
assertEquals("example.com", credentials.getUniverseDomain());
875+
}
876+
834877
@Test
835878
public void getUniverseDomain_defaultUniverse() throws IOException {
836879
PrivateKey privateKey = OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
@@ -879,4 +922,37 @@ private static void testFromStreamException(InputStream stream, String expectedM
879922
assertTrue(expected.getMessage().contains(expectedMessageContent));
880923
}
881924
}
925+
private GenericJson writeServiceAccountJson(
926+
String clientId,
927+
String clientEmail,
928+
String privateKeyPkcs8,
929+
String privateKeyId,
930+
String projectId,
931+
String quotaProjectId,
932+
String universeDomain) {
933+
GenericJson json = new GenericJson();
934+
if (clientId != null) {
935+
json.put("client_id", clientId);
936+
}
937+
if (clientEmail != null) {
938+
json.put("client_email", clientEmail);
939+
}
940+
if (privateKeyPkcs8 != null) {
941+
json.put("private_key", privateKeyPkcs8);
942+
}
943+
if (privateKeyId != null) {
944+
json.put("private_key_id", privateKeyId);
945+
}
946+
if (projectId != null) {
947+
json.put("project_id", projectId);
948+
}
949+
if (quotaProjectId != null) {
950+
json.put("quota_project_id", quotaProjectId);
951+
}
952+
if (universeDomain != null) {
953+
json.put("universe_domain", universeDomain);
954+
}
955+
json.put("type", GoogleCredentials.SERVICE_ACCOUNT_FILE_TYPE);
956+
return json;
957+
}
882958
}

0 commit comments

Comments
 (0)