moved user update user settings to user controller

This commit is contained in:
Henry Oswald 2014-04-09 16:33:54 +01:00
parent bcfc1bee0c
commit bb7025ad6e
4 changed files with 53 additions and 23 deletions

View file

@ -1,6 +1,9 @@
UserDeleter = require("./UserDeleter")
UserLocator = require("./UserLocator")
User = require("../../models/User").User
newsLetterManager = require('../Newsletter/NewsletterManager')
sanitize = require('sanitizer')
logger = require("logger-sharelatex")
module.exports =
@ -14,4 +17,21 @@ module.exports =
unsubscribe: (req, res)->
UserLocator.findById req.session.user._id, (err, user)->
newsLetterManager.unsubscribe user, ->
res.send()
updateUserSettings : (req, res)->
logger.log user: req.session.user, "updating account settings"
User.findById req.session.user._id, (err, user)->
if err? or !user?
logger.err err:err, user_id:req.session.user._id, "problem updaing user settings"
return res.send 500
user.first_name = sanitize.escape(req.body.first_name).trim()
user.last_name = sanitize.escape(req.body.last_name).trim()
user.ace.mode = sanitize.escape(req.body.mode).trim()
user.ace.theme = sanitize.escape(req.body.theme).trim()
user.ace.fontSize = sanitize.escape(req.body.fontSize).trim()
user.ace.autoComplete = req.body.autoComplete == "true"
user.ace.spellCheckLanguage = req.body.spellCheckLanguage
user.ace.pdfViewer = req.body.pdfViewer
user.save ->
res.send()

View file

@ -105,24 +105,6 @@ module.exports =
res.redirect '/login'
apiUpdate : (req, res)->
logger.log user: req.session.user, "updating account settings"
metrics.inc "user.settings-update"
User.findById req.session.user._id, (err, user)->
if(user)
user.first_name = sanitize.escape(req.body.first_name).trim()
user.last_name = sanitize.escape(req.body.last_name).trim()
user.ace.mode = sanitize.escape(req.body.mode).trim()
user.ace.theme = sanitize.escape(req.body.theme).trim()
user.ace.fontSize = sanitize.escape(req.body.fontSize).trim()
user.ace.autoComplete = req.body.autoComplete == "true"
user.ace.spellCheckLanguage = req.body.spellCheckLanguage
user.ace.pdfViewer = req.body.pdfViewer
user.save()
res.send {}
changePassword : (req, res, next = (error) ->)->
metrics.inc "user.password-change"
oldPass = req.body.currentPassword

View file

@ -76,7 +76,7 @@ module.exports = class Router
app.get '/user/bonus', AuthenticationController.requireLogin(), ReferalMiddleware.getUserReferalId, ReferalController.bonus
app.get '/user/settings', AuthenticationController.requireLogin(), UserPagesController.settingsPage
app.post '/user/settings', AuthenticationController.requireLogin(), UserController.apiUpdate
app.post '/user/settings', AuthenticationController.requireLogin(), UserController_new.updateUserSettings
app.post '/user/password/update', AuthenticationController.requireLogin(), UserController.changePassword
app.get '/user/passwordreset', UserPagesController.passwordResetPage
app.post '/user/passwordReset', UserController.doRequestPasswordReset

View file

@ -13,16 +13,18 @@ describe "UserController", ->
beforeEach ->
@user =
_id:"!@£!23123"
save:sinon.stub().callsArgWith(0)
ace:{}
@UserDeleter =
deleteUser: sinon.stub().callsArgWith(1)
@UserLocator =
@User =
findById: sinon.stub().callsArgWith(1, null, @user)
@NewsLetterManager =
unsubscribe: sinon.stub().callsArgWith(1)
@UserController = SandboxedModule.require modulePath, requires:
"./UserDeleter": @UserDeleter
"./UserLocator": @UserLocator
"../../models/User": User:@User
'../Newsletter/NewsletterManager':@NewsLetterManager
"logger-sharelatex": {log:->}
@ -46,7 +48,6 @@ describe "UserController", ->
describe "unsubscribe", ->
it "should send the user to unsubscribe", (done)->
@res.send = (code)=>
@NewsLetterManager.unsubscribe.calledWith(@user).should.equal true
done()
@ -54,3 +55,30 @@ describe "UserController", ->
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