2019-05-29 05:21:06 -04:00
|
|
|
const sinon = require('sinon')
|
2021-03-31 08:20:55 -04:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath = '../../../../app/src/Features/User/UserController.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2019-07-19 05:40:23 -04:00
|
|
|
const OError = require('@overleaf/o-error')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserController', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user_id = '323123'
|
|
|
|
|
|
|
|
this.user = {
|
|
|
|
_id: this.user_id,
|
2020-08-10 09:57:39 -04:00
|
|
|
email: 'email@overleaf.com',
|
2019-05-29 05:21:06 -04:00
|
|
|
save: sinon.stub().callsArgWith(0),
|
2021-04-27 03:52:58 -04:00
|
|
|
ace: {},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.req = {
|
|
|
|
user: {},
|
|
|
|
session: {
|
|
|
|
destroy() {},
|
|
|
|
user: {
|
|
|
|
_id: this.user_id,
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'old@something.com',
|
|
|
|
},
|
2021-09-10 04:30:01 -04:00
|
|
|
analyticsId: this.user_id,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2020-02-20 11:08:18 -05:00
|
|
|
sessionID: '123',
|
2019-06-14 12:31:46 -04:00
|
|
|
body: {},
|
|
|
|
i18n: {
|
2021-04-27 03:52:58 -04:00
|
|
|
translate: text => text,
|
2020-03-02 07:32:12 -05:00
|
|
|
},
|
2020-08-10 09:57:39 -04:00
|
|
|
ip: '0:0:0:0',
|
2021-04-27 03:52:58 -04:00
|
|
|
query: {},
|
2021-09-16 04:14:24 -04:00
|
|
|
headers: {},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-07-18 10:18:56 -04:00
|
|
|
this.UserDeleter = { deleteUser: sinon.stub().yields() }
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserGetter = {
|
|
|
|
getUser: sinon.stub().callsArgWith(1, null, this.user),
|
2021-04-27 03:52:58 -04:00
|
|
|
promises: { getUser: sinon.stub().resolves(this.user) },
|
2020-03-02 07:32:12 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.User = { findById: sinon.stub().callsArgWith(1, null, this.user) }
|
|
|
|
this.NewsLetterManager = { unsubscribe: sinon.stub().callsArgWith(1) }
|
|
|
|
this.UserRegistrationHandler = { registerNewUser: sinon.stub() }
|
|
|
|
this.AuthenticationController = {
|
|
|
|
establishUserSession: sinon.stub().callsArg(2),
|
2021-07-28 04:51:20 -04:00
|
|
|
}
|
|
|
|
this.SessionManager = {
|
2019-05-29 05:21:06 -04:00
|
|
|
getLoggedInUserId: sinon.stub().returns(this.user._id),
|
|
|
|
getSessionUser: sinon.stub().returns(this.req.session.user),
|
2021-04-27 03:52:58 -04:00
|
|
|
setInSessionUser: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.AuthenticationManager = {
|
|
|
|
authenticate: sinon.stub(),
|
2020-08-10 09:57:39 -04:00
|
|
|
validatePassword: sinon.stub(),
|
|
|
|
promises: {
|
|
|
|
authenticate: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
setUserPassword: sinon.stub(),
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserUpdater = {
|
|
|
|
changeEmailAddress: sinon.stub(),
|
|
|
|
promises: {
|
|
|
|
confirmEmail: sinon.stub().resolves(),
|
2021-04-27 03:52:58 -04:00
|
|
|
addAffiliationForNewUser: sinon.stub().resolves(),
|
|
|
|
},
|
2020-03-02 07:32:12 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.settings = { siteUrl: 'sharelatex.example.com' }
|
|
|
|
this.UserHandler = { populateTeamInvites: sinon.stub().callsArgWith(1) }
|
|
|
|
this.UserSessionsManager = {
|
|
|
|
trackSession: sinon.stub(),
|
|
|
|
untrackSession: sinon.stub(),
|
2020-07-27 10:33:57 -04:00
|
|
|
revokeAllUserSessions: sinon.stub().callsArgWith(2, null),
|
|
|
|
promises: {
|
2021-10-20 15:03:18 -04:00
|
|
|
getAllUserSessions: sinon.stub().resolves(),
|
2021-04-27 03:52:58 -04:00
|
|
|
revokeAllUserSessions: sinon.stub().resolves(),
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-07-16 02:47:46 -04:00
|
|
|
this.HttpErrorHandler = {
|
2020-08-10 09:57:39 -04:00
|
|
|
badRequest: sinon.stub(),
|
2020-07-20 09:21:28 -04:00
|
|
|
conflict: sinon.stub(),
|
2020-07-22 10:08:06 -04:00
|
|
|
unprocessableEntity: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
legacyInternal: sinon.stub(),
|
2020-07-16 02:47:46 -04:00
|
|
|
}
|
2020-11-27 08:11:12 -05:00
|
|
|
|
|
|
|
this.UrlHelper = {
|
2021-04-27 03:52:58 -04:00
|
|
|
getSafeRedirectPath: sinon.stub(),
|
2020-11-27 08:11:12 -05:00
|
|
|
}
|
|
|
|
this.UrlHelper.getSafeRedirectPath
|
|
|
|
.withArgs('https://evil.com')
|
|
|
|
.returns(undefined)
|
|
|
|
this.UrlHelper.getSafeRedirectPath.returnsArg(0)
|
2021-09-16 04:14:24 -04:00
|
|
|
this.acceptsJson = sinon.stub().returns(false)
|
2020-11-27 08:11:12 -05:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserController = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-11-27 08:11:12 -05:00
|
|
|
'../Helpers/UrlHelper': this.UrlHelper,
|
2019-05-29 05:21:06 -04:00
|
|
|
'./UserGetter': this.UserGetter,
|
|
|
|
'./UserDeleter': this.UserDeleter,
|
|
|
|
'./UserUpdater': this.UserUpdater,
|
|
|
|
'../../models/User': {
|
2021-04-27 03:52:58 -04:00
|
|
|
User: this.User,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
'../Newsletter/NewsletterManager': this.NewsLetterManager,
|
|
|
|
'./UserRegistrationHandler': this.UserRegistrationHandler,
|
|
|
|
'../Authentication/AuthenticationController': this
|
|
|
|
.AuthenticationController,
|
2021-07-28 04:51:20 -04:00
|
|
|
'../Authentication/SessionManager': this.SessionManager,
|
2019-05-29 05:21:06 -04:00
|
|
|
'../Authentication/AuthenticationManager': this.AuthenticationManager,
|
2020-03-02 07:32:12 -05:00
|
|
|
'../../infrastructure/Features': (this.Features = {
|
2021-04-27 03:52:58 -04:00
|
|
|
hasFeature: sinon.stub(),
|
2020-03-02 07:32:12 -05:00
|
|
|
}),
|
2020-07-27 10:33:57 -04:00
|
|
|
'./UserAuditLogHandler': (this.UserAuditLogHandler = {
|
|
|
|
promises: {
|
2021-04-27 03:52:58 -04:00
|
|
|
addEntry: sinon.stub().resolves(),
|
|
|
|
},
|
2020-07-27 10:33:57 -04:00
|
|
|
}),
|
2019-05-29 05:21:06 -04:00
|
|
|
'./UserHandler': this.UserHandler,
|
|
|
|
'./UserSessionsManager': this.UserSessionsManager,
|
2020-07-16 02:47:46 -04:00
|
|
|
'../Errors/HttpErrorHandler': this.HttpErrorHandler,
|
2021-07-07 05:38:56 -04:00
|
|
|
'@overleaf/settings': this.settings,
|
2020-10-30 04:10:50 -04:00
|
|
|
'@overleaf/metrics': {
|
2021-04-27 03:52:58 -04:00
|
|
|
inc() {},
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2020-03-02 07:32:12 -05:00
|
|
|
'@overleaf/o-error': OError,
|
2020-07-30 09:51:28 -04:00
|
|
|
'../Email/EmailHandler': (this.EmailHandler = {
|
|
|
|
sendEmail: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
promises: { sendEmail: sinon.stub().resolves() },
|
|
|
|
}),
|
2021-09-16 04:14:24 -04:00
|
|
|
'../../infrastructure/RequestContentTypeDetection': {
|
|
|
|
acceptsJson: this.acceptsJson,
|
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
this.res = {
|
|
|
|
send: sinon.stub(),
|
2019-06-14 12:31:46 -04:00
|
|
|
status: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
sendStatus: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
json: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-14 12:31:46 -04:00
|
|
|
this.res.status.returns(this.res)
|
|
|
|
this.next = sinon.stub()
|
|
|
|
this.callback = sinon.stub()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('tryDeleteUser', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body.password = 'wat'
|
|
|
|
this.req.logout = sinon.stub()
|
|
|
|
this.req.session.destroy = sinon.stub().callsArgWith(0, null)
|
2021-07-28 04:51:20 -04:00
|
|
|
this.SessionManager.getLoggedInUserId = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
|
|
|
.returns(this.user._id)
|
|
|
|
this.AuthenticationManager.authenticate = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.user)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send 200', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.sendStatus = code => {
|
|
|
|
code.should.equal(200)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should try to authenticate user', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.AuthenticationManager.authenticate.callCount.should.equal(1)
|
|
|
|
this.AuthenticationManager.authenticate
|
|
|
|
.calledWith({ _id: this.user._id }, this.req.body.password)
|
|
|
|
.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should delete the user', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.UserDeleter.deleteUser.callCount.should.equal(1)
|
|
|
|
this.UserDeleter.deleteUser.calledWith(this.user._id).should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when no password is supplied', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req.body.password = ''
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return 403', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.sendStatus = code => {
|
|
|
|
code.should.equal(403)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when authenticate produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.AuthenticationManager.authenticate = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-03-02 07:32:12 -05:00
|
|
|
.callsArgWith(2, new Error('woops'))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call next with an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.next = err => {
|
|
|
|
expect(err).to.not.equal(null)
|
|
|
|
expect(err).to.be.instanceof(Error)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when authenticate does not produce a user', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.AuthenticationManager.authenticate = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-03-02 07:32:12 -05:00
|
|
|
.callsArgWith(2, null, null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return 403', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.sendStatus = code => {
|
|
|
|
code.should.equal(403)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when deleteUser produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserDeleter.deleteUser = sinon.stub().yields(new Error('woops'))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call next with an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.next = err => {
|
|
|
|
expect(err).to.not.equal(null)
|
|
|
|
expect(err).to.be.instanceof(Error)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when deleteUser produces a known error', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserDeleter.deleteUser = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-03-02 07:32:12 -05:00
|
|
|
.yields(new Errors.SubscriptionAdminDeletionError())
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return a HTTP Unprocessable Entity error', function (done) {
|
2020-07-16 02:47:46 -04:00
|
|
|
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
|
|
|
|
(req, res, message, info) => {
|
|
|
|
expect(req).to.exist
|
|
|
|
expect(res).to.exist
|
|
|
|
expect(message).to.equal('error while deleting user account')
|
|
|
|
expect(info).to.deep.equal({
|
2021-04-27 03:52:58 -04:00
|
|
|
error: 'SubscriptionAdminDeletionError',
|
2020-07-16 02:47:46 -04:00
|
|
|
})
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
this.UserController.tryDeleteUser(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when session.destroy produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req.session.destroy = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2020-03-02 07:32:12 -05:00
|
|
|
.callsArgWith(0, new Error('woops'))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call next with an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.next = err => {
|
|
|
|
expect(err).to.not.equal(null)
|
|
|
|
expect(err).to.be.instanceof(Error)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.tryDeleteUser(this.req, this.res, this.next)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('unsubscribe', function () {
|
|
|
|
it('should send the user to unsubscribe', function (done) {
|
2020-05-06 06:02:16 -04:00
|
|
|
this.res.sendStatus = () => {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.NewsLetterManager.unsubscribe
|
|
|
|
.calledWith(this.user)
|
|
|
|
.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.unsubscribe(this.req, this.res)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('updateUserSettings', function () {
|
|
|
|
beforeEach(function () {
|
2020-08-12 10:19:33 -04:00
|
|
|
this.auditLog = { initiatorId: this.user_id, ipAddress: this.req.ip }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.newEmail = 'hello@world.com'
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req.externalAuthenticationSystemUsed = sinon.stub().returns(false)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call save', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body = {}
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.user.save.called.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the first name', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body = { first_name: 'bobby ' }
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.user.first_name.should.equal('bobby')
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the role', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body = { role: 'student' }
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.user.role.should.equal('student')
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the institution', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body = { institution: 'MIT' }
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.user.institution.should.equal('MIT')
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set some props on ace', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body = { editorTheme: 'something' }
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.user.ace.theme.should.equal('something')
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the overall theme', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body = { overallTheme: 'green-ish' }
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
this.user.ace.overallTheme.should.equal('green-ish')
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send an error if the email is 0 len', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body.email = ''
|
2021-04-14 09:17:21 -04:00
|
|
|
this.res.sendStatus = function (code) {
|
2019-05-29 05:21:06 -04:00
|
|
|
code.should.equal(400)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send an error if the email does not contain an @', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body.email = 'bob at something dot com'
|
2021-04-14 09:17:21 -04:00
|
|
|
this.res.sendStatus = function (code) {
|
2019-05-29 05:21:06 -04:00
|
|
|
code.should.equal(400)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the user updater with the new email and user _id', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body.email = this.newEmail.toUpperCase()
|
2020-08-12 10:19:33 -04:00
|
|
|
this.UserUpdater.changeEmailAddress.callsArgWith(3)
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.sendStatus = code => {
|
|
|
|
code.should.equal(200)
|
|
|
|
this.UserUpdater.changeEmailAddress
|
2020-08-12 10:19:33 -04:00
|
|
|
.calledWith(this.user_id, this.newEmail, this.auditLog)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should update the email on the session', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body.email = this.newEmail.toUpperCase()
|
2020-08-12 10:19:33 -04:00
|
|
|
this.UserUpdater.changeEmailAddress.callsArgWith(3)
|
2019-05-29 05:21:06 -04:00
|
|
|
let callcount = 0
|
|
|
|
this.User.findById = (id, cb) => {
|
|
|
|
if (++callcount === 2) {
|
|
|
|
this.user.email = this.newEmail
|
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
cb(null, this.user)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
code.should.equal(200)
|
2021-07-28 04:51:20 -04:00
|
|
|
this.SessionManager.setInSessionUser
|
|
|
|
.calledWith(this.req.session, {
|
2019-05-29 05:21:06 -04:00
|
|
|
email: this.newEmail,
|
|
|
|
first_name: undefined,
|
2021-04-27 03:52:58 -04:00
|
|
|
last_name: undefined,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call populateTeamInvites', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body.email = this.newEmail.toUpperCase()
|
2020-08-12 10:19:33 -04:00
|
|
|
this.UserUpdater.changeEmailAddress.callsArgWith(3)
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.sendStatus = code => {
|
|
|
|
code.should.equal(200)
|
|
|
|
this.UserHandler.populateTeamInvites
|
|
|
|
.calledWith(this.user)
|
|
|
|
.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2020-04-23 07:52:36 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when changeEmailAddress yields an error', function () {
|
|
|
|
it('should pass on an error and not send a success status', function (done) {
|
2020-04-23 07:52:36 -04:00
|
|
|
this.req.body.email = this.newEmail.toUpperCase()
|
2020-08-12 10:19:33 -04:00
|
|
|
this.UserUpdater.changeEmailAddress.callsArgWith(3, new OError())
|
2020-07-22 10:08:06 -04:00
|
|
|
this.HttpErrorHandler.legacyInternal = sinon.spy(
|
|
|
|
(req, res, message, error) => {
|
|
|
|
expect(req).to.exist
|
|
|
|
expect(req).to.exist
|
|
|
|
message.should.equal('problem_changing_email_address')
|
|
|
|
expect(error).to.be.instanceof(OError)
|
2020-04-23 07:52:36 -04:00
|
|
|
done()
|
2020-07-22 10:08:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
this.UserController.updateUserSettings(this.req, this.res, this.next)
|
2020-04-23 07:52:36 -04:00
|
|
|
})
|
2020-07-20 09:21:28 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the HTTP conflict error handler when the email already exists', function (done) {
|
2020-07-20 09:21:28 -04:00
|
|
|
this.HttpErrorHandler.conflict = sinon.spy((req, res, message) => {
|
|
|
|
expect(req).to.exist
|
|
|
|
expect(req).to.exist
|
|
|
|
message.should.equal('email_already_registered')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.req.body.email = this.newEmail.toUpperCase()
|
|
|
|
this.UserUpdater.changeEmailAddress.callsArgWith(
|
2020-08-12 10:19:33 -04:00
|
|
|
3,
|
2020-07-20 09:21:28 -04:00
|
|
|
new Errors.EmailExistsError()
|
|
|
|
)
|
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when using an external auth source', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserUpdater.changeEmailAddress.callsArgWith(2)
|
|
|
|
this.newEmail = 'someone23@example.com'
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req.externalAuthenticationSystemUsed = sinon.stub().returns(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not set a new email', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.body.email = this.newEmail
|
|
|
|
this.res.sendStatus = code => {
|
|
|
|
code.should.equal(200)
|
|
|
|
this.UserUpdater.changeEmailAddress
|
|
|
|
.calledWith(this.user_id, this.newEmail)
|
|
|
|
.should.equal(false)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.updateUserSettings(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('logout', function () {
|
2021-09-16 04:14:24 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.acceptsJson.returns(false)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should destroy the session', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
|
|
|
this.res.redirect = url => {
|
|
|
|
url.should.equal('/login')
|
|
|
|
this.req.session.destroy.called.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.logout(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should untrack session', function (done) {
|
2020-02-20 11:08:18 -05:00
|
|
|
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
|
|
|
this.res.redirect = url => {
|
|
|
|
url.should.equal('/login')
|
|
|
|
this.UserSessionsManager.untrackSession.callCount.should.equal(1)
|
|
|
|
this.UserSessionsManager.untrackSession
|
|
|
|
.calledWith(sinon.match(this.req.user), this.req.sessionID)
|
|
|
|
.should.equal(true)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2020-02-20 11:08:18 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.logout(this.req, this.res)
|
2020-02-20 11:08:18 -05:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should redirect after logout', function (done) {
|
2019-09-25 10:29:10 -04:00
|
|
|
this.req.body.redirect = '/institutional-login'
|
|
|
|
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
|
|
|
this.res.redirect = url => {
|
|
|
|
url.should.equal(this.req.body.redirect)
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-09-25 10:29:10 -04:00
|
|
|
}
|
2020-11-27 08:11:12 -05:00
|
|
|
this.UserController.logout(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should redirect after logout, but not to evil.com', function (done) {
|
2020-11-27 08:11:12 -05:00
|
|
|
this.req.body.redirect = 'https://evil.com'
|
|
|
|
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
|
|
|
this.res.redirect = url => {
|
|
|
|
url.should.equal('/login')
|
|
|
|
done()
|
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.logout(this.req, this.res)
|
2019-09-25 10:29:10 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should redirect to login after logout when no redirect set', function (done) {
|
2019-09-25 10:29:10 -04:00
|
|
|
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
|
|
|
this.res.redirect = url => {
|
|
|
|
url.should.equal('/login')
|
2020-03-02 07:32:12 -05:00
|
|
|
done()
|
2019-09-25 10:29:10 -04:00
|
|
|
}
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.logout(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2021-09-16 04:14:24 -04:00
|
|
|
|
|
|
|
it('should send json with redir property for json request', function (done) {
|
|
|
|
this.acceptsJson.returns(true)
|
|
|
|
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
|
|
|
this.res.status = code => {
|
|
|
|
code.should.equal(200)
|
|
|
|
return this.res
|
|
|
|
}
|
|
|
|
this.res.json = data => {
|
|
|
|
data.redir.should.equal('/login')
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
this.UserController.logout(this.req, this.res)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('register', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.user, (this.url = 'mock/url'))
|
|
|
|
this.req.body.email = this.user.email = this.email = 'email@example.com'
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserController.register(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should register the user and send them an email', function () {
|
2021-09-10 04:30:01 -04:00
|
|
|
sinon.assert.calledWith(
|
|
|
|
this.UserRegistrationHandler.registerNewUserAndSendActivationEmail,
|
|
|
|
this.email
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the user and activation url', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.res.json
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith({
|
|
|
|
email: this.email,
|
2021-04-27 03:52:58 -04:00
|
|
|
setNewPasswordUrl: this.url,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('clearSessions', function () {
|
|
|
|
describe('success', function () {
|
|
|
|
it('should call revokeAllUserSessions', function (done) {
|
2020-07-30 09:51:28 -04:00
|
|
|
this.res.sendStatus.callsFake(() => {
|
|
|
|
this.UserSessionsManager.promises.revokeAllUserSessions.callCount.should.equal(
|
|
|
|
1
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.clearSessions(this.req, this.res)
|
2020-07-27 10:33:57 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('send a 201 response', function (done) {
|
2020-07-30 09:51:28 -04:00
|
|
|
this.res.sendStatus.callsFake(status => {
|
|
|
|
status.should.equal(201)
|
|
|
|
done()
|
|
|
|
})
|
2020-07-27 10:33:57 -04:00
|
|
|
|
2020-07-30 09:51:28 -04:00
|
|
|
this.UserController.clearSessions(this.req, this.res)
|
2020-07-27 10:33:57 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('sends a security alert email', function (done) {
|
2020-07-30 09:51:28 -04:00
|
|
|
this.res.sendStatus.callsFake(status => {
|
|
|
|
this.EmailHandler.promises.sendEmail.callCount.should.equal(1)
|
2020-09-29 10:05:12 -04:00
|
|
|
const expectedArg = {
|
|
|
|
to: this.user.email,
|
2020-12-15 05:23:54 -05:00
|
|
|
actionDescribed: `active sessions were cleared on your account ${this.user.email}`,
|
2021-04-27 03:52:58 -04:00
|
|
|
action: 'active sessions cleared',
|
2020-09-29 10:05:12 -04:00
|
|
|
}
|
|
|
|
const emailCall = this.EmailHandler.promises.sendEmail.lastCall
|
|
|
|
expect(emailCall.args[0]).to.equal('securityAlert')
|
|
|
|
expect(emailCall.args[1]).to.deep.equal(expectedArg)
|
2020-07-27 10:33:57 -04:00
|
|
|
done()
|
|
|
|
})
|
2020-07-30 09:51:28 -04:00
|
|
|
|
|
|
|
this.UserController.clearSessions(this.req, this.res)
|
2020-07-27 10:33:57 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('errors', function () {
|
|
|
|
describe('when getAllUserSessions produces an error', function () {
|
|
|
|
it('should return an error', function (done) {
|
2020-07-30 09:51:28 -04:00
|
|
|
this.UserSessionsManager.promises.getAllUserSessions.rejects(
|
|
|
|
new Error('woops')
|
|
|
|
)
|
|
|
|
this.UserController.clearSessions(this.req, this.res, error => {
|
|
|
|
expect(error).to.be.instanceof(Error)
|
|
|
|
done()
|
|
|
|
})
|
2020-07-27 10:33:57 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when audit log addEntry produces an error', function () {
|
|
|
|
it('should call next with an error', function (done) {
|
2020-07-30 09:51:28 -04:00
|
|
|
this.UserAuditLogHandler.promises.addEntry.rejects(new Error('woops'))
|
|
|
|
this.UserController.clearSessions(this.req, this.res, error => {
|
|
|
|
expect(error).to.be.instanceof(Error)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when revokeAllUserSessions produces an error', function () {
|
|
|
|
it('should call next with an error', function (done) {
|
2020-07-30 09:51:28 -04:00
|
|
|
this.UserSessionsManager.promises.revokeAllUserSessions.rejects(
|
|
|
|
new Error('woops')
|
|
|
|
)
|
|
|
|
this.UserController.clearSessions(this.req, this.res, error => {
|
|
|
|
expect(error).to.be.instanceof(Error)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when EmailHandler produces an error', function () {
|
2020-09-29 10:05:12 -04:00
|
|
|
const anError = new Error('oops')
|
2021-04-14 09:17:21 -04:00
|
|
|
it('send a 201 response but log error', function (done) {
|
2020-09-29 10:05:12 -04:00
|
|
|
this.EmailHandler.promises.sendEmail.rejects(anError)
|
2020-07-30 09:51:28 -04:00
|
|
|
this.res.sendStatus.callsFake(status => {
|
|
|
|
status.should.equal(201)
|
|
|
|
this.logger.error.callCount.should.equal(1)
|
2020-09-29 10:05:12 -04:00
|
|
|
const loggerCall = this.logger.error.getCall(0)
|
|
|
|
expect(loggerCall.args[0]).to.deep.equal({
|
|
|
|
error: anError,
|
2021-04-27 03:52:58 -04:00
|
|
|
userId: this.user_id,
|
2020-09-29 10:05:12 -04:00
|
|
|
})
|
|
|
|
expect(loggerCall.args[1]).to.contain(
|
|
|
|
'could not send security alert email when sessions cleared'
|
|
|
|
)
|
2020-07-30 09:51:28 -04:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.clearSessions(this.req, this.res)
|
2020-07-27 10:33:57 -04:00
|
|
|
})
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('changePassword', function () {
|
|
|
|
describe('success', function () {
|
|
|
|
beforeEach(function () {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.AuthenticationManager.promises.authenticate.resolves(this.user)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.resolves()
|
|
|
|
this.req.body = {
|
|
|
|
newPassword1: 'newpass',
|
2021-04-27 03:52:58 -04:00
|
|
|
newPassword2: 'newpass',
|
2020-08-10 09:57:39 -04:00
|
|
|
}
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the new password if they do match', function (done) {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.res.json.callsFake(() => {
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.should.have.been.calledWith(
|
2020-10-22 04:11:43 -04:00
|
|
|
this.user,
|
2020-08-10 09:57:39 -04:00
|
|
|
'newpass'
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.changePassword(this.req, this.res)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should log the update', function (done) {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.res.json.callsFake(() => {
|
|
|
|
this.UserAuditLogHandler.promises.addEntry.should.have.been.calledWith(
|
|
|
|
this.user._id,
|
|
|
|
'update-password',
|
|
|
|
this.user._id,
|
|
|
|
this.req.ip
|
|
|
|
)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.callCount.should.equal(
|
|
|
|
1
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.changePassword(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send security alert email', function (done) {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.res.json.callsFake(() => {
|
|
|
|
const expectedArg = {
|
|
|
|
to: this.user.email,
|
2020-12-15 05:23:54 -05:00
|
|
|
actionDescribed: `your password has been changed on your account ${this.user.email}`,
|
2021-04-27 03:52:58 -04:00
|
|
|
action: 'password changed',
|
2020-08-10 09:57:39 -04:00
|
|
|
}
|
|
|
|
const emailCall = this.EmailHandler.sendEmail.lastCall
|
|
|
|
expect(emailCall.args[0]).to.equal('securityAlert')
|
|
|
|
expect(emailCall.args[1]).to.deep.equal(expectedArg)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.changePassword(this.req, this.res)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('errors', function () {
|
|
|
|
it('should check the old password is the current one at the moment', function (done) {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.AuthenticationManager.promises.authenticate.resolves()
|
|
|
|
this.req.body = { currentPassword: 'oldpasshere' }
|
|
|
|
this.HttpErrorHandler.badRequest.callsFake(() => {
|
|
|
|
expect(this.HttpErrorHandler.badRequest).to.have.been.calledWith(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
'Your old password is wrong'
|
|
|
|
)
|
|
|
|
this.AuthenticationManager.promises.authenticate.should.have.been.calledWith(
|
|
|
|
{ _id: this.user._id },
|
|
|
|
'oldpasshere'
|
|
|
|
)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.callCount.should.equal(
|
|
|
|
0
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.changePassword(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('it should not set the new password if they do not match', function (done) {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.AuthenticationManager.promises.authenticate.resolves({})
|
|
|
|
this.req.body = {
|
|
|
|
newPassword1: '1',
|
2021-04-27 03:52:58 -04:00
|
|
|
newPassword2: '2',
|
2020-08-10 09:57:39 -04:00
|
|
|
}
|
|
|
|
this.HttpErrorHandler.badRequest.callsFake(() => {
|
|
|
|
expect(this.HttpErrorHandler.badRequest).to.have.been.calledWith(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
'password_change_passwords_do_not_match'
|
|
|
|
)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.callCount.should.equal(
|
|
|
|
0
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.changePassword(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('it should not set the new password if it is invalid', function (done) {
|
2020-10-22 04:11:43 -04:00
|
|
|
// this.AuthenticationManager.validatePassword = sinon
|
|
|
|
// .stub()
|
|
|
|
// .returns({ message: 'validation-error' })
|
|
|
|
const err = new Error('bad')
|
|
|
|
err.name = 'InvalidPasswordError'
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.rejects(err)
|
2020-08-10 09:57:39 -04:00
|
|
|
this.AuthenticationManager.promises.authenticate.resolves({})
|
|
|
|
this.req.body = {
|
|
|
|
newPassword1: 'newpass',
|
2021-04-27 03:52:58 -04:00
|
|
|
newPassword2: 'newpass',
|
2019-06-14 12:31:46 -04:00
|
|
|
}
|
2020-08-10 09:57:39 -04:00
|
|
|
this.HttpErrorHandler.badRequest.callsFake(() => {
|
|
|
|
expect(this.HttpErrorHandler.badRequest).to.have.been.calledWith(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
2020-10-22 04:11:43 -04:00
|
|
|
err.message
|
2020-08-10 09:57:39 -04:00
|
|
|
)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.callCount.should.equal(
|
2020-10-22 04:11:43 -04:00
|
|
|
1
|
2020-08-10 09:57:39 -04:00
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.changePassword(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserAuditLogHandler error', function () {
|
|
|
|
it('should return error and not update password', function (done) {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.UserAuditLogHandler.promises.addEntry.rejects(new Error('oops'))
|
|
|
|
this.AuthenticationManager.promises.authenticate.resolves(this.user)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.resolves()
|
|
|
|
this.req.body = {
|
|
|
|
newPassword1: 'newpass',
|
2021-04-27 03:52:58 -04:00
|
|
|
newPassword2: 'newpass',
|
2020-08-10 09:57:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.UserController.changePassword(this.req, this.res, error => {
|
|
|
|
expect(error).to.be.instanceof(Error)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.callCount.should.equal(
|
2020-10-22 04:11:43 -04:00
|
|
|
1
|
2020-08-10 09:57:39 -04:00
|
|
|
)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('EmailHandler error', function () {
|
2020-09-29 10:05:12 -04:00
|
|
|
const anError = new Error('oops')
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.AuthenticationManager.promises.authenticate.resolves(this.user)
|
|
|
|
this.AuthenticationManager.promises.setUserPassword.resolves()
|
|
|
|
this.req.body = {
|
|
|
|
newPassword1: 'newpass',
|
2021-04-27 03:52:58 -04:00
|
|
|
newPassword2: 'newpass',
|
2020-08-10 09:57:39 -04:00
|
|
|
}
|
2020-09-29 10:05:12 -04:00
|
|
|
this.EmailHandler.sendEmail.yields(anError)
|
2020-08-10 09:57:39 -04:00
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not return error but should log it', function (done) {
|
2020-08-10 09:57:39 -04:00
|
|
|
this.res.json.callsFake(result => {
|
|
|
|
expect(result.message.type).to.equal('success')
|
|
|
|
this.logger.error.callCount.should.equal(1)
|
2020-09-29 10:05:12 -04:00
|
|
|
expect(this.logger.error).to.have.been.calledWithExactly(
|
|
|
|
{
|
|
|
|
error: anError,
|
2021-04-27 03:52:58 -04:00
|
|
|
userId: this.user_id,
|
2020-09-29 10:05:12 -04:00
|
|
|
},
|
|
|
|
'could not send security alert email when password changed'
|
|
|
|
)
|
2020-08-10 09:57:39 -04:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
this.UserController.changePassword(this.req, this.res)
|
|
|
|
})
|
2019-06-14 12:31:46 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
2020-03-02 07:32:12 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('ensureAffiliationMiddleware', function () {
|
|
|
|
describe('without affiliations feature', function () {
|
|
|
|
beforeEach(async function () {
|
2020-03-11 09:21:44 -04:00
|
|
|
await this.UserController.promises.ensureAffiliationMiddleware(
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not run affiliation check', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.UserGetter.promises.getUser).to.not.have.been.called
|
|
|
|
expect(this.UserUpdater.promises.confirmEmail).to.not.have.been.called
|
|
|
|
expect(this.UserUpdater.promises.addAffiliationForNewUser).to.not.have
|
|
|
|
.been.called
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not return an error', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.next).to.be.calledWith()
|
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('without ensureAffiliation query parameter', function () {
|
|
|
|
beforeEach(async function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.Features.hasFeature.withArgs('affiliations').returns(true)
|
2020-03-11 09:21:44 -04:00
|
|
|
await this.UserController.promises.ensureAffiliationMiddleware(
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not run middleware', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.UserGetter.promises.getUser).to.not.have.been.called
|
|
|
|
expect(this.UserUpdater.promises.confirmEmail).to.not.have.been.called
|
|
|
|
expect(this.UserUpdater.promises.addAffiliationForNewUser).to.not.have
|
|
|
|
.been.called
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not return an error', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.next).to.be.calledWith()
|
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('no flagged email', function () {
|
|
|
|
beforeEach(async function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
const email = 'unit-test@overleaf.com'
|
|
|
|
this.user.email = email
|
|
|
|
this.user.emails = [
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
email,
|
|
|
|
},
|
2020-03-02 07:32:12 -05:00
|
|
|
]
|
|
|
|
this.Features.hasFeature.withArgs('affiliations').returns(true)
|
2020-03-11 09:21:44 -04:00
|
|
|
this.req.query.ensureAffiliation = true
|
|
|
|
await this.UserController.promises.ensureAffiliationMiddleware(
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the user', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.UserGetter.promises.getUser).to.have.been.calledWith(
|
|
|
|
this.user._id
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not try to add affiliation or update user', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.UserUpdater.promises.addAffiliationForNewUser).to.not.have
|
|
|
|
.been.called
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not return an error', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.next).to.be.calledWith()
|
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('flagged non-SSO email', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
let emailFlagged
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
emailFlagged = 'flagged@overleaf.com'
|
|
|
|
this.user.email = emailFlagged
|
|
|
|
this.user.emails = [
|
|
|
|
{
|
|
|
|
email: emailFlagged,
|
2021-04-27 03:52:58 -04:00
|
|
|
affiliationUnchecked: true,
|
|
|
|
},
|
2020-03-02 07:32:12 -05:00
|
|
|
]
|
|
|
|
this.Features.hasFeature.withArgs('affiliations').returns(true)
|
2020-03-11 09:21:44 -04:00
|
|
|
this.req.query.ensureAffiliation = true
|
|
|
|
await this.UserController.promises.ensureAffiliationMiddleware(
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should unflag the emails but not confirm', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(
|
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser
|
|
|
|
).to.have.been.calledWith(this.user._id, emailFlagged)
|
|
|
|
expect(
|
|
|
|
this.UserUpdater.promises.confirmEmail
|
|
|
|
).to.not.have.been.calledWith(this.user._id, emailFlagged)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not return an error', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.next).to.be.calledWith()
|
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('flagged SSO email', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
let emailFlagged
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
emailFlagged = 'flagged@overleaf.com'
|
|
|
|
this.user.email = emailFlagged
|
|
|
|
this.user.emails = [
|
|
|
|
{
|
|
|
|
email: emailFlagged,
|
|
|
|
affiliationUnchecked: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
samlProviderId: '123',
|
|
|
|
},
|
2020-03-02 07:32:12 -05:00
|
|
|
]
|
|
|
|
this.Features.hasFeature.withArgs('affiliations').returns(true)
|
2020-03-11 09:21:44 -04:00
|
|
|
this.req.query.ensureAffiliation = true
|
|
|
|
await this.UserController.promises.ensureAffiliationMiddleware(
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should add affiliation to v1, unflag and confirm on v2', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.UserUpdater.promises.addAffiliationForNewUser).to.have.not
|
|
|
|
.been.called
|
|
|
|
expect(this.UserUpdater.promises.confirmEmail).to.have.been.calledWith(
|
|
|
|
this.user._id,
|
|
|
|
emailFlagged
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not return an error', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.next).to.be.calledWith()
|
|
|
|
})
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when v1 returns an error', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
let emailFlagged
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(async function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
this.UserUpdater.promises.addAffiliationForNewUser.rejects()
|
|
|
|
emailFlagged = 'flagged@overleaf.com'
|
|
|
|
this.user.email = emailFlagged
|
|
|
|
this.user.emails = [
|
|
|
|
{
|
|
|
|
email: emailFlagged,
|
2021-04-27 03:52:58 -04:00
|
|
|
affiliationUnchecked: true,
|
|
|
|
},
|
2020-03-02 07:32:12 -05:00
|
|
|
]
|
|
|
|
this.Features.hasFeature.withArgs('affiliations').returns(true)
|
2020-03-11 09:21:44 -04:00
|
|
|
this.req.query.ensureAffiliation = true
|
|
|
|
await this.UserController.promises.ensureAffiliationMiddleware(
|
2020-03-02 07:32:12 -05:00
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the error', function () {
|
2020-03-02 07:32:12 -05:00
|
|
|
expect(this.next).to.be.calledWith(sinon.match.instanceOf(Error))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|