Skip to content

Commit

Permalink
Adding support for passport-pplsi.
Browse files Browse the repository at this point in the history
LGTM @will-sauer & @Hefley 
Closes #18 
PPLSICORE-12
  • Loading branch information
douglasrlee authored Jan 16, 2019
1 parent 496dbee commit 429921e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "passport-mocked",
"version": "1.2.0",
"version": "1.3.0",
"description": "Mock Passport Strategy",
"main": "./",
"scripts": {
Expand Down
23 changes: 18 additions & 5 deletions strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ module.exports = function (passport, util) {
var clone = function (o) { return JSON.parse(JSON.stringify(o)); }

function MockStrategy (options, verify) {
if (!verify) { throw new TypeError('MockStrategy requires a verify callback'); }
if (!(options.callbackURL || options.client.redirect_uris[0])) { throw new TypeError('MockStrategy requires a callbackURL'); }
if (!(options.callbackURL || options.redirect_uri || options.client.redirect_uris[0])) { throw new TypeError('MockStrategy requires a callbackURL'); }

if (!verify) {
this.verify = function(tokenset, next) {
next(null, { accessToken: tokenset.access_token, refreshToken: tokenset.refresh_token, idToken: tokenset.claims });
};
} else {
this.verify = verify;
}

this.name = options.name || 'mocked';
this.verify = verify;
this._callbackURL = (options.callbackURL || options.client.redirect_uris[0]);
this._callbackURL = (options.callbackURL || options.redirect_uri || options.client.redirect_uris[0]);
this._passReqToCallback = options.passReqToCallback || false;
}

Expand Down Expand Up @@ -54,5 +60,12 @@ module.exports = function (passport, util) {
}
}

return { Strategy: MockStrategy, OAuth2Strategy: MockStrategy };
return {
Strategy: MockStrategy,
OAuth2Strategy: MockStrategy,
OAuth2: {
PasswordStrategy: MockStrategy,
AuthorizationCodeStrategy: MockStrategy
}
};
};
53 changes: 49 additions & 4 deletions test/strategy.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
var Strategy = require('../').Strategy
, PassportMocked = require('../')
, expect = require('chai').expect
, passport = require('passport');

it('inherits from passport', function () {
expect(Strategy.super_).to.eql(passport.Strategy);
});

describe('exports', function () {
it('exports Strategy', function () {
expect(PassportMocked.Strategy).to.exist;
});

it('exports OAuth2Strategy', function () {
expect(PassportMocked.OAuth2Strategy).to.exist;
});

describe('exports OAuth2', function () {
it('exports PasswordStrategy', function () {
expect(PassportMocked.OAuth2.PasswordStrategy).to.exist;
});

it('exports AuthorizationCodeStrategy', function () {
expect(PassportMocked.OAuth2.AuthorizationCodeStrategy).to.exist;
});
});
});

describe('init', function () {
describe('name', function () {
it('has a default', function () {
Expand All @@ -20,10 +41,29 @@ describe('init', function () {
});

describe('verify', function () {
it('requires a verifiy function be passed in', function () {
expect(function () {
new Strategy({ callbackURL: '/cb' });
}).to.throw(Error);
it('has a default', function (done) {
var fakeTokenSet = {
access_token: 'some-access-token',
refresh_token: 'some-refresh-token',
claims: 'some-claims'
};

var strategy = new Strategy({ callbackURL: '/cb' });
strategy.verify(fakeTokenSet, function(user, info) {
expect(user).to.be.null;
expect(info).to.eql({
accessToken: 'some-access-token',
refreshToken: 'some-refresh-token',
idToken: 'some-claims'
});

done();
});
});

it('can be set', function () {
var strategy = new Strategy({ callbackURL: '/cb' }, 'something');
expect(strategy.verify).to.eql('something');
});
});

Expand All @@ -43,6 +83,11 @@ describe('init', function () {
var strategy = Object.create(new Strategy({ client: { redirect_uris: [ '/here' ] } }, function () {}));
expect(strategy._callbackURL).to.eql('/here');
});

it('can be set for PPLSI OpenID Connect', function () {
var strategy = Object.create(new Strategy({ redirect_uri: '/here' }, function () {}));
expect(strategy._callbackURL).to.eql('/here');
});
});

describe('passReqToCallback', function () {
Expand Down

0 comments on commit 429921e

Please sign in to comment.