Last active
May 14, 2024 09:32
-
-
Save mweibel/5219403 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 <[email protected]> | |
* 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 <[email protected]> | |
* 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 <[email protected]> | |
* 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); | |
}); | |
}); |
Thank you for this example, it helped a lot. Otherwise, just wanted to point out that it's always going to pass authentication, regardless of StrategyMock
constructor options:
this.passAuthentication = options.passAuthentication || true;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll give some input as well so that others don't waste hours figuring out why after authentication, it gives a 404 on the login url. It is because you must provide a successRedirect so that passport know where to redirect the client once the authentication has pass.
So in passport-mock.js line 14, it should look like that instead:
And thanks to the author for that code, it helps for unit test my app! :)