Search in sources :

Example 6 with DbxCredential

use of com.dropbox.core.oauth.DbxCredential in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testDontRefreshBeforeCallIfNotExpired.

@Test
public void testDontRefreshBeforeCallIfNotExpired() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    long now = System.currentTimeMillis();
    DbxCredential credential = new DbxCredential("accesstoken", now + 2 * DbxCredential.EXPIRE_MARGIN, "refresh_token", "appkey", "app_secret");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
    assertThat(actual.getName()).isEqualTo(expected.getName());
    assertWithMessage(actual.getClass().toString()).that(actual instanceof FileMetadata).isTrue();
    assertThat(((FileMetadata) actual).getId()).isEqualTo(expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Example 7 with DbxCredential

use of com.dropbox.core.oauth.DbxCredential in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testDontRefreshForInvalidGrant.

@Test
public void testDontRefreshForInvalidGrant() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    ByteArrayInputStream responseStream = new ByteArrayInputStream(("{" + "\"error_description\":\"refresh token is invalid or revoked\"" + ",\"error\":\"invalid_grant\"" + "}").getBytes("UTF-8"));
    HttpRequestor.Response finishResponse = new HttpRequestor.Response(400, responseStream, new HashMap<String, List<String>>());
    DbxCredential credential = new DbxCredential("accesstoken", 10L, "refresh_token", "appkey", "app_secret");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(finishResponse).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(2)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
    assertThat(credential.getExpiresAt()).isEqualTo(new Long(10));
    assertThat(actual.getName()).isEqualTo(expected.getName());
    assertWithMessage(actual.getClass().toString()).that(actual instanceof FileMetadata).isTrue();
    assertThat(((FileMetadata) actual).getId()).isEqualTo(expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Example 8 with DbxCredential

use of com.dropbox.core.oauth.DbxCredential in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRefreshAndRetryWith503Retry.

@Test
public void testRefreshAndRetryWith503Retry() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled().withHttpRequestor(mockRequestor).build();
    long now = System.currentTimeMillis();
    DbxCredential credential = new DbxCredential("accesstoken", now + 2 * DbxCredential.EXPIRE_MARGIN, "refresh_token", "appkey", "app_secret");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createTokenExpiredResponse()).thenReturn(createSuccessRefreshResponse("new_token", 14400L)).thenReturn(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(4)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("new_token");
    assertThat(actual.getName()).isEqualTo(expected.getName());
    assertWithMessage(actual.getClass().toString()).that(actual instanceof FileMetadata).isTrue();
    assertThat(((FileMetadata) actual).getId()).isEqualTo(expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Example 9 with DbxCredential

use of com.dropbox.core.oauth.DbxCredential in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRefreshAndRetryAfterTokenExpiredForDownload.

@Test
public void testRefreshAndRetryAfterTokenExpiredForDownload() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    long now = System.currentTimeMillis();
    DbxCredential credential = new DbxCredential("accesstoken", now + 2 * DbxCredential.EXPIRE_MARGIN, "refresh_token", "appkey", "app_secret");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createTokenExpiredResponse()).thenReturn(createSuccessRefreshResponse("new_token", 14400L)).thenReturn(createDownloadSuccessResponse("data".getBytes(), new String(serialize(expected))));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DbxDownloader<FileMetadata> downloader = client.files().download("/path");
    try {
        downloader.download(out);
    } finally {
        downloader.close();
    }
    assertThat(out.toString()).isEqualTo("data");
    Metadata actual = downloader.getResult();
    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("new_token");
    assertThat(actual.getName()).isEqualTo(expected.getName());
    assertThat(((FileMetadata) actual).getId()).isEqualTo(expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 10 with DbxCredential

use of com.dropbox.core.oauth.DbxCredential in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testOnlineBadTokenResponse.

@Test
public void testOnlineBadTokenResponse() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    DbxCredential credential = new DbxCredential("accesstoken");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createBadTokenResponse());
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.files().getMetadata(expected.getId());
    } catch (BadResponseException ex) {
        verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
        assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
        assertThat(ex.getMessage()).startsWith("Bad JSON:");
        return;
    }
    fail("API v2 call should throw exception");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Aggregations

DbxCredential (com.dropbox.core.oauth.DbxCredential)13 HttpRequestor (com.dropbox.core.http.HttpRequestor)9 FileMetadata (com.dropbox.core.v2.files.FileMetadata)9 Test (org.testng.annotations.Test)9 Metadata (com.dropbox.core.v2.files.Metadata)6 JsonReader (com.dropbox.core.json.JsonReader)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 DbxAppInfo (com.dropbox.core.DbxAppInfo)1 DbxAuthFinish (com.dropbox.core.DbxAuthFinish)1 DbxException (com.dropbox.core.DbxException)1 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 PkceAuthorize (com.dropbox.core.examples.authorize.PkceAuthorize)1 ScopeAuthorize (com.dropbox.core.examples.authorize.ScopeAuthorize)1 ShortLiveTokenAuthorize (com.dropbox.core.examples.authorize.ShortLiveTokenAuthorize)1 JsonReadException (com.dropbox.core.json.JsonReadException)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 AuthError (com.dropbox.core.v2.auth.AuthError)1 FullAccount (com.dropbox.core.v2.users.FullAccount)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1