diff --git a/test/oauth.passreq.test.js b/test/oauth.passreq.test.js deleted file mode 100644 index 4262641..0000000 --- a/test/oauth.passreq.test.js +++ /dev/null @@ -1,151 +0,0 @@ -var chai = require('chai') - , OAuthStrategy = require('../lib/strategy'); - - -describe('OAuthStrategy', function() { - - describe('passing request to verify callback', function() { - var strategy = new OAuthStrategy({ - requestTokenURL: 'https://www.example.com/oauth/request_token', - accessTokenURL: 'https://www.example.com/oauth/access_token', - userAuthorizationURL: 'https://www.example.com/oauth/authorize', - consumerKey: 'ABC123', - consumerSecret: 'secret', - passReqToCallback: true - }, function(req, token, tokenSecret, profile, done) { - if (Object.keys(profile).length !== 0) { return done(null, false); } - - if (token == 'nnch734d00sl2jdk' && tokenSecret == 'pfkkdhi9sl3r4s00') { - return done(null, { id: '1234' }, { message: 'Hello', foo: req.headers['x-foo'] }); - } - return done(null, false); - }); - - // inject a "mock" oauth instance - strategy._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) { - if (token == 'hh5s93j4hdidpola' && tokenSecret == 'hdhd0244k9j7ao03' && verifier == 'hfdp7dh39dks9884') { - return callback(null, 'nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00', {}); - } else { - return callback(null, 'wrong-token', 'wrong-token-secret'); - } - } - - describe('handling an authorized callback request', function() { - var request - , user - , info; - - before(function(done) { - chai.passport.use(strategy) - .success(function(u, i) { - user = u; - info = i; - done(); - }) - .req(function(req) { - request = req; - req.headers['x-foo'] = 'hello'; - req.query = {}; - req.query['oauth_token'] = 'hh5s93j4hdidpola'; - req.query['oauth_verifier'] = 'hfdp7dh39dks9884'; - req.session = {}; - req.session['oauth'] = {}; - req.session['oauth']['oauth_token'] = 'hh5s93j4hdidpola'; - req.session['oauth']['oauth_token_secret'] = 'hdhd0244k9j7ao03'; - }) - .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'); - }); - - it('should supply request header in info', function() { - expect(info.foo).to.equal('hello'); - }); - - it('should remove token and token secret from session', function() { - expect(request.session['oauth']).to.be.undefined; - }); - }); - }); - - describe('passing request to verify callback that accepts params', function() { - var strategy = new OAuthStrategy({ - requestTokenURL: 'https://www.example.com/oauth/request_token', - accessTokenURL: 'https://www.example.com/oauth/access_token', - userAuthorizationURL: 'https://www.example.com/oauth/authorize', - consumerKey: 'ABC123', - consumerSecret: 'secret', - passReqToCallback: true - }, function(req, token, tokenSecret, params, profile, done) { - if (Object.keys(profile).length !== 0) { return done(null, false); } - - if (token == 'nnch734d00sl2jdk' && tokenSecret == 'pfkkdhi9sl3r4s00' && params.elephant == 'purple') { - return done(null, { id: '1234' }, { message: 'Hello', foo: req.headers['x-foo'] }); - } - return done(null, false); - }); - - // inject a "mock" oauth instance - strategy._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) { - if (token == 'hh5s93j4hdidpola' && tokenSecret == 'hdhd0244k9j7ao03' && verifier == 'hfdp7dh39dks9884') { - return callback(null, 'nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00', { elephant: 'purple' }); - } else { - return callback(null, 'wrong-token', 'wrong-token-secret'); - } - } - - describe('handling an authorized callback request', function() { - var request - , user - , info; - - before(function(done) { - chai.passport.use(strategy) - .success(function(u, i) { - user = u; - info = i; - done(); - }) - .req(function(req) { - request = req; - req.headers['x-foo'] = 'hello'; - req.query = {}; - req.query['oauth_token'] = 'hh5s93j4hdidpola'; - req.query['oauth_verifier'] = 'hfdp7dh39dks9884'; - req.session = {}; - req.session['oauth'] = {}; - req.session['oauth']['oauth_token'] = 'hh5s93j4hdidpola'; - req.session['oauth']['oauth_token_secret'] = 'hdhd0244k9j7ao03'; - }) - .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'); - }); - - it('should supply request header in info', function() { - expect(info.foo).to.equal('hello'); - }); - - it('should remove token and token secret from session', function() { - expect(request.session['oauth']).to.be.undefined; - }); - }); - }); - -}); diff --git a/test/oauth.test.js b/test/oauth.test.js index 983f80f..e3e5d7f 100644 --- a/test/oauth.test.js +++ b/test/oauth.test.js @@ -394,6 +394,139 @@ describe('OAuthStrategy', function() { }); }); // that was approved using verify callback that accepts params + describe('that was approved using verify callback, in passReqToCallback mode', function() { + var strategy = new OAuthStrategy({ + requestTokenURL: 'https://www.example.com/oauth/request_token', + accessTokenURL: 'https://www.example.com/oauth/access_token', + userAuthorizationURL: 'https://www.example.com/oauth/authorize', + consumerKey: 'ABC123', + consumerSecret: 'secret', + passReqToCallback: true + }, function(req, token, tokenSecret, profile, done) { + if (req.method != 'GET') { return callback(new Error('incorrect req argument')); } + if (token != 'nnch734d00sl2jdk') { return callback(new Error('incorrect token argument')); } + if (tokenSecret != 'pfkkdhi9sl3r4s00') { return callback(new Error('incorrect tokenSecret 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._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) { + if (token != 'hh5s93j4hdidpola') { return callback(new Error('incorrect token argument')); } + if (tokenSecret != 'hdhd0244k9j7ao03') { return callback(new Error('incorrect tokenSecret argument')); } + if (verifier != 'hfdp7dh39dks9884') { return callback(new Error('incorrect verifier argument')); } + + return callback(null, 'nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00', {}); + }; + + + var request + , user + , info; + + before(function(done) { + chai.passport.use(strategy) + .success(function(u, i) { + user = u; + info = i; + done(); + }) + .req(function(req) { + request = req; + req.query = {}; + req.query['oauth_token'] = 'hh5s93j4hdidpola'; + req.query['oauth_verifier'] = 'hfdp7dh39dks9884'; + req.session = {}; + req.session['oauth'] = {}; + req.session['oauth']['oauth_token'] = 'hh5s93j4hdidpola'; + req.session['oauth']['oauth_token_secret'] = 'hdhd0244k9j7ao03'; + }) + .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'); + }); + + it('should remove token and token secret from session', function() { + expect(request.session['oauth']).to.be.undefined; + }); + }); // that was approved using verify callback, in passReqToCallback mode + + describe('that was approved using verify callback that accepts params, in passReqToCallback mode', function() { + var strategy = new OAuthStrategy({ + requestTokenURL: 'https://www.example.com/oauth/request_token', + accessTokenURL: 'https://www.example.com/oauth/access_token', + userAuthorizationURL: 'https://www.example.com/oauth/authorize', + consumerKey: 'ABC123', + consumerSecret: 'secret', + passReqToCallback: true + }, function(req, token, tokenSecret, params, profile, done) { + if (req.method != 'GET') { return callback(new Error('incorrect req argument')); } + if (token != 'nnch734d00sl2jdk') { return callback(new Error('incorrect token argument')); } + if (tokenSecret != 'pfkkdhi9sl3r4s00') { return callback(new Error('incorrect tokenSecret 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')); } + if (params.elephant != 'purple') { return callback(new Error('incorrect params argument')); } + + return done(null, { id: '1234' }, { message: 'Hello' }); + }); + + strategy._oauth.getOAuthAccessToken = function(token, tokenSecret, verifier, callback) { + if (token != 'hh5s93j4hdidpola') { return callback(new Error('incorrect token argument')); } + if (tokenSecret != 'hdhd0244k9j7ao03') { return callback(new Error('incorrect tokenSecret argument')); } + if (verifier != 'hfdp7dh39dks9884') { return callback(new Error('incorrect verifier argument')); } + + return callback(null, 'nnch734d00sl2jdk', 'pfkkdhi9sl3r4s00', { elephant: 'purple' }); + }; + + + var request + , user + , info; + + before(function(done) { + chai.passport.use(strategy) + .success(function(u, i) { + user = u; + info = i; + done(); + }) + .req(function(req) { + request = req; + req.query = {}; + req.query['oauth_token'] = 'hh5s93j4hdidpola'; + req.query['oauth_verifier'] = 'hfdp7dh39dks9884'; + req.session = {}; + req.session['oauth'] = {}; + req.session['oauth']['oauth_token'] = 'hh5s93j4hdidpola'; + req.session['oauth']['oauth_token_secret'] = 'hdhd0244k9j7ao03'; + }) + .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'); + }); + + it('should remove token and token secret from session', function() { + expect(request.session['oauth']).to.be.undefined; + }); + }); // that was approved using verify callback that accepts params, in passReqToCallback mode + describe('that fails due to verify callback supplying false', function() { var strategy = new OAuthStrategy({ requestTokenURL: 'https://www.example.com/oauth/request_token',