2014-04-09 13:50:12 +00: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 13:43:06 +00:00
|
|
|
assert = require("assert")
|
2014-04-09 13:50:12 +00:00
|
|
|
|
|
|
|
describe "UserController", ->
|
|
|
|
beforeEach ->
|
2014-04-10 16:15:18 +00:00
|
|
|
@user_id = "323123"
|
|
|
|
|
2014-04-09 14:41:19 +00:00
|
|
|
@user =
|
2014-04-10 16:15:18 +00:00
|
|
|
_id:@user_id
|
2014-04-09 15:33:54 +00:00
|
|
|
save:sinon.stub().callsArgWith(0)
|
|
|
|
ace:{}
|
2014-04-09 14:26:07 +00:00
|
|
|
|
|
|
|
@UserDeleter =
|
|
|
|
deleteUser: sinon.stub().callsArgWith(1)
|
2014-04-09 15:59:28 +00:00
|
|
|
@UserLocator =
|
|
|
|
findById: sinon.stub().callsArgWith(1, null, @user)
|
2014-04-09 15:33:54 +00:00
|
|
|
@User =
|
2014-04-09 14:41:19 +00:00
|
|
|
findById: sinon.stub().callsArgWith(1, null, @user)
|
|
|
|
@NewsLetterManager =
|
|
|
|
unsubscribe: sinon.stub().callsArgWith(1)
|
2014-04-10 13:43:06 +00:00
|
|
|
@UserRegistrationHandler =
|
|
|
|
registerNewUser: sinon.stub()
|
|
|
|
@AuthenticationController = {}
|
2014-04-10 16:15:18 +00:00
|
|
|
@AuthenticationManager =
|
|
|
|
authenticate: sinon.stub()
|
|
|
|
setUserPassword: sinon.stub()
|
2014-04-15 12:59:00 +00:00
|
|
|
@ReferalAllocator =
|
|
|
|
allocate:sinon.stub()
|
2014-04-09 13:50:12 +00:00
|
|
|
@UserController = SandboxedModule.require modulePath, requires:
|
2014-04-09 15:59:28 +00:00
|
|
|
"./UserLocator": @UserLocator
|
2014-04-09 13:50:12 +00:00
|
|
|
"./UserDeleter": @UserDeleter
|
2014-04-09 15:33:54 +00:00
|
|
|
"../../models/User": User:@User
|
2014-04-09 14:41:19 +00:00
|
|
|
'../Newsletter/NewsletterManager':@NewsLetterManager
|
2014-04-10 13:43:06 +00:00
|
|
|
"./UserRegistrationHandler":@UserRegistrationHandler
|
|
|
|
"../Authentication/AuthenticationController": @AuthenticationController
|
2014-04-10 16:15:18 +00:00
|
|
|
"../Authentication/AuthenticationManager": @AuthenticationManager
|
2014-04-15 12:59:00 +00:00
|
|
|
"../Referal/ReferalAllocator":@ReferalAllocator
|
2014-04-09 14:04:47 +00:00
|
|
|
"logger-sharelatex": {log:->}
|
|
|
|
|
2014-04-09 13:50:12 +00:00
|
|
|
|
2014-04-09 16:07:19 +00:00
|
|
|
@req =
|
2014-04-10 16:15:18 +00:00
|
|
|
session:
|
|
|
|
destroy:->
|
|
|
|
user :
|
|
|
|
_id : @user_id
|
2014-04-09 16:07:19 +00:00
|
|
|
body:{}
|
|
|
|
@res = {}
|
2014-04-09 13:50:12 +00:00
|
|
|
@next = sinon.stub()
|
|
|
|
describe "deleteUser", ->
|
|
|
|
|
|
|
|
it "should delete the user", (done)->
|
2014-04-09 14:41:19 +00:00
|
|
|
|
2014-04-09 13:50:12 +00:00
|
|
|
@res.send = (code)=>
|
2014-04-09 14:41:19 +00:00
|
|
|
@UserDeleter.deleteUser.calledWith(@user_id)
|
2014-04-09 13:50:12 +00:00
|
|
|
code.should.equal 200
|
|
|
|
done()
|
|
|
|
@UserController.deleteUser @req, @res
|
|
|
|
|
2014-04-09 14:41:19 +00: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 15:33:54 +00:00
|
|
|
describe "updateUserSettings", ->
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it "should set some props on ace", (done)->
|
|
|
|
@req.body =
|
|
|
|
theme: "something "
|
|
|
|
@res.send = (code)=>
|
|
|
|
@user.ace.theme.should.equal "something"
|
|
|
|
done()
|
|
|
|
@UserController.updateUserSettings @req, @res
|
|
|
|
|
|
|
|
|
2014-04-09 15:59:28 +00:00
|
|
|
describe "logout", ->
|
|
|
|
|
|
|
|
it "should destroy the session", (done)->
|
|
|
|
|
2014-04-09 16:07:19 +00:00
|
|
|
@req.session.destroy = sinon.stub().callsArgWith(0)
|
2014-04-09 15:59:28 +00:00
|
|
|
@res.redirect = (url)=>
|
|
|
|
url.should.equal "/login"
|
|
|
|
@req.session.destroy.called.should.equal true
|
|
|
|
done()
|
|
|
|
|
|
|
|
@UserController.logout @req, @res
|
|
|
|
|
|
|
|
|
2014-04-10 13:43:06 +00:00
|
|
|
describe "register", ->
|
|
|
|
|
|
|
|
it "should ask the UserRegistrationHandler to register user", (done)->
|
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
|
|
|
|
@res.send = =>
|
|
|
|
@UserRegistrationHandler.registerNewUser.calledWith(@req.body).should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.register @req, @res
|
|
|
|
|
|
|
|
it "should try and log the user in if there is an EmailAlreadyRegisterd error", (done)->
|
|
|
|
|
2014-04-10 16:15:18 +00:00
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, "EmailAlreadyRegisterd")
|
2014-04-10 13:43:06 +00:00
|
|
|
@AuthenticationController.login = (req, res)=>
|
|
|
|
assert.deepEqual req, @req
|
|
|
|
assert.deepEqual res, @res
|
|
|
|
done()
|
|
|
|
@UserController.register @req, @res
|
|
|
|
|
|
|
|
it "should put the user on the session and mark them as justRegistered", (done)->
|
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
|
|
|
|
@res.send = =>
|
|
|
|
assert.deepEqual @user, @req.session.user
|
|
|
|
assert.equal @req.session.justRegistered, true
|
|
|
|
done()
|
|
|
|
@UserController.register @req, @res
|
|
|
|
|
|
|
|
it "should redirect to project page", (done)->
|
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
|
|
|
|
@res.send = (opts)=>
|
|
|
|
opts.redir.should.equal "/project"
|
|
|
|
done()
|
|
|
|
@UserController.register @req, @res
|
|
|
|
|
|
|
|
|
|
|
|
it "should redirect passed redir if it exists", (done)->
|
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
|
|
|
|
@req.body.redir = "/somewhere"
|
|
|
|
@res.send = (opts)=>
|
|
|
|
opts.redir.should.equal "/somewhere"
|
|
|
|
done()
|
2014-04-10 16:15:18 +00:00
|
|
|
@UserController.register @req, @res
|
|
|
|
|
2014-04-15 12:59:00 +00:00
|
|
|
it "should allocate the referals", (done)->
|
|
|
|
@req.session =
|
|
|
|
referal_id : "23123"
|
|
|
|
referal_source : "email"
|
|
|
|
referal_medium : "bob"
|
|
|
|
|
|
|
|
@UserRegistrationHandler.registerNewUser.callsArgWith(1, null, @user)
|
|
|
|
@req.body.redir = "/somewhere"
|
|
|
|
@res.send = (opts)=>
|
|
|
|
@ReferalAllocator.allocate.calledWith(@req.session.referal_id, @user._id, @req.session.referal_source, @req.session.referal_medium).should.equal true
|
|
|
|
done()
|
|
|
|
@UserController.register @req, @res
|
|
|
|
|
2014-04-10 16:15:18 +00: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()
|
|
|
|
@UserController.changePassword @req, @res
|
|
|
|
|