use of org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.
the class FlickrUtils method getAuth.
public static Auth getAuth(AuthData authData, Flickr flickr) throws FlickrException {
checkArgument(authData instanceof TokenSecretAuthData, "authData expected to be TokenSecretAuthData not %s", authData.getClass().getCanonicalName());
TokenSecretAuthData tokenAuthData = (TokenSecretAuthData) authData;
Token requestToken = new Token(tokenAuthData.getToken(), tokenAuthData.getSecret());
return flickr.getAuthInterface().checkToken(requestToken);
}
use of org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.
the class FlickrPhotosExporterTest method exportAlbumInitial.
@Test
public void exportAlbumInitial() throws FlickrException {
// set up auth, flickr service
when(user.getId()).thenReturn("userId");
when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
when(flickr.getPhotosInterface()).thenReturn(photosInterface);
when(flickr.getAuthInterface()).thenReturn(authInterface);
// setup photoset
Photoset photoset = FlickrTestUtils.initializePhotoset("photosetId", "title", "description");
// setup photoset list (aka album view)
int page = 1;
Photosets photosetsList = new Photosets();
photosetsList.setPage(page);
photosetsList.setPages(page + 1);
photosetsList.setPhotosets(Collections.singletonList(photoset));
when(photosetsInterface.getList(anyString(), anyInt(), anyInt(), anyString())).thenReturn(photosetsList);
// run test
FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr);
AuthData authData = new TokenSecretAuthData("token", "secret");
ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), authData);
// make sure album and photo information is correct
assertThat(result.getExportedData().getPhotos()).isEmpty();
Collection<PhotoAlbum> albums = result.getExportedData().getAlbums();
assertThat(albums.size()).isEqualTo(1);
assertThat(albums).containsExactly(new PhotoAlbum("photosetId", "title", "description"));
// check continuation information
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
assertThat(continuationData.getPaginationData()).isInstanceOf(IntPaginationToken.class);
assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
Collection<? extends ContainerResource> subResources = continuationData.getContainerResources();
assertThat(subResources.size()).isEqualTo(1);
assertThat(subResources).containsExactly(new IdOnlyContainerResource("photosetId"));
}
use of org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.
the class FlickrPhotosExporterTest method exportPhotosFromPhotoset.
@Test
public void exportPhotosFromPhotoset() throws FlickrException {
// set up auth, flickr service
when(user.getId()).thenReturn("userId");
when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
when(flickr.getPhotosInterface()).thenReturn(photosInterface);
when(flickr.getAuthInterface()).thenReturn(authInterface);
// getting photos from a set with id photosetsId and page 1
int page = 1;
String photosetsId = "photosetsId";
ExportInformation exportInformation = new ExportInformation(null, new IdOnlyContainerResource(photosetsId));
// make lots of photos and add them to PhotoList (also adding pagination information)
int numPhotos = 4;
PhotoList<Photo> photosList = new PhotoList<>();
for (int i = 0; i < numPhotos; i++) {
photosList.add(FlickrTestUtils.initializePhoto("title" + 1, "url" + i, "description" + i, MEDIA_TYPE));
}
photosList.setPage(page);
photosList.setPages(page + 1);
when(photosetsInterface.getPhotos(anyString(), anySet(), anyInt(), anyInt(), anyInt())).thenReturn(photosList);
// run test
FlickrPhotosExporter exporter = new FlickrPhotosExporter(flickr);
ExportResult<PhotosContainerResource> result = exporter.export(UUID.randomUUID(), new TokenSecretAuthData("token", "secret"), exportInformation);
assertThat(result.getExportedData().getPhotos().size()).isEqualTo(numPhotos);
assertThat(result.getExportedData().getAlbums()).isEmpty();
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
assertThat(continuationData.getContainerResources()).isEmpty();
assertThat(((IntPaginationToken) continuationData.getPaginationData()).getStart()).isEqualTo(page + 1);
}
use of org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.
the class FlickrPhotosImporterTest method importStoresAlbumInJobStore.
@Test
public void importStoresAlbumInJobStore() throws FlickrException, IOException {
UUID jobId = UUID.randomUUID();
PhotosContainerResource photosContainerResource = new PhotosContainerResource(Collections.singletonList(PHOTO_ALBUM), Collections.singletonList(PHOTO_MODEL));
// Setup Mock
when(user.getId()).thenReturn("userId");
when(authInterface.checkToken(any(Token.class))).thenReturn(auth);
when(flickr.getPhotosetsInterface()).thenReturn(photosetsInterface);
when(flickr.getUploader()).thenReturn(uploader);
when(flickr.getAuthInterface()).thenReturn(authInterface);
when(imageStreamProvider.get(FETCHABLE_URL)).thenReturn(bufferedInputStream);
when(uploader.upload(any(BufferedInputStream.class), any(UploadMetaData.class))).thenReturn(FLICKR_PHOTO_ID);
String flickrAlbumTitle = FlickrPhotosImporter.COPY_PREFIX + ALBUM_NAME;
Photoset photoset = FlickrTestUtils.initializePhotoset(FLICKR_ALBUM_ID, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
when(photosetsInterface.create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID)).thenReturn(photoset);
// Run test
FlickrPhotosImporter importer = new FlickrPhotosImporter(flickr, jobStore, imageStreamProvider);
ImportResult result = importer.importItem(jobId, new TokenSecretAuthData("token", "secret"), photosContainerResource);
// Verify that the image stream provider got the correct URL and that the correct info was uploaded
verify(imageStreamProvider).get(FETCHABLE_URL);
ArgumentCaptor<UploadMetaData> uploadMetaDataArgumentCaptor = ArgumentCaptor.forClass(UploadMetaData.class);
verify(uploader).upload(eq(bufferedInputStream), uploadMetaDataArgumentCaptor.capture());
UploadMetaData actualUploadMetaData = uploadMetaDataArgumentCaptor.getValue();
assertThat(actualUploadMetaData.getTitle()).isEqualTo(FlickrPhotosImporter.COPY_PREFIX + PHOTO_TITLE);
assertThat(actualUploadMetaData.getDescription()).isEqualTo(PHOTO_DESCRIPTION);
// Verify the photosets interface got the command to create the correct album
verify(photosetsInterface).create(flickrAlbumTitle, ALBUM_DESCRIPTION, FLICKR_PHOTO_ID);
// Check contents of JobStore
TempPhotosData tempPhotosData = jobStore.findData(TempPhotosData.class, jobId);
assertThat(tempPhotosData).isNotNull();
String expectedAlbumKey = FlickrPhotosImporter.CACHE_ALBUM_METADATA_PREFIX + ALBUM_ID;
assertThat(tempPhotosData.lookupAlbum(expectedAlbumKey)).isNotNull();
assertThat(tempPhotosData.lookupAlbum(expectedAlbumKey)).isEqualTo(PHOTO_ALBUM);
assertThat(tempPhotosData.lookupNewAlbumId(ALBUM_ID)).isEqualTo(FLICKR_ALBUM_ID);
}
use of org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData in project data-transfer-project by google.
the class FlickrAuthDataGenerator method generateAuthData.
@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected");
Preconditions.checkNotNull(initialAuthData, "Earlier auth data not expected for Flickr flow");
AuthInterface authInterface = flickr.getAuthInterface();
Token token = fromAuthData(initialAuthData);
Token requestToken = authInterface.getAccessToken(token, new Verifier(authCode));
try {
authInterface.checkToken(requestToken);
} catch (FlickrException e) {
logger.warn("Problem verifying auth token {}", e);
return null;
}
return new TokenSecretAuthData(requestToken.getToken(), requestToken.getSecret());
}
Aggregations