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.

53 lines
1.9 KiB

  1. var jwt = require('../index');
  2. var expect = require('chai').expect;
  3. describe('expires option', function() {
  4. it('should work with a number of seconds', function () {
  5. var token = jwt.sign({foo: 123}, '123', { expiresIn: 10 });
  6. var result = jwt.verify(token, '123');
  7. expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + 10, 0.2);
  8. });
  9. it('should work with a string', function () {
  10. var token = jwt.sign({foo: 123}, '123', { expiresIn: '2d' });
  11. var result = jwt.verify(token, '123');
  12. var two_days_in_secs = 2 * 24 * 60 * 60;
  13. expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + two_days_in_secs, 0.2);
  14. });
  15. it('should work with a string second example', function () {
  16. var token = jwt.sign({foo: 123}, '123', { expiresIn: '36h' });
  17. var result = jwt.verify(token, '123');
  18. var day_and_a_half_in_secs = 1.5 * 24 * 60 * 60;
  19. expect(result.exp).to.be.closeTo(Math.floor(Date.now() / 1000) + day_and_a_half_in_secs, 0.2);
  20. });
  21. it('should throw if expires has a bad string format', function () {
  22. expect(function () {
  23. jwt.sign({foo: 123}, '123', { expiresIn: '1 monkey' });
  24. }).to.throw(/"expiresIn" should be a number of seconds or string representing a timespan/);
  25. });
  26. it('should throw if expires is not an string or number', function () {
  27. expect(function () {
  28. jwt.sign({foo: 123}, '123', { expiresIn: { crazy : 213 } });
  29. }).to.throw(/"expiresIn" must be a number/);
  30. });
  31. it('should throw an error if expiresIn and exp are provided', function () {
  32. expect(function () {
  33. jwt.sign({ foo: 123, exp: 839218392183 }, '123', { expiresIn: '5h' });
  34. }).to.throw(/Bad "options.expiresIn" option the payload already has an "exp" property./);
  35. });
  36. it('should throw on deprecated expiresInSeconds option', function () {
  37. expect(function () {
  38. jwt.sign({foo: 123}, '123', { expiresInSeconds: 5 });
  39. }).to.throw('"expiresInSeconds" is not allowed');
  40. });
  41. });