-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert TokenModel to an ES6 class and extract utils functi…
…on for calculating lifetime Merge pull request #223 from menewman/fix-convert-token-model-to-es6 thanks to @menewman
- Loading branch information
Showing
4 changed files
with
129 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @param expiresAt {Date} The date at which something (e.g. a token) expires. | ||
* @return {number} The number of seconds until the expiration date. | ||
*/ | ||
function getLifetimeFromExpiresAt(expiresAt) { | ||
return Math.floor((expiresAt - new Date()) / 1000); | ||
} | ||
|
||
module.exports = { | ||
getLifetimeFromExpiresAt, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const dateUtil = require('../../../lib/utils/date-util'); | ||
|
||
const sinon = require('sinon'); | ||
require('chai').should(); | ||
|
||
describe('DateUtil', function() { | ||
describe('getLifetimeFromExpiresAt', () => { | ||
const now = new Date('2023-01-01T00:00:00.000Z'); | ||
|
||
beforeEach(() => { | ||
sinon.useFakeTimers(now); | ||
}); | ||
|
||
it('should convert a valid expiration date into seconds from now', () => { | ||
const expiresAt = new Date('2023-01-01T00:00:10.000Z'); | ||
const lifetime = dateUtil.getLifetimeFromExpiresAt(expiresAt); | ||
|
||
lifetime.should.be.a('number'); | ||
lifetime.should.be.approximately(10, 2); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
}); | ||
}); |