Skip to content

Commit 342b07b

Browse files
MitMaroziluvatar
authored andcommittedOct 16, 2018
Implement async/sync tests for sub claim (#534)
Refactor existing tests for the sub claim to use the async/sync test helpers.
1 parent 29cd654 commit 342b07b

File tree

1 file changed

+97
-51
lines changed

1 file changed

+97
-51
lines changed
 

‎test/claim-sub.tests.js

+97-51
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
const jwt = require('../');
44
const expect = require('chai').expect;
55
const util = require('util');
6+
const testUtils = require('./test-utils');
67

7-
function signWithSubject(payload, subject) {
8+
function signWithSubject(subject, payload, callback) {
89
const options = {algorithm: 'none'};
910
if (subject !== undefined) {
1011
options.subject = subject;
1112
}
12-
return jwt.sign(payload, undefined, options);
13+
testUtils.signJWTHelper(payload, 'secret', options, callback);
1314
}
1415

1516
describe('subject', function() {
@@ -31,77 +32,122 @@ describe('subject', function() {
3132
{},
3233
{foo: 'bar'},
3334
].forEach((subject) => {
34-
it(`should error with with value ${util.inspect(subject)}`, function () {
35-
expect(() => signWithSubject({}, subject)).to.throw('"subject" must be a string');
35+
it(`should error with with value ${util.inspect(subject)}`, function (done) {
36+
signWithSubject(subject, {}, (err) => {
37+
testUtils.asyncCheck(done, () => {
38+
expect(err).to.be.instanceOf(Error);
39+
expect(err).to.have.property('message', '"subject" must be a string');
40+
});
41+
});
3642
});
3743
});
3844

3945
// 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-
);
46+
it('should error with with value undefined', function (done) {
47+
testUtils.signJWTHelper({}, undefined, {subject: undefined, algorithm: 'none'}, (err) => {
48+
testUtils.asyncCheck(done, () => {
49+
expect(err).to.be.instanceOf(Error);
50+
expect(err).to.have.property('message', '"subject" must be a string');
51+
});
52+
});
4453
});
4554

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-
);
55+
it('should error when "sub" is in payload', function (done) {
56+
signWithSubject('foo', {sub: 'bar'}, (err) => {
57+
testUtils.asyncCheck(done, () => {
58+
expect(err).to.be.instanceOf(Error);
59+
expect(err).to.have.property(
60+
'message',
61+
'Bad "options.subject" option. The payload already has an "sub" property.'
62+
);
63+
});
64+
});
5065
});
5166

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-
);
67+
it('should error with a string payload', function (done) {
68+
signWithSubject('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 subject option for string payload'
74+
);
75+
});
76+
});
5777
});
5878

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-
);
79+
it('should error with a Buffer payload', function (done) {
80+
signWithSubject('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 subject option for object payload'
86+
);
87+
});
88+
});
6389
});
6490
});
6591

6692
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');
93+
it('should verify with a string "subject"', function (done) {
94+
signWithSubject('foo', {}, (e1, token) => {
95+
testUtils.verifyJWTHelper(token, undefined, {subject: 'foo'}, (e2, decoded) => {
96+
testUtils.asyncCheck(done, () => {
97+
expect(e1).to.be.null;
98+
expect(e2).to.be.null;
99+
expect(decoded).to.have.property('sub', 'foo');
100+
});
101+
})
102+
});
73103
});
74104

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');
105+
it('should verify with a string "sub"', function (done) {
106+
signWithSubject(undefined, {sub: 'foo'}, (e1, token) => {
107+
testUtils.verifyJWTHelper(token, undefined, {subject: 'foo'}, (e2, decoded) => {
108+
testUtils.asyncCheck(done, () => {
109+
expect(e1).to.be.null;
110+
expect(e2).to.be.null;
111+
expect(decoded).to.have.property('sub', 'foo');
112+
});
113+
})
114+
});
81115
});
82116

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');
117+
it('should not verify "sub" if verify "subject" option not provided', function(done) {
118+
signWithSubject(undefined, {sub: 'foo'}, (e1, token) => {
119+
testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => {
120+
testUtils.asyncCheck(done, () => {
121+
expect(e1).to.be.null;
122+
expect(e2).to.be.null;
123+
expect(decoded).to.have.property('sub', 'foo');
124+
});
125+
})
126+
});
89127
});
90128

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-
);
129+
it('should error if "sub" does not match verify "subject" option', function(done) {
130+
signWithSubject(undefined, {sub: 'foo'}, (e1, token) => {
131+
testUtils.verifyJWTHelper(token, undefined, {subject: 'bar'}, (e2) => {
132+
testUtils.asyncCheck(done, () => {
133+
expect(e1).to.be.null;
134+
expect(e2).to.be.instanceOf(jwt.JsonWebTokenError);
135+
expect(e2).to.have.property('message', 'jwt subject invalid. expected: bar');
136+
});
137+
})
138+
});
97139
});
98140

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-
);
141+
it('should error without "sub" and with verify "subject" option', function(done) {
142+
signWithSubject(undefined, {}, (e1, token) => {
143+
testUtils.verifyJWTHelper(token, undefined, {subject: 'foo'}, (e2) => {
144+
testUtils.asyncCheck(done, () => {
145+
expect(e1).to.be.null;
146+
expect(e2).to.be.instanceOf(jwt.JsonWebTokenError);
147+
expect(e2).to.have.property('message', 'jwt subject invalid. expected: foo');
148+
});
149+
})
150+
});
105151
});
106152
});
107153
});

0 commit comments

Comments
 (0)
Please sign in to comment.