-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathissue_1000.tests.js
More file actions
112 lines (95 loc) · 3.18 KB
/
Copy pathissue_1000.tests.js
File metadata and controls
112 lines (95 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const jwt = require('../');
const expect = require('chai').expect;
describe('issue 1000 - callback executed twice on payload conflict', function() {
it('should call the callback only once when payload has conflicting "iss" property', function(done) {
let callCount = 0;
jwt.sign(
{ iss: 'bar', iat: 1757476476 },
'secret',
{ algorithm: 'HS256', issuer: 'foo' },
(err, token) => {
callCount++;
// The callback should only be called once
if (callCount > 1) {
done(new Error(`Callback was called ${callCount} times instead of once`));
return;
}
// Give time for potential second callback
setTimeout(() => {
expect(callCount).to.equal(1);
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('Bad "options.issuer" option. The payload already has an "iss" property.');
expect(token).to.be.undefined;
done();
}, 50);
}
);
});
it('should call the callback only once when payload has conflicting "sub" property', function(done) {
let callCount = 0;
jwt.sign(
{ sub: 'bar' },
'secret',
{ algorithm: 'HS256', subject: 'foo' },
(err, token) => {
callCount++;
if (callCount > 1) {
done(new Error(`Callback was called ${callCount} times instead of once`));
return;
}
setTimeout(() => {
expect(callCount).to.equal(1);
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('Bad "options.subject" option. The payload already has an "sub" property.');
expect(token).to.be.undefined;
done();
}, 50);
}
);
});
it('should call the callback only once when payload has conflicting "aud" property', function(done) {
let callCount = 0;
jwt.sign(
{ aud: 'bar' },
'secret',
{ algorithm: 'HS256', audience: 'foo' },
(err, token) => {
callCount++;
if (callCount > 1) {
done(new Error(`Callback was called ${callCount} times instead of once`));
return;
}
setTimeout(() => {
expect(callCount).to.equal(1);
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('Bad "options.audience" option. The payload already has an "aud" property.');
expect(token).to.be.undefined;
done();
}, 50);
}
);
});
it('should call the callback only once when payload has conflicting "jti" property', function(done) {
let callCount = 0;
jwt.sign(
{ jti: 'bar' },
'secret',
{ algorithm: 'HS256', jwtid: 'foo' },
(err, token) => {
callCount++;
if (callCount > 1) {
done(new Error(`Callback was called ${callCount} times instead of once`));
return;
}
setTimeout(() => {
expect(callCount).to.equal(1);
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('Bad "options.jwtid" option. The payload already has an "jti" property.');
expect(token).to.be.undefined;
done();
}, 50);
}
);
});
});