use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testRefreshToken_ServiceAccounts.
public void testRefreshToken_ServiceAccounts() throws Exception {
final String serviceAccountEmail = "36680232662-vrd7ji19q3ne0ah2csanun6bnr@developer.gserviceaccount.com";
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
MockTokenServerTransport transport = new MockTokenServerTransport();
transport.addServiceAccount(serviceAccountEmail, accessToken);
GoogleCredential credential = new GoogleCredential.Builder().setServiceAccountId(serviceAccountEmail).setServiceAccountScopes(SCOPES).setServiceAccountPrivateKey(SecurityTestUtils.newRsaPrivateKey()).setTransport(transport).setJsonFactory(JSON_FACTORY).build();
assertTrue(credential.refreshToken());
assertEquals(accessToken, credential.getAccessToken());
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamServiceAccountMissingPrivateKeyIdThrows.
public void testFromStreamServiceAccountMissingPrivateKeyIdThrows() 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", SA_KEY_TEXT);
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_id"));
}
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamUsertMissingClientSecretThrows.
public void testFromStreamUsertMissingClientSecretThrows() 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(clientId, null, refreshToken);
InputStream userStream = new ByteArrayInputStream(json.getBytes());
try {
GoogleCredential.fromStream(userStream, transport, JSON_FACTORY);
fail();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("client_secret"));
}
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamUsertMissingRefreshTokenThrows.
public void testFromStreamUsertMissingRefreshTokenThrows() 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(clientId, clientSecret, null);
InputStream userStream = new ByteArrayInputStream(json.getBytes());
try {
GoogleCredential.fromStream(userStream, transport, JSON_FACTORY);
fail();
} catch (IOException expected) {
assertTrue(expected.getMessage().contains("refresh_token"));
}
}
use of com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport in project google-api-java-client by google.
the class GoogleCredentialTest method testFromStreamServiceAccountMissingClientEmailThrows.
public void testFromStreamServiceAccountMissingClientEmailThrows() throws IOException {
final String serviceAccountId = "36680232662-vrd7ji19qe3nelgchd0ah2csanun6bnr.apps.googleusercontent.com";
MockHttpTransport transport = new MockTokenServerTransport();
// Write out user file
GenericJson serviceAccountContents = new GenericJson();
serviceAccountContents.setFactory(JSON_FACTORY);
serviceAccountContents.put("client_id", serviceAccountId);
serviceAccountContents.put("private_key", SA_KEY_TEXT);
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("client_email"));
}
}
Aggregations