npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hackmd/passport-mock-strategy

v2.0.0

Published

Mock passport strategy for testing Node.js applications.

Downloads

16

Readme

passport-mock-strategy

Mock passport strategy for testing a Node.js application.

This module allows you to create and authenticate a fake user in a Node application to allow for easy testing of routes. Authenticated routes can then be tested for their functionality with the assumption that the user has been authenticated.

Install

Install using yarn:

$ yarn add passport-mock-strategy --dev

Or use npm if you wish:

$ npm install passport-mock-strategy --save-dev

Usage

To get started you can import MockStrategy and create a new instance.

const MockStrategy = require('passport-mock-strategy');
...
passport.use(new MockStrategy());

You can configure and customize mock strategy by passing options and/or a callback:

passport.use(new MockStrategy({
	name: 'my-mock',
	user: customUserObject
}, (user, done) => {
	// Perform actions on user, call done once finished
}));

You can then use passport.authenticate() specifying the strategy name to authenticate requests. By default the strategy name is mock.

app.get('/auth/mock', passport.authenticate('mock'));

Quick Setup

passport-mock-strategy exports various convenience functions that can be used to quickly and easily setup a mock passport instance.

You can use createMockPassport() to setup an instance of passport that uses MockStrategy and connect it to your Node app.

createMockPassport(app);

Serializate and Deserializate User

setupSerializeAndDeserialize provides an easily way to setup the serializeUser and deserializeUser methods for passport. You can pass null to use the default.

setupSerializeAndDeserialize(passport, null, (id, done) => {
	// custom deserializeUser function
});

Here a custom deserializeUser function is passed, whereas null is passed for serializeUser which means the default will be used.

Connect Passport to app

The connectPassport function initializes a given passport instance and connects it to the given app.

connectPassport(app, passport);
// Calls passport.initialize() and passport.session() internally

Mock User and Storage

mockUser is exported which is the default user that MockStrategy uses internally. createMockStorage() is also exported which creates a mock storage that saves and fetches users asynchronously to mimic fetching from a database.

How it works

MockStrategy always authenticates a mock user when called. This user is either the default exported from mock-user.js or a custom user object that can be passed as an option when initializing a new instance of MockStrategy. Calling passport.authenticate('mock') will then authenticate this mock user. Additionally it will store the user in the app's session which can then be retreived through the use of either express-session or cookie-session.

This method makes it easy to test the functionality of authenticated routes with the assumption that the user has already been authenticated.

Type Checking

This package is written in typescript. All type declarations are published with the package and can be used as needed.

Type definitions are also provided for flow. They can be imported for use.

Example:

// @flow

import type { MockStrategyOptions } from 'passport-mock-strategy';

const options: MockStrategyOptions = { name: 'my-mock' };

Important Note

This package is for testing purposes only! Please do not use this for actual authentication as it provides no security whatsoever!

License

passport-mock-strategy is available under the MIT License.

Contributing

Contributions are welcome. Feel free to open an issue or submit a pull request.