Skip to content

Commit

Permalink
Add unit test for ExternalAccountCredentials test.
Browse files Browse the repository at this point in the history
Discovered ServiceAccountImpersonationOptions needed be serializable as well.
  • Loading branch information
jmahonin committed Feb 16, 2023
1 parent eeae4e5 commit d7eb015
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
*/
public abstract class ExternalAccountCredentials extends GoogleCredentials {

private static final long serialVersionUID = 8049126194174465023L;

/** Base credential source class. Dictates the retrieval method of the external credential. */
abstract static class CredentialSource implements Serializable {

Expand Down Expand Up @@ -639,12 +641,15 @@ private static boolean isValidUrl(String url) {
* }
* </pre>
*/
static final class ServiceAccountImpersonationOptions {
static final class ServiceAccountImpersonationOptions implements Serializable {

private static final long serialVersionUID = 4250771921886280953L;
private static final int DEFAULT_TOKEN_LIFETIME_SECONDS = 3600;
private static final int MAXIMUM_TOKEN_LIFETIME_SECONDS = 43200;
private static final int MINIMUM_TOKEN_LIFETIME_SECONDS = 600;
private static final String TOKEN_LIFETIME_SECONDS_KEY = "token_lifetime_seconds";


private final int lifetime;

ServiceAccountImpersonationOptions(Map<String, Object> optionsMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Clock;
import com.google.auth.TestUtils;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.ExternalAccountCredentialsTest.TestExternalAccountCredentials.TestCredentialSource;
Expand All @@ -62,7 +64,7 @@

/** Tests for {@link ExternalAccountCredentials}. */
@RunWith(JUnit4.class)
public class ExternalAccountCredentialsTest {
public class ExternalAccountCredentialsTest extends BaseSerializationTest {

private static final String STS_URL = "https://sts.googleapis.com";

Expand Down Expand Up @@ -954,6 +956,30 @@ public void getRequestMetadata_withQuotaProjectId() throws IOException {
assertEquals("quotaProjectId", requestMetadata.get("x-goog-user-project").get(0));
}

@Test
public void serialize() throws IOException, ClassNotFoundException {
Map<String, Object> impersonationOpts = new HashMap<String, Object>(){{ put("token_lifetime_seconds", 1000); }};

TestExternalAccountCredentials testCredentials =
(TestExternalAccountCredentials)
TestExternalAccountCredentials.newBuilder()
.setHttpTransportFactory(transportFactory)
.setAudience("audience")
.setSubjectTokenType("subjectTokenType")
.setTokenUrl(STS_URL)
.setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP))
.setServiceAccountImpersonationOptions(impersonationOpts)
.build();

TestExternalAccountCredentials deserializedCredentials = serializeAndDeserialize(testCredentials);
assertEquals(testCredentials, deserializedCredentials);
assertEquals(testCredentials.hashCode(), deserializedCredentials.hashCode());
assertEquals(testCredentials.toString(), deserializedCredentials.toString());
assertEquals(testCredentials.getServiceAccountImpersonationOptions().getLifetime(),
deserializedCredentials.getServiceAccountImpersonationOptions().getLifetime());
assertSame(deserializedCredentials.clock, Clock.SYSTEM);
}

@Test
public void validateTokenUrl_validUrls() {
List<String> validUrls =
Expand Down

0 comments on commit d7eb015

Please sign in to comment.