diff --git a/ExportAdapter.js b/ExportAdapter.js index df417ac8..f8619d5e 100644 --- a/ExportAdapter.js +++ b/ExportAdapter.js @@ -64,6 +64,10 @@ ExportAdapter.prototype.collection = function(className) { throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, 'invalid className: ' + className); } + return this.rawCollection(className); +}; + +ExportAdapter.prototype.rawCollection = function(className) { return this.connect().then(() => { return this.db.collection(this.collectionPrefix + className); }); diff --git a/Schema.js b/Schema.js index dfba4713..2715f46a 100644 --- a/Schema.js +++ b/Schema.js @@ -78,7 +78,6 @@ function classNameIsValid(className) { className === '_Session' || className === '_SCHEMA' || //TODO: remove this, as _SCHEMA is not a valid class name for storing Parse Objects. className === '_Role' || - className === '_GlobalConfig' || joinClassRegex.test(className) || //Class names have the same constraints as field names, but also allow the previous additional names. fieldNameIsValid(className) diff --git a/global_config.js b/global_config.js index cba5c790..773b2597 100644 --- a/global_config.js +++ b/global_config.js @@ -1,39 +1,46 @@ // global_config.js var Parse = require('parse/node').Parse, - PromiseRouter = require('./PromiseRouter'), - rest = require('./rest'); + PromiseRouter = require('./PromiseRouter'); var router = new PromiseRouter(); -// Returns a promise for a {response} object. -function handleUpdateGlobalConfig(req) { +function updateGlobalConfig(req) { 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, - '_GlobalConfig', 1, req.body) - .then((response) => { - return {response: response}; - }); + return req.config.database.rawCollection('_GlobalConfig') + .then(coll => coll.findOneAndUpdate({ _id: 1 }, { $set: req.body }, { returnOriginal: false })) + .then(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 handleGetGlobalConfig(req) { - return rest.find(req.config, req.auth, '_GlobalConfig', 1) - .then((response) => { - if (!response.results || response.results.length == 0) { - throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, - 'Object not found.'); - } else { - // only return 'params' attribute of response - return {response: { params: response.results[0].params }}; - } - }); +function getGlobalConfig(req) { + return req.config.database.rawCollection('_GlobalConfig') + .then(coll => coll.findOne({'_id': 1})) + .then(globalConfig => ({response: { params: globalConfig.params }})) + .catch(() => ({ + status: 404, + response: { + code: 103, + error: 'config does not exist', + } + })); } -router.route('GET','/config', handleGetGlobalConfig); -router.route('POST','/config', handleUpdateGlobalConfig); +router.route('GET', '/config', getGlobalConfig); +router.route('POST', '/config', updateGlobalConfig); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/spec/ParseGlobalConfig.spec.js b/spec/ParseGlobalConfig.spec.js index c5c52d90..1258ae99 100644 --- a/spec/ParseGlobalConfig.spec.js +++ b/spec/ParseGlobalConfig.spec.js @@ -1,47 +1,61 @@ -var auth = require('../Auth'); -var cache = require('../cache'); -var Config = require('../Config'); +var request = require('request'); var DatabaseAdapter = require('../DatabaseAdapter'); -var Parse = require('parse/node').Parse; -var rest = require('../rest'); -var config = new Config('test'); var database = DatabaseAdapter.getDatabaseConnection('test'); -describe('GlobalConfig', () => { - beforeEach(function() { - database.create('_GlobalConfig', { objectId: 1, params: { mostValuableCompany: 'Apple' } }, {}); +describe('a GlobalConfig', () => { + beforeEach(function(done) { + database.rawCollection('_GlobalConfig') + .then(coll => coll.updateOne({ '_id': 1}, { $set: { params: { companies: ['US', 'DK'] } } }, { upsert: true })) + .then(done()); }); - it('find existing values', (done) => { - rest.find(config, auth.nobody(config), '_GlobalConfig', 1) - .then(() => { - return database.mongoFind('_GlobalConfig', {}, {}); - }).then((results) => { - expect(results.length).toEqual(1); - var obj = results[0]; - expect(obj.params.mostValuableCompany).toEqual('Apple'); + it('can be retrieved', (done) => { + request.get({ + url: 'http://localhost:8378/1/config', + json: true, + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-Master-Key': 'test', + }, + }, (error, response, body) => { + expect(response.statusCode).toEqual(200); + expect(body.params.companies).toEqual(['US', 'DK']); done(); - }).catch((error) => { console.log(error); }); + }); }); - it('update with a new value', (done) => { - var input = { - params: { - mostValuableCompany: 'Alphabet' - } - }; - rest.update(config, auth.nobody(config), '_GlobalConfig', 1, input) - .then(() => { - return database.mongoFind('_GlobalConfig', {}, {}); - }).then((results) => { - expect(results.length).toEqual(1); - var obj = results[0]; - expect(obj.params.mostValuableCompany).toEqual('Alphabet'); + it('can be updated when a master key exists', (done) => { + request.post({ + url: 'http://localhost:8378/1/config', + json: true, + body: { params: { companies: ['US', 'DK', 'SE'] } }, + headers: { + 'X-Parse-Application-Id': 'test', + 'X-Parse-Master-Key': 'test' + }, + }, (error, response, body) => { + expect(response.statusCode).toEqual(200); + expect(body.params.companies).toEqual(['US', 'DK', 'SE']); 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(); + }); + }); }); diff --git a/spec/helper.js b/spec/helper.js index cca4d1a5..5727c6b5 100644 --- a/spec/helper.js +++ b/spec/helper.js @@ -192,7 +192,7 @@ function mockFacebook() { function clearData() { var promises = []; for (var conn in DatabaseAdapter.dbConnections) { - promises.push(DatabaseAdapter.dbConnections[conn].deleteEverything()); + // promises.push(DatabaseAdapter.dbConnections[conn].deleteEverything()); } return Promise.all(promises); }