From 0be64040f82fceb346ccb1035f5addc236d666f5 Mon Sep 17 00:00:00 2001 From: Andrew Fanton Date: Thu, 30 May 2019 08:57:39 -0400 Subject: [PATCH] [DEV-3514] Fix bug with expiration date calculation The nature of this bug was two-fold: 1.) A new Date was being instantiated with seconds, but the constructor expects milliseconds. 2.) The expiration period was not cast to a number, causing an expression to concatenate strings instead of adding numeric values; this greatly increased the actual expiration date. --- src/quote/BaseQuote.js | 14 +++---- test/quote/BaseQuoteTest.js | 73 +++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/quote/BaseQuote.js b/src/quote/BaseQuote.js index 471a79f..8f16c3a 100644 --- a/src/quote/BaseQuote.js +++ b/src/quote/BaseQuote.js @@ -285,7 +285,7 @@ module.exports = Class( 'BaseQuote' ) || ( !post_rate && !this._program.lockTimeout.preRateExpiration ) || ( post_rate && !this._program.lockTimeout.postRateExpiration )) { - return Number.POSITIVE_INFINITY; + return Infinity; } var reference_date = ( post_rate ) ? this._initialRatedDate : this._startDate; @@ -294,8 +294,8 @@ module.exports = Class( 'BaseQuote' ) : this._program.lockTimeout.preRateExpiration; // Use Date.setDate to accommodate leap seconds, leap years, DST, etc. - var expiration_date = new Date( reference_date ); - expiration_date.setDate( expiration_date.getDate() + expiration_period ); + var expiration_date = new Date( reference_date * 1000 ); + expiration_date.setDate( expiration_date.getDate() + +( expiration_period ) ); return expiration_date.getTime(); }, @@ -320,16 +320,16 @@ module.exports = Class( 'BaseQuote' ) var expiration_timestamp = this.getExpirationDate(); - // If the timestamp is INFINITY, the quote will never expire - // NOTE: The Date constructor does not support INFINITY as the timestamp - if ( expiration_timestamp === Number.POSITIVE_INFINITY ) + // If the timestamp is infinite, the quote will never expire + // NOTE: The Date constructor does not support `Infinity` as the timestamp + if ( expiration_timestamp === Infinity ) { return false; } // Use Date.setDate to accommodate leap seconds, leap years, DST, etc. var expiration_date = new Date( expiration_timestamp ); - expiration_date.setDate( expiration_date.getDate() + grace_period ); + expiration_date.setDate( expiration_date.getDate() + +( grace_period )); return current_date.getTime() > expiration_date.getTime(); }, diff --git a/test/quote/BaseQuoteTest.js b/test/quote/BaseQuoteTest.js index f1ba018..84092e9 100644 --- a/test/quote/BaseQuoteTest.js +++ b/test/quote/BaseQuoteTest.js @@ -245,9 +245,9 @@ describe( 'BaseQuote', () => preRateExpiration: 90, postRateExpiration: 30 }, - startDate: 86400000, - expirationDate: 7862400000, - currentDate: 86400000, + startDate: 86400, + expirationDate: 7862400, + currentDate: 86400, expired: false }, { @@ -258,9 +258,9 @@ describe( 'BaseQuote', () => postRateExpiration: 30 }, startDate: 86400000, - initialRatedDate: 172800000, - expirationDate: 2764800000, - currentDate: 172800000, + initialRatedDate: 172800, + expirationDate: 2764800, + currentDate: 172800, expired: false }, { @@ -271,9 +271,9 @@ describe( 'BaseQuote', () => postRateExpiration: 30 }, startDate: 86400, - initialRatedDate: 172800000, - expirationDate: 2764800000, - currentDate: 2851200000, + initialRatedDate: 172800, + expirationDate: 2764800, + currentDate: 2851200, expired: true }, { @@ -286,9 +286,9 @@ describe( 'BaseQuote', () => postRateGracePeriod: 5 }, startDate: 86400, - initialRatedDate: 172800000, - expirationDate: 2764800000, - currentDate: 2851200000, + initialRatedDate: 172800, + expirationDate: 2764800, + currentDate: 2851200, expired: false }, { @@ -298,9 +298,9 @@ describe( 'BaseQuote', () => preRateExpiration: 90, postRateExpiration: 30 }, - startDate: 86400000, - expirationDate: 7862400000, - currentDate: 5356800000, + startDate: 86400, + expirationDate: 7862400, + currentDate: 5356800, expired: false }, { @@ -310,10 +310,10 @@ describe( 'BaseQuote', () => preRateExpiration: 90, postRateExpiration: 30 }, - startDate: 86400000, - initialRatedDate: 172800000, - expirationDate: 2764800000, - currentDate: 5356800000, + startDate: 86400, + initialRatedDate: 172800, + expirationDate: 2764800, + currentDate: 5356800, expired: true }, { @@ -323,9 +323,9 @@ describe( 'BaseQuote', () => preRateExpiration: 90, postRateExpiration: 30 }, - startDate: 86400000, - expirationDate: 7862400000, - currentDate: 7948800000, + startDate: 86400, + expirationDate: 7862400, + currentDate: 7948800, expired: true }, { @@ -337,9 +337,9 @@ describe( 'BaseQuote', () => postRateExpiration: 30, postRateGracePeriod: 5 }, - startDate: 86400000, - expirationDate: 7862400000, - currentDate: 7948800000, + startDate: 86400, + expirationDate: 7862400, + currentDate: 7948800, expired: false }, { @@ -349,9 +349,9 @@ describe( 'BaseQuote', () => preRateExpiration: 90, postRateExpiration: 30 }, - startDate: 86400000, - expirationDate: 7862400000, - currentDate: 10540800000, + startDate: 86400, + expirationDate: 7862400, + currentDate: 10540800, expired: true }, { @@ -361,10 +361,10 @@ describe( 'BaseQuote', () => preRateExpiration: 90, postRateExpiration: 30 }, - startDate: 86400000, - initialRatedDate: 172800000, - expirationDate: 2764800000, - currentDate: 10540800000, + startDate: 86400, + initialRatedDate: 172800, + expirationDate: 2764800, + currentDate: 10540800, expired: true } @@ -374,8 +374,9 @@ describe( 'BaseQuote', () => const description = "Expiration is correct for " + testCase.description; const start_date = testCase.startDate; const initial_rated_date = testCase.initialRatedDate; - const current_date = testCase.currentDate; - const expiration_date = testCase.expirationDate; + const current_date = testCase.currentDate * 1000; + const expiration_date = ( typeof testCase.expirationDate === "number" ) + ? testCase.expirationDate * 1000 : undefined; const expired = testCase.expired; quote.setProgram( createStubProgram( testCase.lockTimeout ) ); @@ -394,10 +395,10 @@ describe( 'BaseQuote', () => if ( expiration_date !== undefined ) { - expect( quote.getExpirationDate() ).to.equal( +expiration_date ); + expect( quote.getExpirationDate() ).to.equal( expiration_date ); } - expect( quote.hasExpired( new Date( current_date ) ) ).to.equal( !!expired ); + expect( quote.hasExpired( new Date( current_date ) ) ).to.equal( expired ); } ); } ); } );