@@ -170,26 +170,76 @@ describe('Strategy', function() {
170
170
171
171
} ) ;
172
172
173
- describe ( 'verify function' , function ( ) {
174
- it ( 'should call the secretOrKeyProvider with the token and pass it to verify' , function ( ) {
175
- const provider = sinon . spy ( function ( token , done ) {
176
- done ( null , 'secret' ) ;
173
+
174
+ describe ( 'handling a request when constructed with a secretOrKeyProvider function that succeeds' , function ( ) {
175
+
176
+ var strategy , fakeSecretOrKeyProvider , expectedReqeust ;
177
+
178
+ before ( function ( done ) {
179
+ fakeSecretOrKeyProvider = sinon . spy ( function ( request , token , done ) {
180
+ done ( null , 'secret from callback' ) ;
177
181
} ) ;
178
- const verifyStub = sinon . stub ( ) ;
179
- verify ( test_data . valid_jwt . payload , provider , null , null , verifyStub ) ;
180
- expect ( provider . calledOnce ) . to . be . true ;
181
- expect ( provider . calledWith ( test_data . valid_jwt . payload ) ) . to . be . true ;
182
- expect ( verifyStub . calledWith ( test_data . valid_jwt . payload , 'secret' ) ) . to . be . true ;
182
+ opts = {
183
+ secretOrKeyProvider : fakeSecretOrKeyProvider ,
184
+ jwtFromRequest : function ( request ) {
185
+ return 'an undecoded jwt string' ;
186
+ }
187
+ }
188
+ strategy = new Strategy ( opts , function ( jwtPayload , next ) {
189
+ return next ( null , { user_id : 'dont care' } , { } ) ;
190
+ } ) ;
191
+
192
+ chai . passport . use ( strategy )
193
+ . success ( function ( u , i ) {
194
+ done ( ) ;
195
+ } )
196
+ . req ( function ( req ) {
197
+ expectedReqeust = req ;
198
+ } )
199
+ . authenticate ( ) ;
200
+ } ) ;
201
+
202
+ it ( 'should call the fake secret or key provider with the reqeust' , function ( ) {
203
+ expect ( fakeSecretOrKeyProvider . calledWith ( expectedReqeust , sinon . match . any , sinon . match . any ) ) . to . be . true ;
204
+ } ) ;
205
+
206
+ it ( 'should call the secretOrKeyProvider with the undecoded jwt' , function ( ) {
207
+ expect ( fakeSecretOrKeyProvider . calledWith ( sinon . match . any , 'an undecoded jwt string' , sinon . match . any ) ) . to . be . true ;
183
208
} ) ;
184
209
185
- it ( 'should call the callback with an error if the secretOrKeyProvider fails to return a key' , function ( ) {
186
- const providerStub = function ( token , done ) {
187
- done ( new Error ( 'invalid key' ) ) ;
188
- } ;
189
- const callback = sinon . spy ( ) ;
190
- verify ( test_data . valid_jwt . payload , providerStub , null , callback ) ;
191
- expect ( callback . calledWith ( sinon . match . instanceOf ( Error ) ) ) ;
210
+ it ( 'should call JwtVerifier with the value returned from secretOrKeyProvider' , function ( ) {
211
+ expect ( Strategy . JwtVerifier . calledWith ( sinon . match . any , 'secret from callback' , sinon . match . any , sinon . match . any ) ) . to . be . true ;
192
212
} ) ;
193
213
} ) ;
194
214
215
+
216
+ describe ( 'handling a request when constructed with a secretOrKeyProvider function that errors' , function ( ) {
217
+ var errorMessage ;
218
+
219
+ before ( function ( done ) {
220
+ fakeSecretOrKeyProvider = sinon . spy ( function ( request , token , done ) {
221
+ done ( 'Error occurred looking for the secret' ) ;
222
+ } ) ;
223
+ opts = {
224
+ secretOrKeyProvider : fakeSecretOrKeyProvider ,
225
+ jwtFromRequest : function ( request ) {
226
+ return 'an undecoded jwt string' ;
227
+ }
228
+ }
229
+ strategy = new Strategy ( opts , function ( jwtPayload , next ) {
230
+ return next ( null , { user_id : 'dont care' } , { } ) ;
231
+ } ) ;
232
+
233
+ chai . passport . use ( strategy )
234
+ . fail ( function ( i ) {
235
+ errorMessage = i ;
236
+ done ( ) ;
237
+ } )
238
+ . authenticate ( ) ;
239
+ } ) ;
240
+
241
+ it ( 'should fail with the error message from the secretOrKeyProvider' , function ( ) {
242
+ expect ( errorMessage ) . to . equal ( 'Error occurred looking for the secret' ) ;
243
+ } ) ;
244
+ } ) ;
195
245
} ) ;
0 commit comments