overleaf/services/web/test/UnitTests/coffee/User/UserInfoControllerTests.coffee

163 lines
5.1 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
sinon = require('sinon')
chai = require('chai')
assert = require("chai").assert
2014-02-12 05:23:40 -05:00
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/Features/User/UserInfoController.js"
2014-02-12 05:23:40 -05:00
SandboxedModule = require('sandboxed-module')
events = require "events"
MockResponse = require "../helpers/MockResponse"
MockRequest = require "../helpers/MockRequest"
ObjectId = require("mongojs").ObjectId
describe "UserInfoController", ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@UserDeleter =
deleteUser: sinon.stub().callsArgWith(1)
@UserUpdater =
updatePersonalInfo: sinon.stub()
@sanitizer = escape:(v)->v
sinon.spy @sanitizer, "escape"
2014-06-17 06:48:14 -04:00
@UserGetter = {}
@UserInfoController = SandboxedModule.require modulePath, requires:
2014-06-17 06:48:14 -04:00
"./UserGetter": @UserGetter
"./UserUpdater": @UserUpdater
"./UserDeleter": @UserDeleter
2014-06-17 06:48:14 -04:00
"logger-sharelatex": log:->
"sanitizer":@sanitizer
2016-09-22 08:37:27 -04:00
'../Authentication/AuthenticationController': @AuthenticationController = {getLoggedInUserId: sinon.stub()}
2014-02-12 05:23:40 -05:00
@req = new MockRequest()
@res = new MockResponse()
@next = sinon.stub()
describe "getLoggedInUsersPersonalInfo", ->
beforeEach ->
@user =
_id: ObjectId()
@req.user = @user
2014-06-17 06:48:14 -04:00
@req.session.user = @user
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
2014-06-17 06:48:14 -04:00
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
2016-09-22 08:37:27 -04:00
@AuthenticationController.getLoggedInUserId = sinon.stub().returns(@user._id)
@UserInfoController.getLoggedInUsersPersonalInfo(@req, @res, @next)
2014-02-12 05:23:40 -05:00
it "should call sendFormattedPersonalInfo", ->
@UserInfoController.sendFormattedPersonalInfo
2014-02-12 05:23:40 -05:00
.calledWith(@user, @res, @next)
.should.equal true
describe "getPersonalInfo", ->
describe "when the user exists with sharelatex id", ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@user_id = ObjectId().toString()
@user =
_id: ObjectId(@user_id)
@req.params = user_id: @user_id
2014-02-12 05:23:40 -05:00
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
@UserInfoController.getPersonalInfo(@req, @res, @next)
2014-02-12 05:23:40 -05:00
it "should look up the user in the database", ->
@UserGetter.getUser
.calledWith(
{ _id: ObjectId(@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", ->
@UserInfoController.sendFormattedPersonalInfo
.calledWith(@user, @res, @next)
2014-02-12 05:23:40 -05:00
.should.equal true
describe "when the user exists with overleaf id", ->
beforeEach ->
@user_id = 12345
@user =
_id: ObjectId()
overleaf:
id: @user_id
@req.params = user_id: @user_id.toString()
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
@UserInfoController.sendFormattedPersonalInfo = sinon.stub()
@UserInfoController.getPersonalInfo(@req, @res, @next)
it "should look up the user in the database", ->
@UserGetter.getUser
.calledWith(
{ "overleaf.id": @user_id },
{ _id: true, first_name: true, last_name: true, email: true }
).should.equal true
2014-02-12 05:23:40 -05:00
it "should send the formatted details back to the client", ->
@UserInfoController.sendFormattedPersonalInfo
2014-02-12 05:23:40 -05:00
.calledWith(@user, @res, @next)
.should.equal true
describe "when the user does not exist", ->
beforeEach ->
@user_id = ObjectId().toString()
@req.params = user_id: @user_id
2014-02-12 05:23:40 -05:00
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
@UserInfoController.getPersonalInfo(@req, @res, @next)
2014-02-12 05:23:40 -05:00
it "should return 404 to the client", ->
@res.statusCode.should.equal 404
describe "when the user id is invalid", ->
beforeEach ->
@user_id = "invalid"
@req.params = user_id: @user_id
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
@UserInfoController.getPersonalInfo(@req, @res, @next)
it "should return 400 to the client", ->
@res.statusCode.should.equal 400
2014-02-12 05:23:40 -05:00
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
2017-01-06 07:41:58 -05:00
@UserInfoController.formatPersonalInfo = sinon.stub().returns(@formattedInfo)
@UserInfoController.sendFormattedPersonalInfo @user, @res
2014-02-12 05:23:40 -05:00
it "should format the user details for the response", ->
2017-01-06 07:41:58 -05:00
@UserInfoController.formatPersonalInfo
2014-02-12 05:23:40 -05:00
.calledWith(@user)
.should.equal true
it "should send the formatted details back to the client", ->
@res.body.should.equal JSON.stringify(@formattedInfo)
2017-01-06 07:41:58 -05:00
describe "formatPersonalInfo", ->
2014-02-12 05:23:40 -05:00
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-06-17 06:48:14 -04:00
role:"student"
institution:"sheffield"
2017-01-06 07:41:58 -05:00
expect(@UserInfoController.formatPersonalInfo(@user)).to.deep.equal {
id: @user._id.toString()
first_name: @user.first_name
last_name: @user.last_name
email: @user.email
signUpDate: @user.signUpDate
role: @user.role
institution: @user.institution
}
2014-02-12 05:23:40 -05:00