Skip to content

Commit 12e8db6

Browse files
test: expose test-jar and mock classes in oauth2 (#1224)
* 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>
1 parent 27db5f2 commit 12e8db6

19 files changed

+85
-46
lines changed

appengine/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@
7575
<dependency>
7676
<groupId>com.google.auth</groupId>
7777
<artifactId>google-auth-library-oauth2-http</artifactId>
78-
<type>test-jar</type>
7978
<scope>test</scope>
79+
<type>test-jar</type>
80+
<classifier>testlib</classifier>
8081
</dependency>
8182
</dependencies>
8283
</project>

oauth2_http/javatests/com/google/auth/http/HttpCredentialsAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
import com.google.api.client.http.HttpResponse;
4343
import com.google.api.client.http.HttpTransport;
4444
import com.google.auth.Credentials;
45-
import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory;
4645
import com.google.auth.oauth2.MockTokenCheckingTransport;
46+
import com.google.auth.oauth2.MockTokenServerTransportFactory;
4747
import com.google.auth.oauth2.OAuth2Credentials;
4848
import com.google.auth.oauth2.UserCredentials;
4949
import java.io.IOException;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import com.google.auth.ServiceAccountSigner.SigningException;
5353
import com.google.auth.TestUtils;
5454
import com.google.auth.http.HttpTransportFactory;
55-
import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory;
5655
import java.io.IOException;
5756
import java.net.URI;
5857
import java.util.ArrayDeque;

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
import com.google.auth.TestUtils;
4848
import com.google.auth.http.HttpTransportFactory;
4949
import com.google.auth.oauth2.ComputeEngineCredentialsTest.MockMetadataServerTransportFactory;
50-
import com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory;
51-
import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory;
5250
import java.io.BufferedReader;
5351
import java.io.ByteArrayInputStream;
5452
import java.io.File;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ public void builder_noTransport_defaults() throws IOException {
204204

205205
private static GoogleCredentials getServiceAccountSourceCredentials(boolean canRefresh)
206206
throws IOException {
207-
GoogleCredentialsTest.MockTokenServerTransportFactory transportFactory =
208-
new GoogleCredentialsTest.MockTokenServerTransportFactory();
207+
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
209208

210209
String email = "service-account@google.com";
211210

@@ -228,8 +227,7 @@ private static GoogleCredentials getServiceAccountSourceCredentials(boolean canR
228227
}
229228

230229
private static GoogleCredentials getUserSourceCredentials() {
231-
GoogleCredentialsTest.MockTokenServerTransportFactory transportFactory =
232-
new GoogleCredentialsTest.MockTokenServerTransportFactory();
230+
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
233231
transportFactory.transport.addClient("clientId", "clientSecret");
234232
transportFactory.transport.addRefreshToken("refreshToken", "accessToken");
235233
AccessToken accessToken = new AccessToken("accessToken", new Date());

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import com.google.api.client.testing.http.FixedClock;
4747
import com.google.api.client.util.Clock;
4848
import com.google.auth.TestUtils;
49-
import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory;
5049
import java.io.File;
5150
import java.io.IOException;
5251
import java.io.InputStream;
@@ -510,11 +509,14 @@ public void refreshAccessToken_correct() throws IOException {
510509
GdchCredentials credentials = GdchCredentials.fromJson(json, transportFactory);
511510
GdchCredentials gdchWithAudience = credentials.createWithGdchAudience(API_AUDIENCE);
512511

513-
gdchWithAudience.clock = new FixedClock(0L);
512+
GdchCredentialsTestUtil.registerGdchCredentialWithMockTransport(
513+
gdchWithAudience,
514+
transportFactory.transport,
515+
PROJECT_ID,
516+
SERVICE_IDENTITY_NAME,
517+
tokenString,
518+
TOKEN_SERVER_URI);
514519

515-
transportFactory.transport.addGdchServiceAccount(
516-
GdchCredentials.getIssuerSubjectValue(PROJECT_ID, SERVICE_IDENTITY_NAME), tokenString);
517-
transportFactory.transport.setTokenServerUri(TOKEN_SERVER_URI);
518520
AccessToken accessToken = gdchWithAudience.refreshAccessToken();
519521
assertNotNull(accessToken);
520522
assertEquals(tokenString, accessToken.getTokenValue());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.google.auth.oauth2;
2+
3+
import com.google.api.client.testing.http.FixedClock;
4+
import java.io.IOException;
5+
import java.net.URI;
6+
import java.util.Map;
7+
8+
public class GdchCredentialsTestUtil {
9+
public static void registerGdchCredentialWithMockTransport(
10+
GdchCredentials credentials,
11+
MockTokenServerTransport transport,
12+
String projectId,
13+
String serviceIdentityName,
14+
String tokenString,
15+
URI tokenServerUri) {
16+
credentials.clock = new FixedClock(0L);
17+
transport.addGdchServiceAccount(
18+
GdchCredentials.getIssuerSubjectValue(projectId, serviceIdentityName), tokenString);
19+
transport.setTokenServerUri(tokenServerUri);
20+
}
21+
22+
public static GdchCredentials fromJson(
23+
Map<String, Object> json, MockTokenServerTransportFactory transportFactory)
24+
throws IOException {
25+
return GdchCredentials.fromJson(json, transportFactory);
26+
}
27+
}

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
import static org.junit.Assert.*;
3535

36-
import com.google.api.client.http.HttpTransport;
37-
import com.google.api.client.testing.http.MockHttpTransport;
3836
import com.google.api.client.util.Clock;
3937
import com.google.auth.TestUtils;
4038
import com.google.auth.http.HttpTransportFactory;
@@ -93,26 +91,6 @@ public class GoogleCredentialsTest extends BaseSerializationTest {
9391
private static final Collection<String> DEFAULT_SCOPES =
9492
Collections.unmodifiableCollection(Arrays.asList("scope3"));
9593

96-
static class MockHttpTransportFactory implements HttpTransportFactory {
97-
98-
MockHttpTransport transport = new MockHttpTransport();
99-
100-
@Override
101-
public HttpTransport create() {
102-
return transport;
103-
}
104-
}
105-
106-
public static class MockTokenServerTransportFactory implements HttpTransportFactory {
107-
108-
public MockTokenServerTransport transport = new MockTokenServerTransport();
109-
110-
@Override
111-
public HttpTransport create() {
112-
return transport;
113-
}
114-
}
115-
11694
@Test
11795
public void getApplicationDefault_nullTransport_throws() throws IOException {
11896
try {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import com.google.auth.ServiceAccountSigner.SigningException;
5353
import com.google.auth.TestUtils;
5454
import com.google.auth.http.HttpTransportFactory;
55-
import com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory;
5655
import com.google.common.collect.ImmutableList;
5756
import com.google.common.collect.ImmutableSet;
5857
import java.io.ByteArrayOutputStream;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.google.auth.oauth2;
2+
3+
import com.google.api.client.http.HttpTransport;
4+
import com.google.api.client.testing.http.MockHttpTransport;
5+
import com.google.auth.http.HttpTransportFactory;
6+
7+
public class MockHttpTransportFactory implements HttpTransportFactory {
8+
9+
MockHttpTransport transport = new MockHttpTransport();
10+
11+
@Override
12+
public HttpTransport create() {
13+
return transport;
14+
}
15+
}

0 commit comments

Comments
 (0)