Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

chai-passport-strategy

jaredhanson42.2kMIT3.0.0

Helpers for testing Passport strategies with the Chai assertion library.

chai, passport, strategy

readme

chai-passport-strategy

Helpers for testing Passport strategies with the Chai assertion library.

Install

$ npm install chai-passport-strategy

Usage

Use Plugin

Use this plugin as you would all other Chai plugins:

var chai = require('chai');

chai.use(require('chai-passport-strategy'));

Implement Test Cases

Once used, the chai.passport.use helper function will be available to set up a test case which places a Passport strategy under test.

The helper returns a wrapper on which callbacks are registered to be executed when the strategy invokes its final action function. The callbacks correspond to Passport's strategy API: success(), fail(), redirect(), pass(), and error(). If the strategy invokes an action that doesn't have a registered callback, the test helper will automatically throw an exception.

For example, a Mocha test case that tests a strategy which implements bearer token authentication:

it('should authenticate request with token in header', function(done) {
  chai.passport.use(new Strategy(function(token, cb) {
      expect(token).to.equal('mF_9.B5f-4.1JqM');
      return cb(null, { id: '248289761001' }, { scope: [ 'profile', 'email' ] });
    }))
    .request(function(req) {
      req.headers['authorization'] = 'Bearer mF_9.B5f-4.1JqM';
    })
    .success(function(user, info) {
      expect(user).to.deep.equal({ id: '248289761001' });
      expect(info).to.deep.equal({ scope: [ 'profile', 'email' ] });
      done();
    })
    .authenticate();
});

License

The MIT License

Copyright (c) 2013-2021 Jared Hanson <https://www.jaredhanson.me/>

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

3.0.0 - 2021-10-24

Changed

  • Callbacks invoked when the strategy under test calls an action function (success(), fail(), redirect(), pass(), or error()) have a this context set to the request, rather than the Test instance.

2.0.0 - 2021-10-23

Added

  • Test#finish function, used to test strategies that end the response.

Changed

  • Test#req renamed to Test#request and now takes synchronous callbacks with function(req, res) signature and asynchronous callbacks with function(req, res, cb) signature to mimic http.Server request event.
  • Callbacks invoked when the strategy under test calls an action function (success(), fail(), redirect(), pass(), or error()) have a this context set to the Test instance, rather than the Strategy instance.

1.0.1 - 2017-09-29

Added

  • Added contributing guidelines.

1.0.0 - 2016-01-29

Changed

  • Changed @api tags to @access in order to conform to JSDoc syntax and avoid dox-specific extensions.
  • Use `make-node@0.3.x`-supplied makefiles for build automation.

Removed

  • Removed makefiles in support/mk directory.

0.2.0 - 2013-11-26

Changed

  • Helper function to set up test case is now at chai.passport.use rather than chai.passport to more closely mimic the passport API.

0.1.0 - 2013-08-01

  • Initial release.