Skip to content

Commit 5a7fa23

Browse files
MitMaroziluvatar
authored andcommittedJul 20, 2018
Refactor tests related to subject and sub (#505)
This change extracts all tests in the existing files related to the subject option and sub claim into a single test file. Several other tests are also added that were missing from the existing files.
1 parent e2860a9 commit 5a7fa23

File tree

3 files changed

+107
-43
lines changed

3 files changed

+107
-43
lines changed
 

‎test/claim-sub.tests.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
'use strict';
2+
3+
const jwt = require('../');
4+
const expect = require('chai').expect;
5+
const util = require('util');
6+
7+
function signWithSubject(payload, subject) {
8+
const options = {algorithm: 'none'};
9+
if (subject !== undefined) {
10+
options.subject = subject;
11+
}
12+
return jwt.sign(payload, undefined, options);
13+
}
14+
15+
describe('subject', function() {
16+
describe('`jwt.sign` "subject" option validation', function () {
17+
[
18+
true,
19+
false,
20+
null,
21+
-1,
22+
0,
23+
1,
24+
-1.1,
25+
1.1,
26+
-Infinity,
27+
Infinity,
28+
NaN,
29+
[],
30+
['foo'],
31+
{},
32+
{foo: 'bar'},
33+
].forEach((subject) => {
34+
it(`should error with with value ${util.inspect(subject)}`, function () {
35+
expect(() => signWithSubject({}, subject)).to.throw('"subject" must be a string');
36+
});
37+
});
38+
39+
// undefined needs special treatment because {} is not the same as {subject: undefined}
40+
it('should error with with value undefined', function () {
41+
expect(() => jwt.sign({}, undefined, {subject: undefined, algorithm: 'none'})).to.throw(
42+
'"subject" must be a string'
43+
);
44+
});
45+
46+
it('should error when "sub" is in payload', function () {
47+
expect(() => signWithSubject({sub: 'bar'}, 'foo')).to.throw(
48+
'Bad "options.subject" option. The payload already has an "sub" property.'
49+
);
50+
});
51+
52+
53+
it('should error with a string payload', function () {
54+
expect(() => signWithSubject('a string payload', 'foo')).to.throw(
55+
'invalid subject option for string payload'
56+
);
57+
});
58+
59+
it('should error with a Buffer payload', function () {
60+
expect(() => signWithSubject(new Buffer('a Buffer payload'), 'foo')).to.throw(
61+
'invalid subject option for object payload'
62+
);
63+
});
64+
});
65+
66+
describe('when signing and verifying a token with "subject" option', function () {
67+
it('should verify with a string "subject"', function () {
68+
const token = signWithSubject({}, 'foo');
69+
const decoded = jwt.decode(token);
70+
const verified = jwt.verify(token, undefined, {subject: 'foo'});
71+
expect(decoded).to.deep.equal(verified);
72+
expect(decoded.sub).to.equal('foo');
73+
});
74+
75+
it('should verify with a string "sub"', function () {
76+
const token = signWithSubject({sub: 'foo'});
77+
const decoded = jwt.decode(token);
78+
const verified = jwt.verify(token, undefined, {subject: 'foo'});
79+
expect(decoded).to.deep.equal(verified);
80+
expect(decoded.sub).to.equal('foo');
81+
});
82+
83+
it('should not verify "sub" if "verify.subject" option not provided', function() {
84+
const token = signWithSubject({sub: 'foo'});
85+
const decoded = jwt.decode(token);
86+
const verified = jwt.verify(token, undefined);
87+
expect(decoded).to.deep.equal(verified);
88+
expect(decoded.sub).to.equal('foo');
89+
});
90+
91+
it('should error if "sub" does not match "verify.subject" option', function() {
92+
const token = signWithSubject({sub: 'foo'});
93+
expect(() => jwt.verify(token, undefined, {subject: 'bar'})).to.throw(
94+
jwt.JsonWebTokenError,
95+
'jwt subject invalid. expected: bar'
96+
);
97+
});
98+
99+
it('should error without "sub" and with "verify.subject" option', function() {
100+
const token = signWithSubject({});
101+
expect(() => jwt.verify(token, undefined, {subject: 'foo'})).to.throw(
102+
jwt.JsonWebTokenError,
103+
'jwt subject invalid. expected: foo'
104+
);
105+
});
106+
});
107+
});

‎test/jwt.asymmetric_signing.tests.js

-36
Original file line numberDiff line numberDiff line change
@@ -157,42 +157,6 @@ describe('Asymmetric Algorithms', function(){
157157
});
158158
});
159159

160-
describe('when signing a token with subject', function () {
161-
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, subject: 'subject' });
162-
163-
it('should check subject', function (done) {
164-
jwt.verify(token, pub, { subject: 'subject' }, function (err, decoded) {
165-
assert.isNotNull(decoded);
166-
assert.isNull(err);
167-
done();
168-
});
169-
});
170-
171-
it('should throw when invalid subject', function (done) {
172-
jwt.verify(token, pub, { subject: 'wrongSubject' }, function (err, decoded) {
173-
assert.isUndefined(decoded);
174-
assert.isNotNull(err);
175-
assert.equal(err.name, 'JsonWebTokenError');
176-
assert.instanceOf(err, jwt.JsonWebTokenError);
177-
done();
178-
});
179-
});
180-
});
181-
182-
describe('when signing a token without subject', function () {
183-
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm });
184-
185-
it('should check subject', function (done) {
186-
jwt.verify(token, pub, { subject: 'subject' }, function (err, decoded) {
187-
assert.isUndefined(decoded);
188-
assert.isNotNull(err);
189-
assert.equal(err.name, 'JsonWebTokenError');
190-
assert.instanceOf(err, jwt.JsonWebTokenError);
191-
done();
192-
});
193-
});
194-
});
195-
196160
describe('when signing a token with jwt id', function () {
197161
var token = jwt.sign({ foo: 'bar' }, priv, { algorithm: algorithm, jwtid: 'jwtid' });
198162

‎test/schema.tests.js

-7
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ describe('schema', function() {
5151
sign({issuer: 'foo'});
5252
});
5353

54-
it('should validate subject', function () {
55-
expect(function () {
56-
sign({ subject: 10 });
57-
}).to.throw(/"subject" must be a string/);
58-
sign({subject: 'foo'});
59-
});
60-
6154
it('should validate noTimestamp', function () {
6255
expect(function () {
6356
sign({ noTimestamp: 10 });

0 commit comments

Comments
 (0)
Please sign in to comment.