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());
}
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());
}
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());
}
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());
}
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");
}
Aggregations