Skip to content

Commit

Permalink
Merge pull request #34 from alexjab/master
Browse files Browse the repository at this point in the history
Improve tests suite
  • Loading branch information
hokaccha committed Jan 22, 2016
2 parents 57e9f69 + bc169f6 commit a274520
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
coverage
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: "node_js"
node_js:
- 0.6
- 0.8
- 0.10
- 4.0
- 5.0
17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
"url": "http://github.com/hokaccha/node-jwt-simple.git"
},
"devDependencies": {
"mocha": "*",
"expect.js": "*"
"expect.js": "^0.3.1",
"istanbul": "^0.4.2",
"mocha": "^2.3.4"
},
"scripts": {
"test": "./node_modules/.bin/mocha test/*.js"
"test": "istanbul cover _mocha test/*.js"
},
"license": "MIT",
"engines": {"node": ">= 0.4.0"},
"keywords": ["jwt", "encode", "decode"],
"engines": {
"node": ">= 0.4.0"
},
"keywords": [
"jwt",
"encode",
"decode"
],
"main": "./index"
}
108 changes: 75 additions & 33 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,83 @@ var jwt = require('../index');
var expect = require('expect.js');
var fs = require('fs');

describe('method and property', function() {
it('jwt has version property', function() {
expect(jwt.version).to.be.a('string');
var package = require('../package.json');

describe('jwt', function() {
it('jwt has `version` property', function() {
expect(jwt.decode).to.be.a('function');
});

it('jwt has encode and decode method', function() {
it('jwt has `encode` method', function() {
expect(jwt.encode).to.be.a('function');
});

it('jwt has `decode` method', function() {
expect(jwt.decode).to.be.a('function');
});
});

describe('encode and decode', function() {
describe('version', function() {
it('the version in the library is the same as the one in package.json', function() {
expect(jwt.version).to.equal(package.version);
});
});

describe('encode', function() {
it('encode token', function() {
var token = jwt.encode({ foo: 'bar' }, 'key');
expect(token).to.be.a('string');
expect(token.split('.')).to.have.length(3);
});

it('key is required', function() {
it('throw an error when the key is missing', function() {
var fn = jwt.encode.bind(null, { foo: 'bar' });
expect(fn).to.throwException();
expect(fn).to.throwError(/Require key/);
});

it('throw en error when the specified algorithm is not supported', function() {
var fn = jwt.encode.bind(null, { foo: 'bar' }, 'some_key', 'FooBar256');
expect(fn).to.throwError(/Algorithm not supported/);
});
});

describe('decode', function() {
var key = 'key';
var obj = { foo: 'bar' };
var token = jwt.encode(obj, key);

it('decode token', function() {
var obj = { foo: 'bar' };
var key = 'key';
var token = jwt.encode(obj, key);
var obj2 = jwt.decode(token, key);
expect(obj2).to.eql(obj);
expect(jwt.decode.bind(null, token, 'invalid_key')).to.throwException();
});

it('decode no verify', function() {
it('throw an error when no token is provided', function() {
var fn = jwt.decode.bind(null, null, key);
expect(fn).to.throwError(/No token supplied/);
});

it('throw an error when the token is not correctly formatted', function() {
var fn = jwt.decode.bind(null, 'foo.bar', key);
expect(fn).to.throwError(/Not enough or too many segments/);
});

it('throw an error when the specified algorithm is not supported', function() {
var fn = jwt.decode.bind(null, token, key, false, 'FooBar256');
expect(fn).to.throwError(/Algorithm not supported/);
});

it('throw an error when the signature verification fails', function() {
var fn = jwt.decode.bind(null, token, 'invalid_key');
expect(fn).to.throwError(/Signature verification failed/);
});

it('do not throw any error when verification is disabled', function() {
var obj = { foo: 'bar' };
var key = 'key';
var token = jwt.encode(obj, key);
var fn1 = jwt.decode.bind(null, token, null);
var fn2 = jwt.decode.bind(null, token, null, true);
expect(fn1).to.throwException();
var fn1 = jwt.decode.bind(null, token, 'invalid_key1');
var fn2 = jwt.decode.bind(null, token, 'invalid_key2', true);
expect(fn1).to.throwError(/Signature verification failed/);
expect(fn2()).to.eql(obj);
});

Expand All @@ -50,32 +88,36 @@ describe('encode and decode', function() {
var token = jwt.encode(obj, key, 'HS512');
var obj2 = jwt.decode(token, key, false, 'HS512');
expect(obj2).to.eql(obj);
expect(jwt.decode.bind(null, token, key, false, 'HS256')).to.throwException();
expect(jwt.decode.bind(null, token, 'invalid_key')).to.throwException();
expect(jwt.decode.bind(null, token, key, false, 'HS256')).to.throwError(/Signature verification failed/);
});

it('RS256', function() {
describe('RS256', function() {
var obj = { foo: 'bar' };
var pem = fs.readFileSync(__dirname + '/test.pem').toString('ascii');
var cert = fs.readFileSync(__dirname + '/test.crt').toString('ascii');
var alg = 'RS256';
var token = jwt.encode(obj, pem, alg);
var obj2 = jwt.decode(token, cert);
expect(obj2).to.eql(obj);
expect(jwt.decode.bind(null, token, 'invalid_key')).to.throwException();
});

it('can add jwt header by options.header', function() {
var obj = { foo: 'bar' };
var pem = fs.readFileSync(__dirname + '/test.pem').toString('ascii');
var cert = fs.readFileSync(__dirname + '/test.crt').toString('ascii');
var alg = 'RS256';
var token = jwt.encode(obj, pem, alg, {header: {kid: 'keyidX'}});
var obj2 = jwt.decode(token, cert);
expect(obj2).to.eql(obj);
it('can add jwt header by options.header', function() {
var token = jwt.encode(obj, pem, alg, {header: {kid: 'keyidX'}});
var obj2 = jwt.decode(token, cert);
expect(obj2).to.eql(obj);

var jwtHeader = token.split('.')[0];
expect(JSON.parse(base64urlDecode(jwtHeader))).to.eql({typ:'JWT',alg:alg,kid:'keyidX'});
});


it('decode token given RS256 algorithm', function() {
var token = jwt.encode(obj, pem, alg);
var obj2 = jwt.decode(token, cert);
expect(obj2).to.eql(obj);
});

var jwtHeader = token.split('.')[0];
expect(JSON.parse(base64urlDecode(jwtHeader))).to.eql({typ:"JWT",alg:"RS256",kid:"keyidX"});
it('throw an error when the key is invalid', function() {
var token = jwt.encode(obj, pem, alg);
var obj2 = jwt.decode(token, cert);
expect(jwt.decode.bind(null, token, 'invalid_key')).to.throwError();
});
});
});

Expand Down

0 comments on commit a274520

Please sign in to comment.