diff --git a/Parse/src/main/java/com/parse/CachedCurrentUserController.java b/Parse/src/main/java/com/parse/CachedCurrentUserController.java index cb8836355..16adc49d8 100644 --- a/Parse/src/main/java/com/parse/CachedCurrentUserController.java +++ b/Parse/src/main/java/com/parse/CachedCurrentUserController.java @@ -241,8 +241,6 @@ public ParseUser then(Task task) throws Exception { if (current != null) { synchronized (current.mutex) { current.setIsCurrentUser(true); - current.isLazy = current.getObjectId() == null - && ParseAnonymousUtils.isLinked(current); } return current; } @@ -267,10 +265,10 @@ private ParseUser lazyLogIn() { } /* package for tests */ ParseUser lazyLogIn(String authType, Map authData) { + // Note: if authType != ParseAnonymousUtils.AUTH_TYPE the user is not "lazy". ParseUser user = ParseObject.create(ParseUser.class); synchronized (user.mutex) { user.setIsCurrentUser(true); - user.isLazy = true; user.putAuthData(authType, authData); } diff --git a/Parse/src/main/java/com/parse/ParseUser.java b/Parse/src/main/java/com/parse/ParseUser.java index 4f58a66d2..9f9c07f7d 100644 --- a/Parse/src/main/java/com/parse/ParseUser.java +++ b/Parse/src/main/java/com/parse/ParseUser.java @@ -149,8 +149,6 @@ public boolean isNew() { } } - /* package */ boolean isLazy; - // Whether the object is a currentUser. If so, it will always be persisted to disk on updates. private boolean isCurrentUser; @@ -159,7 +157,6 @@ public boolean isNew() { * have an objectId and will not persist to the database until {@link #signUp} is called. */ public ParseUser() { - isLazy = false; isCurrentUser = false; } @@ -190,7 +187,7 @@ boolean isKeyMutable(String key) { */ /* package */ boolean isLazy() { synchronized (mutex) { - return isLazy; + return getObjectId() == null && ParseAnonymousUtils.isLinked(this); } } @@ -673,9 +670,6 @@ public Task then(final Task signUpTask) throws Exception @Override public Task then(Task task) throws Exception { if (!signUpTask.isCancelled() && !signUpTask.isFaulted()) { - synchronized (mutex) { - isLazy = false; - } return saveCurrentUserAsync(ParseUser.this); } return signUpTask.makeVoid(); @@ -1302,7 +1296,6 @@ private void logOutWith(String authType) { @Override public ParseUser then(Task task) throws Exception { synchronized (mutex) { - isLazy = false; return ParseUser.this; } } @@ -1348,12 +1341,6 @@ public ParseUser then(Task task) throws Exception { } }); } - - // Otherwise, merge it into the current user. - // This is in Android, but not iOS because iOS has handleSignUpOrLogInResult. - synchronized (mutex) { - isLazy = false; - } return Task.forResult(ParseUser.this); } }); diff --git a/Parse/src/test/java/com/parse/CachedCurrentUserControllerTest.java b/Parse/src/test/java/com/parse/CachedCurrentUserControllerTest.java index 05bf8008a..fc23b11f1 100644 --- a/Parse/src/test/java/com/parse/CachedCurrentUserControllerTest.java +++ b/Parse/src/test/java/com/parse/CachedCurrentUserControllerTest.java @@ -331,7 +331,7 @@ public void testLazyLogin() throws Exception { CachedCurrentUserController controller = new CachedCurrentUserController(null); - String authType = "test"; + String authType = ParseAnonymousUtils.AUTH_TYPE; Map authData = new HashMap<>(); authData.put("sessionToken", "testSessionToken"); @@ -342,7 +342,7 @@ public void testLazyLogin() throws Exception { assertTrue(user.isCurrentUser()); Map> authPair = user.getMap(KEY_AUTH_DATA); assertEquals(1, authPair.size()); - Map authDataAgain = authPair.get("test"); + Map authDataAgain = authPair.get(authType); assertEquals(1, authDataAgain.size()); assertEquals("testSessionToken", authDataAgain.get("sessionToken")); // Make sure controller state is correct diff --git a/Parse/src/test/java/com/parse/ParseACLTest.java b/Parse/src/test/java/com/parse/ParseACLTest.java index dfea2cb94..f10045dac 100644 --- a/Parse/src/test/java/com/parse/ParseACLTest.java +++ b/Parse/src/test/java/com/parse/ParseACLTest.java @@ -18,6 +18,9 @@ import org.mockito.stubbing.Answer; import org.skyscreamer.jsonassert.JSONCompareMode; +import java.util.HashMap; +import java.util.Map; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; @@ -141,7 +144,7 @@ public void testToJson() throws Exception { ParseACL acl = new ParseACL(); acl.setReadAccess("userId", true); ParseUser unresolvedUser = new ParseUser(); - unresolvedUser.isLazy = true; + setLazy(unresolvedUser); acl.setReadAccess(unresolvedUser, true); // Mock decoder ParseEncoder mockEncoder = mock(ParseEncoder.class); @@ -188,7 +191,7 @@ public void testCreateACLFromJSONObject() throws Exception { @Test public void testResolveUserWithNewUser() throws Exception { ParseUser unresolvedUser = new ParseUser(); - unresolvedUser.isLazy = true; + setLazy(unresolvedUser); ParseACL acl = new ParseACL(); acl.setReadAccess(unresolvedUser, true); @@ -202,7 +205,7 @@ public void testResolveUserWithNewUser() throws Exception { public void testResolveUserWithUnresolvedUser() throws Exception { ParseACL acl = new ParseACL(); ParseUser unresolvedUser = new ParseUser(); - unresolvedUser.isLazy = true; + setLazy(unresolvedUser); // This will set the unresolvedUser in acl acl.setReadAccess(unresolvedUser, true); acl.setWriteAccess(unresolvedUser, true); @@ -211,8 +214,8 @@ public void testResolveUserWithUnresolvedUser() throws Exception { acl.resolveUser(unresolvedUser); assertNull(acl.getUnresolvedUser()); - assertFalse(acl.getReadAccess(unresolvedUser)); - assertFalse(acl.getWriteAccess(unresolvedUser)); + assertTrue(acl.getReadAccess(unresolvedUser)); + assertTrue(acl.getWriteAccess(unresolvedUser)); assertEquals(1, acl.getPermissionsById().length()); assertFalse(acl.getPermissionsById().has(UNRESOLVED_KEY)); } @@ -525,7 +528,7 @@ public void testGetRoleWriteAccess() throws Exception { public void testGetUserReadAccessWithUnresolvedUser() throws Exception { ParseACL acl = new ParseACL(); ParseUser user = new ParseUser(); - user.isLazy = true; + setLazy(user); // Since user is a lazy user, this will set the acl's unresolved user and give it read access acl.setReadAccess(user ,true); @@ -536,7 +539,7 @@ public void testGetUserReadAccessWithUnresolvedUser() throws Exception { public void testGetUserReadAccessWithLazyUser() throws Exception { ParseACL acl = new ParseACL(); ParseUser user = new ParseUser(); - user.isLazy = true; + setLazy(user); assertFalse(acl.getReadAccess(user)); } @@ -563,7 +566,7 @@ public void testGetUserReadAccessWithNormalUser() throws Exception { public void testGetUserWriteAccessWithUnresolvedUser() throws Exception { ParseACL acl = new ParseACL(); ParseUser user = new ParseUser(); - user.isLazy = true; + setLazy(user); // Since user is a lazy user, this will set the acl's unresolved user and give it write access acl.setWriteAccess(user, true); @@ -613,7 +616,7 @@ public void testIsShared() throws Exception { public void testUnresolvedUser() throws Exception { ParseACL acl = new ParseACL(); ParseUser user = new ParseUser(); - user.isLazy = true; + setLazy(user); // This will set unresolvedUser in acl acl.setReadAccess(user, true); @@ -622,4 +625,10 @@ public void testUnresolvedUser() throws Exception { } //endregion + + private static void setLazy(ParseUser user) { + Map anonymousAuthData = new HashMap<>(); + anonymousAuthData.put("anonymousToken", "anonymousTest"); + user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData); + } } diff --git a/Parse/src/test/java/com/parse/ParseUserTest.java b/Parse/src/test/java/com/parse/ParseUserTest.java index d845ac55b..311e743c9 100644 --- a/Parse/src/test/java/com/parse/ParseUserTest.java +++ b/Parse/src/test/java/com/parse/ParseUserTest.java @@ -383,7 +383,7 @@ public void testLoginWithAsyncWithLinkedLazyUser() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap()); - currentUser.isLazy = true; + setLazy(currentUser); ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken"); doReturn(Task.forResult(null)) @@ -414,7 +414,6 @@ public void testLoginWithAsyncWithLinkedLazyUseAndResolveLazinessFailure() throw Map oldAnonymousAuthData = new HashMap<>(); oldAnonymousAuthData.put("oldKey", "oldToken"); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, oldAnonymousAuthData); - currentUser.isLazy = true; ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken"); doReturn(Task.forError(new Exception())) @@ -447,6 +446,7 @@ public void testLoginWithAsyncWithLinkedLazyUseAndResolveLazinessFailure() throw public void testLoginWithAsyncWithLinkedNotLazyUser() throws Exception { // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); + currentUser.setObjectId("objectId"); // Make it not lazy. currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap()); ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("sessionToken"); @@ -483,6 +483,7 @@ public void testLoginWithAsyncWithLinkedNotLazyUserLinkFailure() throws Exceptio // Register a mock currentUserController to make getCurrentUser work ParseUser currentUser = new ParseUser(); currentUser.putAuthData(ParseAnonymousUtils.AUTH_TYPE, new HashMap()); + currentUser.setObjectId("objectId"); // Make it not lazy. ParseUser partialMockCurrentUser = spy(currentUser); when(partialMockCurrentUser.getSessionToken()).thenReturn("sessionToken"); ParseException linkException = @@ -641,30 +642,10 @@ public void testResolveLazinessAsyncWithNotLazyUser() throws Exception { ParseTaskUtils.wait(user.resolveLazinessAsync(Task.forResult(null))); } - @Test - public void testResolveLazinessAsyncWithNoAuthData() throws Exception { - ParseUser user = new ParseUser(); - user.isLazy = true; - ParseUser partialMockUser = spy(user); - doReturn(Task.forResult(null)) - .when(partialMockUser) - .signUpAsync(any(Task.class)); - - ParseUser userAfterResolveLaziness = - ParseTaskUtils.wait(partialMockUser.resolveLazinessAsync(Task.forResult(null))); - - // Make sure we signUp the lazy user - verify(partialMockUser, times(1)).signUpAsync(any(Task.class)); - // Make sure the user is not lazy - assertFalse(userAfterResolveLaziness.isLazy()); - // Make sure we do not create new user - assertSame(partialMockUser, userAfterResolveLaziness); - } - @Test public void testResolveLazinessAsyncWithAuthDataAndNotNewUser() throws Exception { ParseUser user = new ParseUser(); - user.isLazy = true; + setLazy(user); user.putAuthData("facebook", new HashMap()); // Register a mock userController to make logIn work ParseUserController userController = mock(ParseUserController.class); @@ -702,11 +683,12 @@ public void testResolveLazinessAsyncWithAuthDataAndNotNewUser() throws Exception @Test public void testResolveLazinessAsyncWithAuthDataAndNewUser() throws Exception { ParseUser user = new ParseUser(); - user.isLazy = true; + setLazy(user); user.putAuthData("facebook", new HashMap()); // Register a mock userController to make logIn work ParseUserController userController = mock(ParseUserController.class); ParseUser.State newUserState = new ParseUser.State.Builder() + .objectId("objectId") .put("newKey", "newValue") .sessionToken("newSessionToken") .isNew(true) @@ -738,7 +720,7 @@ public void testResolveLazinessAsyncWithAuthDataAndNewUser() throws Exception { @Test public void testResolveLazinessAsyncWithAuthDataAndNotNewUserAndLDSEnabled() throws Exception { ParseUser user = new ParseUser(); - user.isLazy = true; + setLazy(user); user.putAuthData("facebook", new HashMap()); // To verify handleSaveResultAsync is not called user.setPassword("password"); @@ -794,23 +776,6 @@ public void testValidateSaveWithNoObjectId() throws Exception { user.validateSave(); } - @Test - public void testValidateSaveWithIsAuthenticated() throws Exception { - // Register a mock currentUserController to make getCurrentUser work - ParseUser currentUser = new ParseUser(); - ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class); - when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(null)); - ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController); - - ParseUser user = new ParseUser(); - user.setObjectId("test"); - // A lazy user will be an authenticated user. More complicated cases will be covered in - // isAuthenticated() unit test. - user.isLazy = true; - - user.validateSave(); - } - // TODO(mengyan): Add testValidateSaveWithIsAuthenticatedWithNotDirty // TODO(mengyan): Add testValidateSaveWithIsAuthenticatedWithIsCurrentUser @@ -826,8 +791,6 @@ public void testValidateSaveWithLDSNotEnabled() throws Exception { ParseUser user = new ParseUser(); user.setObjectId("test"); - // Make isAuthenticated return false - user.isLazy = false; // Make isDirty return true user.put("key", "value"); // Make isCurrent return false @@ -847,8 +810,6 @@ public void testValidateSaveWithLDSNotEnabledAndCurrentUserNotMatch() throws Exc ParseUser user = new ParseUser(); user.setObjectId("test"); - // Make isAuthenticated return false - user.isLazy = false; // Make isDirty return true user.put("key", "value"); // Make isCurrent return false @@ -874,11 +835,10 @@ public void testSaveAsyncWithLazyAndCurrentUser() throws Exception { // Set facebook authData to null to verify cleanAuthData() ParseUser.State userState = new ParseUser.State.Builder() - .objectId("test") .putAuthData("facebook", null) .build(); ParseUser user = ParseObject.from(userState); - user.isLazy = true; + setLazy(user); user.setIsCurrentUser(true); ParseUser partialMockUser = spy(user); doReturn(Task.forResult(null)) @@ -903,11 +863,10 @@ public void testSaveAsyncWithLazyAndNotCurrentUser() throws Exception { // Set facebook authData to null to verify cleanAuthData() ParseUser.State userState = new ParseUser.State.Builder() - .objectId("test") .putAuthData("facebook", null) .build(); ParseUser user = ParseObject.from(userState); - user.isLazy = true; + setLazy(user); user.setIsCurrentUser(false); ParseUser partialMockUser = spy(user); doReturn(Task.forResult(null)) @@ -1345,8 +1304,6 @@ public void testValidDelete() throws Exception { ParseUser user = new ParseUser(); user.setObjectId("test"); - // Make isAuthenticated return false - user.isLazy = false; // Make isDirty return true user.put("key", "value"); @@ -1497,4 +1454,10 @@ public void testRemoveWithUserName() throws Exception { } //endregion + + private static void setLazy(ParseUser user) { + Map anonymousAuthData = new HashMap<>(); + anonymousAuthData.put("anonymousToken", "anonymousTest"); + user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData); + } }