chai-passport-strategy

2018-11-09 admin

chai-passport-strategy是什么

什么是chai-passport-strategy,Helpers for testing Passport strategies with the Chai assertion library.

chai-passport-strategy使用教程帮助文档

chai-passport-strategy

Version Build Quality Coverage Dependencies

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 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 © 2013-2017 Jared Hanson <http://jaredhanson.net/>

Sponsor

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-44951.html

文章标题:chai-passport-strategy

回到顶部