Comparing version 0.0.4 to 0.0.5
93
index.js
@@ -29,7 +29,20 @@ /* jshint node: true */ | ||
var _proto = proto(strategy); | ||
backup(_proto); | ||
override(_proto) | ||
.method('userProfile', userProfile(profile)) | ||
.method('authenticate', authenticate(passauth, opts)); | ||
var ovr = override(_proto); | ||
if ('session' === strategy) { | ||
ovr | ||
.method('authenticate', (function(user) { | ||
return function(req, options) { | ||
// user should be the object that is sent to passport.deserializeUser method | ||
// ex: May be a user.id for a user lookup, or can be the entire user object itself | ||
req._passport.session.user = user; | ||
cobbler.__.authenticate.call(this, req, options); // go about as original | ||
}; | ||
})(profile)); | ||
} else { | ||
ovr | ||
.method('userProfile', userProfile(profile)) | ||
.method('authenticate', authenticate(passauth, opts)); | ||
} | ||
this.restore = restore.bind(this, _proto); | ||
@@ -39,28 +52,26 @@ return this; | ||
function backup(proto) { | ||
cobbler.__.userProfile = proto.userProfile; | ||
cobbler.__.authenticate = proto.authenticate; | ||
} | ||
/* | ||
* assign method | ||
* backup original methods | ||
* | ||
* @param {Prototype} proto | ||
* @param {String} name | ||
* @param {Function} fn | ||
* @return {this} | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function method(proto, name, fn) { | ||
proto[name] = fn; | ||
this.method = method.bind(this, proto); | ||
return this; | ||
function backup(proto) { | ||
var method = function(name) { | ||
cobbler.__[name] = proto[name]; | ||
return this; | ||
}; | ||
return { | ||
method: method | ||
}; | ||
} | ||
/* | ||
* override proto | ||
* override method | ||
* | ||
* @param {Prototype} | ||
* @return {this} | ||
* @return {Object} | ||
* @api private | ||
@@ -70,4 +81,16 @@ */ | ||
function override(proto) { | ||
this.method = method.bind(this, proto); | ||
return this; | ||
var method = function(name, fn) { | ||
if (!(name in proto)) { | ||
throw new Error('No method `'+name+'` to override'); | ||
} | ||
backup(proto).method(name); | ||
proto[name] = fn; | ||
return this; | ||
}; | ||
return { | ||
method: method | ||
}; | ||
} | ||
@@ -84,6 +107,11 @@ | ||
function proto(strategy) { | ||
// Session | ||
if ('session' === strategy) { | ||
return require('passport').strategies.SessionStrategy.prototype; | ||
} | ||
// Strategy | ||
if ('string' === typeof strategy) { // npm name | ||
strategy = require(strategy).Strategy; | ||
} | ||
if ('Strategy' in strategy) { // exports | ||
} else if ('Strategy' in strategy) { // exports | ||
strategy = strategy.Strategy; | ||
@@ -104,5 +132,10 @@ } | ||
function restore(proto) { | ||
proto.userProfile = cobbler.__.userProfile; | ||
proto.authenticate = cobbler.__.authenticate; | ||
cobbler.__ = {}; | ||
var methods = Object.keys(cobbler.__); | ||
var i = 0; | ||
var len = methods.length; | ||
for(; i<len; i++) { | ||
var name = methods[i]; | ||
proto[name] = cobbler.__[name]; | ||
delete cobbler.__[name]; | ||
} | ||
} | ||
@@ -140,3 +173,3 @@ | ||
getAuthorizeUrl: function(params) { | ||
return params.redirect_uri+queryp(passauth, opts); | ||
return params.redirect_uri+queryp(passauth); | ||
}, | ||
@@ -147,3 +180,2 @@ getOAuthAccessToken: function(code, params, callback) { | ||
}; | ||
var _orig = cobbler.__.authenticate; | ||
@@ -164,3 +196,3 @@ return function(req, options) { | ||
this._oauth2 = mockoauth2; // mock the oauth2 object | ||
_orig.call(this, req, options); | ||
cobbler.__.authenticate.call(this, req, options); // go about as original | ||
}; | ||
@@ -190,3 +222,2 @@ } | ||
* @param {Boolean} passauth | ||
* @param {Object} opts | ||
* @return {String} | ||
@@ -196,5 +227,5 @@ * @api private | ||
function queryp(passauth, opts) { | ||
function queryp(passauth) { | ||
return passauth ? '?code=12345' : '?error=access_denied'; | ||
} | ||
{ | ||
"name": "cobbler", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Passport mock for integration tests", | ||
@@ -30,3 +30,2 @@ "main": "index.js", | ||
"walking-dead": "0.0.7", | ||
"passport": "^0.2.0", | ||
"passport-github": "^0.1.5", | ||
@@ -38,3 +37,6 @@ "middle-earth": "0.0.6", | ||
"cookie-parser": "^1.1.0" | ||
}, | ||
"dependencies": { | ||
"passport": "^0.2.0" | ||
} | ||
} |
@@ -17,2 +17,4 @@ # Cobbler.js | ||
#### Stratgies | ||
var passport = cobbler('passport-github', {..profile..}); | ||
@@ -51,4 +53,29 @@ | ||
#### Login Session | ||
`cobbler` provides a way to "login as a user" without actually going through the actual login (above). | ||
var passport = cobbler('session', '12345'); | ||
Note the first agrument string *'session'* (**case-sensitive**). The 2nd argument should be the object that is passed to the `passport.deserializeUser` method. ex. | ||
passport.deserializeUser(function(id, done) { | ||
User.findOne({_id: id}) | ||
.exec(function(err, resource) { | ||
if (err) { | ||
return done(err, null); | ||
} | ||
done(null, resource); | ||
}); | ||
}); | ||
`id` would be a `user.id`. | ||
--- | ||
*Your `passport.deserializeUser` will actually be invoked* | ||
#### Restoring | ||
You can restore the old functions by calling `restore()`. | ||
@@ -62,6 +89,8 @@ | ||
--- | ||
## API | ||
`cobbler` can be passed either the npm name eg. '*passport-github*' or the exports eg. `var passportGithub = require('passport-github');` or the strategy eg. `var GithubStrategy = require('passport-github').Strategy;` | ||
`cobbler`, *for stratgies*, can be passed either the npm name eg. '*passport-github*' or the exports eg. `var passportGithub = require('passport-github');` or the strategy eg. `var GithubStrategy = require('passport-github').Strategy;` | ||
*For sessions*, the first argument must be "session". | ||
--- | ||
@@ -68,0 +97,0 @@ |
@@ -11,3 +11,2 @@ /* jshint node: true */ | ||
var url = 'http://localhost:7331'; | ||
var profile = { | ||
@@ -21,109 +20,93 @@ provider: 'github', | ||
var app; | ||
beforeEach(function(done) { | ||
beforeEach(function() { | ||
delete require.cache[require.resolve('./app')]; | ||
app = require('./app'); | ||
done(); | ||
}); | ||
it("success w/ the supplied profile", function(done) { | ||
var passport = cobbler('passport-github', profile); | ||
describe('strategies', function() { | ||
var passport, server; | ||
afterEach(function(done) { | ||
passport.restore(); | ||
server.close(done); | ||
}); | ||
var server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(function(browser, next) { | ||
browser.clickLink('[rel="login-with-github"]', next); | ||
}) | ||
.then(function(browser) { | ||
assert.equal(browser.text("title"), "Welcome!"); | ||
assert.equal(browser.text("h1"), "Hello John Doe!"); | ||
}) | ||
.end(function() { | ||
passport.restore(); | ||
server.close(); | ||
done(); | ||
}); | ||
it("success w/ the supplied profile", function(done) { | ||
passport = cobbler('passport-github', profile); | ||
server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(loginWithgithub) | ||
.then(assertSuccessfullogin) | ||
.end(done); | ||
}); | ||
}); | ||
}); | ||
it("defines a custom callbackURL", function(done) { | ||
var passport = cobbler('passport-github', profile, { | ||
callbackURL: 'http://localhost:7331/auth/github/callback-2' | ||
it("defines a custom callbackURL", function(done) { | ||
passport = cobbler('passport-github', profile, { | ||
callbackURL: 'http://localhost:7331/auth/github/callback-2' | ||
}); | ||
server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(loginWithgithub) | ||
.then(function(browser) { | ||
assert.equal(browser.text("title"), "Welcome Other!"); | ||
assert.equal(browser.text("h1"), "Uh... John Doe!"); | ||
}) | ||
.end(done); | ||
}); | ||
}); | ||
var server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(function(browser, next) { | ||
browser.clickLink('[rel="login-with-github"]', next); | ||
}) | ||
.then(function(browser) { | ||
assert.equal(browser.text("title"), "Welcome Other!"); | ||
assert.equal(browser.text("h1"), "Uh... John Doe!"); | ||
}) | ||
.end(function() { | ||
passport.restore(); | ||
server.close(); | ||
done(); | ||
}); | ||
it("access denied", function(done) { | ||
passport = cobbler("passport-github", false); | ||
server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(loginWithgithub) | ||
.then(function(browser) { | ||
assert.equal(browser.text("title"), "Bad!"); | ||
}) | ||
.end(done); | ||
}); | ||
}); | ||
}); | ||
it("access denied", function(done) { | ||
var passport = cobbler("passport-github", false); | ||
it("can be passed the Strategy", function(done) { | ||
var strategy = require('passport-github').Strategy; | ||
passport = cobbler(strategy, profile); | ||
server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(loginWithgithub) | ||
.then(assertSuccessfullogin) | ||
.end(done); | ||
}); | ||
}); | ||
var server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(function(browser, next) { | ||
browser.clickLink('[rel="login-with-github"]', next); | ||
}) | ||
.then(function(browser) { | ||
assert.equal(browser.text("title"), "Bad!"); | ||
}) | ||
.end(function() { | ||
passport.restore(); | ||
server.close(); | ||
done(); | ||
}); | ||
it("can be passed the prototype object of the strategy", function(done) { | ||
var proto = require('passport-github').Strategy.prototype; | ||
passport = cobbler(proto, profile); | ||
server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(loginWithgithub) | ||
.then(assertSuccessfullogin) | ||
.end(done); | ||
}); | ||
}); | ||
}); | ||
it("can be passed the Strategy", function(done) { | ||
var GithubStrategy = require('passport-github').Strategy; | ||
var passport = cobbler(GithubStrategy, profile); | ||
var server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(function(browser, next) { | ||
browser.clickLink('[rel="login-with-github"]', next); | ||
}) | ||
.then(function(browser) { | ||
assert.equal(browser.text("title"), "Welcome!"); | ||
assert.equal(browser.text("h1"), "Hello John Doe!"); | ||
}) | ||
.end(function() { | ||
passport.restore(); | ||
server.close(); | ||
done(); | ||
}); | ||
describe('sessions', function() { | ||
var session, server; | ||
afterEach(function(done) { | ||
session.restore(); | ||
server.close(done); | ||
}); | ||
}); | ||
it("can be passed the prototype object of the strategy", function(done) { | ||
var passportGithub = require('passport-github'); | ||
var passport = cobbler(passportGithub.Strategy.prototype, profile); | ||
it("logs you in as", function(done) { | ||
var deserializeUser = { | ||
id: profile.id, | ||
name: profile.displayName | ||
}; | ||
var server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.when(function(browser, next) { | ||
browser.clickLink('[rel="login-with-github"]', next); | ||
}) | ||
.then(function(browser) { | ||
assert.equal(browser.text("title"), "Welcome!"); | ||
assert.equal(browser.text("h1"), "Hello John Doe!"); | ||
}) | ||
.end(function() { | ||
passport.restore(); | ||
server.close(); | ||
done(); | ||
}); | ||
session = cobbler('session', deserializeUser); | ||
server = app.listen(7331, function() { | ||
new WalkingDead(url).zombify(zopts) | ||
.then(assertSuccessfullogin) | ||
.end(done); | ||
}); | ||
}); | ||
@@ -142,1 +125,19 @@ }); | ||
}); | ||
/* | ||
* click github login | ||
*/ | ||
function loginWithgithub(browser, next) { | ||
browser.clickLink('[rel="login-with-github"]', next); | ||
} | ||
/* | ||
* assert successful login | ||
*/ | ||
function assertSuccessfullogin(browser) { | ||
assert.equal(browser.text("title"), "Welcome!"); | ||
assert.equal(browser.text("h1"), "Hello John Doe!"); | ||
} | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17464
10
453
115
1
+ Addedpassport@^0.2.0
+ Addedpassport@0.2.2(transitive)
+ Addedpassport-strategy@1.0.0(transitive)
+ Addedpause@0.0.1(transitive)