Files
kami-parse-server/spec/RestQuery.spec.js

269 lines
8.1 KiB
JavaScript
Raw Normal View History

'use strict'
2016-01-28 10:58:12 -08:00
// These tests check the "find" functionality of the REST API.
const auth = require('../src/Auth');
const Config = require('../src/Config');
const rest = require('../src/rest');
2016-01-28 10:58:12 -08:00
const querystring = require('querystring');
const rp = require('request-promise');
let config;
let database;
const nobody = auth.nobody(config);
2016-01-28 10:58:12 -08:00
describe('rest query', () => {
beforeEach(() => {
config = Config.get('test');
database = config.database;
});
2016-01-28 10:58:12 -08:00
it('basic query', (done) => {
rest.create(config, nobody, 'TestObject', {}).then(() => {
return rest.find(config, nobody, 'TestObject', {});
}).then((response) => {
expect(response.results.length).toEqual(1);
done();
});
});
it('query with limit', (done) => {
rest.create(config, nobody, 'TestObject', {foo: 'baz'}
).then(() => {
return rest.create(config, nobody,
'TestObject', {foo: 'qux'});
2016-01-28 10:58:12 -08:00
}).then(() => {
return rest.find(config, nobody,
'TestObject', {}, {limit: 1});
2016-01-28 10:58:12 -08:00
}).then((response) => {
expect(response.results.length).toEqual(1);
expect(response.results[0].foo).toBeTruthy();
done();
});
});
const data = {
username: 'blah',
password: 'pass',
sessionToken: 'abc123',
}
it_exclude_dbs(['postgres'])('query for user w/ legacy credentials without masterKey has them stripped from results', done => {
database.create('_User', data).then(() => {
return rest.find(config, nobody, '_User')
}).then((result) => {
const user = result.results[0];
expect(user.username).toEqual('blah');
expect(user.sessionToken).toBeUndefined();
expect(user.password).toBeUndefined();
done();
});
});
it_exclude_dbs(['postgres'])('query for user w/ legacy credentials with masterKey has them stripped from results', done => {
database.create('_User', data).then(() => {
return rest.find(config, {isMaster: true}, '_User')
}).then((result) => {
const user = result.results[0];
expect(user.username).toEqual('blah');
expect(user.sessionToken).toBeUndefined();
expect(user.password).toBeUndefined();
done();
});
});
2016-01-28 10:58:12 -08:00
// Created to test a scenario in AnyPic
it_exclude_dbs(['postgres'])('query with include', (done) => {
let photo = {
2016-01-28 10:58:12 -08:00
foo: 'bar'
};
let user = {
2016-01-28 10:58:12 -08:00
username: 'aUsername',
password: 'aPassword'
};
const activity = {
2016-01-28 10:58:12 -08:00
type: 'comment',
photo: {
__type: 'Pointer',
className: 'TestPhoto',
objectId: ''
},
fromUser: {
__type: 'Pointer',
className: '_User',
objectId: ''
}
};
const queryWhere = {
2016-01-28 10:58:12 -08:00
photo: {
__type: 'Pointer',
className: 'TestPhoto',
objectId: ''
},
type: 'comment'
};
const queryOptions = {
2016-01-28 10:58:12 -08:00
include: 'fromUser',
order: 'createdAt',
limit: 30
};
rest.create(config, nobody, 'TestPhoto', photo
).then((p) => {
photo = p;
return rest.create(config, nobody, '_User', user);
}).then((u) => {
user = u.response;
activity.photo.objectId = photo.objectId;
activity.fromUser.objectId = user.objectId;
return rest.create(config, nobody,
'TestActivity', activity);
2016-01-28 10:58:12 -08:00
}).then(() => {
queryWhere.photo.objectId = photo.objectId;
return rest.find(config, nobody,
'TestActivity', queryWhere, queryOptions);
2016-01-28 10:58:12 -08:00
}).then((response) => {
const results = response.results;
2016-01-28 10:58:12 -08:00
expect(results.length).toEqual(1);
expect(typeof results[0].objectId).toEqual('string');
expect(typeof results[0].photo).toEqual('object');
expect(typeof results[0].fromUser).toEqual('object');
expect(typeof results[0].fromUser.username).toEqual('string');
done();
}).catch((error) => { console.log(error); });
});
Advancements with postgres (#2510) * Start DB runner from tests * Connect GridstoreAdapter only when needed * removes unused package * better test errors reporting * Adds support for __op.Delete * Better test error reporting * Makes sure all tests can run without crashing * Use xdescribe to skip test suite * Removes unused dependencies * Let volatiles classes be created with PG on start * Do not fail if class dont exist * adds index.spec.js to the pg suite * Use a new config each test to prevent side effects * Enable EmailVerificationToken specs with pg * Makes sure failure output is not cut * Reduces number of ignored tests in ParseObject.spec * Inspect reconfiguration errors * Mark GlobalConfig is incompatible with PG - Problem is with nested updates (param.prop = value) * PG: Nested JSON queries and updates - Adds support for nested json and . operator queries - Adds debug support for PG adapter - Adds loglevel support in helper * Enable working specs in ParseUser * Sets default logLevel in tests to undefined * Adds File type support, retores purchaseValidation specs * Adds support for updating jsonb objects - Restores PushController tests * Proper implementation of deleteByQuery and ORs - Adds ParseInstallation spec to the test suite * xit only failing tests * Nit on ParseAPI spec * add sorting operator * properly bound order keys * reverts describe_only_db behavior * Enables passing tests * Adds basic support for relations, upsertOneObject aliased to createObject * progress on queries options * Fix ACL update related problems * Creates relation tables on class creation * Adds Relation tests * remove flaky tests * use promises instead of CB * disable flaky test * nits * Fixes on schema spec - Next thing is to implemenet geopoint and files correctly * fix failues * Basic GeoPoint support * Adds support for $nearSphere/$maxDistance geopoint queries * enable passing tests * drop tables afterEach for PG, clean up relation tables too * Better initialization/dropTables
2016-08-15 16:48:39 -04:00
it('query non-existent class when disabled client class creation', (done) => {
const customConfig = Object.assign({}, config, {allowClientClassCreation: false});
2016-02-26 22:55:39 +08:00
rest.find(customConfig, auth.nobody(customConfig), 'ClientClassCreation', {})
.then(() => {
fail('Should throw an error');
done();
}, (err) => {
expect(err.code).toEqual(Parse.Error.OPERATION_FORBIDDEN);
expect(err.message).toEqual('This user is not allowed to access ' +
'non-existent class: ClientClassCreation');
done();
});
2016-02-26 22:55:39 +08:00
});
Advancements with postgres (#2510) * Start DB runner from tests * Connect GridstoreAdapter only when needed * removes unused package * better test errors reporting * Adds support for __op.Delete * Better test error reporting * Makes sure all tests can run without crashing * Use xdescribe to skip test suite * Removes unused dependencies * Let volatiles classes be created with PG on start * Do not fail if class dont exist * adds index.spec.js to the pg suite * Use a new config each test to prevent side effects * Enable EmailVerificationToken specs with pg * Makes sure failure output is not cut * Reduces number of ignored tests in ParseObject.spec * Inspect reconfiguration errors * Mark GlobalConfig is incompatible with PG - Problem is with nested updates (param.prop = value) * PG: Nested JSON queries and updates - Adds support for nested json and . operator queries - Adds debug support for PG adapter - Adds loglevel support in helper * Enable working specs in ParseUser * Sets default logLevel in tests to undefined * Adds File type support, retores purchaseValidation specs * Adds support for updating jsonb objects - Restores PushController tests * Proper implementation of deleteByQuery and ORs - Adds ParseInstallation spec to the test suite * xit only failing tests * Nit on ParseAPI spec * add sorting operator * properly bound order keys * reverts describe_only_db behavior * Enables passing tests * Adds basic support for relations, upsertOneObject aliased to createObject * progress on queries options * Fix ACL update related problems * Creates relation tables on class creation * Adds Relation tests * remove flaky tests * use promises instead of CB * disable flaky test * nits * Fixes on schema spec - Next thing is to implemenet geopoint and files correctly * fix failues * Basic GeoPoint support * Adds support for $nearSphere/$maxDistance geopoint queries * enable passing tests * drop tables afterEach for PG, clean up relation tables too * Better initialization/dropTables
2016-08-15 16:48:39 -04:00
it('query existent class when disabled client class creation', (done) => {
const customConfig = Object.assign({}, config, {allowClientClassCreation: false});
config.database.loadSchema()
.then(schema => schema.addClassIfNotExists('ClientClassCreation', {}))
.then(actualSchema => {
expect(actualSchema.className).toEqual('ClientClassCreation');
return rest.find(customConfig, auth.nobody(customConfig), 'ClientClassCreation', {});
})
.then((result) => {
expect(result.results.length).toEqual(0);
done();
}, () => {
fail('Should not throw error')
});
});
it('query with wrongly encoded parameter', (done) => {
rest.create(config, nobody, 'TestParameterEncode', {foo: 'bar'}
).then(() => {
return rest.create(config, nobody,
'TestParameterEncode', {foo: 'baz'});
}).then(() => {
const headers = {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest'
};
2016-12-07 15:17:05 -08:00
const p0 = rp.get({
headers: headers,
2017-06-21 02:54:13 -03:00
url: 'http://localhost:8378/1/classes/TestParameterEncode?' + querystring.stringify({
where: '{"foo":{"$ne": "baz"}}',
limit: 1
}).replace('=', '%3D'),
})
.then(fail, (response) => {
const error = response.error;
const b = JSON.parse(error);
2017-06-21 02:54:13 -03:00
expect(b.code).toEqual(Parse.Error.INVALID_QUERY);
});
2016-12-07 15:17:05 -08:00
const p1 = rp.get({
headers: headers,
2017-06-21 02:54:13 -03:00
url: 'http://localhost:8378/1/classes/TestParameterEncode?' + querystring.stringify({
limit: 1
}).replace('=', '%3D'),
})
.then(fail, (response) => {
const error = response.error;
const b = JSON.parse(error);
2017-06-21 02:54:13 -03:00
expect(b.code).toEqual(Parse.Error.INVALID_QUERY);
});
return Promise.all([p0, p1]);
}).then(done).catch((err) => {
Advancements with postgres (#2510) * Start DB runner from tests * Connect GridstoreAdapter only when needed * removes unused package * better test errors reporting * Adds support for __op.Delete * Better test error reporting * Makes sure all tests can run without crashing * Use xdescribe to skip test suite * Removes unused dependencies * Let volatiles classes be created with PG on start * Do not fail if class dont exist * adds index.spec.js to the pg suite * Use a new config each test to prevent side effects * Enable EmailVerificationToken specs with pg * Makes sure failure output is not cut * Reduces number of ignored tests in ParseObject.spec * Inspect reconfiguration errors * Mark GlobalConfig is incompatible with PG - Problem is with nested updates (param.prop = value) * PG: Nested JSON queries and updates - Adds support for nested json and . operator queries - Adds debug support for PG adapter - Adds loglevel support in helper * Enable working specs in ParseUser * Sets default logLevel in tests to undefined * Adds File type support, retores purchaseValidation specs * Adds support for updating jsonb objects - Restores PushController tests * Proper implementation of deleteByQuery and ORs - Adds ParseInstallation spec to the test suite * xit only failing tests * Nit on ParseAPI spec * add sorting operator * properly bound order keys * reverts describe_only_db behavior * Enables passing tests * Adds basic support for relations, upsertOneObject aliased to createObject * progress on queries options * Fix ACL update related problems * Creates relation tables on class creation * Adds Relation tests * remove flaky tests * use promises instead of CB * disable flaky test * nits * Fixes on schema spec - Next thing is to implemenet geopoint and files correctly * fix failues * Basic GeoPoint support * Adds support for $nearSphere/$maxDistance geopoint queries * enable passing tests * drop tables afterEach for PG, clean up relation tables too * Better initialization/dropTables
2016-08-15 16:48:39 -04:00
jfail(err);
fail('should not fail');
done();
})
});
it('query with limit = 0', (done) => {
rest.create(config, nobody, 'TestObject', {foo: 'baz'}
).then(() => {
return rest.create(config, nobody,
'TestObject', {foo: 'qux'});
}).then(() => {
return rest.find(config, nobody,
'TestObject', {}, {limit: 0});
}).then((response) => {
expect(response.results.length).toEqual(0);
done();
});
});
Advancements with postgres (#2510) * Start DB runner from tests * Connect GridstoreAdapter only when needed * removes unused package * better test errors reporting * Adds support for __op.Delete * Better test error reporting * Makes sure all tests can run without crashing * Use xdescribe to skip test suite * Removes unused dependencies * Let volatiles classes be created with PG on start * Do not fail if class dont exist * adds index.spec.js to the pg suite * Use a new config each test to prevent side effects * Enable EmailVerificationToken specs with pg * Makes sure failure output is not cut * Reduces number of ignored tests in ParseObject.spec * Inspect reconfiguration errors * Mark GlobalConfig is incompatible with PG - Problem is with nested updates (param.prop = value) * PG: Nested JSON queries and updates - Adds support for nested json and . operator queries - Adds debug support for PG adapter - Adds loglevel support in helper * Enable working specs in ParseUser * Sets default logLevel in tests to undefined * Adds File type support, retores purchaseValidation specs * Adds support for updating jsonb objects - Restores PushController tests * Proper implementation of deleteByQuery and ORs - Adds ParseInstallation spec to the test suite * xit only failing tests * Nit on ParseAPI spec * add sorting operator * properly bound order keys * reverts describe_only_db behavior * Enables passing tests * Adds basic support for relations, upsertOneObject aliased to createObject * progress on queries options * Fix ACL update related problems * Creates relation tables on class creation * Adds Relation tests * remove flaky tests * use promises instead of CB * disable flaky test * nits * Fixes on schema spec - Next thing is to implemenet geopoint and files correctly * fix failues * Basic GeoPoint support * Adds support for $nearSphere/$maxDistance geopoint queries * enable passing tests * drop tables afterEach for PG, clean up relation tables too * Better initialization/dropTables
2016-08-15 16:48:39 -04:00
it('query with limit = 0 and count = 1', (done) => {
rest.create(config, nobody, 'TestObject', {foo: 'baz'}
).then(() => {
return rest.create(config, nobody,
'TestObject', {foo: 'qux'});
}).then(() => {
return rest.find(config, nobody,
'TestObject', {}, {limit: 0, count: 1});
}).then((response) => {
expect(response.results.length).toEqual(0);
expect(response.count).toEqual(2);
done();
});
});
it('makes sure null pointers are handed correctly #2189', done => {
2016-12-07 15:17:05 -08:00
const object = new Parse.Object('AnObject');
const anotherObject = new Parse.Object('AnotherObject');
anotherObject.save().then(() => {
object.set('values', [null, null, anotherObject]);
return object.save();
}).then(() => {
2016-12-07 15:17:05 -08:00
const query = new Parse.Query('AnObject');
query.include('values');
return query.first();
}).then((result) => {
2016-12-07 15:17:05 -08:00
const values = result.get('values');
expect(values.length).toBe(3);
let anotherObjectFound = false;
let nullCounts = 0;
2016-12-07 15:17:05 -08:00
for(const value of values) {
if (value === null) {
nullCounts++;
} else if (value instanceof Parse.Object) {
anotherObjectFound = true;
}
}
expect(nullCounts).toBe(2);
expect(anotherObjectFound).toBeTruthy();
done();
}, (err) => {
console.error(err);
fail(err);
done();
});
});
2016-01-28 10:58:12 -08:00
});