Skip to content

Commit 0906a3f

Browse files
MitMaroziluvatar
authored andcommitted
Refactor tests related to iss and issuer (#543)
This change extracts all tests related to the iss claim and the issuer option into a single test file. Additional tests were added that were missing.
1 parent 1956c40 commit 0906a3f

File tree

4 files changed

+205
-66
lines changed

4 files changed

+205
-66
lines changed

test/claim-iss.test.js

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
'use strict';
2+
3+
const jwt = require('../');
4+
const expect = require('chai').expect;
5+
const util = require('util');
6+
const testUtils = require('./test-utils');
7+
8+
function signWithIssuer(issuer, payload, callback) {
9+
const options = {algorithm: 'none'};
10+
if (issuer !== undefined) {
11+
options.issuer = issuer;
12+
}
13+
testUtils.signJWTHelper(payload, 'secret', options, callback);
14+
}
15+
16+
describe('issuer', function() {
17+
describe('`jwt.sign` "issuer" option validation', function () {
18+
[
19+
true,
20+
false,
21+
null,
22+
-1,
23+
0,
24+
1,
25+
-1.1,
26+
1.1,
27+
-Infinity,
28+
Infinity,
29+
NaN,
30+
[],
31+
['foo'],
32+
{},
33+
{foo: 'bar'},
34+
].forEach((issuer) => {
35+
it(`should error with with value ${util.inspect(issuer)}`, function (done) {
36+
signWithIssuer(issuer, {}, (err) => {
37+
testUtils.asyncCheck(done, () => {
38+
expect(err).to.be.instanceOf(Error);
39+
expect(err).to.have.property('message', '"issuer" must be a string');
40+
});
41+
});
42+
});
43+
});
44+
45+
// undefined needs special treatment because {} is not the same as {issuer: undefined}
46+
it('should error with with value undefined', function (done) {
47+
testUtils.signJWTHelper({}, undefined, {issuer: undefined, algorithm: 'none'}, (err) => {
48+
testUtils.asyncCheck(done, () => {
49+
expect(err).to.be.instanceOf(Error);
50+
expect(err).to.have.property('message', '"issuer" must be a string');
51+
});
52+
});
53+
});
54+
55+
it('should error when "iss" is in payload', function (done) {
56+
signWithIssuer('foo', {iss: 'bar'}, (err) => {
57+
testUtils.asyncCheck(done, () => {
58+
expect(err).to.be.instanceOf(Error);
59+
expect(err).to.have.property(
60+
'message',
61+
'Bad "options.issuer" option. The payload already has an "iss" property.'
62+
);
63+
});
64+
});
65+
});
66+
67+
it('should error with a string payload', function (done) {
68+
signWithIssuer('foo', 'a string payload', (err) => {
69+
testUtils.asyncCheck(done, () => {
70+
expect(err).to.be.instanceOf(Error);
71+
expect(err).to.have.property(
72+
'message',
73+
'invalid issuer option for string payload'
74+
);
75+
});
76+
});
77+
});
78+
79+
it('should error with a Buffer payload', function (done) {
80+
signWithIssuer('foo', new Buffer('a Buffer payload'), (err) => {
81+
testUtils.asyncCheck(done, () => {
82+
expect(err).to.be.instanceOf(Error);
83+
expect(err).to.have.property(
84+
'message',
85+
'invalid issuer option for object payload'
86+
);
87+
});
88+
});
89+
});
90+
});
91+
92+
describe('when signing and verifying a token', function () {
93+
it('should not verify "iss" if verify "issuer" option not provided', function(done) {
94+
signWithIssuer(undefined, {iss: 'foo'}, (e1, token) => {
95+
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {
96+
testUtils.asyncCheck(done, () => {
97+
expect(e1).to.be.null;
98+
expect(e2).to.be.null;
99+
expect(decoded).to.have.property('iss', 'foo');
100+
});
101+
})
102+
});
103+
});
104+
105+
describe('with string "issuer" option', function () {
106+
it('should verify with a string "issuer"', function (done) {
107+
signWithIssuer('foo', {}, (e1, token) => {
108+
testUtils.verifyJWTHelper(token, undefined, {issuer: 'foo'}, (e2, decoded) => {
109+
testUtils.asyncCheck(done, () => {
110+
expect(e1).to.be.null;
111+
expect(e2).to.be.null;
112+
expect(decoded).to.have.property('iss', 'foo');
113+
});
114+
})
115+
});
116+
});
117+
118+
it('should verify with a string "iss"', function (done) {
119+
signWithIssuer(undefined, {iss: 'foo'}, (e1, token) => {
120+
testUtils.verifyJWTHelper(token, undefined, {issuer: 'foo'}, (e2, decoded) => {
121+
testUtils.asyncCheck(done, () => {
122+
expect(e1).to.be.null;
123+
expect(e2).to.be.null;
124+
expect(decoded).to.have.property('iss', 'foo');
125+
});
126+
})
127+
});
128+
});
129+
130+
it('should error if "iss" does not match verify "issuer" option', function(done) {
131+
signWithIssuer(undefined, {iss: 'foobar'}, (e1, token) => {
132+
testUtils.verifyJWTHelper(token, undefined, {issuer: 'foo'}, (e2) => {
133+
testUtils.asyncCheck(done, () => {
134+
expect(e1).to.be.null;
135+
expect(e2).to.be.instanceOf(jwt.JsonWebTokenError);
136+
expect(e2).to.have.property('message', 'jwt issuer invalid. expected: foo');
137+
});
138+
})
139+
});
140+
});
141+
142+
it('should error without "iss" and with verify "issuer" option', function(done) {
143+
signWithIssuer(undefined, {}, (e1, token) => {
144+
testUtils.verifyJWTHelper(token, undefined, {issuer: 'foo'}, (e2) => {
145+
testUtils.asyncCheck(done, () => {
146+
expect(e1).to.be.null;
147+
expect(e2).to.be.instanceOf(jwt.JsonWebTokenError);
148+
expect(e2).to.have.property('message', 'jwt issuer invalid. expected: foo');
149+
});
150+
})
151+
});
152+
});
153+
});
154+
155+
describe('with array "issuer" option', function () {
156+
it('should verify with a string "issuer"', function (done) {
157+
signWithIssuer('bar', {}, (e1, token) => {
158+
testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2, decoded) => {
159+
testUtils.asyncCheck(done, () => {
160+
expect(e1).to.be.null;
161+
expect(e2).to.be.null;
162+
expect(decoded).to.have.property('iss', 'bar');
163+
});
164+
})
165+
});
166+
});
167+
168+
it('should verify with a string "iss"', function (done) {
169+
signWithIssuer(undefined, {iss: 'foo'}, (e1, token) => {
170+
testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2, decoded) => {
171+
testUtils.asyncCheck(done, () => {
172+
expect(e1).to.be.null;
173+
expect(e2).to.be.null;
174+
expect(decoded).to.have.property('iss', 'foo');
175+
});
176+
})
177+
});
178+
});
179+
180+
it('should error if "iss" does not match verify "issuer" option', function(done) {
181+
signWithIssuer(undefined, {iss: 'foobar'}, (e1, token) => {
182+
testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2) => {
183+
testUtils.asyncCheck(done, () => {
184+
expect(e1).to.be.null;
185+
expect(e2).to.be.instanceOf(jwt.JsonWebTokenError);
186+
expect(e2).to.have.property('message', 'jwt issuer invalid. expected: foo,bar');
187+
});
188+
})
189+
});
190+
});
191+
192+
it('should error without "iss" and with verify "issuer" option', function(done) {
193+
signWithIssuer(undefined, {}, (e1, token) => {
194+
testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2) => {
195+
testUtils.asyncCheck(done, () => {
196+
expect(e1).to.be.null;
197+
expect(e2).to.be.instanceOf(jwt.JsonWebTokenError);
198+
expect(e2).to.have.property('message', 'jwt issuer invalid. expected: foo,bar');
199+
});
200+
})
201+
});
202+
});
203+
});
204+
});
205+
});

test/issue_196.tests.js

-15
This file was deleted.

test/jwt.asymmetric_signing.tests.js

-44
Original file line numberDiff line numberDiff line change
@@ -113,50 +113,6 @@ describe('Asymmetric Algorithms', function(){
113113
});
114114
});
115115

116-
describe('when signing a token with issuer', function () {
117-
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, issuer: 'urn:foo' });
118-
119-
it('should check issuer', function (done) {
120-
jwt.verify(token, pub, { issuer: 'urn:foo' }, function (err, decoded) {
121-
assert.isNotNull(decoded);
122-
assert.isNull(err);
123-
done();
124-
});
125-
});
126-
127-
it('should check the issuer when providing a list of valid issuers', function (done) {
128-
jwt.verify(token, pub, { issuer: ['urn:foo', 'urn:bar'] }, function (err, decoded) {
129-
assert.isNotNull(decoded);
130-
assert.isNull(err);
131-
done();
132-
});
133-
});
134-
135-
it('should throw when invalid issuer', function (done) {
136-
jwt.verify(token, pub, { issuer: 'urn:wrong' }, function (err, decoded) {
137-
assert.isUndefined(decoded);
138-
assert.isNotNull(err);
139-
assert.equal(err.name, 'JsonWebTokenError');
140-
assert.instanceOf(err, jwt.JsonWebTokenError);
141-
done();
142-
});
143-
});
144-
});
145-
146-
describe('when signing a token without issuer', function () {
147-
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm });
148-
149-
it('should check issuer', function (done) {
150-
jwt.verify(token, pub, { issuer: 'urn:foo' }, function (err, decoded) {
151-
assert.isUndefined(decoded);
152-
assert.isNotNull(err);
153-
assert.equal(err.name, 'JsonWebTokenError');
154-
assert.instanceOf(err, jwt.JsonWebTokenError);
155-
done();
156-
});
157-
});
158-
});
159-
160116
describe('when signing a token with jwt id', function () {
161117
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, jwtid: 'jwtid' });
162118

test/schema.tests.js

-7
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ describe('schema', function() {
4444
sign({encoding: 'utf8'});
4545
});
4646

47-
it('should validate issuer', function () {
48-
expect(function () {
49-
sign({ issuer: 10 });
50-
}).to.throw(/"issuer" must be a string/);
51-
sign({issuer: 'foo'});
52-
});
53-
5447
it('should validate noTimestamp', function () {
5548
expect(function () {
5649
sign({ noTimestamp: 10 });

0 commit comments

Comments
 (0)