From 12e8db6025e0263b801d5385844924a4f5ff7b7e Mon Sep 17 00:00:00 2001 From: Diego Alonso Marquez Palacios Date: Thu, 22 Jun 2023 23:58:27 -0400 Subject: [PATCH] test: expose test-jar and mock classes in oauth2 (#1224) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: expose test-jar and mock classes in oauth2 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * test: adapt appengine oauth2-http import * test: correct appengine import in pom * test: correct base serialization test export * chore: expose MockTokenServerTransportFactory * chore: infer version from parent in appengine pom dependencies --------- Co-authored-by: Owl Bot --- appengine/pom.xml | 3 ++- .../auth/http/HttpCredentialsAdapterTest.java | 2 +- .../oauth2/ComputeEngineCredentialsTest.java | 1 - .../DefaultCredentialsProviderTest.java | 2 -- .../oauth2/DownscopedCredentialsTest.java | 6 ++--- .../auth/oauth2/GdchCredentialsTest.java | 12 +++++---- .../auth/oauth2/GdchCredentialsTestUtil.java | 27 +++++++++++++++++++ .../auth/oauth2/GoogleCredentialsTest.java | 22 --------------- .../oauth2/ImpersonatedCredentialsTest.java | 1 - .../auth/oauth2/MockHttpTransportFactory.java | 15 +++++++++++ .../MockTokenServerTransportFactory.java | 14 ++++++++++ .../auth/oauth2/OAuth2CredentialsTest.java | 2 -- .../oauth2/ServiceAccountCredentialsTest.java | 2 -- ...erviceAccountJwtAccessCredentialsTest.java | 1 - .../google/auth/oauth2/TokenVerifierTest.java | 1 - .../auth/oauth2/UserAuthorizerTest.java | 1 - .../auth/oauth2/UserCredentialsTest.java | 2 -- oauth2_http/pom.xml | 16 +++++++++++ pom.xml | 1 + 19 files changed, 85 insertions(+), 46 deletions(-) create mode 100644 oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTestUtil.java create mode 100644 oauth2_http/javatests/com/google/auth/oauth2/MockHttpTransportFactory.java create mode 100644 oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransportFactory.java diff --git a/appengine/pom.xml b/appengine/pom.xml index 03a438ecd..b76e03031 100644 --- a/appengine/pom.xml +++ b/appengine/pom.xml @@ -75,8 +75,9 @@ com.google.auth google-auth-library-oauth2-http - test-jar test + test-jar + testlib diff --git a/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java b/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java index 723cec837..bdbcd2c91 100644 --- a/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java +++ b/oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java @@ -42,8 +42,8 @@ import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpTransport; import com.google.auth.Credentials; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import com.google.auth.oauth2.MockTokenCheckingTransport; +import com.google.auth.oauth2.MockTokenServerTransportFactory; import com.google.auth.oauth2.OAuth2Credentials; import com.google.auth.oauth2.UserCredentials; import java.io.IOException; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java index a64215fba..e39cc2cb1 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java @@ -52,7 +52,6 @@ import com.google.auth.ServiceAccountSigner.SigningException; import com.google.auth.TestUtils; import com.google.auth.http.HttpTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory; import java.io.IOException; import java.net.URI; import java.util.ArrayDeque; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java b/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java index 826f1d267..3b7abe203 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/DefaultCredentialsProviderTest.java @@ -47,8 +47,6 @@ import com.google.auth.TestUtils; import com.google.auth.http.HttpTransportFactory; import com.google.auth.oauth2.ComputeEngineCredentialsTest.MockMetadataServerTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/DownscopedCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/DownscopedCredentialsTest.java index cf778b003..d7cade9f6 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/DownscopedCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/DownscopedCredentialsTest.java @@ -204,8 +204,7 @@ public void builder_noTransport_defaults() throws IOException { private static GoogleCredentials getServiceAccountSourceCredentials(boolean canRefresh) throws IOException { - GoogleCredentialsTest.MockTokenServerTransportFactory transportFactory = - new GoogleCredentialsTest.MockTokenServerTransportFactory(); + MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); String email = "service-account@google.com"; @@ -228,8 +227,7 @@ private static GoogleCredentials getServiceAccountSourceCredentials(boolean canR } private static GoogleCredentials getUserSourceCredentials() { - GoogleCredentialsTest.MockTokenServerTransportFactory transportFactory = - new GoogleCredentialsTest.MockTokenServerTransportFactory(); + MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient("clientId", "clientSecret"); transportFactory.transport.addRefreshToken("refreshToken", "accessToken"); AccessToken accessToken = new AccessToken("accessToken", new Date()); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java index 84f7e7d85..14688e4f5 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java @@ -46,7 +46,6 @@ import com.google.api.client.testing.http.FixedClock; import com.google.api.client.util.Clock; import com.google.auth.TestUtils; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -510,11 +509,14 @@ public void refreshAccessToken_correct() throws IOException { GdchCredentials credentials = GdchCredentials.fromJson(json, transportFactory); GdchCredentials gdchWithAudience = credentials.createWithGdchAudience(API_AUDIENCE); - gdchWithAudience.clock = new FixedClock(0L); + GdchCredentialsTestUtil.registerGdchCredentialWithMockTransport( + gdchWithAudience, + transportFactory.transport, + PROJECT_ID, + SERVICE_IDENTITY_NAME, + tokenString, + TOKEN_SERVER_URI); - transportFactory.transport.addGdchServiceAccount( - GdchCredentials.getIssuerSubjectValue(PROJECT_ID, SERVICE_IDENTITY_NAME), tokenString); - transportFactory.transport.setTokenServerUri(TOKEN_SERVER_URI); AccessToken accessToken = gdchWithAudience.refreshAccessToken(); assertNotNull(accessToken); assertEquals(tokenString, accessToken.getTokenValue()); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTestUtil.java b/oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTestUtil.java new file mode 100644 index 000000000..51880ddcc --- /dev/null +++ b/oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTestUtil.java @@ -0,0 +1,27 @@ +package com.google.auth.oauth2; + +import com.google.api.client.testing.http.FixedClock; +import java.io.IOException; +import java.net.URI; +import java.util.Map; + +public class GdchCredentialsTestUtil { + public static void registerGdchCredentialWithMockTransport( + GdchCredentials credentials, + MockTokenServerTransport transport, + String projectId, + String serviceIdentityName, + String tokenString, + URI tokenServerUri) { + credentials.clock = new FixedClock(0L); + transport.addGdchServiceAccount( + GdchCredentials.getIssuerSubjectValue(projectId, serviceIdentityName), tokenString); + transport.setTokenServerUri(tokenServerUri); + } + + public static GdchCredentials fromJson( + Map json, MockTokenServerTransportFactory transportFactory) + throws IOException { + return GdchCredentials.fromJson(json, transportFactory); + } +} diff --git a/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java index 415260760..80e28b3ec 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java @@ -33,8 +33,6 @@ import static org.junit.Assert.*; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.testing.http.MockHttpTransport; import com.google.api.client.util.Clock; import com.google.auth.TestUtils; import com.google.auth.http.HttpTransportFactory; @@ -93,26 +91,6 @@ public class GoogleCredentialsTest extends BaseSerializationTest { private static final Collection DEFAULT_SCOPES = Collections.unmodifiableCollection(Arrays.asList("scope3")); - static class MockHttpTransportFactory implements HttpTransportFactory { - - MockHttpTransport transport = new MockHttpTransport(); - - @Override - public HttpTransport create() { - return transport; - } - } - - public static class MockTokenServerTransportFactory implements HttpTransportFactory { - - public MockTokenServerTransport transport = new MockTokenServerTransport(); - - @Override - public HttpTransport create() { - return transport; - } - } - @Test public void getApplicationDefault_nullTransport_throws() throws IOException { try { diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java index 9ff555a47..aa3df6ec4 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java @@ -52,7 +52,6 @@ import com.google.auth.ServiceAccountSigner.SigningException; import com.google.auth.TestUtils; import com.google.auth.http.HttpTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.io.ByteArrayOutputStream; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/MockHttpTransportFactory.java b/oauth2_http/javatests/com/google/auth/oauth2/MockHttpTransportFactory.java new file mode 100644 index 000000000..00976e00f --- /dev/null +++ b/oauth2_http/javatests/com/google/auth/oauth2/MockHttpTransportFactory.java @@ -0,0 +1,15 @@ +package com.google.auth.oauth2; + +import com.google.api.client.http.HttpTransport; +import com.google.api.client.testing.http.MockHttpTransport; +import com.google.auth.http.HttpTransportFactory; + +public class MockHttpTransportFactory implements HttpTransportFactory { + + MockHttpTransport transport = new MockHttpTransport(); + + @Override + public HttpTransport create() { + return transport; + } +} diff --git a/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransportFactory.java b/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransportFactory.java new file mode 100644 index 000000000..a1d5831ad --- /dev/null +++ b/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransportFactory.java @@ -0,0 +1,14 @@ +package com.google.auth.oauth2; + +import com.google.api.client.http.HttpTransport; +import com.google.auth.http.HttpTransportFactory; + +public class MockTokenServerTransportFactory implements HttpTransportFactory { + + public MockTokenServerTransport transport = new MockTokenServerTransport(); + + @Override + public HttpTransport create() { + return transport; + } +} diff --git a/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java index 7032fde32..62aa08225 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/OAuth2CredentialsTest.java @@ -45,10 +45,8 @@ import com.google.auth.TestClock; import com.google.auth.TestUtils; import com.google.auth.http.AuthHttpConstants; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import com.google.auth.oauth2.OAuth2Credentials.OAuthValue; import com.google.auth.oauth2.OAuth2Credentials.RefreshTask; -import com.google.auth.oauth2.OAuth2Credentials.RefreshTaskListener; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.util.concurrent.ListenableFutureTask; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java index 304cf000a..6da93d409 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountCredentialsTest.java @@ -55,8 +55,6 @@ import com.google.auth.TestUtils; import com.google.auth.http.AuthHttpConstants; import com.google.auth.http.HttpTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import com.google.common.collect.ImmutableSet; import java.io.ByteArrayInputStream; import java.io.IOException; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java index 3e03c0368..df95ea2f3 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java @@ -49,7 +49,6 @@ import com.google.auth.RequestMetadataCallback; import com.google.auth.TestClock; import com.google.auth.http.AuthHttpConstants; -import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory; import java.io.IOException; import java.io.InputStream; import java.net.URI; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/TokenVerifierTest.java b/oauth2_http/javatests/com/google/auth/oauth2/TokenVerifierTest.java index 5a81e27a4..5168cb90e 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/TokenVerifierTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/TokenVerifierTest.java @@ -43,7 +43,6 @@ import com.google.api.client.testing.http.MockLowLevelHttpResponse; import com.google.api.client.util.Clock; import com.google.auth.http.HttpTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import com.google.auth.oauth2.TokenVerifier.VerificationException; import com.google.common.io.CharStreams; import java.io.IOException; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java index f90098382..e0a8e2753 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserAuthorizerTest.java @@ -38,7 +38,6 @@ import static org.junit.Assert.fail; import com.google.auth.TestUtils; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import java.io.IOException; import java.net.URI; import java.net.URL; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java index 01010f99e..a5c666936 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/UserCredentialsTest.java @@ -45,8 +45,6 @@ import com.google.auth.RequestMetadataCallback; import com.google.auth.TestUtils; import com.google.auth.http.AuthHttpConstants; -import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory; -import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.io.ByteArrayInputStream; diff --git a/oauth2_http/pom.xml b/oauth2_http/pom.xml index 4515bb19b..0267cc590 100644 --- a/oauth2_http/pom.xml +++ b/oauth2_http/pom.xml @@ -125,6 +125,22 @@ + + + + test-jar + + + false + testlib + + **/Mock* + **/*TestUtil* + **/BaseSerializationTest* + + + + org.apache.maven.plugins diff --git a/pom.xml b/pom.xml index 963473100..2181bfe81 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,7 @@ google-auth-library-oauth2-http ${project.version} test-jar + testlib