Skip to content

Commit 5147852

Browse files
MitMaroziluvatar
authored andcommitted
Add tests for private claims in the payload (#555)
This change adds tests for private claims added to the payload during sign and ensures that after verifying the payload contains the expected claim.
1 parent 7eebbc7 commit 5147852

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

test/claim-private.tests.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const util = require('util');
5+
const testUtils = require('./test-utils');
6+
7+
function signWithPayload(payload, callback) {
8+
testUtils.signJWTHelper(payload, 'secret', {algorithm: 'none'}, callback);
9+
}
10+
11+
describe('with a private claim', function() {
12+
[
13+
true,
14+
false,
15+
null,
16+
-1,
17+
0,
18+
1,
19+
-1.1,
20+
1.1,
21+
'',
22+
'private claim',
23+
'UTF8 - José',
24+
[],
25+
['foo'],
26+
{},
27+
{foo: 'bar'},
28+
].forEach((privateClaim) => {
29+
it(`should sign and verify with claim of ${util.inspect(privateClaim)}`, function (done) {
30+
signWithPayload({privateClaim}, (e1, token) => {
31+
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {
32+
testUtils.asyncCheck(done, () => {
33+
expect(e1).to.be.null;
34+
expect(e2).to.be.null;
35+
expect(decoded).to.have.property('privateClaim').to.deep.equal(privateClaim);
36+
});
37+
})
38+
});
39+
});
40+
});
41+
42+
// these values JSON.stringify to null
43+
[
44+
-Infinity,
45+
Infinity,
46+
NaN,
47+
].forEach((privateClaim) => {
48+
it(`should sign and verify with claim of ${util.inspect(privateClaim)}`, function (done) {
49+
signWithPayload({privateClaim}, (e1, token) => {
50+
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {
51+
testUtils.asyncCheck(done, () => {
52+
expect(e1).to.be.null;
53+
expect(e2).to.be.null;
54+
expect(decoded).to.have.property('privateClaim', null);
55+
});
56+
})
57+
});
58+
});
59+
});
60+
61+
// private claims with value undefined are not added to the payload
62+
it(`should sign and verify with claim of undefined`, function (done) {
63+
signWithPayload({privateClaim: undefined}, (e1, token) => {
64+
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {
65+
testUtils.asyncCheck(done, () => {
66+
expect(e1).to.be.null;
67+
expect(e2).to.be.null;
68+
expect(decoded).to.not.have.property('privateClaim');
69+
});
70+
})
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)