overleaf/services/web/test/UnitTests/coffee/User/UserControllerTests.coffee

223 lines
6.5 KiB
CoffeeScript
Raw Normal View History

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
assert = require("assert")
describe "UserController", ->
beforeEach ->
@user_id = "323123"
@user =
_id:@user_id
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)
@User =
findById: sinon.stub().callsArgWith(1, null, @user)
@NewsLetterManager =
unsubscribe: sinon.stub().callsArgWith(1)
@UserRegistrationHandler =
registerNewUser: sinon.stub()
@AuthenticationController =
establishUserSession: sinon.stub().callsArg(2)
@AuthenticationManager =
authenticate: sinon.stub()
setUserPassword: sinon.stub()
@ReferalAllocator =
allocate:sinon.stub()
@SubscriptionDomainHandler =
autoAllocate:sinon.stub()
2014-05-16 12:45:48 -04:00
@UserUpdater =
changeEmailAddress:sinon.stub()
@settings =
siteUrl: "sharelatex.example.com"
@UserController = SandboxedModule.require modulePath, requires:
2014-04-09 11:59:28 -04:00
"./UserLocator": @UserLocator
"./UserDeleter": @UserDeleter
2014-05-16 12:45:48 -04:00
"./UserUpdater":@UserUpdater
"../../models/User": User:@User
'../Newsletter/NewsletterManager':@NewsLetterManager
"./UserRegistrationHandler":@UserRegistrationHandler
"../Authentication/AuthenticationController": @AuthenticationController
"../Authentication/AuthenticationManager": @AuthenticationManager
"../Referal/ReferalAllocator":@ReferalAllocator
"../Subscription/SubscriptionDomainHandler":@SubscriptionDomainHandler
"settings-sharelatex": @settings
"logger-sharelatex": {log:->}
"../../infrastructure/Metrics": inc:->
2014-04-09 12:07:19 -04:00
@req =
session:
destroy:->
user :
_id : @user_id
2014-04-09 12:07:19 -04:00
body:{}
@res =
send: sinon.stub()
json: sinon.stub()
@next = sinon.stub()
describe "deleteUser", ->
it "should delete the user", (done)->
2015-07-08 11:56:38 -04:00
@res.sendStatus = (code)=>
@UserDeleter.deleteUser.calledWith(@user_id)
code.should.equal 200
done()
@UserController.deleteUser @req, @res
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
describe "updateUserSettings", ->
2014-05-19 06:50:32 -04:00
beforeEach ->
@newEmail = "hello@world.com"
it "should call save", (done)->
@req.body = {}
2015-07-08 11:56:38 -04:00
@res.sendStatus = (code)=>
@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)=>
@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
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)=>
@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)->
@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
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
describe "register", ->
beforeEach ->
@UserRegistrationHandler.registerNewUserAndSendActivationEmail = sinon.stub().callsArgWith(1, null, @user, @url = "mock/url")
@req.body.email = @user.email = @email = "email@example.com"
@UserController.register @req, @res
it "should register the user and send them an email", ->
@UserRegistrationHandler.registerNewUserAndSendActivationEmail
.calledWith(@email)
.should.equal true
it "should return the user and activation url", ->
@res.json
.calledWith({
email: @email,
setNewPasswordUrl: @url
})
.should.equal true
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