-
-
Save visualjeff/8069fd0b973fd9eeb6c3f212d55c65d2 to your computer and use it in GitHub Desktop.
Mock passport.js with an own strategy
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
/** | |
* Author: Michael Weibel <michael.weibel@gmail.com> | |
* License: MIT | |
*/ | |
var passport = require('passport') | |
, StrategyMock = require('./strategy-mock'); | |
module.exports = function(app, options) { | |
// create your verify function on your own -- should do similar things as | |
// the "real" one. | |
passport.use(new StrategyMock(options, verifyFunction)); | |
app.get('/mock/login', passport.authenticate('mock')); | |
}; |
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
/** | |
* Author: Michael Weibel <michael.weibel@gmail.com> | |
* License: MIT | |
*/ | |
"use strict"; | |
var passport = require('passport') | |
, util = require('util'); | |
function StrategyMock(options, verify) { | |
this.name = 'mock'; | |
this.passAuthentication = options.passAuthentication || true; | |
this.userId = options.userId || 1; | |
this.verify = verify; | |
} | |
util.inherits(StrategyMock, passport.Strategy); | |
StrategyMock.prototype.authenticate = function authenticate(req) { | |
if (this.passAuthentication) { | |
var user = { | |
id: this.userId | |
} | |
, self = this; | |
this.verify(user, function(err, resident) { | |
if(err) { | |
self.fail(err); | |
} else { | |
self.success(resident); | |
} | |
}); | |
} else { | |
this.fail('Unauthorized'); | |
} | |
} | |
module.exports = StrategyMock; |
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
/** | |
* Author: Michael Weibel <michael.weibel@gmail.com> | |
* License: MIT | |
*/ | |
var request = require('supertest') | |
, superagent = require('superagent') | |
, path = require('path') | |
, app = require(path.join(process.cwd(), 'index.js'))() | |
, passportMock = require(path.join(process.cwd(), 'src', 'shared', 'test', 'passport-mock')); | |
describe('GET /protected-resource authorized', function() { | |
var agent = superagent.agent(); | |
beforeEach(function(done) { | |
passportMock(app, { | |
passAuthentication: true, | |
userId: 1 | |
}); | |
request(app) | |
.get('/mock/login') | |
.end(function(err, result) { | |
if (!err) { | |
agent.saveCookies(result.res); | |
done(); | |
} else { | |
done(err); | |
} | |
}); | |
}) | |
it('should allow access to /protected-resource', function(done) { | |
var req = request(app).get('/protected-resource'); | |
agent.attachCookies(req); | |
req.expect(200, done); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment