use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method refreshAccessToken_retriesIOException.
@Test
public void refreshAccessToken_retriesIOException() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
MockTokenServerTransport transport = transportFactory.transport;
ServiceAccountCredentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, null);
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken1);
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken1);
transport.addResponseErrorSequence(new IOException());
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken2);
credentials.refresh();
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken2);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method refreshAccessToken_retriesServerError.
@Test
public void refreshAccessToken_retriesServerError() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
MockTokenServerTransport transport = transportFactory.transport;
ServiceAccountCredentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, null);
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken1);
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken1);
transport.addResponseSequence(new MockLowLevelHttpResponse().setStatusCode(500));
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken2);
credentials.refresh();
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken2);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method equals_false_keyId.
@Test
public void equals_false_keyId() throws IOException {
final URI tokenServer1 = URI.create("https://foo1.com/bar");
MockTokenServerTransportFactory serverTransportFactory = new MockTokenServerTransportFactory();
OAuth2Credentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, serverTransportFactory, tokenServer1);
OAuth2Credentials otherCredentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, "otherId", SCOPES, serverTransportFactory, tokenServer1);
assertFalse(credentials.equals(otherCredentials));
assertFalse(otherCredentials.equals(credentials));
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method refreshAccessToken_refreshesToken.
@Test
public void refreshAccessToken_refreshesToken() throws IOException {
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
MockTokenServerTransport transport = transportFactory.transport;
ServiceAccountCredentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, SCOPES, transportFactory, null);
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken1);
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken1);
transport.addServiceAccount(SA_CLIENT_EMAIL, accessToken2);
credentials.refresh();
TestUtils.assertContainsBearerToken(credentials.getRequestMetadata(CALL_URI), accessToken2);
}
use of com.google.auth.oauth2.GoogleCredentialsTest.MockTokenServerTransportFactory in project google-auth-library-java by google.
the class ServiceAccountCredentialsTest method createdScoped_enablesAccessTokens.
@Test
public void createdScoped_enablesAccessTokens() throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addServiceAccount(SA_CLIENT_EMAIL, ACCESS_TOKEN);
GoogleCredentials credentials = ServiceAccountCredentials.fromPkcs8(SA_CLIENT_ID, SA_CLIENT_EMAIL, SA_PRIVATE_KEY_PKCS8, SA_PRIVATE_KEY_ID, null, transportFactory, null);
try {
credentials.getRequestMetadata(CALL_URI);
fail("Should not be able to get token without scopes");
} catch (Exception expected) {
// Expected
}
GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
Map<String, List<String>> metadata = scopedCredentials.getRequestMetadata(CALL_URI);
TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN);
}
Aggregations