2014-02-12 10:23:40 +00:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
2014-04-09 13:45:46 +00:00
|
|
|
modulePath = "../../../../app/js/Features/User/UserInfoController.js"
|
2014-02-12 10:23:40 +00:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
events = require "events"
|
|
|
|
MockResponse = require "../helpers/MockResponse"
|
|
|
|
MockRequest = require "../helpers/MockRequest"
|
|
|
|
ObjectId = require("mongojs").ObjectId
|
|
|
|
|
2014-04-09 13:45:46 +00:00
|
|
|
describe "UserInfoController", ->
|
2014-02-12 10:23:40 +00:00
|
|
|
beforeEach ->
|
2014-04-09 13:43:08 +00:00
|
|
|
@UserDeleter =
|
|
|
|
deleteUser: sinon.stub().callsArgWith(1)
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController = SandboxedModule.require modulePath, requires:
|
2014-02-12 10:23:40 +00:00
|
|
|
"./UserGetter": @UserGetter = {}
|
2014-04-09 13:43:08 +00:00
|
|
|
"./UserDeleter": @UserDeleter
|
|
|
|
|
2014-02-12 10:23:40 +00:00
|
|
|
@req = new MockRequest()
|
|
|
|
@res = new MockResponse()
|
|
|
|
@next = sinon.stub()
|
|
|
|
|
|
|
|
describe "getLoggedInUsersPersonalInfo", ->
|
|
|
|
beforeEach ->
|
|
|
|
@user =
|
|
|
|
_id: ObjectId()
|
|
|
|
@req.user = @user
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
|
|
|
@UserInfoController.getLoggedInUsersPersonalInfo(@req, @res, @next)
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
it "should call sendFormattedPersonalInfo", ->
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController.sendFormattedPersonalInfo
|
2014-02-12 10:23:40 +00:00
|
|
|
.calledWith(@user, @res, @next)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "getPersonalInfo", ->
|
|
|
|
beforeEach ->
|
|
|
|
@user_id = ObjectId().toString()
|
|
|
|
@user =
|
|
|
|
_id: ObjectId(@user_id)
|
|
|
|
@req.params = user_id: @user_id
|
|
|
|
|
|
|
|
describe "when the user exists", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
|
|
|
@UserInfoController.getPersonalInfo(@req, @res, @next)
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
it "should look up the user in the database", ->
|
|
|
|
@UserGetter.getUser
|
|
|
|
.calledWith(@user_id, { _id: true, first_name: true, last_name: true, email: true })
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send the formatted details back to the client", ->
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController.sendFormattedPersonalInfo
|
2014-02-12 10:23:40 +00:00
|
|
|
.calledWith(@user, @res, @next)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "when the user does not exist", ->
|
|
|
|
beforeEach ->
|
|
|
|
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
|
|
|
@UserInfoController.getPersonalInfo(@req, @res, @next)
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
it "should return 404 to the client", ->
|
|
|
|
@res.statusCode.should.equal 404
|
|
|
|
|
|
|
|
describe "sendFormattedPersonalInfo", ->
|
|
|
|
beforeEach ->
|
|
|
|
@user =
|
|
|
|
_id: ObjectId()
|
|
|
|
first_name: "Douglas"
|
|
|
|
last_name: "Adams"
|
|
|
|
email: "doug@sharelatex.com"
|
|
|
|
@formattedInfo =
|
|
|
|
id: @user._id.toString()
|
|
|
|
first_name: @user.first_name
|
|
|
|
last_name: @user.last_name
|
|
|
|
email: @user.email
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController._formatPersonalInfo = sinon.stub().callsArgWith(1, null, @formattedInfo)
|
|
|
|
@UserInfoController.sendFormattedPersonalInfo @user, @res
|
2014-02-12 10:23:40 +00:00
|
|
|
|
|
|
|
it "should format the user details for the response", ->
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController._formatPersonalInfo
|
2014-02-12 10:23:40 +00:00
|
|
|
.calledWith(@user)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should send the formatted details back to the client", ->
|
|
|
|
@res.body.should.equal JSON.stringify(@formattedInfo)
|
|
|
|
|
|
|
|
describe "_formatPersonalInfo", ->
|
|
|
|
it "should return the correctly formatted data", ->
|
|
|
|
@user =
|
|
|
|
_id: ObjectId()
|
|
|
|
first_name: "Douglas"
|
|
|
|
last_name: "Adams"
|
|
|
|
email: "doug@sharelatex.com"
|
|
|
|
password: "should-not-get-included"
|
|
|
|
signUpDate: new Date()
|
2014-04-09 13:45:46 +00:00
|
|
|
@UserInfoController._formatPersonalInfo @user, (error, info) =>
|
2014-02-12 10:23:40 +00:00
|
|
|
expect(info).to.deep.equal {
|
|
|
|
id: @user._id.toString()
|
|
|
|
first_name: @user.first_name
|
|
|
|
last_name: @user.last_name
|
|
|
|
email: @user.email
|
|
|
|
signUpDate: @user.signUpDate
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:43:08 +00:00
|
|
|
|