|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const jws = require('jws'); |
| 4 | +const expect = require('chai').expect; |
| 5 | +const path = require('path'); |
| 6 | +const fs = require('fs'); |
| 7 | +const testUtils = require('./test-utils') |
| 8 | + |
| 9 | +describe('complete option', function () { |
| 10 | + const secret = fs.readFileSync(path.join(__dirname, 'priv.pem')); |
| 11 | + const pub = fs.readFileSync(path.join(__dirname, 'pub.pem')); |
| 12 | + |
| 13 | + const header = { alg: 'RS256' }; |
| 14 | + const payload = { iat: Math.floor(Date.now() / 1000 ) }; |
| 15 | + const signed = jws.sign({ header, payload, secret, encoding: 'utf8' }); |
| 16 | + const signature = jws.decode(signed).signature; |
| 17 | + |
| 18 | + [ |
| 19 | + { |
| 20 | + description: 'should return header, payload and signature', |
| 21 | + complete: true, |
| 22 | + }, |
| 23 | + ].forEach((testCase) => { |
| 24 | + it(testCase.description, function (done) { |
| 25 | + testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => { |
| 26 | + testUtils.asyncCheck(done, () => { |
| 27 | + expect(err).to.be.null; |
| 28 | + expect(decoded.header).to.have.property('alg', header.alg); |
| 29 | + expect(decoded.payload).to.have.property('iat', payload.iat); |
| 30 | + expect(decoded).to.have.property('signature', signature); |
| 31 | + }); |
| 32 | + }); |
| 33 | + }); |
| 34 | + }); |
| 35 | + [ |
| 36 | + { |
| 37 | + description: 'should return payload', |
| 38 | + complete: false, |
| 39 | + }, |
| 40 | + ].forEach((testCase) => { |
| 41 | + it(testCase.description, function (done) { |
| 42 | + testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => { |
| 43 | + testUtils.asyncCheck(done, () => { |
| 44 | + expect(err).to.be.null; |
| 45 | + expect(decoded.header).to.be.undefined; |
| 46 | + expect(decoded.payload).to.be.undefined; |
| 47 | + expect(decoded.signature).to.be.undefined; |
| 48 | + expect(decoded).to.have.property('iat', payload.iat); |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments