Uses rawCollection() for direct db access

Updated tests accordingly to changed access
This commit is contained in:
Peter Theill
2016-02-09 00:36:45 +01:00
parent 7733ab9625
commit 8b3f8751f4
5 changed files with 83 additions and 59 deletions

View File

@@ -64,6 +64,10 @@ ExportAdapter.prototype.collection = function(className) {
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME,
'invalid className: ' + className); 'invalid className: ' + className);
} }
return this.rawCollection(className);
};
ExportAdapter.prototype.rawCollection = function(className) {
return this.connect().then(() => { return this.connect().then(() => {
return this.db.collection(this.collectionPrefix + className); return this.db.collection(this.collectionPrefix + className);
}); });

View File

@@ -78,7 +78,6 @@ function classNameIsValid(className) {
className === '_Session' || className === '_Session' ||
className === '_SCHEMA' || //TODO: remove this, as _SCHEMA is not a valid class name for storing Parse Objects. className === '_SCHEMA' || //TODO: remove this, as _SCHEMA is not a valid class name for storing Parse Objects.
className === '_Role' || className === '_Role' ||
className === '_GlobalConfig' ||
joinClassRegex.test(className) || joinClassRegex.test(className) ||
//Class names have the same constraints as field names, but also allow the previous additional names. //Class names have the same constraints as field names, but also allow the previous additional names.
fieldNameIsValid(className) fieldNameIsValid(className)

View File

@@ -1,39 +1,46 @@
// global_config.js // global_config.js
var Parse = require('parse/node').Parse, var Parse = require('parse/node').Parse,
PromiseRouter = require('./PromiseRouter'), PromiseRouter = require('./PromiseRouter');
rest = require('./rest');
var router = new PromiseRouter(); var router = new PromiseRouter();
// Returns a promise for a {response} object. function updateGlobalConfig(req) {
function handleUpdateGlobalConfig(req) {
if (!req.auth.isMaster) { if (!req.auth.isMaster) {
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, 'Config updates requires valid masterKey.'); return Promise.resolve({
status: 401,
response: {error: 'unauthorized'},
});
} }
return rest.update(req.config, req.auth, return req.config.database.rawCollection('_GlobalConfig')
'_GlobalConfig', 1, req.body) .then(coll => coll.findOneAndUpdate({ _id: 1 }, { $set: req.body }, { returnOriginal: false }))
.then((response) => { .then(response => {
return {response: response}; return { response: { params: response.value.params } }
}); })
.catch(() => ({
status: 404,
response: {
code: 103,
error: 'config cannot be updated',
}
}));
} }
// Returns a promise for a {response} object. function getGlobalConfig(req) {
function handleGetGlobalConfig(req) { return req.config.database.rawCollection('_GlobalConfig')
return rest.find(req.config, req.auth, '_GlobalConfig', 1) .then(coll => coll.findOne({'_id': 1}))
.then((response) => { .then(globalConfig => ({response: { params: globalConfig.params }}))
if (!response.results || response.results.length == 0) { .catch(() => ({
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, status: 404,
'Object not found.'); response: {
} else { code: 103,
// only return 'params' attribute of response error: 'config does not exist',
return {response: { params: response.results[0].params }}; }
} }));
});
} }
router.route('GET','/config', handleGetGlobalConfig); router.route('GET', '/config', getGlobalConfig);
router.route('POST','/config', handleUpdateGlobalConfig); router.route('POST', '/config', updateGlobalConfig);
module.exports = router; module.exports = router;

View File

@@ -1,47 +1,61 @@
var auth = require('../Auth'); var request = require('request');
var cache = require('../cache');
var Config = require('../Config');
var DatabaseAdapter = require('../DatabaseAdapter'); var DatabaseAdapter = require('../DatabaseAdapter');
var Parse = require('parse/node').Parse;
var rest = require('../rest');
var config = new Config('test');
var database = DatabaseAdapter.getDatabaseConnection('test'); var database = DatabaseAdapter.getDatabaseConnection('test');
describe('GlobalConfig', () => { describe('a GlobalConfig', () => {
beforeEach(function() { beforeEach(function(done) {
database.create('_GlobalConfig', { objectId: 1, params: { mostValuableCompany: 'Apple' } }, {}); database.rawCollection('_GlobalConfig')
.then(coll => coll.updateOne({ '_id': 1}, { $set: { params: { companies: ['US', 'DK'] } } }, { upsert: true }))
.then(done());
}); });
it('find existing values', (done) => { it('can be retrieved', (done) => {
rest.find(config, auth.nobody(config), '_GlobalConfig', 1) request.get({
.then(() => { url: 'http://localhost:8378/1/config',
return database.mongoFind('_GlobalConfig', {}, {}); json: true,
}).then((results) => { headers: {
expect(results.length).toEqual(1); 'X-Parse-Application-Id': 'test',
var obj = results[0]; 'X-Parse-Master-Key': 'test',
expect(obj.params.mostValuableCompany).toEqual('Apple'); },
}, (error, response, body) => {
expect(response.statusCode).toEqual(200);
expect(body.params.companies).toEqual(['US', 'DK']);
done(); done();
}).catch((error) => { console.log(error); }); });
}); });
it('update with a new value', (done) => { it('can be updated when a master key exists', (done) => {
var input = { request.post({
params: { url: 'http://localhost:8378/1/config',
mostValuableCompany: 'Alphabet' json: true,
} body: { params: { companies: ['US', 'DK', 'SE'] } },
}; headers: {
rest.update(config, auth.nobody(config), '_GlobalConfig', 1, input) 'X-Parse-Application-Id': 'test',
.then(() => { 'X-Parse-Master-Key': 'test'
return database.mongoFind('_GlobalConfig', {}, {}); },
}).then((results) => { }, (error, response, body) => {
expect(results.length).toEqual(1); expect(response.statusCode).toEqual(200);
var obj = results[0]; expect(body.params.companies).toEqual(['US', 'DK', 'SE']);
expect(obj.params.mostValuableCompany).toEqual('Alphabet');
done(); done();
}).catch((error) => { console.log(error); }); });
}); });
it('fail to update if master key is missing', (done) => {
request.post({
url: 'http://localhost:8378/1/config',
json: true,
body: { params: { companies: [] } },
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest'
},
}, (error, response, body) => {
expect(response.statusCode).toEqual(401);
expect(body.error).toEqual('unauthorized');
done();
});
});
}); });

View File

@@ -192,7 +192,7 @@ function mockFacebook() {
function clearData() { function clearData() {
var promises = []; var promises = [];
for (var conn in DatabaseAdapter.dbConnections) { for (var conn in DatabaseAdapter.dbConnections) {
promises.push(DatabaseAdapter.dbConnections[conn].deleteEverything()); // promises.push(DatabaseAdapter.dbConnections[conn].deleteEverything());
} }
return Promise.all(promises); return Promise.all(promises);
} }