null check user when getting user id from session

This commit is contained in:
Henry Oswald 2014-04-02 15:56:54 +01:00
parent b7b307e82b
commit 479b37a48c
2 changed files with 36 additions and 1 deletions

View file

@ -44,7 +44,11 @@ module.exports = AuthenticationController =
res.send(auth_token) res.send(auth_token)
getLoggedInUserId: (req, callback = (error, user_id) ->) -> getLoggedInUserId: (req, callback = (error, user_id) ->) ->
callback null, req.session.user._id.toString() if req?.session?.user?._id?
callback null, req.session.user._id.toString()
else
e = new Error("user is not on req session")
callback e
getLoggedInUser: (req, options = {allow_auth_token: false}, callback = (error, user) ->) -> getLoggedInUser: (req, options = {allow_auth_token: false}, callback = (error, user) ->) ->
if req.session?.user?._id? if req.session?.user?._id?

View file

@ -122,6 +122,37 @@ describe "AuthenticationController", ->
it "should only redirect to the local path", -> it "should only redirect to the local path", ->
expect(@res.body).to.deep.equal redir: "/test" expect(@res.body).to.deep.equal redir: "/test"
describe "getLoggedInUserId", ->
beforeEach ->
@req =
session :{}
it "should return the user id from the session", (done)->
@user_id = "2134"
@req.session.user =
_id:@user_id
@AuthenticationController.getLoggedInUserId @req, (err, user_id)=>
expect(user_id).to.equal @user_id
done()
it "should return an error if there is no user on the session", (done)->
@AuthenticationController.getLoggedInUserId @req, (err, user_id)=>
expect(err).to.exist
done()
it "should return an error if there is no session", (done)->
@req = {}
@AuthenticationController.getLoggedInUserId @req, (err, user_id)=>
expect(err).to.exist
done()
it "should return an error if there is no req", (done)->
@req = {}
@AuthenticationController.getLoggedInUserId @req, (err, user_id)=>
expect(err).to.exist
done()
describe "getLoggedInUser", -> describe "getLoggedInUser", ->
beforeEach -> beforeEach ->
@UserGetter.getUser = sinon.stub().callsArgWith(1, null, @user) @UserGetter.getUser = sinon.stub().callsArgWith(1, null, @user)