Skip to content

Commit 5fb015e

Browse files
committed
Merge pull request #283 from theill/master
Implementing GET /config and POST /config support
2 parents 6294162 + 1d576bc commit 5fb015e

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

spec/ParseGlobalConfig.spec.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
var request = require('request');
3+
var Parse = require('parse/node').Parse;
4+
var DatabaseAdapter = require('../src/DatabaseAdapter');
5+
6+
var database = DatabaseAdapter.getDatabaseConnection('test');
7+
8+
describe('a GlobalConfig', () => {
9+
beforeEach(function(done) {
10+
database.rawCollection('_GlobalConfig')
11+
.then(coll => coll.updateOne({ '_id': 1}, { $set: { params: { companies: ['US', 'DK'] } } }, { upsert: true }))
12+
.then(done());
13+
});
14+
15+
it('can be retrieved', (done) => {
16+
request.get({
17+
url: 'http://localhost:8378/1/config',
18+
json: true,
19+
headers: {
20+
'X-Parse-Application-Id': 'test',
21+
'X-Parse-Master-Key': 'test',
22+
},
23+
}, (error, response, body) => {
24+
expect(response.statusCode).toEqual(200);
25+
expect(body.params.companies).toEqual(['US', 'DK']);
26+
done();
27+
});
28+
});
29+
30+
it('can be updated when a master key exists', (done) => {
31+
request.put({
32+
url: 'http://localhost:8378/1/config',
33+
json: true,
34+
body: { params: { companies: ['US', 'DK', 'SE'] } },
35+
headers: {
36+
'X-Parse-Application-Id': 'test',
37+
'X-Parse-Master-Key': 'test'
38+
},
39+
}, (error, response, body) => {
40+
expect(response.statusCode).toEqual(200);
41+
expect(body.result).toEqual(true);
42+
done();
43+
});
44+
});
45+
46+
it('fail to update if master key is missing', (done) => {
47+
request.put({
48+
url: 'http://localhost:8378/1/config',
49+
json: true,
50+
body: { params: { companies: [] } },
51+
headers: {
52+
'X-Parse-Application-Id': 'test',
53+
'X-Parse-REST-API-Key': 'rest'
54+
},
55+
}, (error, response, body) => {
56+
expect(response.statusCode).toEqual(401);
57+
expect(body.error).toEqual('unauthorized');
58+
done();
59+
});
60+
});
61+
62+
it('failed getting config when it is missing', (done) => {
63+
database.rawCollection('_GlobalConfig')
64+
.then(coll => coll.deleteOne({ '_id': 1}, {}, {}))
65+
.then(_ => {
66+
request.get({
67+
url: 'http://localhost:8378/1/config',
68+
json: true,
69+
headers: {
70+
'X-Parse-Application-Id': 'test',
71+
'X-Parse-Master-Key': 'test',
72+
},
73+
}, (error, response, body) => {
74+
expect(response.statusCode).toEqual(404);
75+
expect(body.code).toEqual(Parse.Error.INVALID_KEY_NAME);
76+
done();
77+
});
78+
});
79+
});
80+
81+
});

src/ExportAdapter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ ExportAdapter.prototype.collection = function(className) {
4848
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME,
4949
'invalid className: ' + className);
5050
}
51+
return this.rawCollection(className);
52+
};
53+
54+
ExportAdapter.prototype.rawCollection = function(className) {
5155
return this.connect().then(() => {
5256
return this.db.collection(this.collectionPrefix + className);
5357
});

src/global_config.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// global_config.js
2+
3+
var Parse = require('parse/node').Parse,
4+
PromiseRouter = require('./PromiseRouter');
5+
6+
var router = new PromiseRouter();
7+
8+
function getGlobalConfig(req) {
9+
return req.config.database.rawCollection('_GlobalConfig')
10+
.then(coll => coll.findOne({'_id': 1}))
11+
.then(globalConfig => ({response: { params: globalConfig.params }}))
12+
.catch(() => ({
13+
status: 404,
14+
response: {
15+
code: Parse.Error.INVALID_KEY_NAME,
16+
error: 'config does not exist',
17+
}
18+
}));
19+
}
20+
21+
function updateGlobalConfig(req) {
22+
if (!req.auth.isMaster) {
23+
return Promise.resolve({
24+
status: 401,
25+
response: {error: 'unauthorized'},
26+
});
27+
}
28+
29+
return req.config.database.rawCollection('_GlobalConfig')
30+
.then(coll => coll.findOneAndUpdate({ _id: 1 }, { $set: req.body }))
31+
.then(response => {
32+
return { response: { result: true } }
33+
})
34+
.catch(() => ({
35+
status: 404,
36+
response: {
37+
code: Parse.Error.INVALID_KEY_NAME,
38+
error: 'config cannot be updated',
39+
}
40+
}));
41+
}
42+
43+
router.route('GET', '/config', getGlobalConfig);
44+
router.route('PUT', '/config', updateGlobalConfig);
45+
46+
module.exports = router;

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ function ParseServer(args) {
121121
router.merge(require('./installations'));
122122
router.merge(require('./functions'));
123123
router.merge(require('./schemas'));
124+
if (process.env.PARSE_EXPERIMENTAL_CONFIG_ENABLED || process.env.TESTING) {
125+
router.merge(require('./global_config'));
126+
}
124127

125128
batch.mountOnto(router);
126129

0 commit comments

Comments
 (0)