Mock passport JWT strategy for testing a Node.js application.
This module allows you authenticate a fake user in a Node application using your existing passport-jwt
implementation to allow for easy testing of routes.
Install using yarn
:
$ yarn add passport-jwt-mock --dev
Or npm
:
$ npm install passport-jwt-mock --save-dev
To get started you can import MockStrategy
and create a new instance. I believe the easiest approach is to use an environment variable to hot-swap the regular jwtStrategy with the mock strategy.
const passport = require("passport");
let jwtStrategy = null;
if (process.env.NODE_ENV === "test") {
jwtStrategy = require("passport-jwt-mock").Strategy;
} else {
jwtStrategy = require("passport-jwt").Strategy;
}
Once you have replaced the the jwtStrategy
object, you are nearly done! The implementation (however you used it) will work without modifying the implementation.
passport.use(
new jwtStrategy(
{
//secret we used to sign our JWT
secretOrKey: 'secret',
//we expect the user to send the token as a query paramater with the name 'secret_token'
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken()
},
async (token, done) => {
try {
//Pass the user details to the next middleware
return done(null, token.user);
} catch (error) {
done(error);
}
}
)
);
This package is for testing purposes only! Please do not use this for actual authentication as it provides no security whatsoever!
passport-mock-strategy is available under the MIT License.
Contributions are welcome. Feel free to open an issue or submit a pull request.