@Test public void testLoginWithAsyncWithLinkedNotLazyUser() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser.State state = new ParseUser.State.Builder() .objectId("objectId") // Make it not lazy .putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap<String, String>()) .build(); ParseUser currentUser = ParseUser.from(state); ParseUser partialMockCurrentUser = spy(currentUser); // ParseUser.mutex doReturn(Task.<Void>forResult(null)) .when(partialMockCurrentUser) .linkWithInBackground(anyString(), Matchers.<Map<String, String>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync()).thenReturn(Task.forResult(partialMockCurrentUser)); when(currentUserController.getAsync(anyBoolean())) .thenReturn(Task.forResult(partialMockCurrentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithInBackground(authType, authData)); // Make sure we link authData verify(partialMockCurrentUser, times(1)).linkWithInBackground(authType, authData); assertSame(partialMockCurrentUser, userAfterLogin); }
authData.put("token", "test"); ParseTaskUtils.wait(partialMockUser.linkWithInBackground("facebook", authData));
partialMockUser.linkWithInBackground(authType, authData); linkTask.waitForCompletion();
doReturn(Task.<Void>forError(linkException)) .when(partialMockCurrentUser) .linkWithInBackground(anyString(), Matchers.<Map<String, String>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.forResult(partialMockCurrentUser)); verify(partialMockCurrentUser, times(1)).linkWithInBackground(authType, authData);
return user.linkWithInBackground(authType, authData) .continueWithTask(new Continuation<Void, Task<ParseUser>>() { @Override
@Override public Task<Void> then(Task<Map<String, String>> task) throws Exception { return user.linkWithInBackground(AUTH_TYPE, task.getResult()); } });
@Override public Task<Void> then(Task<Map<String, String>> task) throws Exception { return user.linkWithInBackground(provider.getAuthType(), task.getResult()); } });
@Override public Task<Void> then(Task<Map<String, String>> task) throws Exception { Map<String, String> authData = task.getResult(); return user.linkWithInBackground(provider.getAuthType(), authData); } });
/** * Link an existing Parse user with a Facebook account using authorization credentials that have * already been obtained. * * @param user The Parse user to link with. * @param accessToken Authorization credentials of a Facebook user. * @return A task that will be resolved when linking is complete. */ public static Task<Void> linkInBackground(ParseUser user, AccessToken accessToken) { checkInitialization(); return user.linkWithInBackground(AUTH_TYPE, getController().getAuthData(accessToken)); }
/** * Links a ParseUser to a Facebook account, allowing you to use Facebook for authentication, and * providing access to Facebook data for the user. This method allows you to handle getting access * tokens for the user yourself, rather than delegating to the Facebook SDK. * * @param user * The user to link to a Facebook account. * @param facebookId * The facebook ID of the user being linked. * @param accessToken * The access token for the user. * @param expirationDate * The expiration date of the access token. * @return A task that will be resolved when linking is completed. */ public static Task<Void> linkInBackground(ParseUser user, String facebookId, String accessToken, Date expirationDate) { checkInitialization(); return user.linkWithInBackground( provider.getAuthType(), provider.getAuthData(facebookId, accessToken, expirationDate)); }
@NonNull public static Completable linkWith(@NonNull final ParseUser user, @NonNull final String authType, @NonNull final Map<String, String> authData) { return RxTask.completable(() -> user.linkWithInBackground(authType, authData)); }
@Test public void testLinkWithAccessToken() { Map<String, String> authData = new HashMap<>(); when(controller.getAuthData(any(AccessToken.class))).thenReturn(authData); ParseFacebookUtils.isInitialized = true; ParseUser user = mock(ParseUser.class); when(user.linkWithInBackground(anyString(), anyMapOf(String.class, String.class))) .thenReturn(Task.<Void>forResult(null)); AccessToken token = TestUtils.newAccessToken(); Task<Void> task = ParseFacebookUtils.linkInBackground(user, token); verify(controller).getAuthData(token); verify(user).linkWithInBackground("facebook", authData); assertTrue(task.isCompleted()); }
when(user.linkWithInBackground(anyString(), anyMapOf(String.class, String.class))) .thenReturn(Task.<Void>forResult(null)); Task<Void> task; verify(user).linkWithInBackground("facebook", authData); assertTrue(task.isCompleted());