2014-04-09 09:50:12 -04:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/Features/User/UserController.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
events = require "events"
|
|
|
|
MockResponse = require "../helpers/MockResponse"
|
|
|
|
MockRequest = require "../helpers/MockRequest"
|
|
|
|
ObjectId = require("mongojs").ObjectId
|
2014-04-10 09:43:06 -04:00
|
|
|
assert = require("assert")
|
2014-04-09 09:50:12 -04:00
|
|
|
|
|
|
|
describe "UserController", ->
|
|
|
|
beforeEach ->
|
2014-04-10 12:15:18 -04:00
|
|
|
@user_id = "323123"
|
|
|
|
|
2014-04-09 10:41:19 -04:00
|
|
|
@user =
|
2014-04-10 12:15:18 -04:00
|
|
|
_id:@user_id
|
2016-09-07 11:40:49 -04:00
|
|
|
save: sinon.stub().callsArgWith(0)
|
2014-04-09 11:33:54 -04:00
|
|
|
ace:{}
|
2014-04-09 10:26:07 -04:00
|
|
|
|
2016-09-07 11:40:49 -04:00
|
|
|
@req =
|
|
|
|
user: {}
|
|
|
|
session:
|
|
|
|
destroy:->
|
|
|
|
user :
|
|
|
|
_id : @user_id
|
|
|
|
email:"old@something.com"
|
|
|
|
body:{}
|
|
|
|
|
2016-07-01 10:33:59 -04:00
|
|
|
@UserDeleter =
|
2014-04-09 10:26:07 -04:00
|
|
|
deleteUser: sinon.stub().callsArgWith(1)
|
2016-07-01 10:33:59 -04:00
|
|
|
@UserLocator =
|
2014-04-09 11:59:28 -04:00
|
|
|
findById: sinon.stub().callsArgWith(1, null, @user)
|
2014-04-09 11:33:54 -04:00
|
|
|
@User =
|
2014-04-09 10:41:19 -04:00
|
|
|
findById: sinon.stub().callsArgWith(1, null, @user)
|
|
|
|
@NewsLetterManager =
|
|
|
|
unsubscribe: sinon.stub().callsArgWith(1)
|
2014-04-10 09:43:06 -04:00
|
|
|
@UserRegistrationHandler =
|
|
|
|
registerNewUser: sinon.stub()
|
2015-02-13 06:18:17 -05:00
|
|
|
@AuthenticationController =
|
|
|
|
establishUserSession: sinon.stub().callsArg(2)
|
2016-09-07 11:40:49 -04:00
|
|
|
getLoggedInUserId: sinon.stub().returns(@user._id)
|
|
|
|
getSessionUser: sinon.stub().returns(@req.session.user)
|
2016-09-22 11:58:25 -04:00
|
|
|
setInSessionUser: sinon.stub()
|
2014-04-10 12:15:18 -04:00
|
|
|
@AuthenticationManager =
|
|
|
|
authenticate: sinon.stub()
|
|
|
|
setUserPassword: sinon.stub()
|
2014-04-15 08:59:00 -04:00
|
|
|
@ReferalAllocator =
|
|
|
|
allocate:sinon.stub()
|
2016-07-01 10:33:59 -04:00
|
|
|
@SubscriptionDomainHandler =
|
2015-01-27 13:22:51 -05:00
|
|
|
autoAllocate:sinon.stub()
|
2014-05-16 12:45:48 -04:00
|
|
|
@UserUpdater =
|
|
|
|
changeEmailAddress:sinon.stub()
|
2015-03-19 10:22:48 -04:00
|
|
|
@settings =
|
|
|
|
siteUrl: "sharelatex.example.com"
|
2016-07-01 10:33:59 -04:00
|
|
|
@UserHandler =
|
2016-03-13 18:24:39 -04:00
|
|
|
populateGroupLicenceInvite:sinon.stub().callsArgWith(1)
|
2016-07-01 05:20:22 -04:00
|
|
|
@UserSessionsManager =
|
2016-07-01 10:33:59 -04:00
|
|
|
trackSession: sinon.stub()
|
|
|
|
untrackSession: sinon.stub()
|
2016-07-05 09:20:47 -04:00
|
|
|
revokeAllUserSessions: sinon.stub().callsArgWith(2, null)
|
2017-05-15 06:53:52 -04:00
|
|
|
@SudoModeHandler =
|
|
|
|
clearSudoMode: sinon.stub()
|
2014-04-09 09:50:12 -04:00
|
|
|
@UserController = SandboxedModule.require modulePath, requires:
|
2014-04-09 11:59:28 -04:00
|
|
|
"./UserLocator": @UserLocator
|
2014-04-09 09:50:12 -04:00
|
|
|
"./UserDeleter": @UserDeleter
|
2014-05-16 12:45:48 -04:00
|
|
|
"./UserUpdater":@UserUpdater
|
2014-04-09 11:33:54 -04:00
|
|
|
"../../models/User": User:@User
|
2014-04-09 10:41:19 -04:00
|
|
|
'../Newsletter/NewsletterManager':@NewsLetterManager
|
2014-04-10 09:43:06 -04:00
|
|
|
"./UserRegistrationHandler":@UserRegistrationHandler
|
|
|
|
"../Authentication/AuthenticationController": @AuthenticationController
|
2014-04-10 12:15:18 -04:00
|
|
|
"../Authentication/AuthenticationManager": @AuthenticationManager
|
2014-04-15 08:59:00 -04:00
|
|
|
"../Referal/ReferalAllocator":@ReferalAllocator
|
2015-05-27 15:57:54 -04:00
|
|
|
"../Subscription/SubscriptionDomainHandler":@SubscriptionDomainHandler
|
2016-03-13 18:24:39 -04:00
|
|
|
"./UserHandler":@UserHandler
|
2016-07-01 05:20:22 -04:00
|
|
|
"./UserSessionsManager": @UserSessionsManager
|
2017-05-15 06:53:52 -04:00
|
|
|
"../SudoMode/SudoModeHandler": @SudoModeHandler
|
2015-03-19 10:22:48 -04:00
|
|
|
"settings-sharelatex": @settings
|
2016-07-01 10:33:59 -04:00
|
|
|
"logger-sharelatex":
|
2016-06-06 05:50:23 -04:00
|
|
|
log:->
|
|
|
|
err:->
|
2017-04-03 11:18:30 -04:00
|
|
|
"metrics-sharelatex": inc:->
|
2014-04-09 09:50:12 -04:00
|
|
|
|
2015-03-19 10:22:48 -04:00
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
2016-10-07 05:52:58 -04:00
|
|
|
sendStatus: sinon.stub()
|
2015-03-19 10:22:48 -04:00
|
|
|
json: sinon.stub()
|
2014-04-09 09:50:12 -04:00
|
|
|
@next = sinon.stub()
|
2016-10-26 06:01:35 -04:00
|
|
|
|
|
|
|
describe 'tryDeleteUser', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body.password = 'wat'
|
2016-11-28 07:37:53 -05:00
|
|
|
@req.logout = sinon.stub()
|
|
|
|
@req.session.destroy = sinon.stub().callsArgWith(0, null)
|
2016-10-26 06:01:35 -04:00
|
|
|
@AuthenticationController.getLoggedInUserId = sinon.stub().returns(@user._id)
|
|
|
|
@AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, @user)
|
|
|
|
@UserDeleter.deleteUser = sinon.stub().callsArgWith(1, null)
|
|
|
|
|
|
|
|
it 'should send 200', (done) ->
|
|
|
|
@res.sendStatus = (code) =>
|
|
|
|
code.should.equal 200
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
|
|
|
|
|
|
|
it 'should try to authenticate user', (done) ->
|
|
|
|
@res.sendStatus = (code) =>
|
|
|
|
@AuthenticationManager.authenticate.callCount.should.equal 1
|
|
|
|
@AuthenticationManager.authenticate.calledWith({_id: @user._id}, @req.body.password).should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
|
|
|
|
|
|
|
it 'should delete the user', (done) ->
|
|
|
|
@res.sendStatus = (code) =>
|
|
|
|
@UserDeleter.deleteUser.callCount.should.equal 1
|
|
|
|
@UserDeleter.deleteUser.calledWith(@user._id).should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
|
|
|
|
|
|
|
describe 'when no password is supplied', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.body.password = ''
|
|
|
|
|
|
|
|
it 'should return 403', (done) ->
|
|
|
|
@res.sendStatus = (code) =>
|
|
|
|
code.should.equal 403
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
|
|
|
|
|
|
|
describe 'when authenticate produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, new Error('woops'))
|
|
|
|
|
|
|
|
it 'should call next with an error', (done) ->
|
|
|
|
@next = (err) =>
|
|
|
|
expect(err).to.not.equal null
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
|
|
|
|
|
|
|
describe 'when authenticate does not produce a user', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@AuthenticationManager.authenticate = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
|
|
|
|
it 'should return 403', (done) ->
|
|
|
|
@res.sendStatus = (code) =>
|
|
|
|
code.should.equal 403
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
|
|
|
|
|
|
|
describe 'when deleteUser produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@UserDeleter.deleteUser = sinon.stub().callsArgWith(1, new Error('woops'))
|
|
|
|
|
|
|
|
it 'should call next with an error', (done) ->
|
|
|
|
@next = (err) =>
|
|
|
|
expect(err).to.not.equal null
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
|
|
|
|
2016-11-28 07:37:53 -05:00
|
|
|
describe 'when session.destroy produces an error', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@req.session.destroy = sinon.stub().callsArgWith(0, new Error('woops'))
|
|
|
|
|
|
|
|
it 'should call next with an error', (done) ->
|
|
|
|
@next = (err) =>
|
|
|
|
expect(err).to.not.equal null
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
done()
|
|
|
|
@UserController.tryDeleteUser @req, @res, @next
|
2016-10-26 06:01:35 -04:00
|
|
|
|
2014-04-09 10:41:19 -04:00
|
|
|
describe "unsubscribe", ->
|
|
|
|
|
|
|
|
it "should send the user to unsubscribe", (done)->
|
|
|
|
@res.send = (code)=>
|
|
|
|
@NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.unsubscribe @req, @res
|
|
|
|
|
2014-04-09 11:33:54 -04:00
|
|
|
describe "updateUserSettings", ->
|
2014-05-19 06:50:32 -04:00
|
|
|
beforeEach ->
|
|
|
|
@newEmail = "hello@world.com"
|
2017-11-20 05:10:23 -05:00
|
|
|
@req.externalAuthenticationSystemUsed = sinon.stub().returns(false)
|
2014-04-09 11:33:54 -04:00
|
|
|
|
|
|
|
it "should call save", (done)->
|
|
|
|
@req.body = {}
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)=>
|
2014-04-09 11:33:54 -04:00
|
|
|
@user.save.called.should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
|
|
|
it "should set the first name", (done)->
|
|
|
|
@req.body =
|
|
|
|
first_name: "bobby "
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)=>
|
2014-04-09 11:33:54 -04:00
|
|
|
@user.first_name.should.equal "bobby"
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
2014-06-20 06:15:25 -04:00
|
|
|
it "should set the role", (done)->
|
|
|
|
@req.body =
|
|
|
|
role: "student"
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)=>
|
2014-06-20 06:15:25 -04:00
|
|
|
@user.role.should.equal "student"
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
|
|
|
it "should set the institution", (done)->
|
|
|
|
@req.body =
|
|
|
|
institution: "MIT"
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)=>
|
2014-06-20 06:15:25 -04:00
|
|
|
@user.institution.should.equal "MIT"
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
2014-04-09 11:33:54 -04:00
|
|
|
it "should set some props on ace", (done)->
|
|
|
|
@req.body =
|
2014-06-20 04:42:43 -04:00
|
|
|
theme: "something"
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)=>
|
2014-04-09 11:33:54 -04:00
|
|
|
@user.ace.theme.should.equal "something"
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
2014-05-19 06:50:32 -04:00
|
|
|
it "should send an error if the email is 0 len", (done)->
|
|
|
|
@req.body.email = ""
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)->
|
2014-05-19 09:02:54 -04:00
|
|
|
code.should.equal 400
|
2014-05-19 06:50:32 -04:00
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
|
|
|
it "should send an error if the email does not contain an @", (done)->
|
|
|
|
@req.body.email = "bob at something dot com"
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)->
|
2014-05-19 09:02:54 -04:00
|
|
|
code.should.equal 400
|
2014-05-19 06:50:32 -04:00
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
|
|
|
it "should call the user updater with the new email and user _id", (done)->
|
2014-10-13 10:44:45 -04:00
|
|
|
@req.body.email = @newEmail.toUpperCase()
|
2014-05-19 06:50:32 -04:00
|
|
|
@UserUpdater.changeEmailAddress.callsArgWith(2)
|
2015-07-08 11:56:38 -04:00
|
|
|
@res.sendStatus = (code)=>
|
2014-05-19 06:50:32 -04:00
|
|
|
code.should.equal 200
|
|
|
|
@UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
2016-06-06 05:50:23 -04:00
|
|
|
it "should update the email on the session", (done)->
|
|
|
|
@req.body.email = @newEmail.toUpperCase()
|
|
|
|
@UserUpdater.changeEmailAddress.callsArgWith(2)
|
|
|
|
callcount = 0
|
|
|
|
@User.findById = (id, cb)=>
|
|
|
|
if ++callcount == 2
|
|
|
|
@user.email = @newEmail
|
|
|
|
cb(null, @user)
|
|
|
|
@res.sendStatus = (code)=>
|
|
|
|
code.should.equal 200
|
2016-09-22 11:58:25 -04:00
|
|
|
@AuthenticationController.setInSessionUser.calledWith(
|
|
|
|
@req, {email: @newEmail, first_name: undefined, last_name: undefined}
|
|
|
|
).should.equal true
|
2016-06-06 05:50:23 -04:00
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
2016-03-13 18:24:39 -04:00
|
|
|
it "should call populateGroupLicenceInvite", (done)->
|
|
|
|
@req.body.email = @newEmail.toUpperCase()
|
|
|
|
@UserUpdater.changeEmailAddress.callsArgWith(2)
|
|
|
|
@res.sendStatus = (code)=>
|
|
|
|
code.should.equal 200
|
|
|
|
@UserHandler.populateGroupLicenceInvite.calledWith(@user).should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
2014-05-19 06:50:32 -04:00
|
|
|
|
2016-11-17 09:34:02 -05:00
|
|
|
describe 'when using an external auth source', ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@UserUpdater.changeEmailAddress.callsArgWith(2)
|
|
|
|
@newEmail = 'someone23@example.com'
|
2017-11-20 05:10:23 -05:00
|
|
|
@req.externalAuthenticationSystemUsed = sinon.stub().returns(true)
|
2016-11-17 09:34:02 -05:00
|
|
|
|
|
|
|
it 'should not set a new email', (done) ->
|
|
|
|
@req.body.email = @newEmail
|
|
|
|
@res.sendStatus = (code)=>
|
|
|
|
code.should.equal 200
|
|
|
|
@UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal false
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
2014-04-09 11:59:28 -04:00
|
|
|
describe "logout", ->
|
|
|
|
|
|
|
|
it "should destroy the session", (done)->
|
|
|
|
|
2014-04-09 12:07:19 -04:00
|
|
|
@req.session.destroy = sinon.stub().callsArgWith(0)
|
2014-04-09 11:59:28 -04:00
|
|
|
@res.redirect = (url)=>
|
|
|
|
url.should.equal "/login"
|
|
|
|
@req.session.destroy.called.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
@UserController.logout @req, @res
|
|
|
|
|
2017-05-15 06:53:52 -04:00
|
|
|
it 'should clear sudo-mode', (done) ->
|
|
|
|
@req.session.destroy = sinon.stub().callsArgWith(0)
|
|
|
|
@SudoModeHandler.clearSudoMode = sinon.stub()
|
|
|
|
@res.redirect = (url)=>
|
|
|
|
url.should.equal "/login"
|
|
|
|
@SudoModeHandler.clearSudoMode.callCount.should.equal 1
|
|
|
|
@SudoModeHandler.clearSudoMode.calledWith(@user._id).should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
@UserController.logout @req, @res
|
|
|
|
|
2014-04-09 11:59:28 -04:00
|
|
|
|
2014-04-10 09:43:06 -04:00
|
|
|
describe "register", ->
|
2015-03-19 10:22:48 -04:00
|
|
|
beforeEach ->
|
2015-12-11 12:11:13 -05:00
|
|
|
@UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
|
|
|
|
@req.body.email = @user.email = @email = "email@example.com"
|
|
|
|
@UserController.register @req, @res
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-12-11 12:11:13 -05:00
|
|
|
it "should register the user and send them an email", ->
|
|
|
|
@UserRegistrationHandler.registerNewUserAndSendActivationEmail
|
|
|
|
.calledWith(@email)
|
|
|
|
.should.equal true
|
2016-07-01 10:33:59 -04:00
|
|
|
|
2015-12-11 12:11:13 -05:00
|
|
|
it "should return the user and activation url", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith({
|
|
|
|
email: @email,
|
|
|
|
setNewPasswordUrl: @url
|
|
|
|
})
|
|
|
|
.should.equal true
|
2014-04-10 12:15:18 -04:00
|
|
|
|
2016-10-07 05:52:58 -04:00
|
|
|
describe 'clearSessions', ->
|
|
|
|
|
|
|
|
it 'should call revokeAllUserSessions', (done) ->
|
|
|
|
@UserController.clearSessions @req, @res
|
|
|
|
@UserSessionsManager.revokeAllUserSessions.callCount.should.equal 1
|
|
|
|
done()
|
|
|
|
|
|
|
|
it 'send a 201 response', (done) ->
|
|
|
|
@res.sendStatus = (status) =>
|
|
|
|
status.should.equal 201
|
|
|
|
done()
|
|
|
|
@UserController.clearSessions @req, @res
|
|
|
|
|
|
|
|
describe 'when revokeAllUserSessions produces an error', ->
|
|
|
|
|
|
|
|
it 'should call next with an error', (done) ->
|
|
|
|
@UserSessionsManager.revokeAllUserSessions.callsArgWith(2, new Error('woops'))
|
|
|
|
next = (err) =>
|
|
|
|
expect(err).to.not.equal null
|
|
|
|
expect(err).to.be.instanceof Error
|
|
|
|
done()
|
|
|
|
@UserController.clearSessions @req, @res, next
|
|
|
|
|
2014-04-10 12:15:18 -04:00
|
|
|
describe "changePassword", ->
|
|
|
|
|
|
|
|
it "should check the old password is the current one at the moment", (done)->
|
|
|
|
@AuthenticationManager.authenticate.callsArgWith(2)
|
|
|
|
@req.body =
|
|
|
|
currentPassword: "oldpasshere"
|
|
|
|
@res.send = =>
|
|
|
|
@AuthenticationManager.authenticate.calledWith(_id:@user._id, "oldpasshere").should.equal true
|
|
|
|
@AuthenticationManager.setUserPassword.called.should.equal false
|
|
|
|
done()
|
|
|
|
@UserController.changePassword @req, @res
|
|
|
|
|
|
|
|
|
|
|
|
it "it should not set the new password if they do not match", (done)->
|
|
|
|
@AuthenticationManager.authenticate.callsArgWith(2, null, {})
|
|
|
|
@req.body =
|
|
|
|
newPassword1: "1"
|
|
|
|
newPassword2: "2"
|
|
|
|
@res.send = =>
|
|
|
|
@AuthenticationManager.setUserPassword.called.should.equal false
|
|
|
|
done()
|
2016-07-01 10:33:59 -04:00
|
|
|
@UserController.changePassword @req, @res
|
2014-04-10 12:15:18 -04:00
|
|
|
|
|
|
|
it "should set the new password if they do match", (done)->
|
|
|
|
@AuthenticationManager.authenticate.callsArgWith(2, null, @user)
|
|
|
|
@AuthenticationManager.setUserPassword.callsArgWith(2)
|
|
|
|
@req.body =
|
|
|
|
newPassword1: "newpass"
|
|
|
|
newPassword2: "newpass"
|
|
|
|
@res.send = =>
|
|
|
|
@AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
|
|
|
|
done()
|
2014-05-16 12:45:48 -04:00
|
|
|
@UserController.changePassword @req, @res
|