use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class DefaultCredentialProviderTest method testDefaultCredentialUser.
private void testDefaultCredentialUser(File userFile, TestDefaultCredentialProvider testProvider) throws IOException {
final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
final String clientId = "ya29.1.AADtN_UtlxH8cruGAxrN2XQnZTVRvDyVWnYq4I6dws";
final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
// Define a transport that can simulate refreshing tokens
MockTokenServerTransport transport = new MockTokenServerTransport();
transport.addClient(clientId, clientSecret);
transport.addRefreshToken(refreshToken, ACCESS_TOKEN);
String json = GoogleCredentialTest.createUserJson(clientId, clientSecret, refreshToken);
try {
// Write out user file
PrintWriter writer = new PrintWriter(userFile);
writer.println(json);
writer.close();
Credential credential = testProvider.getDefaultCredential(transport, JSON_FACTORY);
assertNotNull(credential);
assertEquals(refreshToken, credential.getRefreshToken());
assertTrue(credential.refreshToken());
assertEquals(ACCESS_TOKEN, credential.getAccessToken());
} finally {
if (userFile.exists()) {
userFile.delete();
}
}
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamUser.
public void testFromStreamUser() throws IOException {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
final String clientId = "ya29.1.AADtN_UtlxN3PSc5yhCqfA9nDFp1dfvH8cruGAxrN2XQnZTVRvDyVWnYq4I6dws";
final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
MockTokenServerTransport transport = new MockTokenServerTransport();
transport.addClient(clientId, clientSecret);
transport.addRefreshToken(refreshToken, accessToken);
// Create user stream.
String json = createUserJson(clientId, clientSecret, refreshToken);
InputStream userStream = new ByteArrayInputStream(json.getBytes());
GoogleCredential defaultCredential = GoogleCredential.fromStream(userStream, transport, JSON_FACTORY);
assertNotNull(defaultCredential);
assertEquals(refreshToken, defaultCredential.getRefreshToken());
assertTrue(defaultCredential.refreshToken());
assertEquals(accessToken, defaultCredential.getAccessToken());
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamUsertMissingClientIdThrows.
public void testFromStreamUsertMissingClientIdThrows() throws IOException {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
final String clientId = "ya29.1.AADtN_UtlxN3PSruGAxrN2XQnZTVRvDyVWnYq4I6dws";
final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
MockTokenServerTransport transport = new MockTokenServerTransport();
transport.addClient(clientId, clientSecret);
transport.addRefreshToken(refreshToken, accessToken);
// Write out user file
String json = createUserJson(null, clientSecret, refreshToken);
InputStream userStream = new ByteArrayInputStream(json.getBytes());
try {
GoogleCredential.fromStream(userStream, transport, JSON_FACTORY);
fail();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("client_id"));
}
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamServiceAccountMissingPrivateKeyThrows.
public void testFromStreamServiceAccountMissingPrivateKeyThrows() throws IOException {
final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com";
final String serviceAccountEmail = "36680232662-vrd7ji19qgchd0ah2csanun6bnr@developer.gserviceaccount.com";
MockHttpTransport transport = new MockTokenServerTransport();
// Write out user file
GenericJson serviceAccountContents = new GenericJson();
serviceAccountContents.setFactory(JSON_FACTORY);
serviceAccountContents.put("client_id", serviceAccountId);
serviceAccountContents.put("client_email", serviceAccountEmail);
serviceAccountContents.put("private_key_id", SA_KEY_ID);
serviceAccountContents.put("type", GoogleCredential.SERVICE_ACCOUNT_FILE_TYPE);
String json = serviceAccountContents.toPrettyString();
InputStream serviceAccountStream = new ByteArrayInputStream(json.getBytes());
try {
GoogleCredential.fromStream(serviceAccountStream, transport, JSON_FACTORY);
fail();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("private_key"));
}
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testRefreshToken_User.
public void testRefreshToken_User() throws Exception {
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
final String clientSecret = "jakuaL9YyieakhECKL2SwZcu";
final String clientId = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws";
final String refreshToken = "1/Tl6awhpFjkMkSJoj1xsli0H2eL5YsMgU_NKPY2TyGWY";
MockTokenServerTransport transport = new MockTokenServerTransport();
transport.addClient(clientId, clientSecret);
transport.addRefreshToken(refreshToken, accessToken);
GoogleCredential credential = new GoogleCredential.Builder().setClientSecrets(clientId, clientSecret).setTransport(transport).setJsonFactory(JSON_FACTORY).build();
credential.setRefreshToken(refreshToken);
assertTrue(credential.refreshToken());
assertEquals(accessToken, credential.getAccessToken());
}
Aggregations