1.3.2 • Published 5 years ago

@zhaow-de/chai-passport-strategy v1.3.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

npm npm (scoped) Build Status Coverage Status Codacy Badge Depfu

@zhaow-de/chai-passport-strategy

Helpers for testing Passport strategies with the Chai assertion library.

Install

npm install @zhaow-de/chai-passport-strategy

Usage

Use Plugin

Use this plugin as you would all other Chai plugins:

var chai = require('chai');

chai.use(require('@zhaow-de/chai-passport-strategy'));

Implement Test Cases

Once used, the chai.passport.use helper function will be available to set up test cases for Passport strategies.

The helper function can be called from a hook to setup the test case. 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.

The following demonstrates a Mocha test case, taken from passport-http-bearer's test suite.

describe('token strategy', function() {
    
  var strategy = new Strategy(function(token, done) {
    if (token == 'vF9dft4qmT') { 
      return done(null, { id: '1234' }, { scope: 'read' });
    }
    return done(null, false);
  });
  
  describe('handling a request with valid credential in header', function() {
    var user
      , info;
    
    before(function(done) {
      chai.passport.use(strategy)
        .success(function(u, i) {
          user = u;
          info = i;
          done();
        })
        .req(function(req) {
          req.headers.authorization = 'Bearer vF9dft4qmT';
        })
        .authenticate();
    });
    
    it('should supply user', function() {
      expect(user).to.be.an.object;
      expect(user.id).to.equal('1234');
    });
    
    it('should supply info', function() {
      expect(info).to.be.an.object;
      expect(info.scope).to.equal('read');
    });
  });
});

License

The MIT License

Copyright (c) 2013-2017 Jared Hanson [http://jaredhanson.net/](http://jaredhanson.net/)

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.0

5 years ago