Skip to content

Commit

Permalink
test: expose test-jar and mock classes in oauth2 (#1224)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
diegomarquezp and gcf-owl-bot[bot] committed Jun 23, 2023
1 parent 27db5f2 commit 12e8db6
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 46 deletions.
3 changes: 2 additions & 1 deletion appengine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<type>test-jar</type>
<scope>test</scope>
<type>test-jar</type>
<classifier>testlib</classifier>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> json, MockTokenServerTransportFactory transportFactory)
throws IOException {
return GdchCredentials.fromJson(json, transportFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,26 +91,6 @@ public class GoogleCredentialsTest extends BaseSerializationTest {
private static final Collection<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions oauth2_http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<skip>false</skip>
<classifier>testlib</classifier>
<includes>
<include>**/Mock*</include>
<include>**/*TestUtil*</include>
<include>**/BaseSerializationTest*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<classifier>testlib</classifier>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down

0 comments on commit 12e8db6

Please sign in to comment.