Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jaredhanson/chai-passport-strategy

Helpers for testing Passport strategies with the Chai assertion library.
https://github.com/jaredhanson/chai-passport-strategy

chai passport

Last synced: 1 day ago
JSON representation

Helpers for testing Passport strategies with the Chai assertion library.

Awesome Lists containing this project

README

        

# chai-passport-strategy

Helpers for testing [Passport](https://www.passportjs.org/) strategies with the
[Chai](https://www.chaijs.com/) assertion library.

## Install

$ npm install chai-passport-strategy

## Usage

#### Use Plugin

Use this plugin as you would all other Chai plugins:

```javascript
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](https://mochajs.org/) test case that tests a strategy
which implements bearer token authentication:

```javascript
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](http://opensource.org/licenses/MIT)

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