From c423672b552cc68594b8f5d29eadecaee41905e3 Mon Sep 17 00:00:00 2001 From: Shane Kilkelly Date: Wed, 18 Jul 2018 12:08:34 +0100 Subject: [PATCH] Unit test for `finishLogin` --- .../AuthenticationControllerTests.coffee | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/services/web/test/unit/coffee/Authentication/AuthenticationControllerTests.coffee b/services/web/test/unit/coffee/Authentication/AuthenticationControllerTests.coffee index 2405e9fba7..8482c28e04 100644 --- a/services/web/test/unit/coffee/Authentication/AuthenticationControllerTests.coffee +++ b/services/web/test/unit/coffee/Authentication/AuthenticationControllerTests.coffee @@ -579,3 +579,56 @@ describe "AuthenticationController", -> @AuthenticationController._clearRedirectFromSession(@req) expect(@req.session.postLoginRedirect).to.equal undefined + + describe 'finishLogin', -> + # - get redirect + # - async handlers + # - afterLoginSessionSetup + # - clear redirect + # - issue redir, two ways + beforeEach -> + @AuthenticationController._getRedirectFromSession = sinon.stub().returns '/some/page' + @AuthenticationController._loginAsyncHandlers = sinon.stub() + @AuthenticationController.afterLoginSessionSetup = sinon.stub().callsArgWith(2, null) + @AuthenticationController._clearRedirectFromSession = sinon.stub() + @req.headers = {accept: 'application/json, whatever'} + @res.json = sinon.stub() + @res.redirect = sinon.stub() + + it 'should extract the redirect from the session', () -> + @AuthenticationController.finishLogin(@user, @req, @res, @next) + expect(@AuthenticationController._getRedirectFromSession.callCount).to.equal 1 + expect(@AuthenticationController._getRedirectFromSession.calledWith(@req)).to.equal true + + it 'should call the async handlers', () -> + @AuthenticationController.finishLogin(@user, @req, @res, @next) + expect(@AuthenticationController._loginAsyncHandlers.callCount).to.equal 1 + expect(@AuthenticationController._loginAsyncHandlers.calledWith(@req, @user)).to.equal true + + it 'should call afterLoginSessionSetup', () -> + @AuthenticationController.finishLogin(@user, @req, @res, @next) + expect(@AuthenticationController.afterLoginSessionSetup.callCount).to.equal 1 + expect(@AuthenticationController.afterLoginSessionSetup.calledWith(@req, @user)).to.equal true + + it 'should clear redirect from session', () -> + @AuthenticationController.finishLogin(@user, @req, @res, @next) + expect(@AuthenticationController._clearRedirectFromSession.callCount).to.equal 1 + expect(@AuthenticationController._clearRedirectFromSession.calledWith(@req)).to.equal true + + it 'should issue a json response with a redirect', () -> + @AuthenticationController.finishLogin(@user, @req, @res, @next) + expect(@res.json.callCount).to.equal 1 + expect(@res.redirect.callCount).to.equal 0 + expect(@res.json.calledWith({ redir: '/some/page' })).to.equal true + + describe 'with a non-json request', -> + beforeEach -> + @req.headers = {} + @res.json = sinon.stub() + @res.redirect = sinon.stub() + + it 'should issue a plain redirect', () -> + @AuthenticationController.finishLogin(@user, @req, @res, @next) + expect(@res.json.callCount).to.equal 0 + expect(@res.redirect.callCount).to.equal 1 + expect(@res.redirect.calledWith('/some/page')).to.equal true