added controler t change user email

This commit is contained in:
Henry Oswald 2014-05-16 17:45:48 +01:00
parent c4a420bd7f
commit 7437115967
3 changed files with 99 additions and 2 deletions

View file

@ -10,6 +10,7 @@ Url = require("url")
AuthenticationController = require("../Authentication/AuthenticationController")
AuthenticationManager = require("../Authentication/AuthenticationManager")
ReferalAllocator = require("../Referal/ReferalAllocator")
UserUpdater = require("./UserUpdater")
module.exports =
@ -100,4 +101,16 @@ module.exports =
res.send
message:
type:'error'
text:'Your old password is wrong'
text:'Your old password is wrong'
changeEmailAddress: (req, res)->
newEmail = req.body.email
user_id = req.session.user._id
if !newEmail? or newEmail.length == 0 or newEmail.indexOf("@") == -1
return res.send(412)
UserUpdater.changeEmailAddress user_id, newEmail, (err)->
if err?
return res.send 500, {message:err?.message}
res.send 200

View file

@ -35,9 +35,12 @@ describe "UserController", ->
setUserPassword: sinon.stub()
@ReferalAllocator =
allocate:sinon.stub()
@UserUpdater =
changeEmailAddress:sinon.stub()
@UserController = SandboxedModule.require modulePath, requires:
"./UserLocator": @UserLocator
"./UserDeleter": @UserDeleter
"./UserUpdater":@UserUpdater
"../../models/User": User:@User
'../Newsletter/NewsletterManager':@NewsLetterManager
"./UserRegistrationHandler":@UserRegistrationHandler
@ -201,5 +204,39 @@ describe "UserController", ->
@res.send = =>
@AuthenticationManager.setUserPassword.calledWith(@user._id, "newpass").should.equal true
done()
@UserController.changePassword @req, @res
@UserController.changePassword @req, @res
describe "changeEmailAddress", ->
beforeEach ->
@newEmail = "new@email.com"
it "should return an error if the email address is null", (done)->
@req.body.email = null
@res.send = (code)->
code.should.equal 412
done()
@UserController.changeEmailAddress @req, @res
it "should send an error if the email is 0 len", (done)->
@req.body.email = ""
@res.send = (code)->
code.should.equal 412
done()
@UserController.changeEmailAddress @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)->
code.should.equal 412
done()
@UserController.changeEmailAddress @req, @res
it "should call the user updater with the new email and user _id", (done)->
@req.body.email = @newEmail
@UserUpdater.changeEmailAddress.callsArgWith(2)
@res.send = (code)=>
code.should.equal 200
@UserUpdater.changeEmailAddress.calledWith(@user_id, @newEmail).should.equal true
done()
@UserController.changeEmailAddress @req, @res

View file

@ -0,0 +1,47 @@
should = require('chai').should()
SandboxedModule = require('sandboxed-module')
assert = require('assert')
path = require('path')
sinon = require('sinon')
modulePath = path.join __dirname, "../../../../app/js/Features/User/UserUpdater"
expect = require("chai").expect
describe "UserUpdater", ->
beforeEach ->
@settings = {}
@mongojs =
db:{}
ObjectId:(id)-> return id
@UserLocator =
findById:sinon.stub()
@UserUpdater = SandboxedModule.require modulePath, requires:
"settings-sharelatex":@settings
"logger-sharelatex": log:->
"./UserLocator":@UserLocator
"../../infrastructure/mongojs":@mongojs
@stubbedUser =
name:"bob"
email:"hello@world.com"
@user_id = "3131231"
@newEmail = "bob@bob.com"
describe "changeEmailAddress", ->
beforeEach ->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2)
it "should check if the new email already has an account", (done)->
@UserLocator.findById.callsArgWith(1, null, @stubbedUser)
@UserUpdater.changeEmailAddress @user_id, @newEmail, (err)=>
@UserUpdater.updateUser.called.should.equal false
should.exist(err)
done()
it "should set the users password", (done)->
@UserLocator.findById.callsArgWith(1, null)
@UserUpdater.changeEmailAddress @user_id, @newEmail, (err)=>
@UserUpdater.updateUser.calledWith(@user_id, $set: { "email": @newEmail}).should.equal true
done()