|
| 1 | +var FacebookStrategy = require('../lib/strategy'); |
| 2 | + |
| 3 | + |
| 4 | +describe('Strategy#userProfile', function() { |
| 5 | + |
| 6 | + describe('handling API errors', function() { |
| 7 | + var strategy = new FacebookStrategy({ |
| 8 | + clientID: 'ABC123', |
| 9 | + clientSecret: 'secret' |
| 10 | + }, |
| 11 | + function() {}); |
| 12 | + |
| 13 | + // mock |
| 14 | + strategy._oauth2.get = function(url, accessToken, callback) { |
| 15 | + if (url != 'https://graph.facebook.com/me') { return callback(new Error('wrong url argument')); } |
| 16 | + if (accessToken != 'token') { return callback(new Error('wrong token argument')); } |
| 17 | + |
| 18 | + var body = '{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}'; |
| 19 | + |
| 20 | + callback({ statusCode: 401, data: body }); |
| 21 | + } |
| 22 | + |
| 23 | + var err, profile; |
| 24 | + before(function(done) { |
| 25 | + strategy.userProfile('token', function(e, p) { |
| 26 | + err = e; |
| 27 | + profile = p; |
| 28 | + done(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should error', function() { |
| 33 | + expect(err).to.be.an.instanceOf(Error); |
| 34 | + expect(err.constructor.name).to.equal('FacebookGraphAPIError'); |
| 35 | + expect(err.message).to.equal('Invalid OAuth access token.'); |
| 36 | + expect(err.type).to.equal('OAuthException'); |
| 37 | + expect(err.code).to.equal(190); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('handling internal OAuth errors', function() { |
| 42 | + var strategy = new FacebookStrategy({ |
| 43 | + clientID: 'ABC123', |
| 44 | + clientSecret: 'secret' |
| 45 | + }, |
| 46 | + function() {}); |
| 47 | + |
| 48 | + // mock |
| 49 | + strategy._oauth2.get = function(url, accessToken, callback) { |
| 50 | + if (url != 'https://graph.facebook.com/me') { return callback(new Error('wrong url argument')); } |
| 51 | + if (accessToken != 'token') { return callback(new Error('wrong token argument')); } |
| 52 | + |
| 53 | + var body = '{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190}}'; |
| 54 | + |
| 55 | + callback({ statusCode: 401, x__data: body }); |
| 56 | + } |
| 57 | + |
| 58 | + var err, profile; |
| 59 | + before(function(done) { |
| 60 | + strategy.userProfile('token', function(e, p) { |
| 61 | + err = e; |
| 62 | + profile = p; |
| 63 | + done(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should error', function() { |
| 68 | + expect(err).to.be.an.instanceOf(Error); |
| 69 | + expect(err.constructor.name).to.equal('InternalOAuthError'); |
| 70 | + expect(err.message).to.equal('Failed to fetch user profile'); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('handling malformed responses', function() { |
| 75 | + var strategy = new FacebookStrategy({ |
| 76 | + clientID: 'ABC123', |
| 77 | + clientSecret: 'secret' |
| 78 | + }, |
| 79 | + function() {}); |
| 80 | + |
| 81 | + // mock |
| 82 | + strategy._oauth2.get = function(url, accessToken, callback) { |
| 83 | + if (url != 'https://graph.facebook.com/me') { return callback(new Error('wrong url argument')); } |
| 84 | + if (accessToken != 'token') { return callback(new Error('wrong token argument')); } |
| 85 | + |
| 86 | + var body = 'Hello, world.'; |
| 87 | + callback(null, body, undefined); |
| 88 | + } |
| 89 | + |
| 90 | + var err, profile; |
| 91 | + before(function(done) { |
| 92 | + strategy.userProfile('token', function(e, p) { |
| 93 | + err = e; |
| 94 | + profile = p; |
| 95 | + done(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should error', function() { |
| 100 | + expect(err).to.be.an.instanceOf(Error); |
| 101 | + expect(err.message).to.equal('Failed to parse user profile'); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | +}); |
0 commit comments