Skip to content

Commit b37d687

Browse files
mtrezzadplewis
authored andcommitted
Add master-key-only option when saving config parameter (#910)
* added master-key-only option when saving config * added docs; removed unnecessary encoding * generalized test config; added test case for retrieving config afer saving * Update package-lock.json * Revert "Update package-lock.json" This reverts commit 04152dc. * added unit test * fix test
1 parent 2fc8da0 commit b37d687

File tree

4 files changed

+98
-33
lines changed

4 files changed

+98
-33
lines changed

integration/test/ParseConfigTest.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@ const clear = require('./clear');
55
const Parse = require('../../node');
66

77
function testConfig() {
8-
const data = {
9-
params: { internal: 'i', public: 'p' },
10-
masterKeyOnly: { internal: true },
11-
};
12-
return Parse.CoreManager.getRESTController().request(
13-
'PUT',
14-
'config',
15-
data,
16-
{ useMasterKey: true }
8+
return Parse.Config.save(
9+
{ internal: "i", string: "s", number: 12 },
10+
{ internal: true }
1711
);
1812
}
1913

@@ -28,37 +22,36 @@ describe('Parse Config', () => {
2822
});
2923

3024
it('can create a config', async () => {
31-
const config = await Parse.Config.save({
32-
str: 'hello',
33-
num: 42
34-
});
35-
assert.equal(config.get('str'), 'hello');
36-
assert.equal(config.get('num'), 42);
25+
const config = await testConfig();
26+
27+
assert.notStrictEqual(config, undefined);
28+
assert.strictEqual(config.get('string'), 's');
29+
assert.strictEqual(config.get('internal'), 'i');
30+
assert.strictEqual(config.get('number'), 12);
3731
});
3832

3933
it('can get a config', async () => {
40-
await Parse.Config.save({
41-
str: 'hello',
42-
num: 42
43-
});
34+
await testConfig();
35+
4436
const config = await Parse.Config.get();
45-
assert.equal(config.get('str'), 'hello');
46-
assert.equal(config.get('num'), 42);
37+
assert.notStrictEqual(config, undefined);
38+
assert.strictEqual(config.get('string'), 's');
39+
assert.strictEqual(config.get('number'), 12);
4740
});
4841

4942
it('can get internal config parameter with masterkey', async () => {
5043
await testConfig();
5144

5245
const config = await Parse.Config.get({ useMasterKey: true });
5346
assert.equal(config.get('internal'), 'i');
54-
assert.equal(config.get('public'), 'p');
47+
assert.equal(config.get('string'), 's');
5548
});
5649

5750
it('cannot get internal config parameter without masterkey', async () => {
5851
await testConfig();
5952

6053
const config = await Parse.Config.get();
6154
assert.equal(config.get('internal'), undefined);
62-
assert.equal(config.get('public'), 'p');
55+
assert.equal(config.get('string'), 's');
6356
});
6457
});

package-lock.json

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ParseConfig.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,20 @@ class ParseConfig {
9292
/**
9393
* Save value keys to the server.
9494
* @static
95+
* @param {Object} attrs The config parameters and values.
96+
* @param {Object} masterKeyOnlyFlags The flags that define whether config parameters listed
97+
* in `attrs` should be retrievable only by using the master key.
98+
* For example: `param1: true` makes `param1` only retrievable by using the master key.
99+
* If a parameter is not provided or set to `false`, it can be retrieved without
100+
* using the master key.
95101
* @return {Promise} A promise that is resolved with a newly-created
96102
* configuration object or with the current with the update.
97103
*/
98-
static save(attrs: { [key: string]: any }) {
104+
static save(attrs: { [key: string]: any }, masterKeyOnlyFlags: { [key: string]: any }) {
99105
const controller = CoreManager.getConfigController();
100106
//To avoid a mismatch with the local and the cloud config we get a new version
101-
return controller.save(attrs).then(() => {
102-
return controller.get();
107+
return controller.save(attrs, masterKeyOnlyFlags).then(() => {
108+
return controller.get({ useMasterKey: true });
103109
},(error) => {
104110
return Promise.reject(error);
105111
});
@@ -184,7 +190,7 @@ const DefaultController = {
184190
});
185191
},
186192

187-
save(attrs: { [key: string]: any }) {
193+
save(attrs: { [key: string]: any }, masterKeyOnlyFlags: { [key: string]: any }) {
188194
const RESTController = CoreManager.getRESTController();
189195
const encodedAttrs = {};
190196
for(const key in attrs){
@@ -193,7 +199,7 @@ const DefaultController = {
193199
return RESTController.request(
194200
'PUT',
195201
'config',
196-
{ params: encodedAttrs },
202+
{ params: encodedAttrs, masterKeyOnly: masterKeyOnlyFlags },
197203
{ useMasterKey: true }
198204
).then(response => {
199205
if(response && response.result){

src/__tests__/ParseConfig-test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
jest.dontMock('../CoreManager');
1111
jest.dontMock('../decode');
12+
jest.dontMock('../encode');
1213
jest.dontMock('../escape');
1314
jest.dontMock('../ParseConfig');
1415
jest.dontMock('../ParseError');
@@ -126,6 +127,74 @@ describe('ParseConfig', () => {
126127
});
127128
});
128129

130+
it('can save a config object that be retrieved with masterkey only', async () => {
131+
CoreManager.setRESTController({
132+
request(method, path, body, options) {
133+
console.log(method, path, body, options);
134+
if (method === 'PUT') {
135+
expect(method).toBe('PUT');
136+
expect(path).toBe('config');
137+
expect(body).toEqual({
138+
params: { internal: 'i', number: 12 },
139+
masterKeyOnly: { internal: true },
140+
});
141+
expect(options).toEqual({ useMasterKey: true });
142+
return Promise.resolve({
143+
params: {
144+
internal: 'i',
145+
number: 12
146+
},
147+
result: true,
148+
});
149+
} else if (method === 'GET') {
150+
expect(method).toBe('GET');
151+
expect(path).toBe('config');
152+
expect(body).toEqual({});
153+
expect(options).toEqual({ useMasterKey: true });
154+
return Promise.resolve({
155+
params: {
156+
internal: 'i',
157+
number: 12
158+
},
159+
});
160+
}
161+
},
162+
ajax() {}
163+
});
164+
const config = await ParseConfig.save(
165+
{ internal: 'i', number: 12 },
166+
{ internal: true }
167+
);
168+
expect(config.get('internal')).toBe('i');
169+
expect(config.get('number')).toBe(12);
170+
});
171+
172+
it('can get a config object with master key', async () => {
173+
CoreManager.setRESTController({
174+
request(method, path, body, options) {
175+
expect(method).toBe('GET');
176+
expect(path).toBe('config');
177+
expect(body).toEqual({});
178+
expect(options).toEqual({ useMasterKey: true });
179+
return Promise.resolve({
180+
params: {
181+
str: 'hello',
182+
num: 45
183+
}
184+
});
185+
},
186+
ajax() {}
187+
});
188+
const config = await ParseConfig.get({ useMasterKey: true });
189+
expect(config.get('str')).toBe('hello');
190+
expect(config.get('num')).toBe(45);
191+
const path = Storage.generatePath('currentConfig');
192+
expect(JSON.parse(Storage.getItem(path))).toEqual({
193+
str: 'hello',
194+
num: 45,
195+
});
196+
});
197+
129198
it('rejects save on invalid response', (done) => {
130199
CoreManager.setRESTController({
131200
request() {

0 commit comments

Comments
 (0)