/* package for tests */ Task<Void> saveAsync(String sessionToken, boolean isLazy, Task<Void> toAwait) { Task<Void> task; if (isLazy) { task = resolveLazinessAsync(toAwait); } else { task = super.saveAsync(sessionToken, toAwait); } if (isCurrentUser()) { // If the user is the currently logged in user, we persist all data to disk return task.onSuccessTask(new Continuation<Void, Task<Void>>() { @Override public Task<Void> then(Task<Void> task) { return cleanUpAuthDataAsync(); } }).onSuccessTask(new Continuation<Void, Task<Void>>() { @Override public Task<Void> then(Task<Void> task) { return saveCurrentUserAsync(ParseUser.this); } }); } return task; }
@Test public void testLoginWithAsyncWithLinkedLazyUser() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap<String, String>()); setLazy(currentUser); ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken"); doReturn(Task.<ParseUser>forResult(null)) .when(partialMockCurrentUser) .resolveLazinessAsync(Matchers.<Task<Void>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).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 stripAnonymity assertNull(userAfterLogin.getAuthData().get(ParseAnonymousUtils.AUTH_TYPE)); // Make sure we update authData assertEquals(authData, userAfterLogin.getAuthData().get("facebook")); // Make sure we resolveLaziness verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(Matchers.<Task<Void>>any()); }
@Test public void testLoginWithAsyncWithLinkedLazyUseAndResolveLazinessFailure() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); Map<String, String> oldAnonymousAuthData = new HashMap<>(); oldAnonymousAuthData.put("oldKey", "oldToken"); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, oldAnonymousAuthData); ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken"); doReturn(Task.<ParseUser>forError(new Exception())) .when(partialMockCurrentUser) .resolveLazinessAsync(Matchers.<Task<Void>>any()); ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.getAsync(false)).thenReturn(Task.forResult(partialMockCurrentUser)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); String authType = "facebook"; Map<String, String> authData = new HashMap<>(); authData.put("token", "123"); Task<ParseUser> loginTask = ParseUser.logInWithInBackground(authType, authData); loginTask.waitForCompletion(); // Make sure we try to resolveLaziness verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(Matchers.<Task<Void>>any()); // Make sure we do not save new authData assertNull(partialMockCurrentUser.getAuthData().get("facebook")); // Make sure we restore anonymity after resolve laziness failure assertEquals(oldAnonymousAuthData, partialMockCurrentUser.getAuthData() .get(ParseAnonymousUtils.AUTH_TYPE)); // Make sure task fails assertTrue(loginTask.isFaulted()); }
@Test public void testSaveAsyncWithLazyAndCurrentUser() throws Exception { // Register a mock currentUserController to make setCurrentUser work ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.setAsync(any(ParseUser.class))) .thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); // Set facebook authData to null to verify cleanAuthData() ParseUser.State userState = new ParseUser.State.Builder() .putAuthData("facebook", null) .build(); ParseUser user = ParseObject.from(userState); setLazy(user); user.setIsCurrentUser(true); ParseUser partialMockUser = spy(user); doReturn(Task.<Void>forResult(null)) .when(partialMockUser) .resolveLazinessAsync(Matchers.<Task<Void>>any()); ParseTaskUtils.wait(partialMockUser.saveAsync("sessionToken", Task.<Void>forResult(null))); // Make sure we clean authData assertFalse(partialMockUser.getAuthData().containsKey("facebook")); // Make sure we save new currentUser verify(currentUserController, times(1)).setAsync(partialMockUser); }
@Test public void testSaveAsyncWithLazyAndNotCurrentUser() throws Exception { // Register a mock currentUserController to make setCurrentUser work ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); when(currentUserController.setAsync(any(ParseUser.class))) .thenReturn(Task.<Void>forResult(null)); ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); // Set facebook authData to null to verify cleanAuthData() ParseUser.State userState = new ParseUser.State.Builder() .putAuthData("facebook", null) .build(); ParseUser user = ParseObject.from(userState); setLazy(user); user.setIsCurrentUser(false); ParseUser partialMockUser = spy(user); doReturn(Task.<Void>forResult(null)) .when(partialMockUser) .resolveLazinessAsync(Matchers.<Task<Void>>any()); ParseTaskUtils.wait(partialMockUser.saveAsync("sessionToken", Task.<Void>forResult(null))); // Make sure we do not clean authData assertTrue(partialMockUser.getAuthData().containsKey("facebook")); // Make sure we do not save new currentUser verify(currentUserController, never()).setAsync(partialMockUser); }
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseTaskUtils.wait(user.resolveLazinessAsync(Task.<Void>forResult(null))); ArgumentCaptor<ParseUser> userAfterResolveLazinessCaptor = ArgumentCaptor.forClass(ParseUser.class);
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); ParseTaskUtils.wait(user.resolveLazinessAsync(Task.<Void>forResult(null)));
ParseTaskUtils.wait(user.resolveLazinessAsync(Task.<Void>forResult(null))); ArgumentCaptor<ParseUser> userAfterResolveLazinessCaptor = ArgumentCaptor.forClass(ParseUser.class);