Merge pull request #901 from ParsePlatform/nlutsenko.config

Migrate ParseGlobalConfig to new database storage API.
This commit is contained in:
Nikita Lutsenko
2016-03-07 14:50:26 -08:00
3 changed files with 45 additions and 51 deletions

View File

@@ -5,21 +5,21 @@ var Parse = require('parse/node').Parse;
let Config = require('../src/Config'); let Config = require('../src/Config');
describe('a GlobalConfig', () => { describe('a GlobalConfig', () => {
beforeEach(function(done) { beforeEach(function (done) {
let config = new Config('test'); let config = new Config('test');
config.database.rawCollection('_GlobalConfig') config.database.adaptiveCollection('_GlobalConfig')
.then(coll => coll.updateOne({ '_id': 1}, { $set: { params: { companies: ['US', 'DK'] } } }, { upsert: true })) .then(coll => coll.upsertOne({ '_id': 1 }, { $set: { params: { companies: ['US', 'DK'] } } }))
.then(done()); .then(done());
}); });
it('can be retrieved', (done) => { it('can be retrieved', (done) => {
request.get({ request.get({
url: 'http://localhost:8378/1/config', url : 'http://localhost:8378/1/config',
json: true, json : true,
headers: { headers: {
'X-Parse-Application-Id': 'test', 'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test', 'X-Parse-Master-Key' : 'test'
}, }
}, (error, response, body) => { }, (error, response, body) => {
expect(response.statusCode).toEqual(200); expect(response.statusCode).toEqual(200);
expect(body.params.companies).toEqual(['US', 'DK']); expect(body.params.companies).toEqual(['US', 'DK']);
@@ -29,13 +29,13 @@ describe('a GlobalConfig', () => {
it('can be updated when a master key exists', (done) => { it('can be updated when a master key exists', (done) => {
request.put({ request.put({
url: 'http://localhost:8378/1/config', url : 'http://localhost:8378/1/config',
json: true, json : true,
body: { params: { companies: ['US', 'DK', 'SE'] } }, body : { params: { companies: ['US', 'DK', 'SE'] } },
headers: { headers: {
'X-Parse-Application-Id': 'test', 'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test' 'X-Parse-Master-Key' : 'test'
}, }
}, (error, response, body) => { }, (error, response, body) => {
expect(response.statusCode).toEqual(200); expect(response.statusCode).toEqual(200);
expect(body.result).toEqual(true); expect(body.result).toEqual(true);
@@ -45,13 +45,13 @@ describe('a GlobalConfig', () => {
it('fail to update if master key is missing', (done) => { it('fail to update if master key is missing', (done) => {
request.put({ request.put({
url: 'http://localhost:8378/1/config', url : 'http://localhost:8378/1/config',
json: true, json : true,
body: { params: { companies: [] } }, body : { params: { companies: [] } },
headers: { headers: {
'X-Parse-Application-Id': 'test', 'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest' 'X-Parse-REST-API-Key' : 'rest'
}, }
}, (error, response, body) => { }, (error, response, body) => {
expect(response.statusCode).toEqual(403); expect(response.statusCode).toEqual(403);
expect(body.error).toEqual('unauthorized: master key is required'); expect(body.error).toEqual('unauthorized: master key is required');
@@ -61,19 +61,19 @@ describe('a GlobalConfig', () => {
it('failed getting config when it is missing', (done) => { it('failed getting config when it is missing', (done) => {
let config = new Config('test'); let config = new Config('test');
config.database.rawCollection('_GlobalConfig') config.database.adaptiveCollection('_GlobalConfig')
.then(coll => coll.deleteOne({ '_id': 1}, {}, {})) .then(coll => coll.deleteOne({ '_id': 1 }))
.then(_ => { .then(() => {
request.get({ request.get({
url: 'http://localhost:8378/1/config', url : 'http://localhost:8378/1/config',
json: true, json : true,
headers: { headers: {
'X-Parse-Application-Id': 'test', 'X-Parse-Application-Id': 'test',
'X-Parse-Master-Key': 'test', 'X-Parse-Master-Key' : 'test'
}, }
}, (error, response, body) => { }, (error, response, body) => {
expect(response.statusCode).toEqual(404); expect(response.statusCode).toEqual(200);
expect(body.code).toEqual(Parse.Error.INVALID_KEY_NAME); expect(body.params).toEqual({});
done(); done();
}); });
}); });

View File

@@ -75,6 +75,10 @@ export default class MongoCollection {
}); });
} }
deleteOne(query) {
return this._mongoCollection.deleteOne(query);
}
remove(query) { remove(query) {
return this._mongoCollection.remove(query); return this._mongoCollection.remove(query);
} }

View File

@@ -1,36 +1,26 @@
// global_config.js // global_config.js
var Parse = require('parse/node').Parse;
import PromiseRouter from '../PromiseRouter'; import PromiseRouter from '../PromiseRouter';
import * as middleware from "../middlewares"; import * as middleware from "../middlewares";
export class GlobalConfigRouter extends PromiseRouter { export class GlobalConfigRouter extends PromiseRouter {
getGlobalConfig(req) { getGlobalConfig(req) {
return req.config.database.rawCollection('_GlobalConfig') return req.config.database.adaptiveCollection('_GlobalConfig')
.then(coll => coll.findOne({'_id': 1})) .then(coll => coll.find({ '_id': 1 }, { limit: 1 }))
.then(globalConfig => ({response: { params: globalConfig.params }})) .then(results => {
.catch(() => ({ if (results.length != 1) {
status: 404, // If there is no config in the database - return empty config.
response: { return { response: { params: {} } };
code: Parse.Error.INVALID_KEY_NAME,
error: 'config does not exist',
} }
})); let globalConfig = results[0];
return { response: { params: globalConfig.params } };
});
} }
updateGlobalConfig(req) { updateGlobalConfig(req) {
return req.config.database.rawCollection('_GlobalConfig') return req.config.database.adaptiveCollection('_GlobalConfig')
.then(coll => coll.findOneAndUpdate({ _id: 1 }, { $set: req.body })) .then(coll => coll.upsertOne({ _id: 1 }, { $set: req.body }))
.then(response => { .then(() => ({ response: { result: true } }));
return { response: { result: true } }
})
.catch(() => ({
status: 404,
response: {
code: Parse.Error.INVALID_KEY_NAME,
error: 'config cannot be updated',
}
}));
} }
mountRoutes() { mountRoutes() {