Skip to content

Commit 2781eff

Browse files
fix(rc): Fix Version update time parsing failure (#1089)
* fix(rc): Fix Version update time parsing failure * Add test cases for 3 and 6 ms places
1 parent 13afb20 commit 2781eff

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

src/remote-config/remote-config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,12 @@ class VersionImpl implements Version {
366366
// as 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') {
369-
if (!validator.isISODateString(version.updateTime) &&
370-
!validator.isUTCDateString(version.updateTime)) {
369+
if (!this.isValidTimestamp(version.updateTime)) {
371370
throw new FirebaseRemoteConfigError(
372371
'invalid-argument',
373372
'Version update time must be a valid date string');
374373
}
375-
if (validator.isISODateString(version.updateTime)) {
376-
// timestamps in output `Version` obtained from the API should be in UTC.
377-
this.updateTime = new Date(version.updateTime).toUTCString();
378-
}
374+
this.updateTime = new Date(version.updateTime).toUTCString();
379375
}
380376
}
381377

@@ -394,4 +390,8 @@ class VersionImpl implements Version {
394390
updateTime: this.updateTime,
395391
}
396392
}
393+
394+
private isValidTimestamp(timestamp: string): boolean {
395+
return validator.isNonEmptyString(timestamp) && (new Date(timestamp)).getTime() > 0;
396+
}
397397
}

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('RemoteConfig', () => {
6464
6565
},
6666
description: 'production version',
67-
updateTime: '2020-06-15T16:45:03.000Z'
67+
updateTime: '2020-06-15T16:45:03.541527Z'
6868
};
6969

7070
const REMOTE_CONFIG_RESPONSE: {
@@ -123,7 +123,7 @@ describe('RemoteConfig', () => {
123123
versions: [
124124
{
125125
versionNumber: '78',
126-
updateTime: '2020-05-07T18:46:09.495Z',
126+
updateTime: '2020-05-07T18:46:09.495234Z',
127127
updateUser: {
128128
129129
imageUrl: 'https://photo.jpg'
@@ -680,5 +680,57 @@ describe('RemoteConfig', () => {
680680
expect(parsed).deep.equals(expectedTemplate);
681681
});
682682
});
683+
684+
it('should resolve with template when Version updateTime contains only 3 ms places', () => {
685+
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
686+
const versionInfo = deepCopy(VERSION_INFO);
687+
versionInfo.updateTime = '2020-11-03T20:24:15.203Z';
688+
response.version = versionInfo;
689+
const stub = sinon
690+
.stub(RemoteConfigApiClient.prototype, operationName)
691+
.resolves(response);
692+
stubs.push(stub);
693+
694+
return rcOperation()
695+
.then((template) => {
696+
expect(template.etag).to.equal('etag-123456789012-5');
697+
698+
const version = template.version!;
699+
expect(version.versionNumber).to.equal('86');
700+
expect(version.updateOrigin).to.equal('ADMIN_SDK_NODE');
701+
expect(version.updateType).to.equal('INCREMENTAL_UPDATE');
702+
expect(version.updateUser).to.deep.equal({
703+
704+
});
705+
expect(version.description).to.equal('production version');
706+
expect(version.updateTime).to.equal('Tue, 03 Nov 2020 20:24:15 GMT');
707+
});
708+
});
709+
710+
it('should resolve with template when Version updateTime contains 6 ms places', () => {
711+
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
712+
const versionInfo = deepCopy(VERSION_INFO);
713+
versionInfo.updateTime = '2020-11-13T17:01:36.541527Z';
714+
response.version = versionInfo;
715+
const stub = sinon
716+
.stub(RemoteConfigApiClient.prototype, operationName)
717+
.resolves(response);
718+
stubs.push(stub);
719+
720+
return rcOperation()
721+
.then((template) => {
722+
expect(template.etag).to.equal('etag-123456789012-5');
723+
724+
const version = template.version!;
725+
expect(version.versionNumber).to.equal('86');
726+
expect(version.updateOrigin).to.equal('ADMIN_SDK_NODE');
727+
expect(version.updateType).to.equal('INCREMENTAL_UPDATE');
728+
expect(version.updateUser).to.deep.equal({
729+
730+
});
731+
expect(version.description).to.equal('production version');
732+
expect(version.updateTime).to.equal('Fri, 13 Nov 2020 17:01:36 GMT');
733+
});
734+
});
683735
}
684736
});

0 commit comments

Comments
 (0)