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