Skip to content

Commit

Permalink
add tests for verify expires
Browse files Browse the repository at this point in the history
  • Loading branch information
junosuarez committed Jul 16, 2015
1 parent 7b9f509 commit d7c5793
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"devDependencies": {
"atob": "^1.1.2",
"chai": "^1.10.0",
"mocha": "^2.1.0"
"mocha": "^2.1.0",
"sinon": "^1.15.4"
},
"engines": {
"npm": ">=1.4.28"
Expand Down
40 changes: 39 additions & 1 deletion test/verify.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ var jwt = require('../index');
var jws = require('jws');
var fs = require('fs');
var path = require('path');
var sinon = require('sinon');

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

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

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

Expand All @@ -23,6 +24,43 @@ describe('verify', function() {
jwt.verify(signed, pub, {typ: 'JWT'}, function(err, p) {
assert.isNull(err);
assert.deepEqual(p, payload);
done();
});
});

describe('expiration', function () {
var clock;
beforeEach(function () {
// clock = sinon.useFakeTimers(1437018650768);
});
afterEach(function () {
try { clock.restore(); } catch (e) {}
});

it('should error on expired token', function (done) {
clock = sinon.useFakeTimers(1437018650768);
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s';
var key = 'key';
jwt.verify(token, key, {algorithms: ['HS256']}, function (err, p) {
assert.equal(err.name, 'TokenExpiredError');
assert.equal(err.message, 'jwt expired');
assert.equal(err.expiredAt.constructor.name, 'Date');
assert.equal(Number(err.expiredAt), 1437018583000);
assert.isUndefined(p);
done();
});
});

it('should not error on unexpired token', function (done) {
clock = sinon.useFakeTimers(1437018582000);
var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0MzcwMTg1ODIsImV4cCI6MTQzNzAxODU4M30.NmMv7sXjM1dW0eALNXud8LoXknZ0mH14GtnFclwJv0s';
var key = 'key';
jwt.verify(token, key, {algorithms: ['HS256']}, function (err, p) {
assert.isNull(err);
assert.equal(p.foo, 'bar');
done();
});
});
});

});

0 comments on commit d7c5793

Please sign in to comment.