Skip to content

Commit b60191e

Browse files
chore(rc): Add more unit tests for timestamp validation (#1092)
1 parent 8755b2f commit b60191e

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/remote-config/remote-config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ class VersionImpl implements Version {
362362
this.isLegacy = version.isLegacy;
363363
}
364364

365-
// The backend API provides timestamps as ISO date strings. The Admin SDK exposes timestamps
366-
// as UTC date strings. If a developer uses a previously obtained template with UTC timestamps
365+
// The backend API provides timestamps in ISO date strings. The Admin SDK exposes timestamps
366+
// in UTC date strings. If a developer uses a previously obtained template with UTC timestamps
367367
// we could still validate it below.
368368
if (typeof version.updateTime !== 'undefined') {
369369
if (!this.isValidTimestamp(version.updateTime)) {
@@ -392,6 +392,8 @@ class VersionImpl implements Version {
392392
}
393393

394394
private isValidTimestamp(timestamp: string): boolean {
395+
// This validation fails for timestamps earlier than January 1, 1970 and considers strings
396+
// such as "1.2" as valid timestamps.
395397
return validator.isNonEmptyString(timestamp) && (new Date(timestamp)).getTime() > 0;
396398
}
397399
}

test/unit/remote-config/remote-config.spec.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,10 @@ describe('RemoteConfig', () => {
681681
});
682682
});
683683

684-
it('should resolve with template when Version updateTime contains only 3 ms places', () => {
684+
it('should resolve with template when Version updateTime contains 3 digits in fractional seconds', () => {
685685
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
686686
const versionInfo = deepCopy(VERSION_INFO);
687-
versionInfo.updateTime = '2020-11-03T20:24:15.203Z';
687+
versionInfo.updateTime = '2020-10-03T17:14:10.203Z';
688688
response.version = versionInfo;
689689
const stub = sinon
690690
.stub(RemoteConfigApiClient.prototype, operationName)
@@ -703,14 +703,14 @@ describe('RemoteConfig', () => {
703703
704704
});
705705
expect(version.description).to.equal('production version');
706-
expect(version.updateTime).to.equal('Tue, 03 Nov 2020 20:24:15 GMT');
706+
expect(version.updateTime).to.equal('Sat, 03 Oct 2020 17:14:10 GMT');
707707
});
708708
});
709709

710-
it('should resolve with template when Version updateTime contains 6 ms places', () => {
710+
it('should resolve with template when Version updateTime contains 6 digits in fractional seconds', () => {
711711
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
712712
const versionInfo = deepCopy(VERSION_INFO);
713-
versionInfo.updateTime = '2020-11-13T17:01:36.541527Z';
713+
versionInfo.updateTime = '2020-08-14T17:01:36.541527Z';
714714
response.version = versionInfo;
715715
const stub = sinon
716716
.stub(RemoteConfigApiClient.prototype, operationName)
@@ -729,7 +729,33 @@ describe('RemoteConfig', () => {
729729
730730
});
731731
expect(version.description).to.equal('production version');
732-
expect(version.updateTime).to.equal('Fri, 13 Nov 2020 17:01:36 GMT');
732+
expect(version.updateTime).to.equal('Fri, 14 Aug 2020 17:01:36 GMT');
733+
});
734+
});
735+
736+
it('should resolve with template when Version updateTime contains 9 digits in fractional seconds', () => {
737+
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
738+
const versionInfo = deepCopy(VERSION_INFO);
739+
versionInfo.updateTime = '2020-11-15T06:57:26.342763941Z';
740+
response.version = versionInfo;
741+
const stub = sinon
742+
.stub(RemoteConfigApiClient.prototype, operationName)
743+
.resolves(response);
744+
stubs.push(stub);
745+
746+
return rcOperation()
747+
.then((template) => {
748+
expect(template.etag).to.equal('etag-123456789012-5');
749+
750+
const version = template.version!;
751+
expect(version.versionNumber).to.equal('86');
752+
expect(version.updateOrigin).to.equal('ADMIN_SDK_NODE');
753+
expect(version.updateType).to.equal('INCREMENTAL_UPDATE');
754+
expect(version.updateUser).to.deep.equal({
755+
756+
});
757+
expect(version.description).to.equal('production version');
758+
expect(version.updateTime).to.equal('Sun, 15 Nov 2020 06:57:26 GMT');
733759
});
734760
});
735761
}

0 commit comments

Comments
 (0)