Skip to content

Commit

Permalink
test: test jwt sign options over existing payload
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jul 22, 2019
1 parent 418e311 commit 98af967
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions test/jwt/sign.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,38 @@ test('options.kid', t => {

test('options.subject', t => {
const subject = 'foo'
const { sub } = JWT.decode(JWT.sign({}, key, { subject }))
const { sub } = JWT.decode(JWT.sign({ sub: 'bar' }, key, { subject }))
t.is(sub, subject)
})

test('options.issuer', t => {
const issuer = 'foo'
const { iss } = JWT.decode(JWT.sign({}, key, { issuer }))
const { iss } = JWT.decode(JWT.sign({ iss: 'bar' }, key, { issuer }))
t.is(iss, issuer)
})

test('options.jti', t => {
const jti = 'foo'
const decoded = JWT.decode(JWT.sign({}, key, { jti }))
const decoded = JWT.decode(JWT.sign({ jti: 'bar' }, key, { jti }))
t.is(decoded.jti, jti)
})

const epoch = 1265328501
const now = new Date(epoch * 1000)

test('options.iat false', t => {
const iat = false
t.deepEqual(JWT.decode(JWT.sign({}, key, { iat })), {})
})

test('options.iat', t => {
t.true(Object.keys(JWT.decode(JWT.sign({}, key))).includes('iat'))
const decoded = JWT.decode(JWT.sign({ iat: 'bar' }, key, { iat: true, now }))
t.is(decoded.iat, epoch)
})

test('options.nonce', t => {
const nonce = 'foo'
const { nonce: pNonce } = JWT.decode(JWT.sign({}, key, { nonce }))
const { nonce: pNonce } = JWT.decode(JWT.sign({ nonce: 'bar' }, key, { nonce }))
t.is(pNonce, nonce)
})

Expand All @@ -131,25 +135,22 @@ test('options.audience', t => {

test('options.audience (array)', t => {
const audience = ['foo']
const { aud } = JWT.decode(JWT.sign({}, key, { audience }))
const { aud } = JWT.decode(JWT.sign({ aud: 'bar' }, key, { audience }))
t.deepEqual(aud, audience)
})

const epoch = 1265328501
const now = new Date(epoch * 1000)

test('options.now', t => {
const { iat } = JWT.decode(JWT.sign({}, key, { now }))
t.deepEqual(iat, epoch)
})

test('options.expiresIn', t => {
const { exp } = JWT.decode(JWT.sign({}, key, { now, expiresIn: '20s' }))
const { exp } = JWT.decode(JWT.sign({ exp: 'bar' }, key, { now, expiresIn: '20s' }))
t.deepEqual(exp, epoch + 20)
})

test('options.notBefore', t => {
const { nbf } = JWT.decode(JWT.sign({}, key, { now, notBefore: '20m' }))
const { nbf } = JWT.decode(JWT.sign({ nbf: 'bar' }, key, { now, notBefore: '20m' }))
t.deepEqual(nbf, epoch + 20 * 60)
})

Expand Down

0 comments on commit 98af967

Please sign in to comment.