Repository: parse-community/Parse-SDK-JS
Pull request #1285 (2021-01-11 20:16:11)

Add useMasterKey option to Parse.File.destroy()
Closes: https://github.com/parse-community/Parse-SDK-JS/issues/1272

peril-parse-community[bot]: 
<!--
  0 failure: 
  0 warning: 
  
  1 markdown notices
  DangerID: danger-id-Peril;
-->



Danger run resulted in 1 warning; to find out more, see the [checks page](https://github.com/parse-community/Parse-SDK-JS/runs/1683795671).
<p align="right">
  Generated by :no_entry_sign: <a href="https://danger.systems/js">dangerJS</a>
</p>

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285?src=pr&el=h1) Report
> Merging [#1285](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285?src=pr&el=desc) (6adbeba) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/a6bbd49be2284e9befcd9d52a8eeeda7e3a4e049?el=desc) (a6bbd49) will **decrease** coverage by `0.01%`.
> The diff coverage is `87.50%`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285/graphs/tree.svg?width=650&height=150&src=pr&token=8nCENM0QHj)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##            master    #1285      +/-   ##
===========================================
- Coverage   100.00%   99.98%   -0.02%     
===========================================
  Files           58       58              
  Lines         5645     5650       +5     
  Branches      1270     1273       +3     
===========================================
+ Hits          5645     5649       +4     
- Misses           0        1       +1     
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseFile.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285/diff?src=pr&el=tree#diff-c3JjL1BhcnNlRmlsZS5qcw==) | `99.57% <87.50%> (-0.43%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285?src=pr&el=footer). Last update [a6bbd49...6adbeba](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1285?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/ParseFile.js b/src/ParseFile.js
index d4ff9b5..9470b48 100644
--- a/src/ParseFile.js
+++ b/src/ParseFile.js
@@ -323,14 +323,23 @@ class ParseFile {
    * Deletes the file from the Parse cloud.
    * In Cloud Code and Node only with Master Key.
    *
+   * @param {object} options
+   *  * Valid options are:<ul>
+   *   <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
+   *     be used for this request.
+   * <pre>
    * @returns {Promise} Promise that is resolved when the delete finishes.
    */
-  destroy() {
+  destroy(options?: FullOptions = {}) {
     if (!this._name) {
       throw new ParseError(ParseError.FILE_DELETE_UNNAMED_ERROR, 'Cannot delete an unnamed file.');
     }
+    const destroyOptions = { useMasterKey: true };
+    if (options.hasOwnProperty('useMasterKey')) {
+      destroyOptions.useMasterKey = options.useMasterKey;
+    }
     const controller = CoreManager.getFileController();
-    return controller.deleteFile(this._name).then(() => {
+    return controller.deleteFile(this._name, destroyOptions).then(() => {
       this._data = null;
       this._requestTask = null;
       return this;
@@ -540,11 +549,13 @@ const DefaultController = {
     });
   },
 
-  deleteFile: function (name) {
+  deleteFile: function (name: string, options?: FullOptions) {
     const headers = {
       'X-Parse-Application-ID': CoreManager.get('APPLICATION_ID'),
-      'X-Parse-Master-Key': CoreManager.get('MASTER_KEY'),
     };
+    if (options.useMasterKey) {
+      headers['X-Parse-Master-Key'] = CoreManager.get('MASTER_KEY');
+    }
     let url = CoreManager.get('SERVER_URL');
     if (url[url.length - 1] !== '/') {
       url += '/';
diff --git a/src/ParseUser.js b/src/ParseUser.js
index 65e6444..9ea7f14 100644
--- a/src/ParseUser.js
+++ b/src/ParseUser.js
@@ -436,7 +436,7 @@ class ParseUser extends ParseObject {
   logIn(options?: FullOptions): Promise<ParseUser> {
     options = options || {};
 
-    const loginOptions = {};
+    const loginOptions = { usePost: true };
     if (options.hasOwnProperty('useMasterKey')) {
       loginOptions.useMasterKey = options.useMasterKey;
     }


Test patch:
diff --git a/src/__tests__/ParseFile-test.js b/src/__tests__/ParseFile-test.js
index afa210b..631a6c1 100644
--- a/src/__tests__/ParseFile-test.js
+++ b/src/__tests__/ParseFile-test.js
@@ -821,16 +821,31 @@ describe('FileController', () => {
     }
   });
 
+  it('should delete file with masterKey', async () => {
+    const file = new ParseFile('filename', [1, 2, 3]);
+    const ajax = jest.fn().mockResolvedValueOnce({ foo: 'bar' });
+    CoreManager.setRESTController({ ajax, request: () => {} });
+    CoreManager.set('MASTER_KEY', 'masterKey');
+    const result = await file.destroy({ useMasterKey: true });
+    expect(result).toEqual(file);
+    expect(ajax).toHaveBeenCalledWith('DELETE', 'https://api.parse.com/1/files/filename', '', {
+      'X-Parse-Application-ID': null,
+      'X-Parse-Master-Key': 'masterKey',
+    });
+    CoreManager.set('MASTER_KEY', null);
+  });
+
   it('should delete file', async () => {
     const file = new ParseFile('filename', [1, 2, 3]);
     const ajax = jest.fn().mockResolvedValueOnce({ foo: 'bar' });
     CoreManager.setRESTController({ ajax, request: () => {} });
-    const result = await file.destroy();
+    CoreManager.set('MASTER_KEY', 'masterKey');
+    const result = await file.destroy({ useMasterKey: false });
     expect(result).toEqual(file);
     expect(ajax).toHaveBeenCalledWith('DELETE', 'https://api.parse.com/1/files/filename', '', {
       'X-Parse-Application-ID': null,
-      'X-Parse-Master-Key': null,
     });
+    CoreManager.set('MASTER_KEY', null);
   });
 
   it('should handle delete file error', async () => {
diff --git a/src/__tests__/ParseUser-test.js b/src/__tests__/ParseUser-test.js
index e081ecb..1e85375 100644
--- a/src/__tests__/ParseUser-test.js
+++ b/src/__tests__/ParseUser-test.js
@@ -223,7 +223,7 @@ describe('ParseUser', () => {
     ParseUser._clearCache();
     CoreManager.setRESTController({
       request(method, path, body) {
-        expect(method).toBe('GET');
+        expect(method).toBe('POST');
         expect(path).toBe('login');
         expect(body.username).toBe('username');
         expect(body.password).toBe('password');
@@ -259,7 +259,7 @@ describe('ParseUser', () => {
     ParseUser._clearCache();
     CoreManager.setRESTController({
       request(method, path, body, options) {
-        expect(method).toBe('GET');
+        expect(method).toBe('POST');
         expect(path).toBe('login');
         expect(body.username).toBe('username');
         expect(body.password).toBe('password');
@@ -288,7 +288,7 @@ describe('ParseUser', () => {
     ParseUser._clearCache();
     CoreManager.setRESTController({
       request(method, path, body) {
-        expect(method).toBe('POST');
+        expect(method).toBe('GET');
         expect(path).toBe('login');
         expect(body.username).toBe('username');
         expect(body.password).toBe('password');
@@ -304,7 +304,7 @@ describe('ParseUser', () => {
       },
       ajax() {},
     });
-    ParseUser.logIn('username', 'password', { usePost: true }).then(u => {
+    ParseUser.logIn('username', 'password', { usePost: false }).then(u => {
       expect(u.id).toBe('uid2');
       expect(u.getSessionToken()).toBe('123abc');
       expect(u.isCurrent()).toBe(true);
@@ -342,7 +342,7 @@ describe('ParseUser', () => {
     ParseUser._clearCache();
     CoreManager.setRESTController({
       request(method, path, body) {
-        expect(method).toBe('GET');
+        expect(method).toBe('POST');
         expect(path).toBe('login');
         expect(body.username).toBe('username');
         expect(body.password).toBe('password');
@@ -1469,7 +1469,7 @@ describe('ParseUser', () => {
     let u = null;
     CoreManager.setRESTController({
       request(method, path, body) {
-        expect(method).toBe('GET');
+        expect(method).toBe('POST');
         expect(path).toBe('login');
         expect(body.username).toBe('username');
         expect(body.password).toBe('password');
@@ -1524,7 +1524,7 @@ describe('ParseUser', () => {
     let u = null;
     CoreManager.setRESTController({
       request(method, path, body) {
-        expect(method).toBe('GET');
+        expect(method).toBe('POST');
         expect(path).toBe('login');
         expect(body.username).toBe('username');
         expect(body.password).toBe('password');