Skip to content

Commit

Permalink
Add test cases for profile parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Aug 15, 2013
1 parent 7a38453 commit f52fcf8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ exports.parse = function(json) {

profile.gender = json.gender;
profile.profileUrl = json.link;
profile.emails = [{ value: json.email }];

if (json.email) {
profile.emails = [{ value: json.email }];
}

if (json.picture) {
if (typeof json.picture == 'object' && json.picture.data) {
Expand Down
2 changes: 1 addition & 1 deletion lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Strategy.prototype.userProfile = function(accessToken, done) {
url += (fields !== '' ? '?fields=' + fields : '');
}

this._oauth2.getProtectedResource(url, accessToken, function (err, body, res) {
this._oauth2.get(url, accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError('Failed to fetch user profile', err)); }

try {
Expand Down
1 change: 1 addition & 0 deletions test/data/picture-2012-10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"500308595","picture":{"data":{"url":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-prn1\/example.jpg","is_silhouette":false}}}
1 change: 1 addition & 0 deletions test/data/picture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":"500308595","picture":"http:\/\/profile.ak.fbcdn.net\/hprofile-ak-prn1\/example.jpg"}
42 changes: 42 additions & 0 deletions test/profile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var fs = require('fs')
, parse = require('../lib/profile').parse;


describe('profile.parse', function() {

describe('picture field', function() {
var profile;

before(function(done) {
fs.readFile('test/data/picture.json', 'utf8', function(err, data) {
if (err) { return done(err); }
profile = parse(data);
done();
});
});

it('should parse profile', function() {
expect(profile.photos).to.have.length(1);
expect(profile.photos[0].value).to.equal('http://profile.ak.fbcdn.net/hprofile-ak-prn1/example.jpg');
expect(profile.emails).to.be.undefined;
});
});

describe('picture field, October 2012 breaking changes', function() {
var profile;

before(function(done) {
fs.readFile('test/data/picture-2012-10.json', 'utf8', function(err, data) {
if (err) { return done(err); }
profile = parse(data);
done();
});
});

it('should parse profile', function() {
expect(profile.photos).to.have.length(1);
expect(profile.photos[0].value).to.equal('http://profile.ak.fbcdn.net/hprofile-ak-prn1/example.jpg');
});
});

});
2 changes: 2 additions & 0 deletions test/strategy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ vows.describe('FacebookStrategy').addBatch({
},
},

// OK
'strategy when loading user profile with id and photos': {
topic: function() {
var strategy = new FacebookStrategy({
Expand Down Expand Up @@ -284,6 +285,7 @@ vows.describe('FacebookStrategy').addBatch({
},
},

// OK
'strategy when loading user profile with id and photos using October 2012 Breaking Changes': {
topic: function() {
var strategy = new FacebookStrategy({
Expand Down
4 changes: 2 additions & 2 deletions test/strategy.options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Strategy#userProfile', function() {
function() {});

// mock
strategy._oauth2.getProtectedResource = function(url, accessToken, callback) {
strategy._oauth2.get = function(url, accessToken, callback) {
if (url != 'https://graph.facebook.com/me?fields=id,username') { return callback(new Error('incorrect url argument')); }
if (accessToken != 'token') { return callback(new Error('incorrect token argument')); }

Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Strategy#userProfile', function() {
function() {});

// mock
strategy._oauth2.getProtectedResource = function(url, accessToken, callback) {
strategy._oauth2.get = function(url, accessToken, callback) {
if (url != 'https://graph.facebook.com/me?fields=id,username,name,last_name,first_name,middle_name,gender,link,email,picture') { return callback(new Error('incorrect url argument')); }
if (accessToken != 'token') { return callback(new Error('incorrect token argument')); }

Expand Down
2 changes: 1 addition & 1 deletion test/strategy.profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Strategy#userProfile', function() {
function() {});

// mock
strategy._oauth2.getProtectedResource = function(url, accessToken, callback) {
strategy._oauth2.get = function(url, accessToken, callback) {
if (url != 'https://graph.facebook.com/me') { return callback(new Error('incorrect url argument')); }
if (accessToken != 'token') { return callback(new Error('incorrect token argument')); }

Expand Down

0 comments on commit f52fcf8

Please sign in to comment.