Skip to content

Commit 53d405e

Browse files
MitMaroziluvatar
authored andcommitted
Refactor tests related to audience and aud (#503)
This change extracts all tests in the existing test files related to audience and aud into a single test file. Several other tests are also added that were missing from the existing files.
1 parent 72f0d9e commit 53d405e

File tree

4 files changed

+323
-203
lines changed

4 files changed

+323
-203
lines changed

test/aud.test.js

+323
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
'use strict';
2+
3+
const jwt = require('../');
4+
const expect = require('chai').expect;
5+
const util = require('util');
6+
7+
function signWithAudience(payload, audience) {
8+
const options = {algorithm: 'none'};
9+
if (audience !== undefined) {
10+
options.audience = audience;
11+
}
12+
return jwt.sign(payload, undefined, options);
13+
}
14+
15+
describe('audience', function() {
16+
describe('`jwt.sign` "audience" option validation', function () {
17+
[
18+
true,
19+
false,
20+
null,
21+
-1,
22+
1,
23+
0,
24+
-1.1,
25+
1.1,
26+
-Infinity,
27+
Infinity,
28+
NaN,
29+
{},
30+
{foo: 'bar'},
31+
].forEach((audience) => {
32+
it(`should error with with value ${util.inspect(audience)}`, function () {
33+
expect(() => signWithAudience({}, audience)).to.throw('"audience" must be a string or array');
34+
});
35+
});
36+
37+
// undefined needs special treatment because {} is not the same as {aud: undefined}
38+
it('should error with with value undefined', function () {
39+
expect(() => jwt.sign({}, undefined, {audience: undefined, algorithm: 'none'})).to.throw(
40+
'"audience" must be a string or array'
41+
);
42+
});
43+
44+
it('should error when "aud" is in payload', function () {
45+
expect(() => signWithAudience({aud: ''}, 'my_aud')).to.throw(
46+
'Bad "options.audience" option. The payload already has an "aud" property.'
47+
);
48+
});
49+
50+
it('should error with a string payload', function () {
51+
expect(() => signWithAudience('a string payload', 'my_aud')).to.throw(
52+
'invalid audience option for string payload'
53+
);
54+
});
55+
56+
it('should error with a Buffer payload', function () {
57+
expect(() => signWithAudience(new Buffer('a Buffer payload'), 'my_aud')).to.throw(
58+
'invalid audience option for object payload'
59+
);
60+
});
61+
});
62+
63+
describe('when signing and verifying a token with "audience" option', function () {
64+
describe('with a string for "aud" value in payload', function () {
65+
let token;
66+
67+
beforeEach(function () {
68+
token = signWithAudience({}, 'urn:foo');
69+
});
70+
71+
it('should verify and decode without verify "audience" option', function () {
72+
const decoded = jwt.decode(token);
73+
const verified = jwt.verify(token, undefined);
74+
75+
expect(decoded).to.deep.equal(verified);
76+
expect(decoded.aud).to.equal('urn:foo');
77+
});
78+
79+
it('should verify with a string "verify.audience" option', function () {
80+
expect(jwt.verify(token, undefined, {
81+
audience: 'urn:foo'
82+
})).to.not.throw;
83+
});
84+
85+
it('should verify with an array of strings "verify.audience" option', function () {
86+
expect(jwt.verify(token, undefined, {
87+
audience: ['urn:no_match', 'urn:foo']
88+
})).to.not.throw;
89+
});
90+
91+
it('should verify with a Regex "verify.audience" option', function () {
92+
expect(jwt.verify(token, undefined, {
93+
audience: /^urn:f[o]{2}$/
94+
})).to.not.throw;
95+
});
96+
97+
it('should verify with an array of Regex "verify.audience" option', function () {
98+
expect(jwt.verify(token, undefined, {
99+
audience: [/^urn:no_match$/, /^urn:f[o]{2}$/]
100+
})).to.not.throw;
101+
});
102+
103+
it('should verify with an array containing a string and a Regex "verify.audience" option', function () {
104+
expect(jwt.verify(token, undefined, {
105+
audience: ['urn:no_match', /^urn:f[o]{2}$/]
106+
})).to.not.throw;
107+
});
108+
109+
it('should verify with an array containing a Regex and a string "verify.audience" option', function () {
110+
expect(jwt.verify(token, undefined, {
111+
audience: [/^urn:no_match$/, 'urn:foo']
112+
})).to.not.throw;
113+
});
114+
115+
it('should error on no match with a string "verify.audience" option', function () {
116+
expect(() => jwt.verify(token, undefined, {
117+
audience: 'urn:no-match'
118+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: urn:no-match');
119+
});
120+
121+
it('should error on no match with an array of string "verify.audience" option', function () {
122+
expect(() => jwt.verify(token, undefined, {
123+
audience: ['urn:no-match-1', 'urn:no-match-2']
124+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: urn:no-match-1 or urn:no-match-2');
125+
});
126+
127+
it('should error on no match with a Regex "verify.audience" option', function () {
128+
expect(() => jwt.verify(token, undefined, {
129+
audience: /^urn:no-match$/
130+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match$/');
131+
});
132+
133+
it('should error on no match with an array of Regex "verify.audience" option', function () {
134+
expect(() => jwt.verify(token, undefined, {
135+
audience: [/^urn:no-match-1$/, /^urn:no-match-2$/]
136+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match-1$/ or /^urn:no-match-2$/');
137+
});
138+
139+
it('should error on no match with an array of a Regex and a string in "verify.audience" option', function () {
140+
expect(() => jwt.verify(token, undefined, {
141+
audience: [/^urn:no-match$/, 'urn:no-match']
142+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match$/ or urn:no-match');
143+
});
144+
});
145+
146+
describe('with an array for "aud" value in payload', function () {
147+
let token;
148+
149+
beforeEach(function () {
150+
token = signWithAudience({}, ['urn:foo', 'urn:bar']);
151+
});
152+
153+
it('should verify and decode without verify "audience" option', function () {
154+
const decoded = jwt.decode(token);
155+
const verified = jwt.verify(token, undefined);
156+
157+
expect(decoded).to.deep.equal(verified);
158+
expect(decoded.aud).to.deep.equal(['urn:foo', 'urn:bar']);
159+
});
160+
161+
it('should error on no match with a string "verify.audience" option', function () {
162+
expect(() => jwt.verify(token, undefined, {
163+
audience: 'urn:no-match'
164+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: urn:no-match');
165+
});
166+
167+
it('should error on no match with an array of string "verify.audience" option', function () {
168+
expect(() => jwt.verify(token, undefined, {
169+
audience: ['urn:no-match-1', 'urn:no-match-2']
170+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: urn:no-match-1 or urn:no-match-2');
171+
});
172+
173+
it('should error on no match with a Regex "verify.audience" option', function () {
174+
expect(() => jwt.verify(token, undefined, {
175+
audience: /^urn:no-match$/
176+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match$/');
177+
});
178+
179+
it('should error on no match with an array of Regex "verify.audience" option', function () {
180+
expect(() => jwt.verify(token, undefined, {
181+
audience: [/^urn:no-match-1$/, /^urn:no-match-2$/]
182+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match-1$/ or /^urn:no-match-2$/');
183+
});
184+
185+
it('should error on no match with an array of a Regex and a string in "verify.audience" option', function () {
186+
expect(() => jwt.verify(token, undefined, {
187+
audience: [/^urn:no-match$/, 'urn:no-match']
188+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match$/ or urn:no-match');
189+
});
190+
191+
describe('when checking matching for both "urn:foo" and "urn:bar"', function() {
192+
193+
it('should verify with an array of stings "verify.audience" option', function () {
194+
expect(jwt.verify(token, undefined, {
195+
audience: ['urn:foo', 'urn:bar']
196+
})).to.not.throw;
197+
});
198+
199+
it('should verify with a Regex "verify.audience" option', function () {
200+
expect(jwt.verify(token, undefined, {
201+
audience: /^urn:[a-z]{3}$/
202+
})).to.not.throw;
203+
});
204+
205+
it('should verify with an array of Regex "verify.audience" option', function () {
206+
expect(jwt.verify(token, undefined, {
207+
audience: [/^urn:f[o]{2}$/, /^urn:b[ar]{2}$/]
208+
})).to.not.throw;
209+
});
210+
});
211+
212+
describe('when checking for a matching for "urn:foo"', function() {
213+
it('should verify with a string "verify.audience"', function () {
214+
expect(jwt.verify(token, undefined, {
215+
audience: 'urn:foo'
216+
})).to.not.throw;
217+
});
218+
219+
it('should verify with a Regex "verify.audience" option', function () {
220+
expect(jwt.verify(token, undefined, {
221+
audience: /^urn:f[o]{2}$/
222+
})).to.not.throw;
223+
});
224+
225+
it('should verify with an array of Regex "verify.audience"', function () {
226+
expect(jwt.verify(token, undefined, {
227+
audience: [/^urn:no-match$/, /^urn:f[o]{2}$/]
228+
})).to.not.throw;
229+
});
230+
231+
it('should verify with an array containing a string and a Regex "verify.audience" option', function () {
232+
expect(jwt.verify(token, undefined, {
233+
audience: ['urn:no_match', /^urn:f[o]{2}$/]
234+
})).to.not.throw;
235+
});
236+
237+
it('should verify with an array containing a Regex and a string "verify.audience" option', function () {
238+
expect(jwt.verify(token, undefined, {
239+
audience: [/^urn:no-match$/, 'urn:foo']
240+
})).to.not.throw;
241+
});
242+
});
243+
244+
describe('when checking matching for "urn:bar"', function() {
245+
it('should verify with a string "verify.audience"', function () {
246+
expect(jwt.verify(token, undefined, {
247+
audience: 'urn:bar'
248+
})).to.not.throw;
249+
});
250+
251+
it('should verify with a Regex "verify.audience" option', function () {
252+
expect(jwt.verify(token, undefined, {
253+
audience: /^urn:b[ar]{2}$/
254+
})).to.not.throw;
255+
});
256+
257+
it('should verify with an array of Regex "verify.audience" option', function () {
258+
expect(jwt.verify(token, undefined, {
259+
audience: [/^urn:no-match$/, /^urn:b[ar]{2}$/]
260+
})).to.not.throw;
261+
});
262+
263+
it('should verify with an array containing a string and a Regex "verify.audience" option', function () {
264+
expect(jwt.verify(token, undefined, {
265+
audience: ['urn:no_match', /^urn:b[ar]{2}$/]
266+
})).to.not.throw;
267+
});
268+
269+
it('should verify with an array containing a Regex and a string "verify.audience" option', function () {
270+
expect(jwt.verify(token, undefined, {
271+
audience: [/^urn:no-match$/, 'urn:bar']
272+
})).to.not.throw;
273+
});
274+
});
275+
});
276+
277+
describe('without a "aud" value in payload', function () {
278+
let token;
279+
280+
beforeEach(function () {
281+
token = signWithAudience({});
282+
});
283+
284+
it('should verify and decode without verify "audience" option', function () {
285+
const decoded = jwt.decode(token);
286+
const verified = jwt.verify(token, undefined);
287+
288+
expect(decoded).to.deep.equal(verified);
289+
expect(decoded).to.not.have.property('aud');
290+
});
291+
292+
it('should error on no match with a string "verify.audience" option', function () {
293+
expect(() => jwt.verify(token, undefined, {
294+
audience: 'urn:no-match'
295+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: urn:no-match');
296+
});
297+
298+
it('should error on no match with an array of string "verify.audience" option', function () {
299+
expect(() => jwt.verify(token, undefined, {
300+
audience: ['urn:no-match-1', 'urn:no-match-2']
301+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: urn:no-match-1 or urn:no-match-2');
302+
});
303+
304+
it('should error on no match with a Regex "verify.audience" option', function () {
305+
expect(() => jwt.verify(token, undefined, {
306+
audience: /^urn:no-match$/
307+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match$/');
308+
});
309+
310+
it('should error on no match with an array of Regex "verify.audience" option', function () {
311+
expect(() => jwt.verify(token, undefined, {
312+
audience: [/^urn:no-match-1$/, /^urn:no-match-2$/]
313+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match-1$/ or /^urn:no-match-2$/');
314+
});
315+
316+
it('should error on no match with an array of a Regex and a string in "verify.audience" option', function () {
317+
expect(() => jwt.verify(token, undefined, {
318+
audience: [/^urn:no-match$/, 'urn:no-match']
319+
})).to.throw(jwt.JsonWebTokenError, 'jwt audience invalid. expected: /^urn:no-match$/ or urn:no-match');
320+
});
321+
});
322+
});
323+
});

0 commit comments

Comments
 (0)