mirror of
https://github.com/hanzoai/node-jsonwebtoken.git
synced 2026-09-24 17:00:48 +00:00
Use lolex for faking date in tests (#491)
Sinon.JS provides a project called lolex to handle faking dates. This change replaces the fakeDate utility with the equivalent Sinon.JS code.
This commit is contained in:
+2
-2
@@ -4,7 +4,7 @@
|
||||
"description": "JSON Web Token implementation (symmetric and asymmetric)",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "nyc --reporter=html --reporter=text mocha --require test/util/fakeDate && nsp check && cost-of-modules"
|
||||
"test": "nyc --reporter=html --reporter=text mocha && nsp check && cost-of-modules"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -37,7 +37,7 @@
|
||||
"mocha": "^2.1.0",
|
||||
"nsp": "^2.6.2",
|
||||
"nyc": "^11.8.0",
|
||||
"sinon": "^1.15.4"
|
||||
"sinon": "^6.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"npm": ">=1.4.28",
|
||||
|
||||
@@ -5,6 +5,7 @@ var path = require('path');
|
||||
var expect = require('chai').expect;
|
||||
var assert = require('chai').assert;
|
||||
var ms = require('ms');
|
||||
var sinon = require('sinon');
|
||||
|
||||
function loadKey(filename) {
|
||||
return fs.readFileSync(path.join(__dirname, filename));
|
||||
@@ -140,14 +141,14 @@ describe('Asymmetric Algorithms', function(){
|
||||
|
||||
|
||||
it('should valid when date are equals', function (done) {
|
||||
Date.fix(1451908031);
|
||||
var fakeClock = sinon.useFakeTimers({now: 1451908031});
|
||||
|
||||
token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, notBefore: 0 });
|
||||
|
||||
jwt.verify(token, pub, function (err, decoded) {
|
||||
fakeClock.uninstall();
|
||||
assert.isNull(err);
|
||||
assert.isNotNull(decoded);
|
||||
Date.unfix();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
var oldDate = global.Date;
|
||||
|
||||
/*
|
||||
* fix new Date() to a fixed unix timestamp.
|
||||
*/
|
||||
global.Date.fix = function (timestamp) {
|
||||
var time = timestamp * 1000;
|
||||
|
||||
if (global.Date.unfake) {
|
||||
global.Date.unfake();
|
||||
}
|
||||
|
||||
global.Date = function (ts) {
|
||||
return new oldDate(ts || time);
|
||||
};
|
||||
|
||||
global.Date.prototype = Object.create(oldDate.prototype);
|
||||
global.Date.prototype.constructor = global.Date;
|
||||
|
||||
global.Date.prototype.now = function () {
|
||||
return time;
|
||||
};
|
||||
|
||||
global.Date.now = function () {
|
||||
return time;
|
||||
};
|
||||
|
||||
global.Date.unfix = function () {
|
||||
global.Date = oldDate;
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user