Search in sources :

Example 11 with MockTokenServerTransport

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());
}
Also used : MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 12 with MockTokenServerTransport

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"));
    }
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 13 with MockTokenServerTransport

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"));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 14 with MockTokenServerTransport

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"));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Example 15 with MockTokenServerTransport

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"));
    }
}
Also used : GenericJson(com.google.api.client.json.GenericJson) MockHttpTransport(com.google.api.client.testing.http.MockHttpTransport) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MockTokenServerTransport(com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)

Aggregations

MockTokenServerTransport (com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport)17 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 IOException (java.io.IOException)8 GenericJson (com.google.api.client.json.GenericJson)7 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)4 PrintWriter (java.io.PrintWriter)3 Credential (com.google.api.client.auth.oauth2.Credential)2 File (java.io.File)2