From a231390cce526d32bf81c0d292284dc8063c1734 Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Fri, 12 Feb 2016 12:53:12 -0800 Subject: [PATCH] Clarify test cases. --- test/oauth2.test.js | 55 +++++++++++++++++++++++++++++++ test/oauth2.verify.test.js | 66 -------------------------------------- 2 files changed, 55 insertions(+), 66 deletions(-) delete mode 100644 test/oauth2.verify.test.js diff --git a/test/oauth2.test.js b/test/oauth2.test.js index 318fe39..f01b166 100644 --- a/test/oauth2.test.js +++ b/test/oauth2.test.js @@ -495,6 +495,61 @@ describe('OAuth2Strategy', function() { }); }); // that was approved with relative redirect URI option + describe('that was approved using verify callback that accepts params', function() { + var strategy = new OAuth2Strategy({ + authorizationURL: 'https://www.example.com/oauth2/authorize', + tokenURL: 'https://www.example.com/oauth2/token', + clientID: 'ABC123', + clientSecret: 'secret', + callbackURL: 'https://www.example.net/auth/example/callback', + }, + function(accessToken, refreshToken, params, profile, done) { + if (accessToken !== '2YotnFZFEjr1zCsicMWpAA') { return done(new Error('incorrect accessToken argument')); } + if (refreshToken !== 'tGzv3JOkF0XG5Qx2TlKWIA') { return done(new Error('incorrect refreshToken argument')); } + if (params.example_parameter !== 'example_value') { return done(new Error('incorrect params argument')); } + if (typeof profile !== 'object') { return done(new Error('incorrect profile argument')); } + if (Object.keys(profile).length !== 0) { return done(new Error('incorrect profile argument')); } + + return done(null, { id: '1234' }, { message: 'Hello' }); + }); + + strategy._oauth2.getOAuthAccessToken = function(code, options, callback) { + if (code !== 'SplxlOBeZQQYbYS6WxSbIA') { return callback(new Error('incorrect code argument')); } + if (options.grant_type !== 'authorization_code') { return callback(new Error('incorrect options.grant_type argument')); } + if (options.redirect_uri !== 'https://www.example.net/auth/example/callback') { return callback(new Error('incorrect options.redirect_uri argument')); } + + return callback(null, '2YotnFZFEjr1zCsicMWpAA', 'tGzv3JOkF0XG5Qx2TlKWIA', { token_type: 'example', expires_in: 3600, example_parameter: 'example_value' }); + } + + + var user + , info; + + before(function(done) { + chai.passport.use(strategy) + .success(function(u, i) { + user = u; + info = i; + done(); + }) + .req(function(req) { + req.query = {}; + req.query.code = 'SplxlOBeZQQYbYS6WxSbIA'; + }) + .authenticate(); + }); + + it('should supply user', function() { + expect(user).to.be.an.object; + expect(user.id).to.equal('1234'); + }); + + it('should supply info', function() { + expect(info).to.be.an.object; + expect(info.message).to.equal('Hello'); + }); + }); // that was approved using verify callback that accepts params + describe('that fails due to verify callback supplying false', function() { var strategy = new OAuth2Strategy({ authorizationURL: 'https://www.example.com/oauth2/authorize', diff --git a/test/oauth2.verify.test.js b/test/oauth2.verify.test.js deleted file mode 100644 index 8b75856..0000000 --- a/test/oauth2.verify.test.js +++ /dev/null @@ -1,66 +0,0 @@ -var chai = require('chai') - , OAuth2Strategy = require('../lib/strategy'); - - -describe('OAuth2Strategy', function() { - - describe('with verify callback that accepts params', function() { - var strategy = new OAuth2Strategy({ - authorizationURL: 'https://www.example.com/oauth2/authorize', - tokenURL: 'https://www.example.com/oauth2/token', - clientID: 'ABC123', - clientSecret: 'secret', - callbackURL: 'https://www.example.net/auth/example/callback', - }, - function(accessToken, refreshToken, params, profile, done) { - if (params.example_parameter !== 'example_value') { return done(null, false); } - if (Object.keys(profile).length !== 0) { return done(null, false); } - - if (accessToken == '2YotnFZFEjr1zCsicMWpAA' && refreshToken == 'tGzv3JOkF0XG5Qx2TlKWIA') { - return done(null, { id: '1234' }, { message: 'Hello' }); - } - return done(null, false); - }); - - // inject a "mock" oauth2 instance - strategy._oauth2.getOAuthAccessToken = function(code, options, callback) { - if (options.grant_type !== 'authorization_code') { return callback(null, 'wrong-access-token', 'wrong-refresh-token'); } - - if (code == 'SplxlOBeZQQYbYS6WxSbIA' && options.redirect_uri == 'https://www.example.net/auth/example/callback') { - callback(null, '2YotnFZFEjr1zCsicMWpAA', 'tGzv3JOkF0XG5Qx2TlKWIA', { token_type: 'example', expires_in: 3600, example_parameter: 'example_value' }); - } else { - callback(null, 'wrong-access-token', 'wrong-refresh-token'); - } - } - - describe('handling an authorized return request', function() { - var user - , info; - - before(function(done) { - chai.passport.use(strategy) - .success(function(u, i) { - user = u; - info = i; - done(); - }) - .req(function(req) { - req.query = {}; - req.query.code = 'SplxlOBeZQQYbYS6WxSbIA'; - }) - .authenticate(); - }); - - it('should supply user', function() { - expect(user).to.be.an.object; - expect(user.id).to.equal('1234'); - }); - - it('should supply info', function() { - expect(info).to.be.an.object; - expect(info.message).to.equal('Hello'); - }); - }); - }); - -});