Giter Site home page Giter Site logo

cobbler.js's Introduction

Cobbler.js

Build Status Code Climate David DM

A spy who creates false passports, visas, diplomas and other documents

Mocking Passport OAuth for integration tests

Install

npm install cobbler --save-dev

Usage

Stratgies

var passport = cobbler('passport-github', {..profile..});

var server = app.listen(7331, function() {
  new WalkingDead(url).zombify(zopts)
    .when(function(browser, next) {
      browser.clickLink('[rel="login-with-github"]', next);
    })
    .then(function(browser) {
      assert.equal(browser.text("title"), "Welcome!");
    })
    .end(function() {
      passport.restore();
      server.close();
      done();
   });
});

cobbler mocks the calls to the OAuth service and redirects you to the callback url allowing you to bypass the network connections, but allows you to go through the full (most of it at least) process without mocking key functions in managing sessions, authenticated or initialization of passport.


If you want to simulate a successful authentication, the "profile" argument should be an object similar to what you would receive from an OAuth userProfile call. This will hit your Strategy callback and go through the normal chain of events.

var passport = cobbler('passport-github', {
  provider: 'github',
  id: 12345,
  displayName: 'John Doe'
});

To simulate a failed authentication, the "profile" argument should be false.

var passport = cobbler('passport-github', false);

Login Session

cobbler provides a way to "login as a user" without actually going through the actual login (above).

var passport = cobbler('session', '12345');

Note the first agrument string 'session' (case-sensitive). The 2nd argument should be the object that is passed to the passport.deserializeUser method. ex.

passport.deserializeUser(function(id, done) {
  User.findOne({_id: id})
    .exec(function(err, resource) {
      if (err) {
        return done(err, null);
      }
      done(null, resource);
    });
});

id would be a user.id.


Your passport.deserializeUser will actually be invoked

Restoring

You can restore the old functions by calling restore().

var passport = cobbler('passport-github', {..profile..});

// do the test

passport.restore();

API

cobbler, for stratgies, can be passed either the npm name eg. 'passport-github' or the exports eg. var passportGithub = require('passport-github'); or the strategy eg. var GithubStrategy = require('passport-github').Strategy;

For sessions, the first argument must be "session".


It should be noted that you may need to clear the require.cache of your app between tests.

Notes

TODO

  • More test coverage for other Passport Strategies, especially non-OAuth strategies.
  • Ability to set arguments such as accessToken, refreshToken, params
  • Ability to test fail and error calls.
  • Ability to set callback url code and error query params

Contributors. Many Thanks!

License

MIT

cobbler.js's People

Contributors

nowk avatar jeremybuis avatar

Stargazers

Showcasefloyd avatar  avatar Pablo Novas avatar Christian avatar Ben Murrell avatar Stefan Walther avatar

Watchers

 avatar Pablo Novas avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.