Skip to content

Commit f05724f

Browse files
committed
Clarify test cases.
1 parent 0ff401b commit f05724f

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed

test/oauth2.state.session.test.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,112 @@ describe('OAuth2Strategy', function() {
274274

275275
}); // using default session state store
276276

277+
278+
describe('using default session state store with session key option', function() {
279+
var strategy = new OAuth2Strategy({
280+
authorizationURL: 'https://www.example.com/oauth2/authorize',
281+
tokenURL: 'https://www.example.com/oauth2/token',
282+
clientID: 'ABC123',
283+
clientSecret: 'secret',
284+
callbackURL: 'https://www.example.net/auth/example/callback',
285+
state: true,
286+
sessionKey: 'oauth2:example'
287+
},
288+
function(accessToken, refreshToken, profile, done) {
289+
if (accessToken !== '2YotnFZFEjr1zCsicMWpAA') { return done(new Error('incorrect accessToken argument')); }
290+
if (refreshToken !== 'tGzv3JOkF0XG5Qx2TlKWIA') { return done(new Error('incorrect refreshToken argument')); }
291+
if (typeof profile !== 'object') { return done(new Error('incorrect profile argument')); }
292+
if (Object.keys(profile).length !== 0) { return done(new Error('incorrect profile argument')); }
293+
294+
return done(null, { id: '1234' }, { message: 'Hello' });
295+
});
296+
297+
strategy._oauth2.getOAuthAccessToken = function(code, options, callback) {
298+
if (code !== 'SplxlOBeZQQYbYS6WxSbIA') { return callback(new Error('incorrect code argument')); }
299+
if (options.grant_type !== 'authorization_code') { return callback(new Error('incorrect options.grant_type argument')); }
300+
if (options.redirect_uri !== 'https://www.example.net/auth/example/callback') { return callback(new Error('incorrect options.redirect_uri argument')); }
301+
302+
return callback(null, '2YotnFZFEjr1zCsicMWpAA', 'tGzv3JOkF0XG5Qx2TlKWIA', { token_type: 'example' });
303+
}
304+
305+
306+
describe('issuing authorization request', function() {
307+
308+
describe('that redirects to service provider', function() {
309+
var request, url;
310+
311+
before(function(done) {
312+
chai.passport.use(strategy)
313+
.redirect(function(u) {
314+
url = u;
315+
done();
316+
})
317+
.req(function(req) {
318+
request = req;
319+
req.session = {};
320+
})
321+
.authenticate();
322+
});
323+
324+
it('should be redirected', function() {
325+
var u = uri.parse(url, true);
326+
expect(u.query.state).to.have.length(24);
327+
});
328+
329+
it('should save state in session', function() {
330+
var u = uri.parse(url, true);
331+
332+
expect(request.session['oauth2:example'].state).to.have.length(24);
333+
expect(request.session['oauth2:example'].state).to.equal(u.query.state);
334+
});
335+
}); // that redirects to service provider
336+
337+
}); // issuing authorization request
338+
339+
describe('processing response to authorization request', function() {
340+
341+
describe('that was approved', function() {
342+
var request
343+
, user
344+
, info;
345+
346+
before(function(done) {
347+
chai.passport.use(strategy)
348+
.success(function(u, i) {
349+
user = u;
350+
info = i;
351+
done();
352+
})
353+
.req(function(req) {
354+
request = req;
355+
356+
req.query = {};
357+
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
358+
req.query.state = 'DkbychwKu8kBaJoLE5yeR5NK';
359+
req.session = {};
360+
req.session['oauth2:example'] = {};
361+
req.session['oauth2:example']['state'] = 'DkbychwKu8kBaJoLE5yeR5NK';
362+
})
363+
.authenticate();
364+
});
365+
366+
it('should supply user', function() {
367+
expect(user).to.be.an.object;
368+
expect(user.id).to.equal('1234');
369+
});
370+
371+
it('should supply info', function() {
372+
expect(info).to.be.an.object;
373+
expect(info.message).to.equal('Hello');
374+
});
375+
376+
it('should remove state from session', function() {
377+
expect(request.session['oauth2:example']).to.be.undefined;
378+
});
379+
}); // that was approved
380+
381+
}); // processing response to authorization request
382+
383+
}); // using default session state store with session key option
384+
277385
});

test/oauth2.state.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ describe('OAuth2Strategy', function() {
287287
}
288288
}
289289

290+
// OK
290291
describe('handling an authorized return request with correct state', function() {
291292
var request
292293
, user
@@ -327,6 +328,7 @@ describe('OAuth2Strategy', function() {
327328
});
328329
});
329330

331+
// OK
330332
describe('handling a request to be redirected for authorization', function() {
331333
var request, url;
332334

@@ -356,7 +358,8 @@ describe('OAuth2Strategy', function() {
356358
});
357359
});
358360
});
359-
361+
362+
// OK
360363
describe('with explicit state declared as authenticate option', function() {
361364
var strategy = new OAuth2Strategy({
362365
authorizationURL: 'https://www.example.com/oauth2/authorize',

test/oauth2.test.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,35 @@ describe('OAuth2Strategy', function() {
271271
});
272272
}); // that redirects to service provider with scope option as array using non-standard separator
273273

274+
describe('that redirects to service provider with state option', function() {
275+
var strategy = new OAuth2Strategy({
276+
authorizationURL: 'https://www.example.com/oauth2/authorize',
277+
tokenURL: 'https://www.example.com/oauth2/token',
278+
clientID: 'ABC123',
279+
clientSecret: 'secret',
280+
callbackURL: 'https://www.example.net/auth/example/callback',
281+
},
282+
function(accessToken, refreshToken, profile, done) {});
283+
284+
285+
var url;
286+
287+
before(function(done) {
288+
chai.passport.use(strategy)
289+
.redirect(function(u) {
290+
url = u;
291+
done();
292+
})
293+
.req(function(req) {
294+
})
295+
.authenticate({ state: 'foo123' });
296+
});
297+
298+
it('should be redirected', function() {
299+
expect(url).to.equal('https://www.example.com/oauth2/authorize?response_type=code&redirect_uri=https%3A%2F%2Fwww.example.net%2Fauth%2Fexample%2Fcallback&state=foo123&client_id=ABC123');
300+
});
301+
}); // that redirects to service provider with state option
302+
274303
describe('that redirects to service provider with redirect URI option', function() {
275304
var strategy = new OAuth2Strategy({
276305
authorizationURL: 'https://www.example.com/oauth2/authorize',
@@ -332,8 +361,6 @@ describe('OAuth2Strategy', function() {
332361
});
333362
}); // that redirects to service provider with relative redirect URI option
334363

335-
// WIP: Proxy security tests
336-
337364
}); // issuing authorization request
338365

339366

0 commit comments

Comments
 (0)