UNPKG

passport-cookie

Version:

Cookie authentication strategy for Passport

297 lines (262 loc) 8.49 kB
var Strategy = require("../strategy"); var chai = require("chai"); var expect = chai.expect; var passportStrategy = require("chai-passport-strategy"); chai.use(passportStrategy); describe("Strategy", function() { it("should be named cookie", function() { var strategy = new Strategy(function(){}); expect(strategy.name).to.equal("cookie"); }); it("should throw if constructed without a verify callback", function() { expect(function() { new Strategy(); }).to.throw(TypeError, "CookieStrategy requires a verify callback"); }); it("should create a strategy with empty `cookieName` and set it to default value", function() { var strategy = new Strategy({ cookieName: "" }, function() {}); expect(strategy._cookieName).to.equal("token"); }); it("should create a strategy with non-empty `cookieName`", function() { var strategy = new Strategy({ cookieName: "cookie" }, function() {}); expect(strategy._cookieName).to.equal("cookie"); }); it("should throw an error if req.cookies is undefined", function() { var strategy = new Strategy(function() {}); expect(function() { strategy.authenticate({}); }).to.throw(TypeError, "Maybe you forgot to use cookie-parser?"); }); it("should create a strategy with signed cookies", function () { var strategy = new Strategy({ signed: true }, function () {}); expect(strategy._signed).to.equal(true); }); it("should call the verify callback with the token value", function(done) { var strategy = new Strategy(function(token) { expect(token).to.equal("abc"); return done(); }); strategy.authenticate({ cookies: { token: "abc" }}); }); it("should call the verify callback and call fail because the token is empty", function(done) { var strategy = new Strategy(function(token, next) { expect(token).to.equal(""); return next(); }); chai.passport.use(strategy) .fail(function(err) { expect(err).to.equal(401); return done(); }) .success(function() { return done(new Error("It should not call this")); }) .req(function(req) { req.cookies = { token: "" }; }).authenticate(); }); it("should call the verify callback and call fail because the user is not found", function(done) { var strategy = new Strategy(function(token, next) { expect(token).to.equal("abc"); return next(null, false); }); chai.passport.use(strategy) .fail(function(err) { expect(err).to.equal(401); return done(); }) .success(function() { return done(new Error("It should not call this")); }) .req(function(req) { req.cookies = { token: "abc" }; }).authenticate(); }); it("should call the verify callback and call fail because the user is not found with info message", function(done) { var strategy = new Strategy(function(token, next) { expect(token).to.equal("abc"); return next(null, false, {message: 'user not found'}); }); chai.passport.use(strategy) .fail(function(message, err) { expect(message).to.equal('user not found') expect(err).to.equal(401); return done(); }) .success(function() { return done(new Error("It should not call this")); }) .req(function(req) { req.cookies = { token: "abc" }; }).authenticate(); }); it("should call the verify callback and call fail because the user is not found with signed cookies", function(done) { var strategy = new Strategy({ signed: true }, function(token, next) { expect(token).to.equal("abc"); return next(null, false); }); chai.passport.use(strategy) .fail(function(err) { expect(err).to.equal(401); return done(); }) .success(function() { return done(new Error("It should not call this")); }) .req(function(req) { req.signedCookies = { token: "abc" }; }).authenticate(); }); it("should call the verify callback and call next with an error", function(done) { var strategy = new Strategy(function(token, next) { expect(token).to.equal("abc"); return next(new Error("Failed")); }); chai.passport.use(strategy) .error(function(err) { expect(err.message).to.equal("Failed"); return done(); }) .success(function() { return done(new Error("It should not call this")); }) .req(function(req) { req.cookies = { token: "abc" }; }).authenticate(); }); it("should call the verify callback and call next with an error with signed cookies", function(done) { var strategy = new Strategy({ signed: true }, function(token, next) { expect(token).to.equal("abc"); return next(new Error("Failed")); }); chai.passport.use(strategy) .error(function(err) { expect(err.message).to.equal("Failed"); return done(); }) .success(function() { return done(new Error("It should not call this")); }) .req(function(req) { req.signedCookies = { token: "abc" }; }).authenticate(); }); it("should call the verify callback and call next with success", function(done) { var strategy = new Strategy(function(token, next) { expect(token).to.equal("abc"); return next(null, { id: "userid" }); }); chai.passport.use(strategy) .error(function(err) { return done(new Error("It should not call this")); }) .success(function() { return done(); }) .req(function(req) { req.cookies = { token: "abc" }; }).authenticate(); }); it("should call the verify callback and call next with success with signed cookies", function(done) { var strategy = new Strategy({ signed: true }, function(token, next) { expect(token).to.equal("abc"); return next(null, { id: "userid" }); }); chai.passport.use(strategy) .error(function(err) { return done(new Error("It should not call this")); }) .success(function() { return done(); }) .req(function(req) { req.signedCookies = { token: "abc" }; }).authenticate(); }); it("should call the verify callback and call next with success with signed cookies and info", function(done) { var strategy = new Strategy({ signed: true }, function(token, next) { expect(token).to.equal("abc"); return next(null, { id: "userid" }, {role: 'user'}); }); chai.passport.use(strategy) .error(function(err) { return done(new Error("It should not call this")); }) .success(function(user, info) { expect(user).to.deep.equal({id: 'userid'}); expect(info).to.deep.equal({role: 'user'}); return done(); }) .req(function(req) { req.signedCookies = { token: "abc" }; }).authenticate(); }); it("should pass request to verify callback", function(done) { var strategy = new Strategy({passReqToCallback: true}, function(req, token, next) { expect(req.body.username).to.equal('enricofermi'); expect(req.body.password).to.equal('fermion'); expect(token).to.equal("abc"); return next(null, { id: "userid" }); }); chai.passport.use(strategy) .error(function(err) { return done(new Error("It should not call this")); }) .success(function() { return done(); }) .req(function(req) { req.cookies = { token: "abc" }; req.body = {}; req.body.username = 'enricofermi'; req.body.password = 'fermion'; }).authenticate(); }); it("should pass request to verify callback with signed cookies", function(done) { var strategy = new Strategy({passReqToCallback: true, signed: true}, function(req, token, next) { expect(req.body.username).to.equal('enricofermi'); expect(req.body.password).to.equal('fermion'); expect(token).to.equal("abc"); return next(null, { id: "userid" }); }); chai.passport.use(strategy) .error(function(err) { return done(new Error("It should not call this")); }) .success(function() { return done(); }) .req(function(req) { req.signedCookies = { token: "abc" }; req.body = {}; req.body.username = 'enricofermi'; req.body.password = 'fermion'; }).authenticate(); }); });