Repository: parse-community/Parse-SDK-JS
Pull request #1056 (2020-01-08 21:35:58)

Fix user.become for AsyncStorage
Closes: https://github.com/parse-community/Parse-SDK-JS/issues/1052

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

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

```diff
@@            Coverage Diff             @@
##           master    #1056      +/-   ##
==========================================
- Coverage   92.19%   92.19%   -0.01%     
==========================================
  Files          54       54              
  Lines        5151     5150       -1     
  Branches     1145     1145              
==========================================
- Hits         4749     4748       -1     
  Misses        402      402
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1056?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseUser.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/1056/diff?src=pr&el=tree#diff-c3JjL1BhcnNlVXNlci5qcw==) | `83.59% <100%> (-0.04%)` | :arrow_down: |

------

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


Patch:
diff --git a/src/ParseUser.js b/src/ParseUser.js
index 2752d89..5014e37 100644
--- a/src/ParseUser.js
+++ b/src/ParseUser.js
@@ -891,16 +891,15 @@ const DefaultController = {
     return Storage.removeItemAsync(path);
   },
 
-  setCurrentUser(user) {
-    const currentUser = this.currentUser();
-    let promise = Promise.resolve();
+  async setCurrentUser(user) {
+    const currentUser = await this.currentUserAsync();
     if (currentUser && !user.equals(currentUser) && AnonymousUtils.isLinked(currentUser)) {
-      promise = currentUser.destroy({ sessionToken: currentUser.getSessionToken() })
+      await currentUser.destroy({ sessionToken: currentUser.getSessionToken() })
     }
     currentUserCache = user;
     user._cleanupAuthData();
     user._synchronizeAllAuthData();
-    return promise.then(() => DefaultController.updateUserOnDisk(user));
+    return DefaultController.updateUserOnDisk(user);
   },
 
   currentUser(): ?ParseUser {


Test patch:
diff --git a/src/__tests__/ParseUser-test.js b/src/__tests__/ParseUser-test.js
index 3331b5d..78d6cf8 100644
--- a/src/__tests__/ParseUser-test.js
+++ b/src/__tests__/ParseUser-test.js
@@ -35,7 +35,9 @@ jest.mock('uuid/v4', () => {
   return () => value++;
 });
 jest.dontMock('./test_helpers/mockXHR');
+jest.dontMock('./test_helpers/mockAsyncStorage');
 
+const mockAsyncStorage = require('./test_helpers/mockAsyncStorage');
 const CoreManager = require('../CoreManager');
 const CryptoController = require('../CryptoController');
 const LocalDatastore = require('../LocalDatastore');
@@ -299,6 +301,34 @@ describe('ParseUser', () => {
     });
   });
 
+  it('can become a user with async storage', async () => {
+    const currentStorage = CoreManager.getStorageController();
+    CoreManager.setStorageController(mockAsyncStorage);
+    ParseUser.enableUnsafeCurrentUser();
+    ParseUser._clearCache();
+    CoreManager.setRESTController({
+      request(method, path, body, options) {
+        expect(method).toBe('GET');
+        expect(path).toBe('users/me');
+        expect(options.sessionToken).toBe('123abc');
+
+        return Promise.resolve({
+          objectId: 'uid3',
+          username: 'username',
+          sessionToken: '123abc'
+        }, 200);
+      },
+      ajax() {}
+    });
+
+    const u = await ParseUser.become('123abc');
+    expect(u.id).toBe('uid3');
+    expect(u.isCurrent()).toBe(true);
+    expect(u.existed()).toBe(true);
+    CoreManager.setStorageController(currentStorage);
+  });
+
+
   it('can hydrate a user with sessionToken in server environment', async () => {
     ParseUser.enableUnsafeCurrentUser();
     ParseUser._clearCache();
diff --git a/src/__tests__/test_helpers/mockAsyncStorage.js b/src/__tests__/test_helpers/mockAsyncStorage.js
new file mode 100644
index 0000000..cc617d2
--- /dev/null
+++ b/src/__tests__/test_helpers/mockAsyncStorage.js
@@ -0,0 +1,25 @@
+let mockStorage = {};
+const mockAsyncStorage = {
+  async: 1,
+  async getItemAsync(path) {
+    return mockStorage[path];
+  },
+
+  async setItemAsync(path, value) {
+    mockStorage[path] = value;
+  },
+
+  async removeItemAsync(path) {
+    delete mockStorage[path];
+  },
+
+  async getAllKeysAsync() {
+    return Object.keys(mockStorage);
+  },
+
+  clear() {
+    mockStorage = {};
+  },
+};
+
+module.exports = mockAsyncStorage;