Skip to content

feat: Add support for setting Parse.ACL from JSON #2097

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 6 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 18 additions & 2 deletions integration/test/ParseACLTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ describe('Parse.ACL', () => {
Parse.User.enableUnsafeCurrentUser();
});

it('acl must be valid', () => {
it('can handle invalid acl', () => {
const user = new Parse.User();
assert.equal(user.setACL(`Ceci n'est pas un ACL.`), false);
user.setACL(`Ceci n'est pas un ACL.`)
assert.equal(user.getACL(), null);
assert.equal(user.get('ACL'), null);
});

it('can refresh object with acl', async () => {
Expand All @@ -27,6 +29,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
7 changes: 4 additions & 3 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ class ParseObject {
* @returns {*}
*/
get(attr: string): mixed {
if (attr === 'ACL') {
const acl = this.attributes[attr];
return acl instanceof ParseACL ? acl : null;
}
return this.attributes[attr];
}

Expand Down Expand Up @@ -1043,9 +1047,6 @@ class ParseObject {
* @see Parse.Object#set
*/
validate(attrs: AttributeMap): ParseError | boolean {
if (attrs.hasOwnProperty('ACL') && !(attrs.ACL instanceof ParseACL)) {
return new ParseError(ParseError.OTHER_CAUSE, 'ACL must be a Parse ACL.');
}
for (const key in attrs) {
if (!/^[A-Za-z][0-9A-Za-z_.]*$/.test(key)) {
return new ParseError(ParseError.INVALID_KEY_NAME);
Expand Down
49 changes: 26 additions & 23 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 Expand Up @@ -869,12 +876,6 @@ describe('ParseObject', () => {

it('can validate attributes', () => {
const o = new ParseObject('Listing');
expect(
o.validate({
ACL: 'not an acl',
})
).toEqual(new ParseError(ParseError.OTHER_CAUSE, 'ACL must be a Parse ACL.'));

expect(
o.validate({
'invalid!key': 12,
Expand All @@ -896,8 +897,6 @@ describe('ParseObject', () => {

it('validates attributes on set()', () => {
const o = new ParseObject('Listing');
expect(o.set('ACL', 'not an acl')).toBe(false);
expect(o.set('ACL', { '*': { read: true, write: false } })).toBe(o);
expect(o.set('$$$', 'o_O')).toBe(false);

o.set('$$$', 'o_O', {
Expand All @@ -908,6 +907,16 @@ describe('ParseObject', () => {
});
});

it('validates attributes on save()', async () => {
const o = new ParseObject('Listing');
try {
await o.save({ '$$$': 'o_O' });
expect(true).toBe(false);
} catch (e) {
expect(e.code).toBe(ParseError.INVALID_KEY_NAME);
}
});

it('ignores validation if ignoreValidation option is passed to set()', () => {
const o = new ParseObject('Listing');
expect(o.set('$$$', 'o_O', { ignoreValidation: true })).toBe(o);
Expand Down Expand Up @@ -1616,27 +1625,21 @@ describe('ParseObject', () => {
});
});

it('accepts attribute changes on save', done => {
it('accepts attribute changes on save', async () => {
CoreManager.getRESTController()._setXHR(
mockXHR([
{
status: 200,
response: { objectId: 'newattributes' },
},
{ status: 200, response: { objectId: 'newattributes' } },
{ status: 200, response: { objectId: 'newattributes' } },
])
);
let o = new ParseObject('Item');
o.save({ key: 'value' })
.then(() => {
expect(o.get('key')).toBe('value');
await o.save({ key: 'value' })
expect(o.get('key')).toBe('value');

o = new ParseObject('Item');
return o.save({ ACL: 'not an acl' });
})
.then(null, error => {
expect(error.code).toBe(-1);
done();
});
o = new ParseObject('Item');
await o.save({ ACL: 'not an acl' });
expect(o.getACL()).toBe(null);
expect(o.get('ACL')).toBe(null);
});

it('accepts context on save', async () => {
Expand Down