Skip to content

Commit

Permalink
Test case for parsing error response.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Aug 15, 2013
1 parent df904f4 commit d180ed9
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions test/oauth2.error.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var chai = require('chai')
, OAuth2Strategy = require('../lib/strategy')
, AccessTokenError = require('../lib/errors/accesstokenerror')
, InternalOAuthError = require('../lib/errors/internaloautherror');


Expand Down Expand Up @@ -50,6 +51,96 @@ describe('OAuth2Strategy', function() {
});
});

describe('that encounters a node-oauth object literal error with OAuth-compatible body obtaining an access token', 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' && 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) {
return callback({ statusCode: 400, data: '{"error":"invalid_grant","error_description":"The provided value for the input parameter \'code\' is not valid."} '});
}

describe('handling an authorized return request', function() {
var err;

before(function(done) {
chai.passport(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(AccessTokenError)
expect(err.message).to.equal('The provided value for the input parameter \'code\' is not valid.');
expect(err.code).to.equal('invalid_grant');
expect(err.oauthError).to.be.undefined;
});
});
});

describe('that encounters a node-oauth object literal error with non-OAuth-compatible body obtaining an access token', 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' && 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) {
return callback({ statusCode: 500, data: 'Something went wrong'});
}

describe('handling an authorized return request', function() {
var err;

before(function(done) {
chai.passport(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('Something went wrong');
});
});
});

describe('that encounters an error during verification', function() {

var strategy = new OAuth2Strategy({
Expand Down

0 comments on commit d180ed9

Please sign in to comment.