You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.0 KiB

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