2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const sinon = require('sinon')
|
2021-03-31 08:20:55 -04:00
|
|
|
const { assert, expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath = '../../../../app/src/Features/User/UserInfoController.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const events = require('events')
|
|
|
|
const MockResponse = require('../helpers/MockResponse')
|
|
|
|
const MockRequest = require('../helpers/MockRequest')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserInfoController', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.UserDeleter = { deleteUser: sinon.stub().callsArgWith(1) }
|
|
|
|
this.UserUpdater = { updatePersonalInfo: sinon.stub() }
|
|
|
|
this.UserGetter = {}
|
|
|
|
|
|
|
|
this.UserInfoController = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2020-10-05 04:23:21 -04:00
|
|
|
mongodb: { ObjectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'./UserGetter': this.UserGetter,
|
|
|
|
'./UserUpdater': this.UserUpdater,
|
|
|
|
'./UserDeleter': this.UserDeleter,
|
|
|
|
'../Authentication/AuthenticationController': (this.AuthenticationController = {
|
2021-04-27 03:52:58 -04:00
|
|
|
getLoggedInUserId: sinon.stub(),
|
|
|
|
}),
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
this.req = new MockRequest()
|
|
|
|
this.res = new MockResponse()
|
|
|
|
return (this.next = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getLoggedInUsersPersonalInfo', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user = { _id: ObjectId() }
|
|
|
|
this.req.user = this.user
|
|
|
|
this.req.session.user = this.user
|
|
|
|
this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
|
|
|
|
this.AuthenticationController.getLoggedInUserId = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.user._id)
|
|
|
|
return this.UserInfoController.getLoggedInUsersPersonalInfo(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call sendFormattedPersonalInfo', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserInfoController.sendFormattedPersonalInfo
|
|
|
|
.calledWith(this.user, this.res, this.next)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getPersonalInfo', function () {
|
|
|
|
describe('when the user exists with sharelatex id', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
this.user = { _id: ObjectId(this.user_id) }
|
|
|
|
this.req.params = { user_id: this.user_id }
|
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
|
|
|
|
this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
|
|
|
return this.UserInfoController.getPersonalInfo(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should look up the user in the database', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserGetter.getUser
|
|
|
|
.calledWith(
|
|
|
|
{ _id: ObjectId(this.user_id) },
|
|
|
|
{ _id: true, first_name: true, last_name: true, email: true }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send the formatted details back to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserInfoController.sendFormattedPersonalInfo
|
|
|
|
.calledWith(this.user, this.res, this.next)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user exists with overleaf id', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user_id = 12345
|
|
|
|
this.user = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
overleaf: {
|
2021-04-27 03:52:58 -04:00
|
|
|
id: this.user_id,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.req.params = { user_id: this.user_id.toString() }
|
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
|
|
|
|
this.UserInfoController.sendFormattedPersonalInfo = sinon.stub()
|
|
|
|
return this.UserInfoController.getPersonalInfo(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should look up the user in the database', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserGetter.getUser
|
|
|
|
.calledWith(
|
|
|
|
{ 'overleaf.id': this.user_id },
|
|
|
|
{ _id: true, first_name: true, last_name: true, email: true }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send the formatted details back to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserInfoController.sendFormattedPersonalInfo
|
|
|
|
.calledWith(this.user, this.res, this.next)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user does not exist', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
this.req.params = { user_id: this.user_id }
|
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
return this.UserInfoController.getPersonalInfo(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return 404 to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.statusCode.should.equal(404)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the user id is invalid', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user_id = 'invalid'
|
|
|
|
this.req.params = { user_id: this.user_id }
|
|
|
|
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
return this.UserInfoController.getPersonalInfo(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return 400 to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.statusCode.should.equal(400)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('sendFormattedPersonalInfo', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
first_name: 'Douglas',
|
|
|
|
last_name: 'Adams',
|
2021-04-27 03:52:58 -04:00
|
|
|
email: 'doug@sharelatex.com',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.formattedInfo = {
|
|
|
|
id: this.user._id.toString(),
|
|
|
|
first_name: this.user.first_name,
|
|
|
|
last_name: this.user.last_name,
|
2021-04-27 03:52:58 -04:00
|
|
|
email: this.user.email,
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.UserInfoController.formatPersonalInfo = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.formattedInfo)
|
|
|
|
return this.UserInfoController.sendFormattedPersonalInfo(
|
|
|
|
this.user,
|
|
|
|
this.res
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should format the user details for the response', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserInfoController.formatPersonalInfo
|
|
|
|
.calledWith(this.user)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send the formatted details back to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.body.should.equal(JSON.stringify(this.formattedInfo))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('formatPersonalInfo', function () {
|
|
|
|
it('should return the correctly formatted data', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user = {
|
|
|
|
_id: ObjectId(),
|
|
|
|
first_name: 'Douglas',
|
|
|
|
last_name: 'Adams',
|
|
|
|
email: 'doug@sharelatex.com',
|
|
|
|
password: 'should-not-get-included',
|
|
|
|
signUpDate: new Date(),
|
|
|
|
role: 'student',
|
2021-04-27 03:52:58 -04:00
|
|
|
institution: 'sheffield',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return expect(
|
|
|
|
this.UserInfoController.formatPersonalInfo(this.user)
|
|
|
|
).to.deep.equal({
|
|
|
|
id: this.user._id.toString(),
|
|
|
|
first_name: this.user.first_name,
|
|
|
|
last_name: this.user.last_name,
|
|
|
|
email: this.user.email,
|
|
|
|
signUpDate: this.user.signUpDate,
|
|
|
|
role: this.user.role,
|
2021-04-27 03:52:58 -04:00
|
|
|
institution: this.user.institution,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|