From 8ac5f34d4c29cf4c8cf272af263a93d5878bab38 Mon Sep 17 00:00:00 2001 From: joshAg Date: Tue, 4 Dec 2018 17:12:30 -0800 Subject: [PATCH 1/4] add toJSON function to MockFirebaseUser --- src/user.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/user.js b/src/user.js index dd5f6948..e5c86e3f 100644 --- a/src/user.js +++ b/src/user.js @@ -16,7 +16,7 @@ function MockFirebaseUser(ref, data) { this.emailVerified = !!data.emailVerified; this.isAnonymous = !!data.isAnonymous; this.metadata = data.metadata; - this.providerData = data.providerData; + this.providerData = data.providerData || []; this.providerId = data.providerId; this.refreshToken = data.refreshToken; } @@ -111,4 +111,26 @@ MockFirebaseUser.prototype.getIdToken = function (forceRefresh) { }); }; +MockFirebaseUser.prototype.toJSON = function() { + const json = { + uid: this.uid, + email: this.email, + emailVerified: this.emailVerified, + displayName: this.displayName, + photoURL: this.photoURL, + phoneNumber: this.phoneNumber, + disabled: this.disabled, + metadata: this.metadata && this.metadata.toJSON(), + passwordHash: this.passwordHash, + passwordSalt: this.passwordSalt, + customClaims: JSON.parse(JSON.stringify(this.customClaims)), + tokensValidAfterTime: this.tokensValidAfterTime, + }; + json.providerData = []; + for (const entry of this.providerData) { + json.providerData.push(entry.toJSON()); + } + return json; +} + module.exports = MockFirebaseUser; From 8c42be1b488326c015fecb8a432484e587dea06c Mon Sep 17 00:00:00 2001 From: Dolan Murvihill Date: Wed, 20 Nov 2019 10:12:10 -0500 Subject: [PATCH 2/4] Fix linter --- src/user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user.js b/src/user.js index e5c86e3f..9374ae03 100644 --- a/src/user.js +++ b/src/user.js @@ -131,6 +131,6 @@ MockFirebaseUser.prototype.toJSON = function() { json.providerData.push(entry.toJSON()); } return json; -} +}; module.exports = MockFirebaseUser; From 93c7f14d824bf796cc1c4cdea33fb5bb38f838a9 Mon Sep 17 00:00:00 2001 From: Dolan Murvihill Date: Tue, 26 Nov 2019 17:29:10 -0500 Subject: [PATCH 3/4] Update to partially mock Firebase 5 --- src/user.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/user.js b/src/user.js index 9374ae03..960aa852 100644 --- a/src/user.js +++ b/src/user.js @@ -119,13 +119,11 @@ MockFirebaseUser.prototype.toJSON = function() { displayName: this.displayName, photoURL: this.photoURL, phoneNumber: this.phoneNumber, - disabled: this.disabled, - metadata: this.metadata && this.metadata.toJSON(), - passwordHash: this.passwordHash, - passwordSalt: this.passwordSalt, - customClaims: JSON.parse(JSON.stringify(this.customClaims)), - tokensValidAfterTime: this.tokensValidAfterTime, }; + if (this.metadata) { + json.createdAt = this.metadata.createdAt; + json.lastLoginAt = this.metadata.lastLoginAt; + } json.providerData = []; for (const entry of this.providerData) { json.providerData.push(entry.toJSON()); From 3f4c2cce2a1ddaf78ec245087ffa592f17a11569 Mon Sep 17 00:00:00 2001 From: Dolan Murvihill Date: Tue, 26 Nov 2019 17:29:45 -0500 Subject: [PATCH 4/4] Add tests --- test/unit/user.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/unit/user.js b/test/unit/user.js index 93f03293..dd4da6d1 100644 --- a/test/unit/user.js +++ b/test/unit/user.js @@ -140,4 +140,46 @@ describe('User', function () { return expect(user.getIdToken(true)).to.eventually.not.equal(token); }); }); + + describe('#toJSON', () => { + describe('most fields', () => { + it('should be the same', () => { + const user = new User(auth, {}); + const json = user.toJSON(); + [ + 'uid', + 'email', + 'emailVerified', + 'displayName', + 'photoURL', + 'phoneNumber', + 'providerData', + ].forEach(k => expect(json[k]).to.deep.equal(user[k])); + }); + }); + + describe('.metadata', () => { + it('keys should be missing if omitted', () => { + const user = new User(auth, {}); + expect(user.toJSON()).not.to.haveOwnProperty('lastLoginAt'); + expect(user.toJSON()).not.to.haveOwnProperty('createdAt'); + }); + + it('should populate to lastLogin if present', () => { + const metadata = { + lastLoginAt: new Date(11).getTime().toString(10), + }; + const user = new User(auth, { metadata: metadata, }); + expect(user.toJSON().lastLoginAt).to.equal(metadata.lastLoginAt); + }); + + it('should populate to createdAt if present', () => { + const metadata = { + createdAt: new Date(12).getTime().toString(10), + }; + const user = new User(auth, { metadata: metadata, }); + expect(user.toJSON().createdAt).to.equal(metadata.createdAt); + }); + }); + }); });