From d175fee8d15a76606925b76b97585493f277c462 Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Tue, 2 Feb 2016 16:12:11 -0800 Subject: [PATCH] More test cases. --- test/strategy.test.js | 56 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/test/strategy.test.js b/test/strategy.test.js index bc00585..e0f3829 100644 --- a/test/strategy.test.js +++ b/test/strategy.test.js @@ -125,6 +125,60 @@ describe('Strategy', function() { expect(err.message).to.equal('The code passed is incorrect or expired.'); expect(err.code).to.equal('bad_verification_code'); }); - }); // error caused by invalid code sent to token endpoint, with response indicating success + }); // error caused by invalid code sent to token endpoint, with response erroneously indicating success + + describe('error caused by invalid code sent to token endpoint, with response correctly indicating success', function() { + var OAuth2Strategy = require('passport-oauth2').Strategy; + var OAuth2; + if (existsSync('node_modules/oauth')) { // npm 3.x + OAuth2 = require('oauth').OAuth2; + } else { + OAuth2 = require('passport-oauth2/node_modules/oauth').OAuth2; + } + + var MockOAuth2Strategy = function(options, verify) { + OAuth2Strategy.call(this, options, verify); + + this._oauth2 = new OAuth2(options.clientID, options.clientSecret, + '', options.authorizationURL, options.tokenURL, options.customHeaders); + this._oauth2.getOAuthAccessToken = function(code, options, callback) { + return callback({ + statusCode: 401, + data: '{"error":"bad_verification_code","error_description":"The code passed is incorrect or expired.","error_uri":"https://developer.github.com/v3/oauth/#bad-verification-code"}' }); + }; + } + util.inherits(MockOAuth2Strategy, OAuth2Strategy); + + var GitHubStrategy = $require('../lib/strategy', { + 'passport-oauth2': MockOAuth2Strategy + }) + + var strategy = new GitHubStrategy({ + clientID: 'ABC123', + clientSecret: 'secret' + }, function() {}); + + + var err; + + before(function(done) { + chai.passport.use(strategy) + .error(function(e) { + err = e; + done(); + }) + .req(function(req) { + req.query = {}; + req.query.code = 'SplxlOBeZQQYbYS6WxSbIA+ALT1'; + }) + .authenticate(); + }); + + it('should error', function() { + expect(err.constructor.name).to.equal('TokenError'); + expect(err.message).to.equal('The code passed is incorrect or expired.'); + expect(err.code).to.equal('bad_verification_code'); + }); + }); // error caused by invalid code sent to token endpoint, with response correctly indicating success });