From f8a175f1145c4efdffa7e4c511a642f608e11c0f Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Thu, 19 May 2022 10:18:27 -0700 Subject: [PATCH] Add tests. --- test/http/request.test.js | 57 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/test/http/request.test.js b/test/http/request.test.js index 94566979..3c98cc5b 100644 --- a/test/http/request.test.js +++ b/test/http/request.test.js @@ -293,7 +293,7 @@ describe('http.ServerRequest', function() { expect(req.session.id).to.equal('2'); }); - it('should keep session data', function() { + it('should not keep session data', function() { expect(req.session.cart).to.be.undefined; }); @@ -721,6 +721,61 @@ describe('http.ServerRequest', function() { }); }); + describe('existing session and keeping session data', function() { + var passport = new Passport(); + + var req = new Object(); + req.logout = request.logout; + req.isAuthenticated = request.isAuthenticated; + req.isUnauthenticated = request.isUnauthenticated; + req.user = { id: '1', username: 'root' }; + req._passport = {}; + req._passport.instance = passport; + req._sessionManager = passport._sm; + req.session = { cart: [ '1', '2', ] }; + Object.defineProperty(req.session, 'id', { value: '1' }); + req.session['passport'] = {}; + req.session['passport'].user = '1'; + req.session.save = function(cb) { + expect(req.session['passport'].user).to.be.undefined; + process.nextTick(cb); + }; + req.session.regenerate = function(cb) { + req.session = { id: '2' }; + process.nextTick(cb); + }; + + var error; + + before(function(done) { + req.logout({ keepSessionData: true }, function(err) { + error = err; + done(); + }); + }); + + it('should not error', function() { + expect(error).to.be.undefined; + }); + + it('should not be authenticated', function() { + expect(req.isAuthenticated()).to.be.false; + expect(req.isUnauthenticated()).to.be.true; + }); + + it('should clear user', function() { + expect(req.user).to.be.null; + }); + + it('should clear serialized user', function() { + expect(req.session['passport'].user).to.be.undefined; + }); + + it('should keep session data', function() { + expect(req.session.cart).to.deep.equal([ '1', '2' ]); + }); + }); + describe('existing session and clearing custom user property', function() { var passport = new Passport();