Skip to content

Commit d7c5793

Browse files
committed
add tests for verify expires
1 parent 7b9f509 commit d7c5793

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"devDependencies": {
2525
"atob": "^1.1.2",
2626
"chai": "^1.10.0",
27-
"mocha": "^2.1.0"
27+
"mocha": "^2.1.0",
28+
"sinon": "^1.15.4"
2829
},
2930
"engines": {
3031
"npm": ">=1.4.28"

test/verify.tests.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ var jwt = require('../index');
22
var jws = require('jws');
33
var fs = require('fs');
44
var path = require('path');
5+
var sinon = require('sinon');
56

67
var assert = require('chai').assert;
78

89
describe('verify', function() {
910
var pub = fs.readFileSync(path.join(__dirname, 'pub.pem'));
1011
var priv = fs.readFileSync(path.join(__dirname, 'priv.pem'));
1112

12-
it('should first assume JSON claim set', function () {
13+
it('should first assume JSON claim set', function (done) {
1314
var header = { alg: 'RS256' };
1415
var payload = { iat: Math.floor(Date.now() / 1000 ) };
1516

@@ -23,6 +24,43 @@ describe('verify', function() {
2324
jwt.verify(signed, pub, {typ: 'JWT'}, function(err, p) {
2425
assert.isNull(err);
2526
assert.deepEqual(p, payload);
27+
done();
2628
});
2729
});
30+
31+
describe('expiration', function () {
32+
var clock;
33+
beforeEach(function () {
34+
// clock = sinon.useFakeTimers(1437018650768);
35+
});
36+
afterEach(function () {
37+
try { clock.restore(); } catch (e) {}
38+
});
39+
40+
it('should error on expired token', function (done) {
41+
clock = sinon.useFakeTimers(1437018650768);
42+
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s';
43+
var key = 'key';
44+
jwt.verify(token, key, {algorithms: ['HS256']}, function (err, p) {
45+
assert.equal(err.name, 'TokenExpiredError');
46+
assert.equal(err.message, 'jwt expired');
47+
assert.equal(err.expiredAt.constructor.name, 'Date');
48+
assert.equal(Number(err.expiredAt), 1437018583000);
49+
assert.isUndefined(p);
50+
done();
51+
});
52+
});
53+
54+
it('should not error on unexpired token', function (done) {
55+
clock = sinon.useFakeTimers(1437018582000);
56+
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s';
57+
var key = 'key';
58+
jwt.verify(token, key, {algorithms: ['HS256']}, function (err, p) {
59+
assert.isNull(err);
60+
assert.equal(p.foo, 'bar');
61+
done();
62+
});
63+
});
64+
});
65+
2866
});

0 commit comments

Comments
 (0)