Skip to content

Remove isLazy property in ParseUser #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ public ParseUser then(Task<ParseUser> task) throws Exception {
if (current != null) {
synchronized (current.mutex) {
current.setIsCurrentUser(true);
current.isLazy = current.getObjectId() == null
&& ParseAnonymousUtils.isLinked(current);
}
return current;
}
Expand All @@ -267,10 +265,10 @@ private ParseUser lazyLogIn() {
}

/* package for tests */ ParseUser lazyLogIn(String authType, Map<String, String> 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);
}

Expand Down
15 changes: 1 addition & 14 deletions Parse/src/main/java/com/parse/ParseUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -190,7 +187,7 @@ boolean isKeyMutable(String key) {
*/
/* package */ boolean isLazy() {
synchronized (mutex) {
return isLazy;
return getObjectId() == null && ParseAnonymousUtils.isLinked(this);
}
}

Expand Down Expand Up @@ -673,9 +670,6 @@ public Task<Void> then(final Task<ParseUser.State> signUpTask) throws Exception
@Override
public Task<Void> then(Task<Void> task) throws Exception {
if (!signUpTask.isCancelled() && !signUpTask.isFaulted()) {
synchronized (mutex) {
isLazy = false;
}
return saveCurrentUserAsync(ParseUser.this);
}
return signUpTask.makeVoid();
Expand Down Expand Up @@ -1302,7 +1296,6 @@ private void logOutWith(String authType) {
@Override
public ParseUser then(Task<Void> task) throws Exception {
synchronized (mutex) {
isLazy = false;
return ParseUser.this;
}
}
Expand Down Expand Up @@ -1348,12 +1341,6 @@ public ParseUser then(Task<Void> 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);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void testLazyLogin() throws Exception {
CachedCurrentUserController controller =
new CachedCurrentUserController(null);

String authType = "test";
String authType = ParseAnonymousUtils.AUTH_TYPE;
Map<String, String> authData = new HashMap<>();
authData.put("sessionToken", "testSessionToken");

Expand All @@ -342,7 +342,7 @@ public void testLazyLogin() throws Exception {
assertTrue(user.isCurrentUser());
Map<String, Map<String, String>> authPair = user.getMap(KEY_AUTH_DATA);
assertEquals(1, authPair.size());
Map<String, String> authDataAgain = authPair.get("test");
Map<String, String> authDataAgain = authPair.get(authType);
assertEquals(1, authDataAgain.size());
assertEquals("testSessionToken", authDataAgain.get("sessionToken"));
// Make sure controller state is correct
Expand Down
27 changes: 18 additions & 9 deletions Parse/src/test/java/com/parse/ParseACLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify this change, it looks like we were testing an illegal scenario originally, since there's no way for a ParseUser to be lazy and have an objectId, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the person who wrote the test thought that unresolvedUser is still unresolved?
It definitely doesn't make sense.

assertEquals(1, acl.getPermissionsById().length());
assertFalse(acl.getPermissionsById().has(UNRESOLVED_KEY));
}
Expand Down Expand Up @@ -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);

Expand All @@ -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));
}
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -622,4 +625,10 @@ public void testUnresolvedUser() throws Exception {
}

//endregion

private static void setLazy(ParseUser user) {
Map<String, String> anonymousAuthData = new HashMap<>();
anonymousAuthData.put("anonymousToken", "anonymousTest");
user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData);
}
}
67 changes: 15 additions & 52 deletions Parse/src/test/java/com/parse/ParseUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>());
currentUser.isLazy = true;
setLazy(currentUser);
ParseUser partialMockCurrentUser = spy(currentUser);
when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken");
doReturn(Task.<ParseUser>forResult(null))
Expand Down Expand Up @@ -414,7 +414,6 @@ public void testLoginWithAsyncWithLinkedLazyUseAndResolveLazinessFailure() throw
Map<String, String> 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.<ParseUser>forError(new Exception()))
Expand Down Expand Up @@ -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<String, String>());
ParseUser partialMockCurrentUser = spy(currentUser);
when(partialMockCurrentUser.getSessionToken()).thenReturn("sessionToken");
Expand Down Expand Up @@ -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<String, String>());
currentUser.setObjectId("objectId"); // Make it not lazy.
ParseUser partialMockCurrentUser = spy(currentUser);
when(partialMockCurrentUser.getSessionToken()).thenReturn("sessionToken");
ParseException linkException =
Expand Down Expand Up @@ -641,30 +642,10 @@ public void testResolveLazinessAsyncWithNotLazyUser() throws Exception {
ParseTaskUtils.wait(user.resolveLazinessAsync(Task.<Void>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.<Void>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<String, String>());
// Register a mock userController to make logIn work
ParseUserController userController = mock(ParseUserController.class);
Expand Down Expand Up @@ -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<String, String>());
// 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)
Expand Down Expand Up @@ -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<String, String>());
// To verify handleSaveResultAsync is not called
user.setPassword("password");
Expand Down Expand Up @@ -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.<ParseUser>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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.<Void>forResult(null))
Expand All @@ -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.<Void>forResult(null))
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -1497,4 +1454,10 @@ public void testRemoveWithUserName() throws Exception {
}

//endregion

private static void setLazy(ParseUser user) {
Map<String, String> anonymousAuthData = new HashMap<>();
anonymousAuthData.put("anonymousToken", "anonymousTest");
user.putAuthData(ParseAnonymousUtils.AUTH_TYPE, anonymousAuthData);
}
}