forked from jaredhanson/passport-facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a80d40b
commit a4f3ec3
Showing
3 changed files
with
154 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* `FacebookGraphAPIError` error. | ||
* | ||
* References: | ||
* - https://developers.facebook.com/docs/reference/api/errors/ | ||
* | ||
* @constructor | ||
* @param {String} [message] | ||
* @param {String} [type] | ||
* @param {Number} [code] | ||
* @param {Number} [subcode] | ||
* @api public | ||
*/ | ||
function FacebookGraphAPIError(message, type, code, subcode) { | ||
Error.call(this); | ||
Error.captureStackTrace(this, arguments.callee); | ||
this.name = 'FacebookGraphAPIError'; | ||
this.message = message; | ||
this.type = type; | ||
this.code = code; | ||
this.subcode = subcode; | ||
this.status = 500; | ||
} | ||
|
||
/** | ||
* Inherit from `Error`. | ||
*/ | ||
FacebookGraphAPIError.prototype.__proto__ = Error.prototype; | ||
|
||
|
||
/** | ||
* Expose `FacebookGraphAPIError`. | ||
*/ | ||
module.exports = FacebookGraphAPIError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
var FacebookStrategy = require('../lib/strategy'); | ||
|
||
|
||
describe('Strategy#userProfile', function() { | ||
|
||
describe('handling API errors', function() { | ||
var strategy = new FacebookStrategy({ | ||
clientID: 'ABC123', | ||
clientSecret: 'secret' | ||
}, | ||
function() {}); | ||
|
||
// mock | ||
strategy._oauth2.get = function(url, accessToken, callback) { | ||
if (url != 'https://graph.facebook.com/me') { return callback(new Error('wrong url argument')); } | ||
if (accessToken != 'token') { return callback(new Error('wrong token argument')); } | ||
|
||
var body = '{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}'; | ||
|
||
callback({ statusCode: 401, data: body }); | ||
} | ||
|
||
var err, profile; | ||
before(function(done) { | ||
strategy.userProfile('token', function(e, p) { | ||
err = e; | ||
profile = p; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should error', function() { | ||
expect(err).to.be.an.instanceOf(Error); | ||
expect(err.constructor.name).to.equal('FacebookGraphAPIError'); | ||
expect(err.message).to.equal('Invalid OAuth access token.'); | ||
expect(err.type).to.equal('OAuthException'); | ||
expect(err.code).to.equal(190); | ||
}); | ||
}); | ||
|
||
describe('handling internal OAuth errors', function() { | ||
var strategy = new FacebookStrategy({ | ||
clientID: 'ABC123', | ||
clientSecret: 'secret' | ||
}, | ||
function() {}); | ||
|
||
// mock | ||
strategy._oauth2.get = function(url, accessToken, callback) { | ||
if (url != 'https://graph.facebook.com/me') { return callback(new Error('wrong url argument')); } | ||
if (accessToken != 'token') { return callback(new Error('wrong token argument')); } | ||
|
||
var body = '{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}'; | ||
|
||
callback({ statusCode: 401, x__data: body }); | ||
} | ||
|
||
var err, profile; | ||
before(function(done) { | ||
strategy.userProfile('token', function(e, p) { | ||
err = e; | ||
profile = p; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should error', function() { | ||
expect(err).to.be.an.instanceOf(Error); | ||
expect(err.constructor.name).to.equal('InternalOAuthError'); | ||
expect(err.message).to.equal('Failed to fetch user profile'); | ||
}); | ||
}); | ||
|
||
describe('handling malformed responses', function() { | ||
var strategy = new FacebookStrategy({ | ||
clientID: 'ABC123', | ||
clientSecret: 'secret' | ||
}, | ||
function() {}); | ||
|
||
// mock | ||
strategy._oauth2.get = function(url, accessToken, callback) { | ||
if (url != 'https://graph.facebook.com/me') { return callback(new Error('wrong url argument')); } | ||
if (accessToken != 'token') { return callback(new Error('wrong token argument')); } | ||
|
||
var body = 'Hello, world.'; | ||
callback(null, body, undefined); | ||
} | ||
|
||
var err, profile; | ||
before(function(done) { | ||
strategy.userProfile('token', function(e, p) { | ||
err = e; | ||
profile = p; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should error', function() { | ||
expect(err).to.be.an.instanceOf(Error); | ||
expect(err.message).to.equal('Failed to parse user profile'); | ||
}); | ||
}); | ||
|
||
}); |