Repository: parse-community/Parse-SDK-JS
Pull request #845 (2019-07-03 22:54:24)

Fix Facebook login so it properly distinguishes between new and existing users.
fixes: #831

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/845?src=pr&el=h1) Report
> Merging [#845](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/845?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/4fcbabe8cacdde79269f95a9a0a460415dc767cc?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

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

```diff
@@            Coverage Diff             @@
##           master     #845      +/-   ##
==========================================
+ Coverage   92.07%   92.08%   +0.01%     
==========================================
  Files          54       54              
  Lines        4933     4941       +8     
  Branches     1097     1100       +3     
==========================================
+ Hits         4542     4550       +8     
  Misses        391      391
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/845?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseObject.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/845/diff?src=pr&el=tree#diff-c3JjL1BhcnNlT2JqZWN0Lmpz) | `90.09% <100%> (+0.06%)` | :arrow_up: |
| [src/RESTController.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/845/diff?src=pr&el=tree#diff-c3JjL1JFU1RDb250cm9sbGVyLmpz) | `81.63% <100%> (+0.25%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/845?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/845?src=pr&el=footer). Last update [4fcbabe...42082f1](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/845?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

dplewis: Thanks for looking into this. Can you write tests cases for this?

Here is an example for the issue you were having https://github.com/parse-community/Parse-SDK-JS/blob/master/integration/test/ParseUserTest.js#L697
fatbattk: Just pushed with requested changes.
davimacedo: Seems good to me. @dplewis could you also review again?

Patch:
diff --git a/src/ParseObject.js b/src/ParseObject.js
index 73ef826..cdd6156 100644
--- a/src/ParseObject.js
+++ b/src/ParseObject.js
@@ -2134,6 +2134,9 @@ const DefaultController = {
 
     const RESTController = CoreManager.getRESTController();
     const stateController = CoreManager.getObjectStateController();
+
+    options = options || {};
+    options.returnStatus = options.returnStatus || true;
     if (Array.isArray(target)) {
       if (target.length < 1) {
         return Promise.resolve([]);
@@ -2199,9 +2202,11 @@ const DefaultController = {
             batchReady.push(ready);
             const task = function() {
               ready.resolve();
-              return batchReturned.then((responses, status) => {
+              return batchReturned.then((responses) => {
                 if (responses[index].hasOwnProperty('success')) {
                   const objectId = responses[index].success.objectId;
+                  const status = responses[index]._status;
+                  delete responses[index]._status;
                   mapIdForPin[objectId] = obj._localId;
                   obj._handleSaveResponse(responses[index].success, status);
                 } else {
@@ -2228,9 +2233,7 @@ const DefaultController = {
                 return params;
               })
             }, options);
-          }).then((response, status) => {
-            batchReturned.resolve(response, status);
-          }, (error) => {
+          }).then(batchReturned.resolve, (error) => {
             batchReturned.reject(new ParseError(ParseError.INCORRECT_TYPE, error.message));
           });
 
@@ -2258,7 +2261,9 @@ const DefaultController = {
           params.path,
           params.body,
           options
-        ).then((response, status) => {
+        ).then((response) => {
+          const status = response._status;
+          delete response._status;
           targetCopy._handleSaveResponse(response, status);
         }, (error) => {
           targetCopy._handleSaveError();
diff --git a/src/RESTController.js b/src/RESTController.js
index df4d3e3..2a2ff09 100644
--- a/src/RESTController.js
+++ b/src/RESTController.js
@@ -16,6 +16,7 @@ export type RequestOptions = {
   useMasterKey?: boolean;
   sessionToken?: string;
   installationId?: string;
+  returnStatus?: boolean;
   batchSize?: number;
   include?: any;
   progress?: any;
@@ -255,8 +256,12 @@ const RESTController = {
       }
 
       const payloadString = JSON.stringify(payload);
-      return RESTController.ajax(method, url, payloadString, {}, options).then(({ response }) => {
-        return response;
+      return RESTController.ajax(method, url, payloadString, {}, options).then(({ response, status })=>{
+        if (options.returnStatus) {
+          return { ...response, _status: status };
+        } else {
+          return response;
+        }
       });
     }).catch(function(response: { responseText: string }) {
       // Transform the error into an instance of ParseError by trying to parse


Test patch:
diff --git a/integration/test/ParseUserTest.js b/integration/test/ParseUserTest.js
index 865fafb..1f492db 100644
--- a/integration/test/ParseUserTest.js
+++ b/integration/test/ParseUserTest.js
@@ -66,6 +66,7 @@ describe('Parse User', () => {
   it('can sign up users via static method', (done) => {
     Parse.User.signUp('asdf', 'zxcv').then((user) => {
       assert(user.getSessionToken());
+      expect(user.existed()).toBe(false);
       done();
     });
   });
@@ -76,6 +77,7 @@ describe('Parse User', () => {
     user.setUsername('zxcv');
     user.signUp().then((user) => {
       assert(user.getSessionToken());
+      expect(user.existed()).toBe(false);
       done();
     });
   });
@@ -101,6 +103,7 @@ describe('Parse User', () => {
       return Parse.User.logIn('asdf', 'zxcv');
     }).then((user) => {
       assert.equal(user.get('username'), 'asdf');
+      expect(user.existed()).toBe(true);
       done();
     });
   });
diff --git a/src/__tests__/ParseUser-test.js b/src/__tests__/ParseUser-test.js
index 55b4e41..ef0fea9 100644
--- a/src/__tests__/ParseUser-test.js
+++ b/src/__tests__/ParseUser-test.js
@@ -881,7 +881,7 @@ describe('ParseUser', () => {
     ParseUser._clearCache();
     CoreManager.setRESTController({
       request(method, path, body, options) {
-        expect(options).toEqual({ useMasterKey: true });
+        expect(options).toEqual(expect.objectContaining({ useMasterKey: true }));
         return Promise.resolve({
           objectId: 'uid5',
           sessionToken: 'r:123abc',
diff --git a/src/__tests__/RESTController-test.js b/src/__tests__/RESTController-test.js
index fe9788a..4d6ce9b 100644
--- a/src/__tests__/RESTController-test.js
+++ b/src/__tests__/RESTController-test.js
@@ -362,6 +362,16 @@ describe('RESTController', () => {
     });
   });
 
+  it('includes the status code when requested', (done) => {
+    RESTController._setXHR(mockXHR([{ status: 200, response: { success: true }}]));
+    RESTController.request('POST', 'users', {}, { returnStatus: true })
+      .then((response) => {
+        expect(response).toEqual(expect.objectContaining({ success: true }));
+        expect(response._status).toBe(200);
+        done();
+      });
+  });
+
   it('throws when attempted to use an unprovided master key', () => {
     CoreManager.set('MASTER_KEY', undefined);
     const xhr = {