Handle null, undefined and false in isUserLoggedIn

This commit is contained in:
Shane Kilkelly 2016-09-23 16:53:07 +01:00
parent f9030a0a38
commit dd14e51713
2 changed files with 20 additions and 2 deletions

View file

@ -94,7 +94,7 @@ module.exports = AuthenticationController =
isUserLoggedIn: (req) -> isUserLoggedIn: (req) ->
user_id = AuthenticationController.getLoggedInUserId(req) user_id = AuthenticationController.getLoggedInUserId(req)
return user_id != null return (user_id not in [null, undefined, false])
# TODO: perhaps should produce an error if the current user is not present # TODO: perhaps should produce an error if the current user is not present
getLoggedInUserId: (req) -> getLoggedInUserId: (req) ->

View file

@ -44,6 +44,24 @@ describe "AuthenticationController", ->
afterEach -> afterEach ->
tk.reset() tk.reset()
describe 'isUserLoggedIn', () ->
beforeEach ->
@stub = sinon.stub(@AuthenticationController, 'getLoggedInUserId')
afterEach ->
@stub.restore()
it 'should do the right thing in all cases', () ->
@AuthenticationController.getLoggedInUserId.returns('some_id')
expect(@AuthenticationController.isUserLoggedIn(@req)).to.equal true
@AuthenticationController.getLoggedInUserId.returns(null)
expect(@AuthenticationController.isUserLoggedIn(@req)).to.equal false
@AuthenticationController.getLoggedInUserId.returns(false)
expect(@AuthenticationController.isUserLoggedIn(@req)).to.equal false
@AuthenticationController.getLoggedInUserId.returns(undefined)
expect(@AuthenticationController.isUserLoggedIn(@req)).to.equal false
describe 'setInSessionUser', () -> describe 'setInSessionUser', () ->
beforeEach -> beforeEach ->
@ -361,7 +379,7 @@ describe "AuthenticationController", ->
describe "with a user session", -> describe "with a user session", ->
beforeEach -> beforeEach ->
@req.session = @req.session =
user: {"mock": "user"} user: {"mock": "user", "_id": "some_id"}
@AuthenticationController.requireGlobalLogin @req, @res, @next @AuthenticationController.requireGlobalLogin @req, @res, @next
it "should call next() directly", -> it "should call next() directly", ->