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
|
2014-04-09 11:33:54 -04:00
|
|
|
save:sinon.stub().callsArgWith(0)
|
|
|
|
ace:{}
|
2014-04-09 10:26:07 -04:00
|
|
|
|
|
|
|
@UserDeleter =
|
|
|
|
deleteUser: sinon.stub().callsArgWith(1)
|
2014-04-09 11:59:28 -04:00
|
|
|
@UserLocator =
|
|
|
|
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)
|
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()
|
2015-01-27 13:22:51 -05:00
|
|
|
@SubscriptionDomainAllocator =
|
|
|
|
autoAllocate:sinon.stub()
|
2014-05-16 12:45:48 -04:00
|
|
|
@UserUpdater =
|
|
|
|
changeEmailAddress:sinon.stub()
|
2015-03-18 11:57:01 -04:00
|
|
|
@EmailHandler =
|
|
|
|
sendEmail:sinon.stub().callsArgWith(2)
|
2015-03-19 10:22:48 -04:00
|
|
|
@PasswordResetTokenHandler =
|
|
|
|
getNewToken: sinon.stub()
|
|
|
|
@settings =
|
|
|
|
siteUrl: "sharelatex.example.com"
|
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-01-27 13:22:51 -05:00
|
|
|
"../Subscription/SubscriptionDomainAllocator":@SubscriptionDomainAllocator
|
2015-03-18 11:57:01 -04:00
|
|
|
"../Email/EmailHandler": @EmailHandler
|
2015-03-19 10:22:48 -04:00
|
|
|
"../PasswordReset/PasswordResetTokenHandler": @PasswordResetTokenHandler
|
|
|
|
"crypto": @crypto = {}
|
|
|
|
"settings-sharelatex": @settings
|
2014-04-09 10:04:47 -04:00
|
|
|
"logger-sharelatex": {log:->}
|
|
|
|
|
2014-04-09 09:50:12 -04:00
|
|
|
|
2014-04-09 12:07:19 -04:00
|
|
|
@req =
|
2014-04-10 12:15:18 -04:00
|
|
|
session:
|
|
|
|
destroy:->
|
|
|
|
user :
|
|
|
|
_id : @user_id
|
2014-04-09 12:07:19 -04:00
|
|
|
body:{}
|
2015-03-19 10:22:48 -04:00
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
json: sinon.stub()
|
2014-04-09 09:50:12 -04:00
|
|
|
@next = sinon.stub()
|
|
|
|
describe "deleteUser", ->
|
|
|
|
|
|
|
|
it "should delete the user", (done)->
|
2014-04-09 10:41:19 -04:00
|
|
|
|
2014-04-09 09:50:12 -04:00
|
|
|
@res.send = (code)=>
|
2014-04-09 10:41:19 -04:00
|
|
|
@UserDeleter.deleteUser.calledWith(@user_id)
|
2014-04-09 09:50:12 -04:00
|
|
|
code.should.equal 200
|
|
|
|
done()
|
|
|
|
@UserController.deleteUser @req, @res
|
|
|
|
|
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"
|
2014-04-09 11:33:54 -04:00
|
|
|
|
|
|
|
it "should call save", (done)->
|
|
|
|
@req.body = {}
|
|
|
|
@res.send = (code)=>
|
|
|
|
@user.save.called.should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
|
|
|
it "should set the first name", (done)->
|
|
|
|
@req.body =
|
|
|
|
first_name: "bobby "
|
|
|
|
@res.send = (code)=>
|
|
|
|
@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"
|
|
|
|
@res.send = (code)=>
|
|
|
|
@user.role.should.equal "student"
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
|
|
|
it "should set the institution", (done)->
|
|
|
|
@req.body =
|
|
|
|
institution: "MIT"
|
|
|
|
@res.send = (code)=>
|
|
|
|
@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"
|
2014-04-09 11:33:54 -04:00
|
|
|
@res.send = (code)=>
|
|
|
|
@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 = ""
|
|
|
|
@res.send = (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"
|
|
|
|
@res.send = (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)
|
|
|
|
@res.send = (code)=>
|
|
|
|
code.should.equal 200
|
|
|
|
@UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2014-04-10 09:43:06 -04:00
|
|
|
describe "register", ->
|
2015-03-19 10:22:48 -04:00
|
|
|
beforeEach ->
|
|
|
|
@req.body.email = @user.email = "email@example.com"
|
|
|
|
@crypto.randomBytes = sinon.stub().returns({toString: () => @password = "mock-password"})
|
|
|
|
@PasswordResetTokenHandler.getNewToken.callsArgWith(2, null, @token = "mock-token")
|
|
|
|
|
|
|
|
describe "with a new user", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
|
|
|
|
@UserController.register @req, @res
|
|
|
|
|
|
|
|
it "should ask the UserRegistrationHandler to register user", ->
|
|
|
|
@UserRegistrationHandler.registerNewUser
|
|
|
|
.calledWith({
|
|
|
|
email: @req.body.email
|
|
|
|
password: @password
|
|
|
|
}).should.equal true
|
|
|
|
|
|
|
|
it "should generate a new password reset token", ->
|
|
|
|
@PasswordResetTokenHandler.getNewToken
|
|
|
|
.calledWith(@user_id, expiresIn: 7 * 24 * 60 * 60)
|
2015-02-13 06:18:17 -05:00
|
|
|
.should.equal true
|
2014-04-10 09:43:06 -04:00
|
|
|
|
2015-03-19 10:22:48 -04:00
|
|
|
it "should send a registered email", ->
|
|
|
|
@EmailHandler.sendEmail
|
|
|
|
.calledWith("registered", {
|
|
|
|
to: @user.email
|
2015-04-30 06:59:44 -04:00
|
|
|
setNewPasswordUrl: "#{@settings.siteUrl}/user/password/set?passwordResetToken=#{@token}&email=#{encodeURIComponent(@user.email)}"
|
2015-03-19 10:22:48 -04:00
|
|
|
})
|
|
|
|
.should.equal true
|
2014-04-15 08:59:00 -04:00
|
|
|
|
2015-03-19 10:22:48 -04:00
|
|
|
it "should return the user", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith({
|
|
|
|
email: @user.email
|
2015-04-30 06:59:44 -04:00
|
|
|
setNewPasswordUrl: "#{@settings.siteUrl}/user/password/set?passwordResetToken=#{@token}&email=#{encodeURIComponent(@user.email)}"
|
2015-03-19 10:22:48 -04:00
|
|
|
})
|
|
|
|
.should.equal true
|
2014-04-10 12:15:18 -04:00
|
|
|
|
2015-03-19 10:22:48 -04:00
|
|
|
describe "with a user that already exists", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, new Error("EmailAlreadyRegistered"), @user)
|
|
|
|
@UserController.register @req, @res
|
|
|
|
|
|
|
|
it "should still generate a new password token and email", ->
|
|
|
|
@PasswordResetTokenHandler.getNewToken.called.should.equal true
|
|
|
|
@EmailHandler.sendEmail.called.should.equal true
|
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()
|
|
|
|
@UserController.changePassword @req, @res
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|