From 7c95bd1836485449e2876bb5127abfa7a70e8d59 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Sun, 15 Nov 2020 06:52:45 -0500 Subject: [PATCH] fix(rc): Add more unit tests for timestamp validation --- src/remote-config/remote-config.ts | 6 ++-- test/unit/remote-config/remote-config.spec.ts | 30 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/remote-config/remote-config.ts b/src/remote-config/remote-config.ts index ba25f9c011..79a261687c 100644 --- a/src/remote-config/remote-config.ts +++ b/src/remote-config/remote-config.ts @@ -362,8 +362,8 @@ class VersionImpl implements Version { this.isLegacy = version.isLegacy; } - // The backend API provides timestamps as ISO date strings. The Admin SDK exposes timestamps - // as UTC date strings. If a developer uses a previously obtained template with UTC timestamps + // The backend API provides timestamps in ISO date strings. The Admin SDK exposes timestamps + // in UTC date strings. If a developer uses a previously obtained template with UTC timestamps // we could still validate it below. if (typeof version.updateTime !== 'undefined') { if (!this.isValidTimestamp(version.updateTime)) { @@ -392,6 +392,8 @@ class VersionImpl implements Version { } private isValidTimestamp(timestamp: string): boolean { + // This validation fails for timestamps earlier than January 1, 1970 and considers strings + // such as "1.2" as valid timestamps. return validator.isNonEmptyString(timestamp) && (new Date(timestamp)).getTime() > 0; } } diff --git a/test/unit/remote-config/remote-config.spec.ts b/test/unit/remote-config/remote-config.spec.ts index 462d46341e..e2f16a032f 100644 --- a/test/unit/remote-config/remote-config.spec.ts +++ b/test/unit/remote-config/remote-config.spec.ts @@ -681,7 +681,7 @@ describe('RemoteConfig', () => { }); }); - it('should resolve with template when Version updateTime contains only 3 ms places', () => { + it('should resolve with template when Version updateTime contains 3 fractional secs places', () => { const response = deepCopy(REMOTE_CONFIG_RESPONSE); const versionInfo = deepCopy(VERSION_INFO); versionInfo.updateTime = '2020-11-03T20:24:15.203Z'; @@ -707,7 +707,7 @@ describe('RemoteConfig', () => { }); }); - it('should resolve with template when Version updateTime contains 6 ms places', () => { + it('should resolve with template when Version updateTime contains 6 fractional secs places', () => { const response = deepCopy(REMOTE_CONFIG_RESPONSE); const versionInfo = deepCopy(VERSION_INFO); versionInfo.updateTime = '2020-11-13T17:01:36.541527Z'; @@ -732,5 +732,31 @@ describe('RemoteConfig', () => { expect(version.updateTime).to.equal('Fri, 13 Nov 2020 17:01:36 GMT'); }); }); + + it('should resolve with template when Version updateTime contains 9 fractional secs places', () => { + const response = deepCopy(REMOTE_CONFIG_RESPONSE); + const versionInfo = deepCopy(VERSION_INFO); + versionInfo.updateTime = '2020-11-13T17:01:36.541527341Z'; + response.version = versionInfo; + const stub = sinon + .stub(RemoteConfigApiClient.prototype, operationName) + .resolves(response); + stubs.push(stub); + + return rcOperation() + .then((template) => { + expect(template.etag).to.equal('etag-123456789012-5'); + + const version = template.version!; + expect(version.versionNumber).to.equal('86'); + expect(version.updateOrigin).to.equal('ADMIN_SDK_NODE'); + expect(version.updateType).to.equal('INCREMENTAL_UPDATE'); + expect(version.updateUser).to.deep.equal({ + email: 'firebase-adminsdk@gserviceaccount.com' + }); + expect(version.description).to.equal('production version'); + expect(version.updateTime).to.equal('Fri, 13 Nov 2020 17:01:36 GMT'); + }); + }); } });