@Test public void testSetAccessWithPermissionsAndNotAllowed() { ParseACL acl = new ParseACL(); acl.setReadAccess("userId", true); acl.setReadAccess("userId", false); // Make sure we remove the read access assertFalse(acl.getReadAccess("userId")); assertEquals(0, acl.getPermissionsById().size()); }
public ParseACL get() { if (defaultACLUsesCurrentUser && defaultACL != null) { ParseUser currentUser = ParseUser.getCurrentUser(); if (currentUser != null) { // If the currentUser has changed, generate a new ACL from the defaultACL. ParseUser last = lastCurrentUser != null ? lastCurrentUser.get() : null; if (last != currentUser) { ParseACL newDefaultACLWithCurrentUser = defaultACL.copy(); newDefaultACLWithCurrentUser.setShared(true); newDefaultACLWithCurrentUser.setReadAccess(currentUser, true); newDefaultACLWithCurrentUser.setWriteAccess(currentUser, true); defaultACLWithCurrentUser = newDefaultACLWithCurrentUser; lastCurrentUser = new WeakReference<>(currentUser); } return defaultACLWithCurrentUser; } } return defaultACL; }
@Test public void testSetPublicReadAccessAllowed() { ParseACL acl = new ParseACL(); acl.setPublicReadAccess(true); assertTrue(acl.getPublicReadAccess()); }
/** * Set whether the given user id is allowed to write this object. */ public void setWriteAccess(String userId, boolean allowed) { if (userId == null) { throw new IllegalArgumentException("cannot setWriteAccess for null userId"); } boolean readPermission = getReadAccess(userId); setPermissionsIfNonEmpty(userId, readPermission, allowed); }
/** * Set whether the given user id is allowed to read this object. */ public void setReadAccess(String userId, boolean allowed) { if (userId == null) { throw new IllegalArgumentException("cannot setReadAccess for null userId"); } boolean writePermission = getWriteAccess(userId); setPermissionsIfNonEmpty(userId, allowed, writePermission); }
/** * Sets a default ACL that will be applied to all {@link ParseObject}s when they are created. * * @param acl The ACL to use as a template for all {@link ParseObject}s created after set * has been called. This value will be copied and used as a template for the creation of * new ACLs, so changes to the instance after {@code set(ParseACL, boolean)} * has been called will not be reflected in new {@link ParseObject}s. * @param withAccessForCurrentUser If {@code true}, the {@code ParseACL} that is applied to newly-created * {@link ParseObject}s will provide read and write access to the * {@link ParseUser#getCurrentUser()} at the time of creation. If {@code false}, the * provided ACL will be used without modification. If acl is {@code null}, this value is * ignored. */ public void set(ParseACL acl, boolean withAccessForCurrentUser) { defaultACLWithCurrentUser = null; lastCurrentUser = null; if (acl != null) { ParseACL newDefaultACL = acl.copy(); newDefaultACL.setShared(true); defaultACL = newDefaultACL; defaultACLUsesCurrentUser = withAccessForCurrentUser; } else { defaultACL = null; } }
@Test public void testParcelable() { ParseACL acl = new ParseACL(); acl.setReadAccess("userId", true); ParseUser user = new ParseUser(); user.setObjectId("userId2"); acl.setReadAccess(user, true); acl.setRoleWriteAccess("role", true); acl.setShared(true); Parcel parcel = Parcel.obtain(); acl.writeToParcel(parcel, 0); parcel.setDataPosition(0); acl = ParseACL.CREATOR.createFromParcel(parcel); assertTrue(acl.getReadAccess("userId")); assertTrue(acl.getReadAccess(user)); assertTrue(acl.getRoleWriteAccess("role")); assertTrue(acl.isShared()); assertFalse(acl.getPublicReadAccess()); assertFalse(acl.getPublicWriteAccess()); }
@Test public void testCopy() { ParseACL acl = new ParseACL(); final ParseUser unresolvedUser = mock(ParseUser.class); when(unresolvedUser.isLazy()).thenReturn(true); // This will set unresolvedUser and permissionsById acl.setReadAccess(unresolvedUser, true); acl.setWriteAccess(unresolvedUser, true); // We need to reset unresolvedUser since registerSaveListener will be triggered once in // setReadAccess() reset(unresolvedUser); ParseACL copiedACL = new ParseACL(acl); assertEquals(1, copiedACL.getPermissionsById().size()); assertTrue(copiedACL.getPermissionsById().containsKey(UNRESOLVED_KEY)); assertTrue(copiedACL.getReadAccess(unresolvedUser)); assertTrue(copiedACL.getWriteAccess(unresolvedUser)); assertFalse(copiedACL.isShared()); assertSame(unresolvedUser, copiedACL.getUnresolvedUser()); verify(unresolvedUser, times(1)).registerSaveListener(any(GetCallback.class)); }
@Test public void testResolveUserWithUnresolvedUser() { ParseACL acl = new ParseACL(); ParseUser unresolvedUser = new ParseUser(); setLazy(unresolvedUser); // This will set the unresolvedUser in acl acl.setReadAccess(unresolvedUser, true); acl.setWriteAccess(unresolvedUser, true); unresolvedUser.setObjectId("test"); acl.resolveUser(unresolvedUser); assertNull(acl.getUnresolvedUser()); assertTrue(acl.getReadAccess(unresolvedUser)); assertTrue(acl.getWriteAccess(unresolvedUser)); assertEquals(1, acl.getPermissionsById().size()); assertFalse(acl.getPermissionsById().containsKey(UNRESOLVED_KEY)); }
@Test public void testSetUserWriteAccessWithLazyUser() { ParseUser user = mock(ParseUser.class); when(user.isLazy()).thenReturn(true); ParseACL acl = new ParseACL(); acl.setWriteAccess(user, true); assertSame(user, acl.getUnresolvedUser()); verify(user, times(1)).registerSaveListener(any(GetCallback.class)); assertTrue(acl.getPermissionsById().containsKey(UNRESOLVED_KEY)); assertTrue(acl.getWriteAccess(user)); assertEquals(1, acl.getPermissionsById().size()); } //endregion
@Test public void testGetAccessWithPermission() { ParseACL acl = new ParseACL(); acl.setReadAccess("userId", true); assertTrue(acl.getReadAccess("userId")); }
@Test public void testGetACLWithSharedACL() { ParseObject object = new ParseObject("Test"); ParseACL acl = new ParseACL(); acl.setShared(true); acl.setPublicReadAccess(true); object.put("ACL", acl); ParseACL aclAgain = object.getACL(); assertTrue(aclAgain.getPublicReadAccess()); }
@Test(expected = IllegalArgumentException.class) public void testGetReadAccessWithNullUserId() { ParseACL acl = new ParseACL(); String userId = null; acl.getReadAccess(userId); }
@Test public void testResolveUserWithNewUser() { ParseUser unresolvedUser = new ParseUser(); setLazy(unresolvedUser); ParseACL acl = new ParseACL(); acl.setReadAccess(unresolvedUser, true); ParseUser other = new ParseUser(); // local id creation fails if we don't have Android environment unresolvedUser.localId = "someId"; other.localId = "someOtherId"; acl.resolveUser(other); // Make sure unresolvedUser is not changed assertSame(unresolvedUser, acl.getUnresolvedUser()); }
@Test public void testCreateACLFromJSONObject() throws Exception { JSONObject aclJson = new JSONObject(); JSONObject permission = new JSONObject(); permission.put(READ_PERMISSION, true); permission.put(WRITE_PERMISSION, true); aclJson.put("userId", permission); ParseUser unresolvedUser = new ParseUser(); JSONObject unresolvedUserJson = new JSONObject(); aclJson.put("unresolvedUser", unresolvedUserJson); // Mock decoder ParseDecoder mockDecoder = mock(ParseDecoder.class); when(mockDecoder.decode(eq(unresolvedUserJson))).thenReturn(unresolvedUser); ParseACL acl = ParseACL.createACLFromJSONObject(aclJson, mockDecoder); assertSame(unresolvedUser, acl.getUnresolvedUser()); assertTrue(acl.getReadAccess("userId")); assertTrue(acl.getWriteAccess("userId")); assertEquals(1, acl.getPermissionsById().size()); }
@Test public void testGetUserWriteAccessWithUnresolvedUser() { ParseACL acl = new ParseACL(); ParseUser user = new ParseUser(); 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); assertTrue(acl.getWriteAccess(user)); }
@Test public void testGetPublicReadAccess() { ParseACL acl = new ParseACL(); acl.setPublicWriteAccess(true); assertTrue(acl.getPublicWriteAccess()); }
@Test public void testParcelableWithUnresolvedUser() { ParseFieldOperations.registerDefaultDecoders(); // Needed for unparceling ParseObjects ParseACL acl = new ParseACL(); ParseUser unresolved = new ParseUser(); setLazy(unresolved); acl.setReadAccess(unresolved, true); // unresolved users need a local id when parcelling and unparcelling. // Since we don't have an Android environment, local id creation will fail. unresolved.localId = "localId"; Parcel parcel = Parcel.obtain(); acl.writeToParcel(parcel, 0); parcel.setDataPosition(0); // Do not user ParseObjectParcelDecoder because it requires local ids acl = new ParseACL(parcel, new ParseParcelDecoder()); assertTrue(acl.getReadAccess(unresolved)); }
/* package for tests */ ParseACL copy() { return new ParseACL(this); }
@Test public void testSetPublicWriteAccessAllowed() { ParseACL acl = new ParseACL(); acl.setPublicWriteAccess(true); assertTrue(acl.getPublicWriteAccess()); assertEquals(1, acl.getPermissionsById().size()); }