2014-02-12 05:23:40 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
2018-10-30 14:18:42 -04:00
|
|
|
sinonChai = require "sinon-chai"
|
|
|
|
chai.use sinonChai
|
2014-02-12 05:23:40 -05:00
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/Features/Authentication/AuthenticationController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
events = require "events"
|
|
|
|
tk = require("timekeeper")
|
|
|
|
MockRequest = require("../helpers/MockRequest")
|
|
|
|
MockResponse = require("../helpers/MockResponse")
|
|
|
|
ObjectId = require("mongojs").ObjectId
|
|
|
|
|
|
|
|
describe "AuthenticationController", ->
|
|
|
|
beforeEach ->
|
2018-06-05 08:41:17 -04:00
|
|
|
tk.freeze(Date.now())
|
2018-10-30 14:18:42 -04:00
|
|
|
@UserModel = findOne: sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
@AuthenticationController = SandboxedModule.require modulePath, requires:
|
|
|
|
"./AuthenticationManager": @AuthenticationManager = {}
|
2018-09-05 10:28:26 -04:00
|
|
|
"../User/UserUpdater" : @UserUpdater = {updateUser:sinon.stub()}
|
2017-04-03 11:18:30 -04:00
|
|
|
"metrics-sharelatex": @Metrics = { inc: sinon.stub() }
|
2014-02-12 05:23:40 -05:00
|
|
|
"../Security/LoginRateLimiter": @LoginRateLimiter = { processLoginRequest:sinon.stub(), recordSuccessfulLogin:sinon.stub() }
|
2016-02-18 05:16:50 -05:00
|
|
|
"../User/UserHandler": @UserHandler = {setupLoginData:sinon.stub()}
|
2016-08-11 09:09:57 -04:00
|
|
|
"../Analytics/AnalyticsManager": @AnalyticsManager = { recordEvent: sinon.stub() }
|
2016-09-21 08:03:37 -04:00
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub(), err: sinon.stub() }
|
2019-02-12 10:26:20 -05:00
|
|
|
"settings-sharelatex": { siteUrl: 'http://www.foo.bar' }
|
2016-09-15 09:36:11 -04:00
|
|
|
"passport": @passport =
|
|
|
|
authenticate: sinon.stub().returns(sinon.stub())
|
2016-07-01 10:33:59 -04:00
|
|
|
"../User/UserSessionsManager": @UserSessionsManager =
|
|
|
|
trackSession: sinon.stub()
|
|
|
|
untrackSession: sinon.stub()
|
|
|
|
revokeAllUserSessions: sinon.stub().callsArgWith(1, null)
|
2018-08-03 11:10:50 -04:00
|
|
|
"../../infrastructure/Modules": @Modules = {hooks: {fire: sinon.stub().callsArgWith(2, null, [])}}
|
2018-09-20 09:59:30 -04:00
|
|
|
"../SudoMode/SudoModeHandler": @SudoModeHandler = {activateSudoMode: sinon.stub().callsArgWith(1, null)}
|
2018-10-05 11:24:05 -04:00
|
|
|
"../Notifications/NotificationsBuilder": @NotificationsBuilder =
|
|
|
|
ipMatcherAffiliation: sinon.stub()
|
2018-10-30 14:18:42 -04:00
|
|
|
"../V1/V1Api": @V1Api = request: sinon.stub()
|
|
|
|
"../../models/User": { User: @UserModel }
|
2019-05-14 05:27:21 -04:00
|
|
|
"../../../../modules/oauth2-server/app/js/Oauth2Server": @Oauth2Server =
|
|
|
|
Request: sinon.stub()
|
|
|
|
Response: sinon.stub()
|
|
|
|
server: authenticate: sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
@user =
|
|
|
|
_id: ObjectId()
|
|
|
|
email: @email = "USER@example.com"
|
|
|
|
first_name: "bob"
|
|
|
|
last_name: "brown"
|
|
|
|
referal_id: 1234
|
|
|
|
isAdmin: false
|
|
|
|
@password = "banana"
|
|
|
|
@req = new MockRequest()
|
|
|
|
@res = new MockResponse()
|
|
|
|
@callback = @next = sinon.stub()
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
tk.reset()
|
|
|
|
|
2016-09-23 11:53:07 -04:00
|
|
|
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
|
|
|
|
|
2016-09-22 11:58:25 -04:00
|
|
|
describe 'setInSessionUser', () ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@user = {
|
|
|
|
_id: 'id'
|
|
|
|
first_name: 'a'
|
|
|
|
last_name: 'b'
|
|
|
|
email: 'c'
|
|
|
|
}
|
|
|
|
@req.session.passport = {user: @user}
|
|
|
|
@req.session.user = @user
|
|
|
|
|
|
|
|
it 'should update the right properties', () ->
|
|
|
|
@AuthenticationController.setInSessionUser(@req, {first_name: 'new_first_name', email: 'new_email'})
|
|
|
|
expectedUser = {
|
|
|
|
_id: 'id'
|
|
|
|
first_name: 'new_first_name'
|
|
|
|
last_name: 'b'
|
|
|
|
email: 'new_email'
|
|
|
|
}
|
|
|
|
expect(@req.session.passport.user).to.deep.equal(expectedUser)
|
|
|
|
expect(@req.session.user).to.deep.equal(expectedUser)
|
|
|
|
|
2016-09-15 09:36:11 -04:00
|
|
|
describe 'passportLogin', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@info = null
|
|
|
|
@req.login = sinon.stub().callsArgWith(1, null)
|
|
|
|
@res.json = sinon.stub()
|
2016-11-22 09:24:36 -05:00
|
|
|
@req.session = @session = {
|
|
|
|
passport: {user: @user},
|
|
|
|
postLoginRedirect: "/path/to/redir/to"
|
|
|
|
}
|
2016-11-08 11:00:18 -05:00
|
|
|
@req.session.destroy = sinon.stub().callsArgWith(0, null)
|
2016-09-21 08:03:37 -04:00
|
|
|
@req.session.save = sinon.stub().callsArgWith(0, null)
|
|
|
|
@req.sessionStore = {generate: sinon.stub()}
|
2018-07-18 04:57:05 -04:00
|
|
|
@AuthenticationController.finishLogin = sinon.stub()
|
2016-09-15 09:36:11 -04:00
|
|
|
@passport.authenticate.callsArgWith(1, null, @user, @info)
|
2018-02-27 10:03:21 -05:00
|
|
|
@err = new Error('woops')
|
2016-09-15 09:36:11 -04:00
|
|
|
|
|
|
|
it 'should call passport.authenticate', () ->
|
|
|
|
@AuthenticationController.passportLogin @req, @res, @next
|
|
|
|
@passport.authenticate.callCount.should.equal 1
|
|
|
|
|
|
|
|
describe 'when authenticate produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@passport.authenticate.callsArgWith(1, @err)
|
|
|
|
|
|
|
|
it 'should return next with an error', () ->
|
|
|
|
@AuthenticationController.passportLogin @req, @res, @next
|
|
|
|
@next.calledWith(@err).should.equal true
|
|
|
|
|
|
|
|
describe 'when authenticate produces a user', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2016-11-22 09:24:36 -05:00
|
|
|
@req.session.postLoginRedirect = 'some_redirect'
|
2016-09-15 09:36:11 -04:00
|
|
|
@passport.authenticate.callsArgWith(1, null, @user, @info)
|
|
|
|
|
|
|
|
afterEach ->
|
2016-11-22 09:24:36 -05:00
|
|
|
delete @req.session.postLoginRedirect
|
2016-09-15 09:36:11 -04:00
|
|
|
|
2018-07-18 04:57:05 -04:00
|
|
|
it 'should call finishLogin', () ->
|
2016-09-15 09:36:11 -04:00
|
|
|
@AuthenticationController.passportLogin @req, @res, @next
|
2018-07-18 04:57:05 -04:00
|
|
|
@AuthenticationController.finishLogin.callCount.should.equal 1
|
|
|
|
@AuthenticationController.finishLogin.calledWith(@user).should.equal true
|
2016-09-21 08:03:37 -04:00
|
|
|
|
2016-09-15 09:36:11 -04:00
|
|
|
describe 'when authenticate does not produce a user', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@info = {text: 'a', type: 'b'}
|
|
|
|
@passport.authenticate.callsArgWith(1, null, false, @info)
|
|
|
|
|
2018-07-18 04:57:05 -04:00
|
|
|
it 'should not call finishLogin', () ->
|
2016-09-15 09:36:11 -04:00
|
|
|
@AuthenticationController.passportLogin @req, @res, @next
|
2018-07-18 04:57:05 -04:00
|
|
|
@AuthenticationController.finishLogin.callCount.should.equal 0
|
2016-09-15 09:36:11 -04:00
|
|
|
|
2016-11-24 06:38:13 -05:00
|
|
|
it 'should not send a json response with redirect', () ->
|
2016-09-15 09:36:11 -04:00
|
|
|
@AuthenticationController.passportLogin @req, @res, @next
|
|
|
|
@res.json.callCount.should.equal 1
|
|
|
|
@res.json.calledWith({message: @info}).should.equal true
|
2016-11-24 06:38:13 -05:00
|
|
|
expect(@res.json.lastCall.args[0].redir?).to.equal false
|
2016-09-15 09:36:11 -04:00
|
|
|
|
2016-11-01 10:06:54 -04:00
|
|
|
describe 'afterLoginSessionSetup', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.login = sinon.stub().callsArgWith(1, null)
|
|
|
|
@req.session = @session = {passport: {user: @user}}
|
|
|
|
@req.session =
|
|
|
|
passport: {user: {_id: "one"}}
|
2016-11-08 11:00:18 -05:00
|
|
|
@req.session.destroy = sinon.stub().callsArgWith(0, null)
|
2016-11-01 10:06:54 -04:00
|
|
|
@req.session.save = sinon.stub().callsArgWith(0, null)
|
|
|
|
@req.sessionStore = {generate: sinon.stub()}
|
|
|
|
@UserSessionsManager.trackSession = sinon.stub()
|
|
|
|
@call = (callback) =>
|
|
|
|
@AuthenticationController.afterLoginSessionSetup @req, @user, callback
|
|
|
|
|
|
|
|
it 'should not produce an error', (done) ->
|
|
|
|
@call (err) =>
|
|
|
|
expect(err).to.equal null
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should call req.login', (done) ->
|
|
|
|
@call (err) =>
|
|
|
|
@req.login.callCount.should.equal 1
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should call req.session.save', (done) ->
|
|
|
|
@call (err) =>
|
|
|
|
@req.session.save.callCount.should.equal 1
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should call UserSessionsManager.trackSession', (done) ->
|
|
|
|
@call (err) =>
|
|
|
|
@UserSessionsManager.trackSession.callCount.should.equal 1
|
|
|
|
done()
|
|
|
|
|
|
|
|
describe 'when req.session.save produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.session.save = sinon.stub().callsArgWith(0, new Error('woops'))
|
|
|
|
|
|
|
|
it 'should produce an error', (done) ->
|
|
|
|
@call (err) =>
|
|
|
|
expect(err).to.not.be.oneOf [null, undefined]
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'should not call UserSessionsManager.trackSession', (done) ->
|
|
|
|
@call (err) =>
|
|
|
|
@UserSessionsManager.trackSession.callCount.should.equal 0
|
|
|
|
done()
|
|
|
|
|
2016-09-07 09:05:51 -04:00
|
|
|
describe 'getSessionUser', ->
|
|
|
|
|
|
|
|
it 'should get the user object from session', ->
|
|
|
|
@req.session =
|
|
|
|
passport:
|
|
|
|
user: {_id: 'one'}
|
|
|
|
user = @AuthenticationController.getSessionUser(@req)
|
|
|
|
expect(user).to.deep.equal {_id: 'one'}
|
|
|
|
|
|
|
|
it 'should work with legacy sessions', ->
|
|
|
|
@req.session =
|
|
|
|
user: {_id: 'one'}
|
|
|
|
user = @AuthenticationController.getSessionUser(@req)
|
|
|
|
expect(user).to.deep.equal {_id: 'one'}
|
|
|
|
|
|
|
|
describe "doPassportLogin", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
|
|
|
@AuthenticationController._recordFailedLogin = sinon.stub()
|
|
|
|
@AuthenticationController._recordSuccessfulLogin = sinon.stub()
|
2018-08-17 07:04:05 -04:00
|
|
|
@Modules.hooks.fire = sinon.stub().callsArgWith(3, null, [])
|
2016-09-07 09:05:51 -04:00
|
|
|
# @AuthenticationController.establishUserSession = sinon.stub().callsArg(2)
|
2014-02-12 05:23:40 -05:00
|
|
|
@req.body =
|
|
|
|
email: @email
|
|
|
|
password: @password
|
2016-11-22 09:24:36 -05:00
|
|
|
session:
|
|
|
|
postLoginRedirect: "/path/to/redir/to"
|
2016-09-07 09:05:51 -04:00
|
|
|
@cb = sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-08-03 11:10:50 -04:00
|
|
|
describe "when the preDoPassportLogin hooks produce an info object", ->
|
|
|
|
beforeEach ->
|
2018-08-17 07:04:05 -04:00
|
|
|
@Modules.hooks.fire = sinon.stub().callsArgWith(3, null, [null, {redir: '/somewhere'}, null])
|
2018-08-03 11:10:50 -04:00
|
|
|
|
|
|
|
it "should stop early and call done with this info object", (done) ->
|
|
|
|
@AuthenticationController.doPassportLogin(@req, @req.body.email, @req.body.password, @cb)
|
|
|
|
@cb.callCount.should.equal 1
|
|
|
|
@cb.calledWith(null, false, {redir: '/somewhere'}).should.equal true
|
|
|
|
@LoginRateLimiter.processLoginRequest.callCount.should.equal 0
|
|
|
|
done()
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "when the users rate limit", ->
|
|
|
|
|
2016-09-07 09:05:51 -04:00
|
|
|
beforeEach ->
|
2014-02-12 05:23:40 -05:00
|
|
|
@LoginRateLimiter.processLoginRequest.callsArgWith(1, null, false)
|
2016-09-07 09:05:51 -04:00
|
|
|
|
|
|
|
it "should block the request if the limit has been exceeded", (done)->
|
|
|
|
@AuthenticationController.doPassportLogin(@req, @req.body.email, @req.body.password, @cb)
|
|
|
|
@cb.callCount.should.equal 1
|
|
|
|
@cb.calledWith(null, null).should.equal true
|
|
|
|
done()
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe 'when the user is authenticated', ->
|
|
|
|
beforeEach ->
|
2016-09-07 09:05:51 -04:00
|
|
|
@cb = sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
@LoginRateLimiter.processLoginRequest.callsArgWith(1, null, true)
|
|
|
|
@AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, @user)
|
2017-03-22 11:50:49 -04:00
|
|
|
@req.sessionID = Math.random()
|
2016-09-07 09:05:51 -04:00
|
|
|
@AuthenticationController.doPassportLogin(@req, @req.body.email, @req.body.password, @cb)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it "should attempt to authorise the user", ->
|
|
|
|
@AuthenticationManager.authenticate
|
|
|
|
.calledWith(email: @email.toLowerCase(), @password)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should establish the user's session", ->
|
2016-09-07 09:05:51 -04:00
|
|
|
@cb.calledWith(null, @user).should.equal true
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
describe '_loginAsyncHandlers', ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserHandler.setupLoginData = sinon.stub()
|
|
|
|
@LoginRateLimiter.recordSuccessfulLogin = sinon.stub()
|
|
|
|
@AuthenticationController._recordSuccessfulLogin = sinon.stub()
|
|
|
|
@AnalyticsManager.recordEvent = sinon.stub()
|
|
|
|
@AnalyticsManager.identifyUser = sinon.stub()
|
|
|
|
@AuthenticationController._loginAsyncHandlers(@req, @user)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
it "should call identifyUser", ->
|
|
|
|
@AnalyticsManager.identifyUser.calledWith(@user._id, @req.sessionID).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
it "should setup the user data in the background", ->
|
|
|
|
@UserHandler.setupLoginData.calledWith(@user).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
it "should set res.session.justLoggedIn", ->
|
|
|
|
@req.session.justLoggedIn.should.equal true
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
it "should record the successful login", ->
|
|
|
|
@AuthenticationController._recordSuccessfulLogin
|
|
|
|
.calledWith(@user._id)
|
|
|
|
.should.equal true
|
2018-07-18 04:57:05 -04:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
it "should tell the rate limiter that there was a success for that email", ->
|
|
|
|
@LoginRateLimiter.recordSuccessfulLogin.calledWith(@user.email).should.equal true
|
2018-07-18 04:57:05 -04:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
it "should log the successful login", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(email: @user.email, user_id: @user._id.toString(), "successful log in")
|
|
|
|
.should.equal true
|
2018-07-18 04:57:05 -04:00
|
|
|
|
2018-07-18 06:13:42 -04:00
|
|
|
it "should track the login event", ->
|
|
|
|
@AnalyticsManager.recordEvent
|
|
|
|
.calledWith(@user._id, "user-logged-in")
|
|
|
|
.should.equal true
|
2016-08-11 09:09:57 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe 'when the user is not authenticated', ->
|
|
|
|
beforeEach ->
|
|
|
|
@LoginRateLimiter.processLoginRequest.callsArgWith(1, null, true)
|
|
|
|
@AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, null)
|
2016-09-07 09:05:51 -04:00
|
|
|
@cb = sinon.stub()
|
|
|
|
@AuthenticationController.doPassportLogin(@req, @req.body.email, @req.body.password, @cb)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-09-07 09:05:51 -04:00
|
|
|
it "should not establish the login", ->
|
|
|
|
@cb.callCount.should.equal 1
|
|
|
|
@cb.calledWith(null, false)
|
2014-08-08 05:21:17 -04:00
|
|
|
# @res.body.should.exist
|
2016-09-15 09:36:11 -04:00
|
|
|
expect(@cb.lastCall.args[2]).to.contain.all.keys ['text', 'type']
|
2014-08-08 05:21:17 -04:00
|
|
|
# message:
|
|
|
|
# text: 'Your email or password were incorrect. Please try again',
|
|
|
|
# type: 'error'
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-02-18 05:16:50 -05:00
|
|
|
it "should not setup the user data in the background", ->
|
|
|
|
@UserHandler.setupLoginData.called.should.equal false
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it "should record a failed login", ->
|
|
|
|
@AuthenticationController._recordFailedLogin.called.should.equal true
|
|
|
|
|
|
|
|
it "should log the failed login", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(email: @email.toLowerCase(), "failed log in")
|
|
|
|
.should.equal true
|
|
|
|
|
2014-04-02 10:56:54 -04:00
|
|
|
describe "getLoggedInUserId", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
2016-07-01 10:33:59 -04:00
|
|
|
@req =
|
2014-04-02 10:56:54 -04:00
|
|
|
session :{}
|
|
|
|
|
2016-09-07 09:05:51 -04:00
|
|
|
it "should return the user id from the session", ()->
|
2014-04-02 10:56:54 -04:00
|
|
|
@user_id = "2134"
|
2016-07-01 10:33:59 -04:00
|
|
|
@req.session.user =
|
2014-04-02 10:56:54 -04:00
|
|
|
_id:@user_id
|
2016-09-07 09:05:51 -04:00
|
|
|
result = @AuthenticationController.getLoggedInUserId @req
|
|
|
|
expect(result).to.equal @user_id
|
2014-04-02 10:56:54 -04:00
|
|
|
|
2016-09-07 09:05:51 -04:00
|
|
|
it 'should return user for passport session', () ->
|
|
|
|
@user_id = "2134"
|
|
|
|
@req.session = {
|
|
|
|
passport: {
|
|
|
|
user: {
|
|
|
|
_id:@user_id
|
|
|
|
}
|
|
|
|
}
|
2018-06-25 19:27:47 -04:00
|
|
|
}
|
2016-09-07 09:05:51 -04:00
|
|
|
result = @AuthenticationController.getLoggedInUserId @req
|
|
|
|
expect(result).to.equal @user_id
|
|
|
|
|
|
|
|
it "should return null if there is no user on the session", ()->
|
|
|
|
result = @AuthenticationController.getLoggedInUserId @req
|
|
|
|
expect(result).to.equal null
|
2014-04-02 10:56:54 -04:00
|
|
|
|
2016-09-07 09:05:51 -04:00
|
|
|
it "should return null if there is no session", ()->
|
2014-04-02 10:56:54 -04:00
|
|
|
@req = {}
|
2016-09-07 09:05:51 -04:00
|
|
|
result = @AuthenticationController.getLoggedInUserId @req
|
|
|
|
expect(result).to.equal null
|
2014-04-02 10:56:54 -04:00
|
|
|
|
2016-09-07 09:05:51 -04:00
|
|
|
it "should return null if there is no req", ()->
|
2014-04-02 10:56:54 -04:00
|
|
|
@req = {}
|
2016-09-07 09:05:51 -04:00
|
|
|
result = @AuthenticationController.getLoggedInUserId @req
|
|
|
|
expect(result).to.equal null
|
2014-04-02 10:56:54 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "requireLogin", ->
|
|
|
|
beforeEach ->
|
|
|
|
@user =
|
|
|
|
_id: "user-id-123"
|
|
|
|
email: "user@sharelatex.com"
|
2016-03-10 12:15:14 -05:00
|
|
|
@middleware = @AuthenticationController.requireLogin()
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2016-03-10 12:15:14 -05:00
|
|
|
describe "when the user is logged in", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2016-03-10 12:15:14 -05:00
|
|
|
@req.session =
|
|
|
|
user: @user = {
|
|
|
|
_id: "user-id-123"
|
|
|
|
email: "user@sharelatex.com"
|
|
|
|
}
|
|
|
|
@middleware(@req, @res, @next)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-03-10 12:15:14 -05:00
|
|
|
it "should call the next method in the chain", ->
|
|
|
|
@next.called.should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-03-10 12:15:14 -05:00
|
|
|
describe "when the user is not logged in", ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2016-03-10 12:15:14 -05:00
|
|
|
@req.session = {}
|
|
|
|
@AuthenticationController._redirectToLoginOrRegisterPage = sinon.stub()
|
|
|
|
@req.query = {}
|
2014-02-12 05:23:40 -05:00
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
2016-03-10 12:15:14 -05:00
|
|
|
it "should redirect to the register or login page", ->
|
|
|
|
@AuthenticationController._redirectToLoginOrRegisterPage.calledWith(@req, @res).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2018-10-30 14:18:42 -04:00
|
|
|
describe "requireOauth", ->
|
|
|
|
beforeEach ->
|
2019-05-14 05:27:21 -04:00
|
|
|
@res.sendStatus = sinon.stub()
|
2018-10-30 14:18:42 -04:00
|
|
|
@res.send = sinon.stub()
|
|
|
|
@res.status = sinon.stub().returns(@res)
|
2019-05-14 05:29:24 -04:00
|
|
|
@res.sendStatus = sinon.stub()
|
2018-10-30 14:18:42 -04:00
|
|
|
@middleware = @AuthenticationController.requireOauth()
|
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
describe "when Oauth2Server authenticates", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
beforeEach ->
|
2019-05-14 05:27:21 -04:00
|
|
|
@token =
|
|
|
|
accessToken: "token"
|
|
|
|
user: "user"
|
|
|
|
@Oauth2Server.server.authenticate.yields null, @token
|
2018-10-30 14:18:42 -04:00
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should set oauth_token on request", ->
|
|
|
|
@req.oauth_token.should.equal @token
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should set oauth on request", ->
|
|
|
|
@req.oauth.access_token.should.equal @token.accessToken
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should set oauth_user on request", ->
|
|
|
|
@req.oauth_user.should.equal "user"
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should call next", ->
|
|
|
|
@next.should.have.been.calledOnce
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
describe "when Oauth2Server does not authenticate", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
beforeEach ->
|
2019-05-14 05:27:21 -04:00
|
|
|
@Oauth2Server.server.authenticate.yields code: 401
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
describe "when token not provided", ->
|
|
|
|
beforeEach ->
|
|
|
|
@middleware(@req, @res, @next)
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should return 401 error", ->
|
|
|
|
@res.sendStatus.should.have.been.calledWith 401
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
describe "when token provided", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
beforeEach ->
|
2019-05-14 05:27:21 -04:00
|
|
|
@V1Api.request = sinon.stub().yields("error", {}, {})
|
|
|
|
@req.token = "foo"
|
2018-10-30 14:18:42 -04:00
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should make request to v1 api with token", ->
|
|
|
|
@V1Api.request.should.have.been.calledWith {
|
|
|
|
expectedStatusCodes: [401]
|
|
|
|
json: token: "foo"
|
|
|
|
method: "POST"
|
|
|
|
uri: "/api/v1/sharelatex/oauth_authorize"
|
|
|
|
}
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
describe "when v1 api returns error", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
beforeEach ->
|
2019-05-14 05:27:21 -04:00
|
|
|
@V1Api.request = sinon.stub().yields("error", {}, {})
|
|
|
|
@req.token = "foo"
|
2018-10-30 14:18:42 -04:00
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should return status", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
@next.should.have.been.calledWith "error"
|
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
describe "when v1 api status code is not 200", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
beforeEach ->
|
2019-05-14 05:27:21 -04:00
|
|
|
@V1Api.request = sinon.stub().yields(null, {statusCode: 401}, {})
|
|
|
|
@req.token = "foo"
|
2018-10-30 14:18:42 -04:00
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should return status", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
@res.status.should.have.been.calledWith 401
|
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
describe "when v1 api returns authorized profile and access token", ->
|
2018-10-30 14:18:42 -04:00
|
|
|
beforeEach ->
|
2019-05-14 05:27:21 -04:00
|
|
|
@oauth_authorize =
|
|
|
|
access_token: "access_token"
|
|
|
|
user_profile: id: "overleaf-id"
|
|
|
|
@V1Api.request = sinon.stub().yields(null, {statusCode: 200}, @oauth_authorize)
|
|
|
|
@req.token = "foo"
|
|
|
|
|
|
|
|
describe "in all cases", ->
|
|
|
|
beforeEach ->
|
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should find user", ->
|
|
|
|
@UserModel.findOne.should.have.been.calledWithMatch { "overleaf.id": "overleaf-id" }
|
|
|
|
|
|
|
|
describe "when user find returns error", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserModel.findOne = sinon.stub().yields("error")
|
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should return error", ->
|
|
|
|
@next.should.have.been.calledWith "error"
|
|
|
|
|
|
|
|
describe "when user is not found", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserModel.findOne = sinon.stub().yields(null, null)
|
|
|
|
@middleware(@req, @res, @next)
|
|
|
|
|
|
|
|
it "should return unauthorized", ->
|
|
|
|
@res.status.should.have.been.calledWith 401
|
|
|
|
|
|
|
|
describe "when user is found", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserModel.findOne = sinon.stub().yields(null, "user")
|
|
|
|
@middleware(@req, @res, @next)
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should add user to request", ->
|
|
|
|
@req.oauth_user.should.equal "user"
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2019-05-14 05:27:21 -04:00
|
|
|
it "should add access_token to request", ->
|
|
|
|
@req.oauth.access_token.should.equal "access_token"
|
2018-10-30 14:18:42 -04:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
describe "requireGlobalLogin", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.headers = {}
|
|
|
|
@AuthenticationController.httpAuth = sinon.stub()
|
2018-11-30 08:03:35 -05:00
|
|
|
@setRedirect = sinon.spy(@AuthenticationController, 'setRedirectInSession')
|
2017-01-10 10:42:36 -05:00
|
|
|
|
|
|
|
afterEach ->
|
2018-11-30 08:03:35 -05:00
|
|
|
@setRedirect.restore()
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
describe "with white listed url", ->
|
|
|
|
beforeEach ->
|
|
|
|
@AuthenticationController.addEndpointToLoginWhitelist "/login"
|
2015-04-30 06:57:40 -04:00
|
|
|
@req._parsedUrl.pathname = "/login"
|
2015-04-15 06:14:38 -04:00
|
|
|
@AuthenticationController.requireGlobalLogin @req, @res, @next
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
it "should call next() directly", ->
|
|
|
|
@next.called.should.equal true
|
2015-04-30 06:57:40 -04:00
|
|
|
|
|
|
|
describe "with white listed url and a query string", ->
|
|
|
|
beforeEach ->
|
|
|
|
@AuthenticationController.addEndpointToLoginWhitelist "/login"
|
|
|
|
@req._parsedUrl.pathname = "/login"
|
|
|
|
@req.url = "/login?query=something"
|
|
|
|
@AuthenticationController.requireGlobalLogin @req, @res, @next
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-04-30 06:57:40 -04:00
|
|
|
it "should call next() directly", ->
|
2016-07-01 10:33:59 -04:00
|
|
|
@next.called.should.equal true
|
2015-04-30 06:57:40 -04:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
describe "with http auth", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.headers["authorization"] = "Mock Basic Auth"
|
|
|
|
@AuthenticationController.requireGlobalLogin @req, @res, @next
|
|
|
|
|
|
|
|
it "should pass the request onto httpAuth", ->
|
|
|
|
@AuthenticationController.httpAuth
|
|
|
|
.calledWith(@req, @res, @next)
|
|
|
|
.should.equal true
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
describe "with a user session", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.session =
|
2016-09-23 11:53:07 -04:00
|
|
|
user: {"mock": "user", "_id": "some_id"}
|
2015-04-15 06:14:38 -04:00
|
|
|
@AuthenticationController.requireGlobalLogin @req, @res, @next
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
it "should call next() directly", ->
|
|
|
|
@next.called.should.equal true
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
describe "with no login credentials", ->
|
|
|
|
beforeEach ->
|
2016-09-22 11:58:25 -04:00
|
|
|
@req.session = {}
|
2015-04-15 06:14:38 -04:00
|
|
|
@AuthenticationController.requireGlobalLogin @req, @res, @next
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2017-01-10 10:42:36 -05:00
|
|
|
it 'should have called setRedirectInSession', ->
|
2018-11-30 08:03:35 -05:00
|
|
|
@setRedirect.callCount.should.equal 1
|
2017-01-10 10:42:36 -05:00
|
|
|
|
2015-04-15 06:14:38 -04:00
|
|
|
it "should redirect to the /login page", ->
|
|
|
|
@res.redirectedTo.should.equal "/login"
|
|
|
|
|
2015-02-13 06:18:17 -05:00
|
|
|
describe "_redirectToLoginOrRegisterPage", ->
|
|
|
|
beforeEach ->
|
|
|
|
@middleware = @AuthenticationController.requireLogin(@options = { load_from_db: false })
|
|
|
|
@req.session = {}
|
|
|
|
@AuthenticationController._redirectToRegisterPage = sinon.stub()
|
|
|
|
@AuthenticationController._redirectToLoginPage = sinon.stub()
|
|
|
|
@req.query = {}
|
|
|
|
|
|
|
|
describe "they have come directly to the url", ->
|
2016-07-01 10:33:59 -04:00
|
|
|
beforeEach ->
|
2014-11-13 12:12:39 -05:00
|
|
|
@req.query = {}
|
2015-02-13 06:18:17 -05:00
|
|
|
@middleware(@req, @res, @next)
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2015-02-13 06:18:17 -05:00
|
|
|
it "should redirect to the login page", ->
|
|
|
|
@AuthenticationController._redirectToRegisterPage.calledWith(@req, @res).should.equal false
|
|
|
|
@AuthenticationController._redirectToLoginPage.calledWith(@req, @res).should.equal true
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2015-02-13 06:18:17 -05:00
|
|
|
describe "they have come via a templates link", ->
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2016-07-01 10:33:59 -04:00
|
|
|
beforeEach ->
|
2015-02-13 06:18:17 -05:00
|
|
|
@req.query.zipUrl = "something"
|
|
|
|
@middleware(@req, @res, @next)
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2015-02-13 06:18:17 -05:00
|
|
|
it "should redirect to the register page", ->
|
|
|
|
@AuthenticationController._redirectToRegisterPage.calledWith(@req, @res).should.equal true
|
|
|
|
@AuthenticationController._redirectToLoginPage.calledWith(@req, @res).should.equal false
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2015-02-13 06:18:17 -05:00
|
|
|
describe "they have been invited to a project", ->
|
2016-07-01 10:33:59 -04:00
|
|
|
|
|
|
|
beforeEach ->
|
2015-02-13 06:18:17 -05:00
|
|
|
@req.query.project_name = "something"
|
|
|
|
@middleware(@req, @res, @next)
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2015-02-13 06:18:17 -05:00
|
|
|
it "should redirect to the register page", ->
|
|
|
|
@AuthenticationController._redirectToRegisterPage.calledWith(@req, @res).should.equal true
|
|
|
|
@AuthenticationController._redirectToLoginPage.calledWith(@req, @res).should.equal false
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
describe "_redirectToRegisterPage", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.path = "/target/url"
|
|
|
|
@req.query =
|
|
|
|
extra_query: "foo"
|
|
|
|
@AuthenticationController._redirectToRegisterPage(@req, @res)
|
|
|
|
|
|
|
|
it "should redirect to the register page with a query string attached", ->
|
2016-11-22 09:24:36 -05:00
|
|
|
@req.session.postLoginRedirect.should.equal '/target/url?extra_query=foo'
|
|
|
|
@res.redirectedTo.should.equal "/register?extra_query=foo"
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it "should log out a message", ->
|
|
|
|
@logger.log
|
|
|
|
.calledWith(url: @url, "user not logged in so redirecting to register page")
|
|
|
|
.should.equal true
|
|
|
|
|
2014-11-13 12:12:39 -05:00
|
|
|
describe "_redirectToLoginPage", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.path = "/target/url"
|
|
|
|
@req.query =
|
|
|
|
extra_query: "foo"
|
|
|
|
@AuthenticationController._redirectToLoginPage(@req, @res)
|
|
|
|
|
|
|
|
it "should redirect to the register page with a query string attached", ->
|
2016-11-22 09:24:36 -05:00
|
|
|
@req.session.postLoginRedirect.should.equal '/target/url?extra_query=foo'
|
|
|
|
@res.redirectedTo.should.equal "/login?extra_query=foo"
|
2014-11-13 12:12:39 -05:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe "_recordSuccessfulLogin", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserUpdater.updateUser = sinon.stub().callsArg(2)
|
|
|
|
@AuthenticationController._recordSuccessfulLogin(@user._id, @callback)
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it "should increment the user.login.success metric", ->
|
|
|
|
@Metrics.inc
|
|
|
|
.calledWith("user.login.success")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should update the user's login count and last logged in date", ->
|
|
|
|
@UserUpdater.updateUser.args[0][1]["$set"]["lastLoggedIn"].should.not.equal undefined
|
|
|
|
@UserUpdater.updateUser.args[0][1]["$inc"]["loginCount"].should.equal 1
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "_recordFailedLogin", ->
|
|
|
|
beforeEach ->
|
|
|
|
@AuthenticationController._recordFailedLogin(@callback)
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
it "should increment the user.login.failed metric", ->
|
|
|
|
@Metrics.inc
|
|
|
|
.calledWith("user.login.failed")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
2016-11-22 09:24:36 -05:00
|
|
|
|
|
|
|
|
2018-11-30 08:03:35 -05:00
|
|
|
describe 'setRedirectInSession', ->
|
2016-11-22 09:24:36 -05:00
|
|
|
beforeEach ->
|
|
|
|
@req = {session: {}}
|
|
|
|
@req.path = "/somewhere"
|
|
|
|
@req.query = {one: "1"}
|
|
|
|
|
|
|
|
it 'should set redirect property on session', ->
|
2018-11-30 08:03:35 -05:00
|
|
|
@AuthenticationController.setRedirectInSession(@req)
|
2016-11-22 09:24:36 -05:00
|
|
|
expect(@req.session.postLoginRedirect).to.equal "/somewhere?one=1"
|
|
|
|
|
2016-11-22 11:54:03 -05:00
|
|
|
it 'should set the supplied value', ->
|
2018-11-30 08:03:35 -05:00
|
|
|
@AuthenticationController.setRedirectInSession(@req, '/somewhere/specific')
|
2016-11-22 11:54:03 -05:00
|
|
|
expect(@req.session.postLoginRedirect).to.equal "/somewhere/specific"
|
|
|
|
|
2019-02-12 10:26:20 -05:00
|
|
|
it 'should not allow open redirects', ->
|
|
|
|
@AuthenticationController.setRedirectInSession(@req, 'https://evil.com')
|
|
|
|
expect(@req.session.postLoginRedirect).to.be.undefined
|
|
|
|
|
2017-05-12 10:46:16 -04:00
|
|
|
describe 'with a png', ->
|
|
|
|
beforeEach ->
|
|
|
|
@req = {session: {}}
|
|
|
|
|
|
|
|
it 'should not set the redirect', ->
|
2018-11-30 08:03:35 -05:00
|
|
|
@AuthenticationController.setRedirectInSession(@req, '/something.png')
|
2017-05-12 10:46:16 -04:00
|
|
|
expect(@req.session.postLoginRedirect).to.equal undefined
|
|
|
|
|
2017-01-17 09:35:37 -05:00
|
|
|
describe 'with a js path', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req = {session: {}}
|
|
|
|
|
|
|
|
it 'should not set the redirect', ->
|
2018-11-30 08:03:35 -05:00
|
|
|
@AuthenticationController.setRedirectInSession(@req, '/js/something.js')
|
2017-01-17 09:35:37 -05:00
|
|
|
expect(@req.session.postLoginRedirect).to.equal undefined
|
|
|
|
|
2016-11-22 09:24:36 -05:00
|
|
|
describe '_getRedirectFromSession', ->
|
|
|
|
it 'should get redirect property from session', ->
|
2019-02-12 10:26:20 -05:00
|
|
|
@req = session: { postLoginRedirect: '/a?b=c' }
|
2016-11-22 09:24:36 -05:00
|
|
|
expect(@AuthenticationController._getRedirectFromSession(@req)).to.equal "/a?b=c"
|
|
|
|
|
2019-02-12 10:26:20 -05:00
|
|
|
it 'should not allow open redirects', ->
|
|
|
|
@req = session: { postLoginRedirect: 'https://evil.com' }
|
|
|
|
expect(@AuthenticationController._getRedirectFromSession(@req)).to.be.null
|
|
|
|
|
|
|
|
it 'handle null values', ->
|
|
|
|
@req = session: {}
|
|
|
|
expect(@AuthenticationController._getRedirectFromSession(@req)).to.be.null
|
|
|
|
|
|
|
|
describe '_getSafeRedirectPath', ->
|
|
|
|
it 'sanitize redirect path to prevent open redirects', ->
|
|
|
|
expect(
|
|
|
|
@AuthenticationController._getSafeRedirectPath('https://evil.com')
|
|
|
|
).to.be.undefined
|
|
|
|
|
|
|
|
expect(
|
|
|
|
@AuthenticationController._getSafeRedirectPath('//evil.com')
|
|
|
|
).to.be.undefined
|
|
|
|
|
|
|
|
expect(
|
|
|
|
@AuthenticationController._getSafeRedirectPath('//ol.com/evil')
|
|
|
|
).to.equal '/evil'
|
|
|
|
|
|
|
|
expect(
|
|
|
|
@AuthenticationController._getSafeRedirectPath('////evil.com')
|
|
|
|
).to.be.undefined
|
|
|
|
|
|
|
|
expect(
|
|
|
|
@AuthenticationController._getSafeRedirectPath('%2F%2Fevil.com')
|
|
|
|
).to.equal '/%2F%2Fevil.com'
|
|
|
|
|
|
|
|
expect(
|
|
|
|
@AuthenticationController._getSafeRedirectPath('.evil.com')
|
|
|
|
).to.equal '/.evil.com'
|
|
|
|
|
2016-11-22 09:24:36 -05:00
|
|
|
describe '_clearRedirectFromSession', ->
|
|
|
|
beforeEach ->
|
|
|
|
@req = {session: {postLoginRedirect: "/a?b=c"}}
|
|
|
|
|
|
|
|
it 'should remove the redirect property from session', ->
|
|
|
|
@AuthenticationController._clearRedirectFromSession(@req)
|
|
|
|
expect(@req.session.postLoginRedirect).to.equal undefined
|
|
|
|
|
2018-07-18 07:08:34 -04:00
|
|
|
|
|
|
|
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()
|
2019-04-17 10:00:13 -04:00
|
|
|
@AuthenticationController._redirectToReconfirmPage = sinon.stub()
|
2018-07-18 07:08:34 -04:00
|
|
|
@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
|
2019-04-17 10:00:13 -04:00
|
|
|
|
|
|
|
describe "when user is flagged to reconfirm", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req.session = {}
|
|
|
|
@user.must_reconfirm = true
|
|
|
|
it "should redirect to reconfirm page", () ->
|
|
|
|
@AuthenticationController.finishLogin(@user, @req, @res, @next)
|
|
|
|
expect(@AuthenticationController._redirectToReconfirmPage.calledWith(@req)).to.equal true
|