Merge pull request #4338 from overleaf/ab-session-manager

Extract functions from AuthenticationController to SessionManager

GitOrigin-RevId: 86870ce03a762e1a837dcf493759e8851e759883
This commit is contained in:
Alexandre Bourdin
2021-07-28 12:36:22 +00:00
committed by Copybot
parent 7e61fc4035
commit 9468e5cb4f
66 changed files with 460 additions and 458 deletions
@@ -8,7 +8,7 @@ const sinon = require('sinon')
describe('AnalyticsController', function () {
beforeEach(function () {
this.AuthenticationController = { getLoggedInUserId: sinon.stub() }
this.SessionManager = { getLoggedInUserId: sinon.stub() }
this.AnalyticsManager = {
updateEditingSession: sinon.stub(),
@@ -22,8 +22,7 @@ describe('AnalyticsController', function () {
this.controller = SandboxedModule.require(modulePath, {
requires: {
'./AnalyticsManager': this.AnalyticsManager,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../../infrastructure/Features': this.Features,
'../../infrastructure/GeoIpLookup': (this.GeoIpLookup = {
getDetails: sinon.stub(),
@@ -50,7 +49,7 @@ describe('AnalyticsController', function () {
})
it('delegates to the AnalyticsManager', function (done) {
this.AuthenticationController.getLoggedInUserId.returns('1234')
this.SessionManager.getLoggedInUserId.returns('1234')
this.controller.updateEditingSession(this.req, this.res)
this.AnalyticsManager.updateEditingSession
@@ -73,7 +72,7 @@ describe('AnalyticsController', function () {
})
it('should use the user_id', function (done) {
this.AuthenticationController.getLoggedInUserId.returns('1234')
this.SessionManager.getLoggedInUserId.returns('1234')
this.controller.recordEvent(this.req, this.res)
this.AnalyticsManager.recordEvent
.calledWith('1234', this.req.params.event, this.req.body)
@@ -15,6 +15,20 @@ describe('AuthenticationController', function () {
this.httpAuthUsers = {
'valid-test-user': Math.random().toString(16).slice(2),
}
this.user = {
_id: ObjectId(),
email: (this.email = 'USER@example.com'),
first_name: 'bob',
last_name: 'brown',
referal_id: 1234,
isAdmin: false,
}
this.password = 'banana'
this.req = new MockRequest()
this.res = new MockResponse()
this.callback = sinon.stub()
this.next = sinon.stub()
this.AuthenticationController = SandboxedModule.require(modulePath, {
requires: {
'../User/UserAuditLogHandler': (this.UserAuditLogHandler = {
@@ -72,25 +86,16 @@ describe('AuthenticationController', function () {
'../Helpers/UrlHelper': (this.UrlHelper = {
getSafeRedirectPath: sinon.stub(),
}),
'./SessionManager': (this.SessionManager = {
isUserLoggedIn: sinon.stub().returns(true),
getSessionUser: sinon.stub().returns(this.user),
}),
},
})
this.UrlHelper.getSafeRedirectPath
.withArgs('https://evil.com')
.returns(undefined)
this.UrlHelper.getSafeRedirectPath.returnsArg(0)
this.user = {
_id: ObjectId(),
email: (this.email = 'USER@example.com'),
first_name: 'bob',
last_name: 'brown',
referal_id: 1234,
isAdmin: false,
}
this.password = 'banana'
this.req = new MockRequest()
this.res = new MockResponse()
this.callback = sinon.stub()
this.next = sinon.stub()
})
afterEach(function () {
@@ -116,119 +121,53 @@ describe('AuthenticationController', function () {
it('should skip when adminDomains are not configured', function (done) {
this.Settings.adminDomains = []
this.AuthenticationController.getSessionUser = sinon
.stub()
.returns(this.normalUser)
this.SessionManager.getSessionUser = sinon.stub().returns(this.normalUser)
this.AuthenticationController.validateAdmin(this.req, this.res, err => {
this.AuthenticationController.getSessionUser.called.should.equal(false)
this.SessionManager.getSessionUser.called.should.equal(false)
expect(err).to.not.exist
done()
})
})
it('should skip non-admin user', function (done) {
this.AuthenticationController.getSessionUser = sinon
.stub()
.returns(this.normalUser)
this.SessionManager.getSessionUser = sinon.stub().returns(this.normalUser)
this.AuthenticationController.validateAdmin(this.req, this.res, err => {
this.AuthenticationController.getSessionUser.called.should.equal(true)
this.SessionManager.getSessionUser.called.should.equal(true)
expect(err).to.not.exist
done()
})
})
it('should permit an admin with the right doman', function (done) {
this.AuthenticationController.getSessionUser = sinon
.stub()
.returns(this.goodAdmin)
this.SessionManager.getSessionUser = sinon.stub().returns(this.goodAdmin)
this.AuthenticationController.validateAdmin(this.req, this.res, err => {
this.AuthenticationController.getSessionUser.called.should.equal(true)
this.SessionManager.getSessionUser.called.should.equal(true)
expect(err).to.not.exist
done()
})
})
it('should block an admin with a missing email', function (done) {
this.AuthenticationController.getSessionUser = sinon
this.SessionManager.getSessionUser = sinon
.stub()
.returns({ isAdmin: true })
this.AuthenticationController.validateAdmin(this.req, this.res, err => {
this.AuthenticationController.getSessionUser.called.should.equal(true)
this.SessionManager.getSessionUser.called.should.equal(true)
expect(err).to.exist
done()
})
})
it('should block an admin with a bad domain', function (done) {
this.AuthenticationController.getSessionUser = sinon
.stub()
.returns(this.badAdmin)
this.SessionManager.getSessionUser = sinon.stub().returns(this.badAdmin)
this.AuthenticationController.validateAdmin(this.req, this.res, err => {
this.AuthenticationController.getSessionUser.called.should.equal(true)
this.SessionManager.getSessionUser.called.should.equal(true)
expect(err).to.exist
done()
})
})
})
describe('isUserLoggedIn', function () {
beforeEach(function () {
this.stub = sinon.stub(this.AuthenticationController, 'getLoggedInUserId')
})
afterEach(function () {
this.stub.restore()
})
it('should do the right thing in all cases', function () {
this.AuthenticationController.getLoggedInUserId.returns('some_id')
expect(this.AuthenticationController.isUserLoggedIn(this.req)).to.equal(
true
)
this.AuthenticationController.getLoggedInUserId.returns(null)
expect(this.AuthenticationController.isUserLoggedIn(this.req)).to.equal(
false
)
this.AuthenticationController.getLoggedInUserId.returns(false)
expect(this.AuthenticationController.isUserLoggedIn(this.req)).to.equal(
false
)
this.AuthenticationController.getLoggedInUserId.returns(undefined)
expect(this.AuthenticationController.isUserLoggedIn(this.req)).to.equal(
false
)
})
})
describe('setInSessionUser', function () {
beforeEach(function () {
this.user = {
_id: 'id',
first_name: 'a',
last_name: 'b',
email: 'c',
}
this.AuthenticationController.getSessionUser = sinon
.stub()
.returns(this.user)
})
it('should update the right properties', function () {
this.AuthenticationController.setInSessionUser(this.req, {
first_name: 'new_first_name',
email: 'new_email',
})
const expectedUser = {
_id: 'id',
first_name: 'new_first_name',
last_name: 'b',
email: 'new_email',
}
expect(this.user).to.deep.equal(expectedUser)
expect(this.user).to.deep.equal(expectedUser)
})
})
describe('passportLogin', function () {
beforeEach(function () {
this.info = null
@@ -444,49 +383,6 @@ describe('AuthenticationController', function () {
})
})
describe('getLoggedInUserId', function () {
beforeEach(function () {
this.req = { session: {} }
})
it('should return the user id from the session', function () {
this.user_id = '2134'
this.req.session.user = { _id: this.user_id }
const result = this.AuthenticationController.getLoggedInUserId(this.req)
expect(result).to.equal(this.user_id)
})
it('should return user for passport session', function () {
this.user_id = '2134'
this.req.session = {
passport: {
user: {
_id: this.user_id,
},
},
}
const result = this.AuthenticationController.getLoggedInUserId(this.req)
expect(result).to.equal(this.user_id)
})
it('should return null if there is no user on the session', function () {
const result = this.AuthenticationController.getLoggedInUserId(this.req)
expect(result).to.equal(null)
})
it('should return null if there is no session', function () {
this.req = {}
const result = this.AuthenticationController.getLoggedInUserId(this.req)
expect(result).to.equal(null)
})
it('should return null if there is no req', function () {
this.req = {}
const result = this.AuthenticationController.getLoggedInUserId(this.req)
expect(result).to.equal(null)
})
})
describe('requireLogin', function () {
beforeEach(function () {
this.user = {
@@ -517,6 +413,7 @@ describe('AuthenticationController', function () {
this.req.session = {}
this.AuthenticationController._redirectToLoginOrRegisterPage = sinon.stub()
this.req.query = {}
this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
this.middleware(this.req, this.res, this.next)
})
@@ -712,6 +609,7 @@ describe('AuthenticationController', function () {
describe('with no login credentials', function () {
beforeEach(function () {
this.req.session = {}
this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
this.AuthenticationController.requireGlobalLogin(
this.req,
this.res,
@@ -815,6 +713,7 @@ describe('AuthenticationController', function () {
describe('they have come directly to the url', function () {
beforeEach(function () {
this.req.query = {}
this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
this.middleware(this.req, this.res, this.next)
})
@@ -831,6 +730,7 @@ describe('AuthenticationController', function () {
describe('they have come via a templates link', function () {
beforeEach(function () {
this.req.query.zipUrl = 'something'
this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
this.middleware(this.req, this.res, this.next)
})
@@ -847,6 +747,7 @@ describe('AuthenticationController', function () {
describe('they have been invited to a project', function () {
beforeEach(function () {
this.req.query.project_name = 'something'
this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
this.middleware(this.req, this.res, this.next)
})
@@ -0,0 +1,114 @@
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath =
'../../../../app/src/Features/Authentication/SessionManager.js'
const SandboxedModule = require('sandboxed-module')
const tk = require('timekeeper')
const { ObjectId } = require('mongodb')
describe('SessionManager', function () {
beforeEach(function () {
this.UserModel = { findOne: sinon.stub() }
this.SessionManager = SandboxedModule.require(modulePath, {
requires: {},
})
this.user = {
_id: ObjectId(),
email: (this.email = 'USER@example.com'),
first_name: 'bob',
last_name: 'brown',
referal_id: 1234,
isAdmin: false,
}
this.session = sinon.stub()
})
afterEach(function () {
tk.reset()
})
describe('isUserLoggedIn', function () {
beforeEach(function () {
this.stub = sinon.stub(this.SessionManager, 'getLoggedInUserId')
})
afterEach(function () {
this.stub.restore()
})
it('should do the right thing in all cases', function () {
this.SessionManager.getLoggedInUserId.returns('some_id')
expect(this.SessionManager.isUserLoggedIn(this.session)).to.equal(true)
this.SessionManager.getLoggedInUserId.returns(null)
expect(this.SessionManager.isUserLoggedIn(this.session)).to.equal(false)
this.SessionManager.getLoggedInUserId.returns(false)
expect(this.SessionManager.isUserLoggedIn(this.session)).to.equal(false)
this.SessionManager.getLoggedInUserId.returns(undefined)
expect(this.SessionManager.isUserLoggedIn(this.session)).to.equal(false)
})
})
describe('setInSessionUser', function () {
beforeEach(function () {
this.user = {
_id: 'id',
first_name: 'a',
last_name: 'b',
email: 'c',
}
this.SessionManager.getSessionUser = sinon.stub().returns(this.user)
})
it('should update the right properties', function () {
this.SessionManager.setInSessionUser(this.session, {
first_name: 'new_first_name',
email: 'new_email',
})
const expectedUser = {
_id: 'id',
first_name: 'new_first_name',
last_name: 'b',
email: 'new_email',
}
expect(this.user).to.deep.equal(expectedUser)
expect(this.user).to.deep.equal(expectedUser)
})
})
describe('getLoggedInUserId', function () {
beforeEach(function () {
this.req = { session: {} }
})
it('should return the user id from the session', function () {
this.user_id = '2134'
this.session.user = { _id: this.user_id }
const result = this.SessionManager.getLoggedInUserId(this.session)
expect(result).to.equal(this.user_id)
})
it('should return user for passport session', function () {
this.user_id = '2134'
this.session = {
passport: {
user: {
_id: this.user_id,
},
},
}
const result = this.SessionManager.getLoggedInUserId(this.session)
expect(result).to.equal(this.user_id)
})
it('should return null if there is no user on the session', function () {
this.session = {}
const result = this.SessionManager.getLoggedInUserId(this.session)
expect(result).to.equal(null)
})
it('should return null if there is no session', function () {
const result = this.SessionManager.getLoggedInUserId(undefined)
expect(result).to.equal(null)
})
})
})
@@ -11,7 +11,8 @@ describe('AuthorizationMiddleware', function () {
this.userId = 'user-id-123'
this.project_id = 'project-id-123'
this.token = 'some-token'
this.AuthenticationController = {
this.AuthenticationController = {}
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns(this.userId),
isUserLoggedIn: sinon.stub().returns(true),
}
@@ -35,6 +36,7 @@ describe('AuthorizationMiddleware', function () {
'../Errors/HttpErrorHandler': this.HttpErrorHandler,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
},
})
@@ -49,9 +51,7 @@ describe('AuthorizationMiddleware', function () {
})
it('should get the user from session', function (done) {
this.AuthenticationController.getLoggedInUserId = sinon
.stub()
.returns('1234')
this.SessionManager.getLoggedInUserId = sinon.stub().returns('1234')
this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
expect(err).to.not.exist
expect(userId).to.equal('1234')
@@ -60,9 +60,7 @@ describe('AuthorizationMiddleware', function () {
})
it('should get oauth_user from request', function (done) {
this.AuthenticationController.getLoggedInUserId = sinon
.stub()
.returns(null)
this.SessionManager.getLoggedInUserId = sinon.stub().returns(null)
this.req.oauth_user = { _id: '5678' }
this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
expect(err).to.not.exist
@@ -72,9 +70,7 @@ describe('AuthorizationMiddleware', function () {
})
it('should fall back to null', function (done) {
this.AuthenticationController.getLoggedInUserId = sinon
.stub()
.returns(null)
this.SessionManager.getLoggedInUserId = sinon.stub().returns(null)
this.req.oauth_user = undefined
this.AuthorizationMiddleware._getUserId(this.req, (err, userId) => {
expect(err).to.not.exist
@@ -117,7 +113,7 @@ describe('AuthorizationMiddleware', function () {
describe('with logged in user', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
this.SessionManager.getLoggedInUserId.returns(this.userId)
})
describe('when user has permission', function () {
@@ -161,7 +157,7 @@ describe('AuthorizationMiddleware', function () {
describe('with anonymous user', function () {
describe('when user has permission', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager[managerMethod]
.withArgs(null, this.project_id, this.token)
.yields(null, true)
@@ -179,7 +175,7 @@ describe('AuthorizationMiddleware', function () {
describe("when user doesn't have permission", function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager[managerMethod]
.withArgs(null, this.project_id, this.token)
.yields(null, false)
@@ -244,7 +240,7 @@ describe('AuthorizationMiddleware', function () {
describe('with logged in user', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
this.SessionManager.getLoggedInUserId.returns(this.userId)
})
describe('when user has permission', function () {
@@ -284,7 +280,7 @@ describe('AuthorizationMiddleware', function () {
describe('with anonymous user', function () {
describe('when user has permission', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager.canUserAdminProject
.withArgs(null, this.project_id, this.token)
.yields(null, true)
@@ -302,7 +298,7 @@ describe('AuthorizationMiddleware', function () {
describe("when user doesn't have permission", function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager.canUserAdminProject
.withArgs(null, this.project_id, this.token)
.yields(null, false)
@@ -345,7 +341,7 @@ describe('AuthorizationMiddleware', function () {
describe('with logged in user', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
this.SessionManager.getLoggedInUserId.returns(this.userId)
})
describe('when user has permission', function () {
@@ -389,7 +385,7 @@ describe('AuthorizationMiddleware', function () {
describe('with anonymous user', function () {
describe('when user has permission', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager.isUserSiteAdmin
.withArgs(null)
.yields(null, true)
@@ -407,7 +403,7 @@ describe('AuthorizationMiddleware', function () {
describe("when user doesn't have permission", function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager.isUserSiteAdmin
.withArgs(null)
.yields(null, false)
@@ -486,7 +482,7 @@ describe('AuthorizationMiddleware', function () {
describe('with logged in user', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
this.SessionManager.getLoggedInUserId.returns(this.userId)
})
describe('when user has permission to access all projects', function () {
@@ -537,7 +533,7 @@ describe('AuthorizationMiddleware', function () {
describe('when user has permission', function () {
describe('when user has permission to access all projects', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager.canUserReadProject
.withArgs(null, 'project1', this.token)
.yields(null, true)
@@ -558,7 +554,7 @@ describe('AuthorizationMiddleware', function () {
describe("when user doesn't have permission to access one of the projects", function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId.returns(null)
this.SessionManager.getLoggedInUserId.returns(null)
this.AuthorizationManager.canUserReadProject
.withArgs(null, 'project1', this.token)
.yields(null, true)
@@ -28,7 +28,7 @@ describe('ChatController', function () {
this.settings = {}
this.ChatApiHandler = {}
this.EditorRealTimeController = { emitToRoom: sinon.stub() }
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns(this.user_id),
}
this.ChatController = SandboxedModule.require(modulePath, {
@@ -36,8 +36,7 @@ describe('ChatController', function () {
'@overleaf/settings': this.settings,
'./ChatApiHandler': this.ChatApiHandler,
'../Editor/EditorRealTimeController': this.EditorRealTimeController,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../User/UserInfoManager': (this.UserInfoManager = {}),
'../User/UserInfoController': (this.UserInfoController = {}),
},
@@ -41,7 +41,7 @@ describe('CollaboratorsController', function () {
removeProjectFromAllTags: sinon.stub().resolves(),
},
}
this.AuthenticationController = {
this.SessionManager = {
getSessionUser: sinon.stub().returns(this.user),
getLoggedInUserId: sinon.stub().returns(this.user._id),
}
@@ -60,8 +60,7 @@ describe('CollaboratorsController', function () {
'../Editor/EditorRealTimeController': this.EditorRealTimeController,
'../../Features/Errors/HttpErrorHandler': this.HttpErrorHandler,
'../Tags/TagsHandler': this.TagsHandler,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
},
})
})
@@ -51,7 +51,7 @@ describe('CompileController', function () {
this.ClsiCookieManager = {
getCookieJar: sinon.stub().callsArgWith(1, null, this.jar),
}
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
getLoggedInUserId: sinon.stub().returns(this.user_id),
getSessionUser: sinon.stub().returns(this.user),
@@ -66,8 +66,7 @@ describe('CompileController', function () {
'./CompileManager': this.CompileManager,
'../User/UserGetter': this.UserGetter,
'./ClsiManager': this.ClsiManager,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../../infrastructure/RateLimiter': this.RateLimiter,
'./ClsiCookieManager': () => this.ClsiCookieManager,
},
@@ -98,8 +97,8 @@ describe('CompileController', function () {
})
it('should look up the user id', function () {
return this.AuthenticationController.getLoggedInUserId
.calledWith(this.req)
return this.SessionManager.getLoggedInUserId
.calledWith(this.req.session)
.should.equal(true)
})
@@ -18,15 +18,13 @@ const SandboxedModule = require('sandboxed-module')
describe('ContactController', function () {
beforeEach(function () {
this.AuthenticationController = { getLoggedInUserId: sinon.stub() }
this.SessionManager = { getLoggedInUserId: sinon.stub() }
this.ContactController = SandboxedModule.require(modulePath, {
requires: {
'../User/UserGetter': (this.UserGetter = {}),
'./ContactManager': (this.ContactManager = {}),
'../Authentication/AuthenticationController': (this.AuthenticationController = {}),
'../Authentication/SessionManager': (this.SessionManager = {}),
'../../infrastructure/Modules': (this.Modules = { hooks: {} }),
'../Authentication/AuthenticationController': this
.AuthenticationController,
},
})
@@ -65,9 +63,7 @@ describe('ContactController', function () {
unsued: 'foo',
},
]
this.AuthenticationController.getLoggedInUserId = sinon
.stub()
.returns(this.user_id)
this.SessionManager.getLoggedInUserId = sinon.stub().returns(this.user_id)
this.ContactManager.getContactIds = sinon
.stub()
.callsArgWith(2, null, this.contact_ids)
@@ -80,8 +76,8 @@ describe('ContactController', function () {
})
it('should look up the logged in user id', function () {
return this.AuthenticationController.getLoggedInUserId
.calledWith(this.req)
return this.SessionManager.getLoggedInUserId
.calledWith(this.req.session)
.should.equal(true)
})
@@ -111,7 +111,7 @@ describe('EditorHttpController', function () {
getRequestToken: sinon.stub().returns(this.token),
protectTokens: sinon.stub(),
}
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns(this.user._id),
}
this.ProjectEntityUpdateHandler = {
@@ -141,8 +141,7 @@ describe('EditorHttpController', function () {
'../Collaborators/CollaboratorsInviteHandler': this
.CollaboratorsInviteHandler,
'../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../../infrastructure/FileWriter': this.FileWriter,
'../Project/ProjectEntityUpdateHandler': this
.ProjectEntityUpdateHandler,
@@ -20,15 +20,14 @@ describe('HistoryController', function () {
beforeEach(function () {
this.callback = sinon.stub()
this.user_id = 'user-id-123'
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns(this.user_id),
}
this.HistoryController = SandboxedModule.require(modulePath, {
requires: {
request: (this.request = sinon.stub()),
'@overleaf/settings': (this.settings = {}),
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'./HistoryManager': (this.HistoryManager = {}),
'../Project/ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
'../Project/ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {}),
@@ -117,8 +116,8 @@ describe('HistoryController', function () {
})
it('should get the user id', function () {
return this.AuthenticationController.getLoggedInUserId
.calledWith(this.req)
return this.SessionManager.getLoggedInUserId
.calledWith(this.req.session)
.should.equal(true)
})
@@ -150,8 +149,8 @@ describe('HistoryController', function () {
})
it('should get the user id', function () {
return this.AuthenticationController.getLoggedInUserId
.calledWith(this.req)
return this.SessionManager.getLoggedInUserId
.calledWith(this.req.session)
.should.equal(true)
})
@@ -209,8 +208,8 @@ describe('HistoryController', function () {
})
it('should get the user id', function () {
return this.AuthenticationController.getLoggedInUserId
.calledWith(this.req)
return this.SessionManager.getLoggedInUserId
.calledWith(this.req.session)
.should.equal(true)
})
@@ -249,8 +248,8 @@ describe('HistoryController', function () {
})
it('should get the user id', function () {
return this.AuthenticationController.getLoggedInUserId
.calledWith(this.req)
return this.SessionManager.getLoggedInUserId
.calledWith(this.req.session)
.should.equal(true)
})
@@ -76,7 +76,7 @@ describe('ProjectController', function () {
isArchivedOrTrashed: sinon.stub(),
getAllowedImagesForUser: sinon.stub().returns([]),
}
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
getLoggedInUserId: sinon.stub().returns(this.user._id),
getSessionUser: sinon.stub().returns(this.user),
@@ -153,8 +153,7 @@ describe('ProjectController', function () {
'./ProjectUpdateHandler': this.ProjectUpdateHandler,
'./ProjectGetter': this.ProjectGetter,
'./ProjectDetailsHandler': this.ProjectDetailsHandler,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
'../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
'./ProjectEntityHandler': this.ProjectEntityHandler,
@@ -1220,9 +1219,7 @@ describe('ProjectController', function () {
function tagAnonymous() {
beforeEach(function () {
this.AuthenticationController.isUserLoggedIn = sinon
.stub()
.returns(false)
this.SessionManager.isUserLoggedIn = sinon.stub().returns(false)
})
}
@@ -1563,7 +1560,7 @@ describe('ProjectController', function () {
.stub()
.callsArgWith(2, null, [])
this.ProjectController._buildProjectList = sinon.stub().returns(projects)
this.AuthenticationController.getLoggedInUserId = sinon
this.SessionManager.getLoggedInUserId = sinon
.stub()
.returns(this.user._id)
done()
@@ -1585,9 +1582,7 @@ describe('ProjectController', function () {
describe('projectEntitiesJson', function () {
beforeEach(function () {
this.AuthenticationController.getLoggedInUserId = sinon
.stub()
.returns('abc')
this.SessionManager.getLoggedInUserId = sinon.stub().returns('abc')
this.req.params = { Project_id: 'abcd' }
this.project = { _id: 'abcd' }
this.docs = [
@@ -20,7 +20,7 @@ const modulePath = require('path').join(
describe('RateLimiterMiddleware', function () {
beforeEach(function () {
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUserId: () => {
return __guard__(
__guard__(
@@ -36,8 +36,7 @@ describe('RateLimiterMiddleware', function () {
'@overleaf/settings': (this.settings = {}),
'../../infrastructure/RateLimiter': (this.RateLimiter = {}),
'./LoginRateLimiter': {},
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
},
})
this.req = { params: {} }
@@ -47,7 +47,7 @@ describe('SubscriptionController', function () {
this.activeRecurlySubscription =
mockSubscriptions['subscription-123-active']
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUser: sinon.stub().callsArgWith(1, null, this.user),
getLoggedInUserId: sinon.stub().returns(this.user._id),
getSessionUser: sinon.stub().returns(this.user),
@@ -121,8 +121,7 @@ describe('SubscriptionController', function () {
}
this.SubscriptionController = SandboxedModule.require(modulePath, {
requires: {
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'./SubscriptionHandler': this.SubscriptionHandler,
'./PlansLocator': this.PlansLocator,
'./SubscriptionViewModelBuilder': this.SubscriptionViewModelBuilder,
@@ -46,12 +46,12 @@ describe('SubscriptionGroupController', function () {
getSubscription: sinon.stub().callsArgWith(1, null, this.subscription),
}
this.AuthenticationController = {
getLoggedInUserId(req) {
return req.session.user._id
this.SessionManager = {
getLoggedInUserId(session) {
return session.user._id
},
getSessionUser(req) {
return req.session.user
getSessionUser(session) {
return session.user
},
}
@@ -59,8 +59,7 @@ describe('SubscriptionGroupController', function () {
requires: {
'./SubscriptionGroupHandler': this.GroupHandler,
'./SubscriptionLocator': this.SubscriptionLocator,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
},
}))
})
@@ -32,16 +32,15 @@ describe('TagsController', function () {
renameTag: sinon.stub().callsArg(3),
createTag: sinon.stub(),
}
this.AuthenticationController = {
getLoggedInUserId: req => {
return req.session.user._id
this.SessionManager = {
getLoggedInUserId: session => {
return session.user._id
},
}
this.controller = SandboxedModule.require(modulePath, {
requires: {
'./TagsHandler': this.handler,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
},
})
this.req = {
@@ -68,7 +68,7 @@ describe('TemplatesManager', function () {
'../Project/ProjectOptionsHandler': this.ProjectOptionsHandler,
'../Project/ProjectRootDocManager': this.ProjectRootDocManager,
'../Project/ProjectDetailsHandler': this.ProjectDetailsHandler,
'../Authentication/AuthenticationController': (this.AuthenticationController = {
'../Authentication/SessionManager': (this.SessionManager = {
getLoggedInUserId: sinon.stub(),
}),
'../../infrastructure/FileWriter': this.FileWriter,
@@ -9,7 +9,7 @@ const modulePath = require('path').join(
describe('TpdsController', function () {
beforeEach(function () {
this.TpdsUpdateHandler = {}
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns('user-id'),
}
this.TpdsQueueManager = {
@@ -24,8 +24,7 @@ describe('TpdsController', function () {
'../Notifications/NotificationsBuilder': (this.NotificationsBuilder = {
tpdsFileLimit: sinon.stub().returns({ create: sinon.stub() }),
}),
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'./TpdsQueueManager': this.TpdsQueueManager,
'@overleaf/metrics': {
inc() {},
@@ -271,8 +270,7 @@ describe('TpdsController', function () {
})
it('should use userId from session', function () {
this.AuthenticationController.getLoggedInUserId.should.have.been
.calledOnce
this.SessionManager.getLoggedInUserId.should.have.been.calledOnce
this.TpdsQueueManager.promises.getQueues.should.have.been.calledWith(
'user-id'
)
@@ -37,7 +37,7 @@ describe('ProjectUploadController', function () {
return Timer
})()),
}
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns(this.user_id),
}
@@ -48,8 +48,7 @@ describe('ProjectUploadController', function () {
'./ProjectUploadManager': (this.ProjectUploadManager = {}),
'./FileSystemImportManager': (this.FileSystemImportManager = {}),
'@overleaf/metrics': this.metrics,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'./ArchiveErrors': ArchiveErrors,
fs: (this.fs = {}),
},
@@ -44,6 +44,8 @@ describe('UserController', function () {
this.UserRegistrationHandler = { registerNewUser: sinon.stub() }
this.AuthenticationController = {
establishUserSession: sinon.stub().callsArg(2),
}
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns(this.user._id),
getSessionUser: sinon.stub().returns(this.req.session.user),
setInSessionUser: sinon.stub(),
@@ -102,6 +104,7 @@ describe('UserController', function () {
'./UserRegistrationHandler': this.UserRegistrationHandler,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../Authentication/AuthenticationManager': this.AuthenticationManager,
'../../infrastructure/Features': (this.Features = {
hasFeature: sinon.stub(),
@@ -142,7 +145,7 @@ describe('UserController', function () {
this.req.body.password = 'wat'
this.req.logout = sinon.stub()
this.req.session.destroy = sinon.stub().callsArgWith(0, null)
this.AuthenticationController.getLoggedInUserId = sinon
this.SessionManager.getLoggedInUserId = sinon
.stub()
.returns(this.user._id)
this.AuthenticationManager.authenticate = sinon
@@ -397,8 +400,8 @@ describe('UserController', function () {
}
this.res.sendStatus = code => {
code.should.equal(200)
this.AuthenticationController.setInSessionUser
.calledWith(this.req, {
this.SessionManager.setInSessionUser
.calledWith(this.req.session, {
email: this.newEmail,
first_name: undefined,
last_name: undefined,
@@ -23,7 +23,7 @@ describe('UserEmailsController', function () {
getUser: sinon.stub().resolves(this.user),
},
}
this.AuthenticationController = {
this.SessionManager = {
getSessionUser: sinon.stub().returns(this.user),
getLoggedInUserId: sinon.stub().returns(this.user._id),
setInSessionUser: sinon.stub(),
@@ -51,8 +51,7 @@ describe('UserEmailsController', function () {
this.HttpErrorHandler = { conflict: sinon.stub() }
this.UserEmailsController = SandboxedModule.require(modulePath, {
requires: {
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'../../infrastructure/Features': this.Features,
'./UserSessionsManager': this.UserSessionsManager,
'./UserGetter': this.UserGetter,
@@ -274,7 +273,7 @@ describe('UserEmailsController', function () {
this.email = 'email_to_set_default@bar.com'
this.req.body.email = this.email
this.EmailHelper.parseEmail.returns(this.email)
this.AuthenticationController.setInSessionUser.returns(null)
this.SessionManager.setInSessionUser.returns(null)
})
it('sets default email', function (done) {
@@ -285,9 +284,11 @@ describe('UserEmailsController', function () {
code.should.equal(200)
assertCalledWith(this.EmailHelper.parseEmail, this.email)
assertCalledWith(
this.AuthenticationController.setInSessionUser,
this.req,
{ email: this.email }
this.SessionManager.setInSessionUser,
this.req.session,
{
email: this.email,
}
)
assertCalledWith(
this.UserUpdater.setDefaultEmailAddress,
@@ -31,7 +31,7 @@ describe('UserInfoController', function () {
'./UserGetter': this.UserGetter,
'./UserUpdater': this.UserUpdater,
'./UserDeleter': this.UserDeleter,
'../Authentication/AuthenticationController': (this.AuthenticationController = {
'../Authentication/SessionManager': (this.SessionManager = {
getLoggedInUserId: sinon.stub(),
}),
},
@@ -49,7 +49,7 @@ describe('UserInfoController', function () {
this.req.session.user = this.user
this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
this.AuthenticationController.getLoggedInUserId = sinon
this.SessionManager.getLoggedInUserId = sinon
.stub()
.returns(this.user._id)
return this.UserInfoController.getLoggedInUsersPersonalInfo(
@@ -47,9 +47,11 @@ describe('UserPagesController', function () {
this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
this.dropboxStatus = {}
this.ErrorController = { notFound: sinon.stub() }
this.AuthenticationController = {
this.SessionManager = {
getLoggedInUserId: sinon.stub().returns(this.user._id),
getSessionUser: sinon.stub().returns(this.user),
}
this.AuthenticationController = {
_getRedirectFromSession: sinon.stub(),
setRedirectInSession: sinon.stub(),
}
@@ -61,6 +63,7 @@ describe('UserPagesController', function () {
'../Errors/ErrorController': this.ErrorController,
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
request: (this.request = sinon.stub()),
},
})
@@ -54,7 +54,7 @@ describe('UserMembershipController', function () {
},
]
this.AuthenticationController = {
this.SessionManager = {
getSessionUser: sinon.stub().returns(this.user),
getLoggedInUserId: sinon.stub().returns(this.user._id),
}
@@ -69,8 +69,7 @@ describe('UserMembershipController', function () {
modulePath,
{
requires: {
'../Authentication/AuthenticationController': this
.AuthenticationController,
'../Authentication/SessionManager': this.SessionManager,
'./UserMembershipHandler': this.UserMembershipHandler,
},
}