Skip to content

Commit

Permalink
Setup Mocha for tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Aug 15, 2013
1 parent 564d4b8 commit 614b540
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
SOURCES = lib/**/*.js
TESTS ?= test/*-test.js
SOURCES = lib/*.js
TESTS ?= test/*.test.js

lint: lint-jshint
test: test-vows
test-cov: test-istanbul-vows
test: test-mocha
test-cov: test-istanbul-mocha
view-cov: view-istanbul-report


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) {
}

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

try {
var json = JSON.parse(body);
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
"passport-oauth2": "1.0.0"
},
"devDependencies": {
"vows": "0.6.x"
"vows": "0.6.x",
"mocha": "1.x.x",
"chai": "1.x.x"
},
"engines": { "node": ">= 0.4.0" },
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions test/bootstrap/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var chai = require('chai');

global.expect = chai.expect;
14 changes: 14 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var strategy = require('..');

describe('passport-facebook', function() {

it('should export Strategy constructor directly from package', function() {
expect(strategy).to.be.a('function');
expect(strategy).to.equal(strategy.Strategy);
});

it('should export Strategy constructor', function() {
expect(strategy.Strategy).to.be.a('function');
});

});
5 changes: 5 additions & 0 deletions test/strategy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var FacebookStrategy = require('../lib/strategy');

vows.describe('FacebookStrategy').addBatch({

// OK
'strategy': {
topic: function() {
return new FacebookStrategy({
Expand Down Expand Up @@ -70,6 +71,7 @@ vows.describe('FacebookStrategy').addBatch({
}
},

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

// OK
'strategy when loading user profile with profileURL option': {
topic: function() {
var strategy = new FacebookStrategy({
Expand Down Expand Up @@ -179,6 +182,7 @@ vows.describe('FacebookStrategy').addBatch({
},
},

// OK
'strategy when loading user profile with mapped profile fields': {
topic: function() {
var strategy = new FacebookStrategy({
Expand Down Expand Up @@ -330,6 +334,7 @@ vows.describe('FacebookStrategy').addBatch({
},
},

// OK
'strategy when loading user profile and encountering an error': {
topic: function() {
var strategy = new FacebookStrategy({
Expand Down
78 changes: 78 additions & 0 deletions test/strategy.options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var FacebookStrategy = require('../lib/strategy');


describe('Strategy#userProfile', function() {

describe('with profile URL option', function() {
var strategy = new FacebookStrategy({
clientID: 'ABC123',
clientSecret: 'secret',
profileURL: 'https://graph.facebook.com/me?fields=id,username'
},
function() {});

// mock
strategy._oauth2.getProtectedResource = 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')); }

var body = '{"id":"500308595","name":"Jared Hanson","first_name":"Jared","last_name":"Hanson","link":"http:\\/\\/www.facebook.com\\/jaredhanson","username":"jaredhanson","gender":"male","email":"jaredhanson\\u0040example.com"}';
callback(null, body, undefined);
}

describe('loading profile', function() {
var profile;

before(function(done) {
strategy.userProfile('token', function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});

it('should parse profile', function() {
expect(profile.provider).to.equal('facebook');
expect(profile.id).to.equal('500308595');
expect(profile.username).to.equal('jaredhanson');
});
});
});

describe('with profile fields mapped from portable contacts schema', function() {
var strategy = new FacebookStrategy({
clientID: 'ABC123',
clientSecret: 'secret',
profileFields: ['id', 'username', 'displayName', 'name', 'gender', 'profileUrl', 'emails', 'photos']
},
function() {});

// mock
strategy._oauth2.getProtectedResource = 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')); }

var body = '{"id":"500308595","name":"Jared Hanson","first_name":"Jared","last_name":"Hanson","link":"http:\\/\\/www.facebook.com\\/jaredhanson","username":"jaredhanson","gender":"male","email":"jaredhanson\\u0040example.com"}';
callback(null, body, undefined);
}

describe('loading profile', function() {
var profile;

before(function(done) {
strategy.userProfile('token', function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});

it('should parse profile', function() {
expect(profile.provider).to.equal('facebook');
expect(profile.id).to.equal('500308595');
expect(profile.username).to.equal('jaredhanson');
});
});
});

});
78 changes: 78 additions & 0 deletions test/strategy.profile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var FacebookStrategy = require('../lib/strategy');


describe('Strategy#userProfile', function() {

var strategy = new FacebookStrategy({
clientID: 'ABC123',
clientSecret: 'secret'
},
function() {});

// mock
strategy._oauth2.getProtectedResource = 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')); }

var body = '{"id":"500308595","name":"Jared Hanson","first_name":"Jared","last_name":"Hanson","link":"http:\\/\\/www.facebook.com\\/jaredhanson","username":"jaredhanson","gender":"male","email":"jaredhanson\\u0040example.com"}';
callback(null, body, undefined);
}

describe('loading profile', function() {
var profile;

before(function(done) {
strategy.userProfile('token', function(err, p) {
if (err) { return done(err); }
profile = p;
done();
});
});

it('should parse profile', function() {
expect(profile.provider).to.equal('facebook');

expect(profile.id).to.equal('500308595');
expect(profile.username).to.equal('jaredhanson');
expect(profile.displayName).to.equal('Jared Hanson');
expect(profile.name.familyName).to.equal('Hanson');
expect(profile.name.givenName).to.equal('Jared');
expect(profile.gender).to.equal('male');
expect(profile.profileUrl).to.equal('http://www.facebook.com/jaredhanson');
expect(profile.emails).to.have.length(1);
expect(profile.emails[0].value).to.equal('[email protected]');
expect(profile.photos).to.be.undefined;
});

it('should set raw property', function() {
expect(profile._raw).to.be.a('string');
});

it('should set json property', function() {
expect(profile._json).to.be.an('object');
});
});

describe('encountering an error', function() {
var err, profile;

before(function(done) {
strategy.userProfile('wrong-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');
});

it('should not load profile', function() {
expect(profile).to.be.undefined;
});
});

});
16 changes: 16 additions & 0 deletions test/strategy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var FacebookStrategy = require('../lib/strategy');


describe('Strategy', function() {

var strategy = new FacebookStrategy({
clientID: 'ABC123',
clientSecret: 'secret'
},
function() {});

it('should be named facebook', function() {
expect(strategy.name).to.equal('facebook');
});

});

0 comments on commit 614b540

Please sign in to comment.