use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class OAuth2CredentialsTest method removeChangeListener_unregisters_observer.
@Test
public void removeChangeListener_unregisters_observer() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1);
OAuth2Credentials userCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).build();
// Use a fixed clock so tokens don't expire
userCredentials.clock = new TestClock();
TestChangeListener listener = new TestChangeListener();
userCredentials.addChangeListener(listener);
assertEquals(0, listener.callCount);
// Get a first token
userCredentials.getRequestMetadata(CALL_URI);
assertEquals(1, listener.callCount);
// Change server to a different token and refresh
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
// Refresh to force getting next token
userCredentials.refresh();
assertEquals(2, listener.callCount);
// Remove the listener and refresh the credential again
userCredentials.removeChangeListener(listener);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
userCredentials.refresh();
assertEquals(2, listener.callCount);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class OAuth2CredentialsTest method getRequestMetadata_async.
@Test
public void getRequestMetadata_async() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken1);
TestClock clock = new TestClock();
OAuth2Credentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).build();
credentials.clock = clock;
MockExecutor executor = new MockExecutor();
MockRequestMetadataCallback callback = new MockRequestMetadataCallback();
// Verify getting the first token, which uses the transport and calls the callback in the
// executor.
credentials.getRequestMetadata(CALL_URI, executor, callback);
assertEquals(0, transportFactory.transport.buildRequestCount);
assertNull(callback.metadata);
assertEquals(1, executor.runTasksExhaustively());
assertNotNull(callback.metadata);
TestUtils.assertContainsBearerToken(callback.metadata, accessToken1);
assertEquals(1, transportFactory.transport.buildRequestCount--);
// Change server to a different token
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, accessToken2);
// Make transport fail when used next time.
IOException error = new IOException("error");
transportFactory.transport.setError(error);
// Advance 5 minutes and verify original token. Callback is called inline.
callback.reset();
clock.addToCurrentTime(5 * 60 * 1000);
assertNull(callback.metadata);
credentials.getRequestMetadata(CALL_URI, executor, callback);
assertNotNull(callback.metadata);
assertEquals(0, executor.numTasks());
TestUtils.assertContainsBearerToken(callback.metadata, accessToken1);
// Advance 60 minutes and verify revised token, which uses the executor.
callback.reset();
clock.addToCurrentTime(60 * 60 * 1000);
credentials.getRequestMetadata(CALL_URI, executor, callback);
assertEquals(0, transportFactory.transport.buildRequestCount);
assertNull(callback.exception);
assertEquals(1, executor.runTasksExhaustively());
assertSame(error, callback.exception);
assertEquals(1, transportFactory.transport.buildRequestCount--);
// Reset the error and try again
transportFactory.transport.setError(null);
callback.reset();
credentials.getRequestMetadata(CALL_URI, executor, callback);
assertEquals(0, transportFactory.transport.buildRequestCount);
assertNull(callback.metadata);
assertEquals(1, executor.runTasksExhaustively());
assertNotNull(callback.metadata);
TestUtils.assertContainsBearerToken(callback.metadata, accessToken2);
assertEquals(1, transportFactory.transport.buildRequestCount--);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class UserCredentialsTest method fromJson_hasAccessToken.
@Test
public void fromJson_hasAccessToken() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN);
GenericJson json = writeUserJson(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN);
GoogleCredentials credentials = UserCredentials.fromJson(json, transportFactory);
Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class UserCredentialsTest method getRequestMetadata_customTokenServer_hasAccessToken.
@Test
public void getRequestMetadata_customTokenServer_hasAccessToken() throws IOException {
final URI TOKEN_SERVER = URI.create("https://foo.com/bar");
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient(CLIENT_ID, CLIENT_SECRET);
transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN);
transportFactory.transport.setTokenServerUri(TOKEN_SERVER);
UserCredentials userCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setHttpTransportFactory(transportFactory).setTokenServerUri(TOKEN_SERVER).build();
Map<String, List<String>> metadata = userCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class UserCredentialsTest method hashCode_equals.
@Test
public void hashCode_equals() throws IOException {
final URI tokenServer = URI.create("https://foo.com/bar");
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, null);
UserCredentials credentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(transportFactory).setTokenServerUri(tokenServer).build();
UserCredentials otherCredentials = UserCredentials.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).setRefreshToken(REFRESH_TOKEN).setAccessToken(accessToken).setHttpTransportFactory(transportFactory).setTokenServerUri(tokenServer).build();
assertEquals(credentials.hashCode(), otherCredentials.hashCode());
}
Aggregations