arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/jsonwebtoken/test/async_sign.tests.js
node_modules/jsonwebtoken/test/async_sign.tests.jsRaw
 1var jwt = require('../index');
 2var expect = require('chai').expect;
 3var jws = require('jws');
 4
 5describe('signing a token asynchronously', function() {
 6
 7  describe('when signing a token', function() {
 8    var secret = 'shhhhhh';
 9
10    it('should return the same result as singing synchronously', function(done) {
11      jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }, function (err, asyncToken) {
12        if (err) return done(err);
13        var syncToken = jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' });
14        expect(asyncToken).to.be.a('string');
15        expect(asyncToken.split('.')).to.have.length(3);
16        expect(asyncToken).to.equal(syncToken);
17        done();
18      });
19    });
20
21    it('should work', function (done) {
22      jwt.sign({abc: 1}, "secret", {}, function (err, res) {
23        expect(err).to.be.null();
24        done();
25      });
26    });
27
28    it('should return error when secret is not a cert for RS256', function(done) {
29      //this throw an error because the secret is not a cert and RS256 requires a cert.
30      jwt.sign({ foo: 'bar' }, secret, { algorithm: 'RS256' }, function (err) {
31        expect(err).to.be.ok();
32        done();
33      });
34    });
35
36    it('should return error on wrong arguments', function(done) {
37      //this throw an error because the secret is not a cert and RS256 requires a cert.
38      jwt.sign({ foo: 'bar' }, secret, { notBefore: {} }, function (err) {
39        expect(err).to.be.ok();
40        done();
41      });
42    });
43
44    it('should return error on wrong arguments (2)', function(done) {
45      jwt.sign('string', 'secret', {noTimestamp: true}, function (err) {
46        expect(err).to.be.ok();
47        expect(err).to.be.instanceof(Error);
48        done();
49      });
50    });
51
52    it('should not stringify the payload', function (done) {
53      jwt.sign('string', 'secret', {}, function (err, token) {
54        if (err) { return done(err); }
55        expect(jws.decode(token).payload).to.equal('string');
56        done();
57      });
58    });
59  });
60});