Skip to content

Commit

Permalink
Clarify test cases for subclass that implements parseErrorResponse.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Feb 17, 2016
1 parent b977798 commit 347204f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 120 deletions.
120 changes: 0 additions & 120 deletions test/oauth2.sub.parseerrorresponse.test.js

This file was deleted.

105 changes: 105 additions & 0 deletions test/oauth2.subclass.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var OAuth2Strategy = require('../lib/strategy')
, AuthorizationError = require('../lib/errors/authorizationerror')
, InternalOAuthError = require('../lib/errors/internaloautherror')
, chai = require('chai')
, util = require('util');

Expand Down Expand Up @@ -142,4 +143,108 @@ describe('OAuth2Strategy subclass', function() {

}); // that overrides tokenParams


describe('that overrides parseErrorResponse', function() {
function FooOAuth2Strategy(options, verify) {
OAuth2Strategy.call(this, options, verify);
}
util.inherits(FooOAuth2Strategy, OAuth2Strategy);

FooOAuth2Strategy.prototype.parseErrorResponse = function(body, status) {
if (status === 500) { throw new Error('something went horribly wrong'); }

var e = new Error('Custom OAuth error');
e.body = body;
e.status = status;
return e;
}


describe('and supplies error', function() {
var strategy = new FooOAuth2Strategy({
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, profile, done) {
if (accessToken == '2YotnFZFEjr1zCsicMWpAA' && refreshToken == 'tGzv3JOkF0XG5Qx2TlKWIA') {
return done(null, { id: '1234' }, { message: 'Hello' });
}
return done(null, false);
});

strategy._oauth2.getOAuthAccessToken = function(code, options, callback) {
return callback({ statusCode: 400, data: 'Invalid code' });
}


var err;

before(function(done) {
chai.passport.use(strategy)
.error(function(e) {
err = e;
done();
})
.req(function(req) {
req.query = {};
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
})
.authenticate();
});

it('should error', function() {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('Custom OAuth error');
expect(err.body).to.equal('Invalid code');
expect(err.status).to.equal(400);
});
}); // and supplies error

describe('and throws exception', function() {
var strategy = new FooOAuth2Strategy({
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, profile, done) {
if (accessToken == '2YotnFZFEjr1zCsicMWpAA' && refreshToken == 'tGzv3JOkF0XG5Qx2TlKWIA') {
return done(null, { id: '1234' }, { message: 'Hello' });
}
return done(null, false);
});

strategy._oauth2.getOAuthAccessToken = function(code, options, callback) {
return callback({ statusCode: 500, data: 'Invalid code' });
}


var err;

before(function(done) {
chai.passport.use(strategy)
.error(function(e) {
err = e;
done();
})
.req(function(req) {
req.query = {};
req.query.code = 'SplxlOBeZQQYbYS6WxSbIA';
})
.authenticate();
});

it('should error', function() {
expect(err).to.be.an.instanceof(InternalOAuthError)
expect(err.message).to.equal('Failed to obtain access token');
expect(err.oauthError.statusCode).to.equal(500);
expect(err.oauthError.data).to.equal('Invalid code');
});
}); // and throws exception
});

});

0 comments on commit 347204f

Please sign in to comment.