Skip to content

Commit 1b60df4

Browse files
authored
Add useMasterKey option to Parse.File.destroy() (#1285)
* Add useMasterKey option to Parse.File.destroy() * improve coverage
1 parent 38465cc commit 1b60df4

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/ParseFile.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,23 @@ class ParseFile {
323323
* Deletes the file from the Parse cloud.
324324
* In Cloud Code and Node only with Master Key.
325325
*
326+
* @param {object} options
327+
* * Valid options are:<ul>
328+
* <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
329+
* be used for this request.
330+
* <pre>
326331
* @returns {Promise} Promise that is resolved when the delete finishes.
327332
*/
328-
destroy() {
333+
destroy(options?: FullOptions = {}) {
329334
if (!this._name) {
330335
throw new ParseError(ParseError.FILE_DELETE_UNNAMED_ERROR, 'Cannot delete an unnamed file.');
331336
}
337+
const destroyOptions = { useMasterKey: true };
338+
if (options.hasOwnProperty('useMasterKey')) {
339+
destroyOptions.useMasterKey = options.useMasterKey;
340+
}
332341
const controller = CoreManager.getFileController();
333-
return controller.deleteFile(this._name).then(() => {
342+
return controller.deleteFile(this._name, destroyOptions).then(() => {
334343
this._data = null;
335344
this._requestTask = null;
336345
return this;
@@ -540,11 +549,13 @@ const DefaultController = {
540549
});
541550
},
542551

543-
deleteFile: function (name) {
552+
deleteFile: function (name: string, options?: FullOptions) {
544553
const headers = {
545554
'X-Parse-Application-ID': CoreManager.get('APPLICATION_ID'),
546-
'X-Parse-Master-Key': CoreManager.get('MASTER_KEY'),
547555
};
556+
if (options.useMasterKey) {
557+
headers['X-Parse-Master-Key'] = CoreManager.get('MASTER_KEY');
558+
}
548559
let url = CoreManager.get('SERVER_URL');
549560
if (url[url.length - 1] !== '/') {
550561
url += '/';

src/__tests__/ParseFile-test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,16 +821,31 @@ describe('FileController', () => {
821821
}
822822
});
823823

824+
it('should delete file with masterKey', async () => {
825+
const file = new ParseFile('filename', [1, 2, 3]);
826+
const ajax = jest.fn().mockResolvedValueOnce({ foo: 'bar' });
827+
CoreManager.setRESTController({ ajax, request: () => {} });
828+
CoreManager.set('MASTER_KEY', 'masterKey');
829+
const result = await file.destroy({ useMasterKey: true });
830+
expect(result).toEqual(file);
831+
expect(ajax).toHaveBeenCalledWith('DELETE', 'https://api.parse.com/1/files/filename', '', {
832+
'X-Parse-Application-ID': null,
833+
'X-Parse-Master-Key': 'masterKey',
834+
});
835+
CoreManager.set('MASTER_KEY', null);
836+
});
837+
824838
it('should delete file', async () => {
825839
const file = new ParseFile('filename', [1, 2, 3]);
826840
const ajax = jest.fn().mockResolvedValueOnce({ foo: 'bar' });
827841
CoreManager.setRESTController({ ajax, request: () => {} });
828-
const result = await file.destroy();
842+
CoreManager.set('MASTER_KEY', 'masterKey');
843+
const result = await file.destroy({ useMasterKey: false });
829844
expect(result).toEqual(file);
830845
expect(ajax).toHaveBeenCalledWith('DELETE', 'https://api.parse.com/1/files/filename', '', {
831846
'X-Parse-Application-ID': null,
832-
'X-Parse-Master-Key': null,
833847
});
848+
CoreManager.set('MASTER_KEY', null);
834849
});
835850

836851
it('should handle delete file error', async () => {

0 commit comments

Comments
 (0)