Search in sources :

Example 16 with MockTokenServerTransportFactory

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);
}
Also used : TestClock(com.google.auth.TestClock) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) Test(org.junit.Test)

Example 17 with MockTokenServerTransportFactory

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--);
}
Also used : TestClock(com.google.auth.TestClock) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) IOException(java.io.IOException) Test(org.junit.Test)

Example 18 with MockTokenServerTransportFactory

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);
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 19 with MockTokenServerTransportFactory

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);
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) URI(java.net.URI) Test(org.junit.Test)

Example 20 with MockTokenServerTransportFactory

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());
}
Also used : MockTokenServerTransportFactory(com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory) URI(java.net.URI) Test(org.junit.Test)

Aggregations

MockTokenServerTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory)50 Test (org.junit.Test)49 URI (java.net.URI)17 List (java.util.List)15 ImmutableList (com.google.common.collect.ImmutableList)9 TestClock (com.google.auth.TestClock)6 IOException (java.io.IOException)6 GenericJson (com.google.api.client.json.GenericJson)4 InputStream (java.io.InputStream)4 HttpRequest (com.google.api.client.http.HttpRequest)3 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)3 MockLowLevelHttpResponse (com.google.api.client.testing.http.MockLowLevelHttpResponse)3 OAuth2Credentials (com.google.auth.oauth2.OAuth2Credentials)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 MockHttpTransportFactory (com.google.auth.oauth2.GoogleCredentialsTest.MockHttpTransportFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpResponse (com.google.api.client.http.HttpResponse)1 HttpTransport (com.google.api.client.http.HttpTransport)1 FixedClock (com.google.api.client.testing.http.FixedClock)1