Skip to content

Commit

Permalink
feat: Add support for setting Parse.ACL from json (#2097)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis committed Mar 31, 2024
1 parent 3c2303c commit 72bc9ac
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
14 changes: 14 additions & 0 deletions integration/test/ParseACLTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ describe('Parse.ACL', () => {
assert(o);
});

it('can set ACL from json', async () => {
Parse.User.enableUnsafeCurrentUser();
const user = new Parse.User();
const object = new TestObject();
user.set('username', 'torn');
user.set('password', 'acl');
await user.signUp();
const acl = new Parse.ACL(user);
object.setACL(acl);
const json = object.toJSON();
await object.save(json);
assert.equal(acl.equals(object.getACL()), true);
});

it('disables public get access', async () => {
const user = new Parse.User();
const object = new TestObject();
Expand Down
43 changes: 43 additions & 0 deletions integration/test/ParseEventuallyQueueTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,49 @@ describe('Parse EventuallyQueue', () => {
assert.strictEqual(results.length, 1);
});

it('can saveEventually on object with ACL', async () => {
Parse.User.enableUnsafeCurrentUser();
const parseServer = await reconfigureServer();
const user = new Parse.User();
user.set('username', 'torn');
user.set('password', 'acl');
await user.signUp();

const acl = new Parse.ACL(user);
const object = new TestObject({ hash: 'saveSecret' });
object.setACL(acl);

await new Promise((resolve) => parseServer.server.close(resolve));

await object.saveEventually();

let length = await Parse.EventuallyQueue.length();
assert(Parse.EventuallyQueue.isPolling());
assert.strictEqual(length, 1);

await reconfigureServer({});

while (Parse.EventuallyQueue.isPolling()) {
await sleep(100);
}
assert.strictEqual(Parse.EventuallyQueue.isPolling(), false);

length = await Parse.EventuallyQueue.length();
while (length) {
await sleep(100);
}
length = await Parse.EventuallyQueue.length();
assert.strictEqual(length, 0);

const query = new Parse.Query('TestObject');
query.equalTo('hash', 'saveSecret');
let results = await query.find();
while (results.length === 0) {
results = await query.find();
}
assert.strictEqual(results.length, 1);
});

it('can destroyEventually', async () => {
const parseServer = await reconfigureServer();
const object = new TestObject({ hash: 'deleteSecret' });
Expand Down
14 changes: 8 additions & 6 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,15 +1308,17 @@ class ParseObject {
options = arg3;
}

options = options || {};
if (attrs) {
const validation = this.validate(attrs);
if (validation) {
return Promise.reject(validation);
let validationError;
options.error = (_, validation) => {
validationError = validation;
};
const success = this.set(attrs, options);
if (!success) {
return Promise.reject(validationError);
}
this.set(attrs, options);
}

options = options || {};
const saveOptions = {};
if (options.hasOwnProperty('useMasterKey')) {
saveOptions.useMasterKey = !!options.useMasterKey;
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ describe('ParseObject', () => {
expect(o.getACL()).toEqual(ACL);
});

it('encodes ACL from json', () => {
const ACL = new ParseACL({ user1: { read: true } });
const o = new ParseObject('Item');
o.set({ ACL: ACL.toJSON() });
expect(o.getACL()).toEqual(ACL);
});

it('can be rendered to JSON', () => {
let o = new ParseObject('Item');
o.set({
Expand Down

0 comments on commit 72bc9ac

Please sign in to comment.