From bb7025ad6e7c046555b0887b7f62b4f6f65395c1 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Wed, 9 Apr 2014 16:33:54 +0100 Subject: [PATCH] moved user update user settings to user controller --- .../Features/User/UserController.coffee | 22 +++++++++++- .../coffee/controllers/UserController.coffee | 18 ---------- services/web/app/coffee/router.coffee | 2 +- .../coffee/User/UserControllerTests.coffee | 34 +++++++++++++++++-- 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/services/web/app/coffee/Features/User/UserController.coffee b/services/web/app/coffee/Features/User/UserController.coffee index 373b9278a3..c09eb61542 100644 --- a/services/web/app/coffee/Features/User/UserController.coffee +++ b/services/web/app/coffee/Features/User/UserController.coffee @@ -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() \ No newline at end of file diff --git a/services/web/app/coffee/controllers/UserController.coffee b/services/web/app/coffee/controllers/UserController.coffee index c6ac5a5ae5..9ded474b0e 100644 --- a/services/web/app/coffee/controllers/UserController.coffee +++ b/services/web/app/coffee/controllers/UserController.coffee @@ -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 diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index f1bf6f586c..afbf602bc7 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -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 diff --git a/services/web/test/UnitTests/coffee/User/UserControllerTests.coffee b/services/web/test/UnitTests/coffee/User/UserControllerTests.coffee index e9952d21b6..cd067f0315 100644 --- a/services/web/test/UnitTests/coffee/User/UserControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/User/UserControllerTests.coffee @@ -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 + +