endpoint for user details is written

This commit is contained in:
Henry Oswald 2014-06-11 12:46:28 +01:00
parent 3aff131428
commit 473da4fa4c
5 changed files with 55 additions and 11 deletions

View file

@ -22,13 +22,13 @@ module.exports = UserController =
req.session.destroy()
updatePersonalInfo: (req, res, next = (error)->) ->
{first_name, last_name, role, university} = req.body
{first_name, last_name, role, institution} = req.body
update =
first_name:sanitize.escape(first_name)
last_name:sanitize.escape(last_name)
role:sanitize.escape(role)
university:sanitize.escape(university)
UserUpdater.updatePersonalInfo update, (err)->
institution:sanitize.escape(institution)
UserUpdater.updatePersonalInfo req.session.user._id, update, (err)->
if err?
res.send 500
else

View file

@ -28,3 +28,14 @@ module.exports = UserUpdater =
return callback(err)
callback()
updatePersonalInfo: (user_id, info, callback)->
self = @
update =
$set:
"first_name": info.first_name
"last_name": info.last_name
"role": info.role
"institution": info.institution
self.updateUser user_id.toString(), update, (err)->
callback(err)

View file

@ -10,6 +10,8 @@ UserSchema = new Schema
email : {type : String, default : ''}
first_name : {type : String, default : ''}
last_name : {type : String, default : ''}
role : {type : String, default : ''}
institution : {type : String, default : ''}
hashedPassword : String
isAdmin : {type : Boolean, default : false}
confirmed : {type : Boolean, default : false}

View file

@ -117,41 +117,45 @@ describe "UserInfoController", ->
describe "setPersonalInfo", ->
beforeEach ->
@req = {}
@req =
session:
user:
_id:"123123j321jikuj90jlk"
@req.body =
first_name: "bob"
last_name: "smith"
role:"student"
university: "Sheffield"
institution: "Sheffield"
notWanted: "something"
it "should send the data from the body to the user updater", (done)->
@UserUpdater.updatePersonalInfo.callsArgWith(1, null)
@UserUpdater.updatePersonalInfo.callsArgWith(2, null)
@res.send = (statusCode)=>
statusCode.should.equal 204
args = @UserUpdater.updatePersonalInfo.args[0][0]
@UserUpdater.updatePersonalInfo.args[0][0].should.equal @req.session.user._id
args = @UserUpdater.updatePersonalInfo.args[0][1]
args.first_name.should.equal @req.body.first_name
args.last_name.should.equal @req.body.last_name
args.role.should.equal @req.body.role
args.university.should.equal @req.body.university
args.institution.should.equal @req.body.institution
assert.equal args.notWanted, undefined
done()
@UserInfoController.updatePersonalInfo @req, @res
it "should sanitize the data", (done)->
@UserUpdater.updatePersonalInfo.callsArgWith(1, null)
@UserUpdater.updatePersonalInfo.callsArgWith(2, null)
@res.send = (statusCode)=>
@sanitizer.escape.calledWith(@req.body.first_name).should.equal true
@sanitizer.escape.calledWith(@req.body.last_name).should.equal true
@sanitizer.escape.calledWith(@req.body.role).should.equal true
@sanitizer.escape.calledWith(@req.body.university).should.equal true
@sanitizer.escape.calledWith(@req.body.institution).should.equal true
done()
@UserInfoController.updatePersonalInfo @req, @res
it "should send an error if the UpserUpdater returns on", (done)->
@UserUpdater.updatePersonalInfo.callsArgWith(1, "error")
@UserUpdater.updatePersonalInfo.callsArgWith(2, "error")
@res.send = (statusCode)->
statusCode.should.equal 500
done()

View file

@ -45,3 +45,30 @@ describe "UserUpdater", ->
@UserUpdater.changeEmailAddress @user_id, @newEmail, (err)=>
@UserUpdater.updateUser.calledWith(@user_id, $set: { "email": @newEmail}).should.equal true
done()
describe "updatePersonalInfo", ->
beforeEach ->
@info =
first_name:"billy"
last_name:"brag"
role:"student"
institution:"sheffield"
it "should set the names role and institution", (done)->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2)
@UserUpdater.updatePersonalInfo @user_id, @info, (err)=>
@UserUpdater.updateUser.args[0][0].should.equal @user_id
args = @UserUpdater.updateUser.args[0][1]
args["$set"].first_name.should.equal @info.first_name
args["$set"].last_name.should.equal @info.last_name
args["$set"].role.should.equal @info.role
args["$set"].institution.should.equal @info.institution
done()
it "should return the error", (done)->
@UserUpdater.updateUser = sinon.stub().callsArgWith(2, "error")
@UserUpdater.updatePersonalInfo @user_id, @info, (err)=>
should.exist(err)
done()