Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rc): Add more unit tests for timestamp validation #1092

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/remote-config/remote-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}
}
38 changes: 32 additions & 6 deletions test/unit/remote-config/remote-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,10 +681,10 @@ 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 digits in fractional seconds', () => {
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
const versionInfo = deepCopy(VERSION_INFO);
versionInfo.updateTime = '2020-11-03T20:24:15.203Z';
versionInfo.updateTime = '2020-10-03T17:14:10.203Z';
response.version = versionInfo;
const stub = sinon
.stub(RemoteConfigApiClient.prototype, operationName)
Expand All @@ -703,14 +703,14 @@ describe('RemoteConfig', () => {
email: '[email protected]'
});
expect(version.description).to.equal('production version');
expect(version.updateTime).to.equal('Tue, 03 Nov 2020 20:24:15 GMT');
expect(version.updateTime).to.equal('Sat, 03 Oct 2020 17:14:10 GMT');
});
});

it('should resolve with template when Version updateTime contains 6 ms places', () => {
it('should resolve with template when Version updateTime contains 6 digits in fractional seconds', () => {
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
const versionInfo = deepCopy(VERSION_INFO);
versionInfo.updateTime = '2020-11-13T17:01:36.541527Z';
versionInfo.updateTime = '2020-08-14T17:01:36.541527Z';
response.version = versionInfo;
const stub = sinon
.stub(RemoteConfigApiClient.prototype, operationName)
Expand All @@ -729,7 +729,33 @@ describe('RemoteConfig', () => {
email: '[email protected]'
});
expect(version.description).to.equal('production version');
expect(version.updateTime).to.equal('Fri, 13 Nov 2020 17:01:36 GMT');
expect(version.updateTime).to.equal('Fri, 14 Aug 2020 17:01:36 GMT');
});
});

it('should resolve with template when Version updateTime contains 9 digits in fractional seconds', () => {
const response = deepCopy(REMOTE_CONFIG_RESPONSE);
const versionInfo = deepCopy(VERSION_INFO);
versionInfo.updateTime = '2020-11-15T06:57:26.342763941Z';
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: '[email protected]'
});
expect(version.description).to.equal('production version');
expect(version.updateTime).to.equal('Sun, 15 Nov 2020 06:57:26 GMT');
});
});
}
Expand Down