Created
April 19, 2013 05:47
-
-
Save jkresner/5418373 to your computer and use it in GitHub Desktop.
Mocking user / session with passportJS authentication. This gist helps you test code that requires a user that has been authenticated by passport JS
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
module.exports = | |
initialize: (sessionUserObject) -> | |
(req, res, next) -> | |
passport = @ | |
passport._key = 'passport' | |
passport._userProperty = 'user' | |
passport.serializeUser = (user, done) -> done null, user | |
passport.deserializeUser = (user, done) -> done null, user | |
req._passport = instance: passport | |
req._passport.session = user: sessionUserObject | |
next() |
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
express = require 'express' | |
passport = require 'passport' | |
passportMock = require './mock-passport-middleware' | |
mockUser = name: { givenName: "Jonathon", familyName: "Kresner" } | |
global.app = express() | |
app.configure -> | |
app.use express.static(__dirname + '/public') | |
app.use express.bodyParser() | |
app.use express.cookieParser() | |
app.use express.session { secret: 'your secret' } | |
# use our passportMock middleware instead of original passport.initialize() | |
# app.use passport.initialize() | |
app.use passportMock.initialize( mockUser ) # pass in the object wanted as session/user | |
app.use passport.session() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment