3
3
const jwt = require ( '../' ) ;
4
4
const expect = require ( 'chai' ) . expect ;
5
5
const util = require ( 'util' ) ;
6
+ const testUtils = require ( './test-utils' ) ;
6
7
7
- function signWithSubject ( payload , subject ) {
8
+ function signWithSubject ( subject , payload , callback ) {
8
9
const options = { algorithm : 'none' } ;
9
10
if ( subject !== undefined ) {
10
11
options . subject = subject ;
11
12
}
12
- return jwt . sign ( payload , undefined , options ) ;
13
+ testUtils . signJWTHelper ( payload , 'secret' , options , callback ) ;
13
14
}
14
15
15
16
describe ( 'subject' , function ( ) {
@@ -31,77 +32,122 @@ describe('subject', function() {
31
32
{ } ,
32
33
{ foo : 'bar' } ,
33
34
] . 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
+ } ) ;
36
42
} ) ;
37
43
} ) ;
38
44
39
45
// 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
+ } ) ;
44
53
} ) ;
45
54
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
+ } ) ;
50
65
} ) ;
51
66
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
+ } ) ;
57
77
} ) ;
58
78
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
+ } ) ;
63
89
} ) ;
64
90
} ) ;
65
91
66
92
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
+ } ) ;
73
103
} ) ;
74
104
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
+ } ) ;
81
115
} ) ;
82
116
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
+ } ) ;
89
127
} ) ;
90
128
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
+ } ) ;
97
139
} ) ;
98
140
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
+ } ) ;
105
151
} ) ;
106
152
} ) ;
107
153
} ) ;
0 commit comments