Skip to content

Add master-key-only option when saving config parameter #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions integration/test/ParseConfigTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ const clear = require('./clear');
const Parse = require('../../node');

function testConfig() {
const data = {
params: { internal: 'i', public: 'p' },
masterKeyOnly: { internal: true },
};
return Parse.CoreManager.getRESTController().request(
'PUT',
'config',
data,
{ useMasterKey: true }
return Parse.Config.save(
{ internal: "i", string: "s", number: 12 },
{ internal: true }
);
}

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

it('can create a config', async () => {
const config = await Parse.Config.save({
str: 'hello',
num: 42
});
assert.equal(config.get('str'), 'hello');
assert.equal(config.get('num'), 42);
const config = await testConfig();

assert.notStrictEqual(config, undefined);
assert.strictEqual(config.get('string'), 's');
assert.strictEqual(config.get('internal'), 'i');
assert.strictEqual(config.get('number'), 12);
});

it('can get a config', async () => {
await Parse.Config.save({
str: 'hello',
num: 42
});
await testConfig();

const config = await Parse.Config.get();
assert.equal(config.get('str'), 'hello');
assert.equal(config.get('num'), 42);
assert.notStrictEqual(config, undefined);
assert.strictEqual(config.get('string'), 's');
assert.strictEqual(config.get('number'), 12);
});

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

const config = await Parse.Config.get({ useMasterKey: true });
assert.equal(config.get('internal'), 'i');
assert.equal(config.get('public'), 'p');
assert.equal(config.get('string'), 's');
});

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

const config = await Parse.Config.get();
assert.equal(config.get('internal'), undefined);
assert.equal(config.get('public'), 'p');
assert.equal(config.get('string'), 's');
});
});
7 changes: 2 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions src/ParseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ class ParseConfig {
/**
* Save value keys to the server.
* @static
* @param {Object} attrs The config parameters and values.
* @param {Object} masterKeyOnlyFlags The flags that define whether config parameters listed
* in `attrs` should be retrievable only by using the master key.
* For example: `param1: true` makes `param1` only retrievable by using the master key.
* If a parameter is not provided or set to `false`, it can be retrieved without
* using the master key.
* @return {Promise} A promise that is resolved with a newly-created
* configuration object or with the current with the update.
*/
static save(attrs: { [key: string]: any }) {
static save(attrs: { [key: string]: any }, masterKeyOnlyFlags: { [key: string]: any }) {
const controller = CoreManager.getConfigController();
//To avoid a mismatch with the local and the cloud config we get a new version
return controller.save(attrs).then(() => {
return controller.get();
return controller.save(attrs, masterKeyOnlyFlags).then(() => {
return controller.get({ useMasterKey: true });
},(error) => {
return Promise.reject(error);
});
Expand Down Expand Up @@ -184,7 +190,7 @@ const DefaultController = {
});
},

save(attrs: { [key: string]: any }) {
save(attrs: { [key: string]: any }, masterKeyOnlyFlags: { [key: string]: any }) {
const RESTController = CoreManager.getRESTController();
const encodedAttrs = {};
for(const key in attrs){
Expand All @@ -193,7 +199,7 @@ const DefaultController = {
return RESTController.request(
'PUT',
'config',
{ params: encodedAttrs },
{ params: encodedAttrs, masterKeyOnly: masterKeyOnlyFlags },
{ useMasterKey: true }
).then(response => {
if(response && response.result){
Expand Down
69 changes: 69 additions & 0 deletions src/__tests__/ParseConfig-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

jest.dontMock('../CoreManager');
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('../escape');
jest.dontMock('../ParseConfig');
jest.dontMock('../ParseError');
Expand Down Expand Up @@ -126,6 +127,74 @@ describe('ParseConfig', () => {
});
});

it('can save a config object that be retrieved with masterkey only', async () => {
CoreManager.setRESTController({
request(method, path, body, options) {
console.log(method, path, body, options);
if (method === 'PUT') {
expect(method).toBe('PUT');
expect(path).toBe('config');
expect(body).toEqual({
params: { internal: 'i', number: 12 },
masterKeyOnly: { internal: true },
});
expect(options).toEqual({ useMasterKey: true });
return Promise.resolve({
params: {
internal: 'i',
number: 12
},
result: true,
});
} else if (method === 'GET') {
expect(method).toBe('GET');
expect(path).toBe('config');
expect(body).toEqual({});
expect(options).toEqual({ useMasterKey: true });
return Promise.resolve({
params: {
internal: 'i',
number: 12
},
});
}
},
ajax() {}
});
const config = await ParseConfig.save(
{ internal: 'i', number: 12 },
{ internal: true }
);
expect(config.get('internal')).toBe('i');
expect(config.get('number')).toBe(12);
});

it('can get a config object with master key', async () => {
CoreManager.setRESTController({
request(method, path, body, options) {
expect(method).toBe('GET');
expect(path).toBe('config');
expect(body).toEqual({});
expect(options).toEqual({ useMasterKey: true });
return Promise.resolve({
params: {
str: 'hello',
num: 45
}
});
},
ajax() {}
});
const config = await ParseConfig.get({ useMasterKey: true });
expect(config.get('str')).toBe('hello');
expect(config.get('num')).toBe(45);
const path = Storage.generatePath('currentConfig');
expect(JSON.parse(Storage.getItem(path))).toEqual({
str: 'hello',
num: 45,
});
});

it('rejects save on invalid response', (done) => {
CoreManager.setRESTController({
request() {
Expand Down