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 SandboxedModule = require('sandboxed-module')
|
|
|
|
const assert = require('assert')
|
|
|
|
const path = require('path')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../../app/src/Features/User/UserPagesController'
|
|
|
|
)
|
|
|
|
const { expect } = require('chai')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('UserPagesController', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.settings = {
|
|
|
|
apis: {
|
|
|
|
v1: {
|
|
|
|
url: 'some.host',
|
|
|
|
user: 'one',
|
2021-04-27 03:52:58 -04:00
|
|
|
pass: 'two',
|
|
|
|
},
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.user = {
|
|
|
|
_id: (this.user_id = 'kwjewkl'),
|
|
|
|
features: {},
|
|
|
|
email: 'joe@example.com',
|
|
|
|
thirdPartyIdentifiers: [
|
|
|
|
{
|
|
|
|
providerId: 'google',
|
2021-04-27 03:52:58 -04:00
|
|
|
externalUserId: 'testId',
|
|
|
|
},
|
|
|
|
],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.UserGetter = { getUser: sinon.stub() }
|
|
|
|
this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
|
|
|
|
this.dropboxStatus = {}
|
|
|
|
this.ErrorController = { notFound: sinon.stub() }
|
|
|
|
this.AuthenticationController = {
|
|
|
|
getLoggedInUserId: sinon.stub().returns(this.user._id),
|
|
|
|
getSessionUser: sinon.stub().returns(this.user),
|
|
|
|
_getRedirectFromSession: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
setRedirectInSession: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.UserPagesController = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'settings-sharelatex': this.settings,
|
|
|
|
'./UserGetter': this.UserGetter,
|
|
|
|
'./UserSessionsManager': this.UserSessionsManager,
|
|
|
|
'../Errors/ErrorController': this.ErrorController,
|
|
|
|
'../Authentication/AuthenticationController': this
|
|
|
|
.AuthenticationController,
|
2021-04-27 03:52:58 -04:00
|
|
|
request: (this.request = sinon.stub()),
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
this.req = {
|
|
|
|
query: {},
|
|
|
|
session: {
|
2021-04-27 03:52:58 -04:00
|
|
|
user: this.user,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return (this.res = {})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('registerPage', function () {
|
|
|
|
it('should render the register page', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = page => {
|
|
|
|
page.should.equal('user/register')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.registerPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set sharedProjectData', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.query.project_name = 'myProject'
|
|
|
|
this.req.query.user_first_name = 'user_first_name_here'
|
|
|
|
|
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
opts.sharedProjectData.project_name.should.equal('myProject')
|
|
|
|
opts.sharedProjectData.user_first_name.should.equal(
|
|
|
|
'user_first_name_here'
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.registerPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set newTemplateData', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.session.templateData = { templateName: 'templateName' }
|
|
|
|
|
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
opts.newTemplateData.templateName.should.equal('templateName')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.registerPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not set the newTemplateData if there is nothing in the session', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
assert.equal(opts.newTemplateData.templateName, undefined)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.registerPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('loginForm', function () {
|
|
|
|
it('should render the login page', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = page => {
|
|
|
|
page.should.equal('user/login')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.loginPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when an explicit redirect is set via query string', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.AuthenticationController._getRedirectFromSession = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(null)
|
|
|
|
this.AuthenticationController.setRedirectInSession = sinon.stub()
|
|
|
|
return (this.req.query.redir = '/somewhere/in/particular')
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set a redirect', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = page => {
|
|
|
|
this.AuthenticationController.setRedirectInSession.callCount.should.equal(
|
|
|
|
1
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
this.AuthenticationController.setRedirectInSession.lastCall.args[1]
|
|
|
|
).to.equal(this.req.query.redir)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.loginPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('sessionsPage', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserSessionsManager.getAllUserSessions.callsArgWith(
|
|
|
|
2,
|
|
|
|
null,
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should render user/sessions', function (done) {
|
|
|
|
this.res.render = function (page) {
|
2019-05-29 05:21:06 -04:00
|
|
|
page.should.equal('user/sessions')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.sessionsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should have called getAllUserSessions', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = page => {
|
|
|
|
this.UserSessionsManager.getAllUserSessions.callCount.should.equal(1)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.sessionsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when getAllUserSessions produces an error', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.UserSessionsManager.getAllUserSessions.callsArgWith(
|
|
|
|
2,
|
|
|
|
new Error('woops')
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call next with an error', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.next = err => {
|
|
|
|
assert(err !== null)
|
|
|
|
assert(err instanceof Error)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.sessionsPage(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('settingsPage', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 200 }, { has_password: true })
|
|
|
|
return (this.UserGetter.getUser = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, this.user))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should render user/settings', function (done) {
|
|
|
|
this.res.render = function (page) {
|
2019-05-29 05:21:06 -04:00
|
|
|
page.should.equal('user/settings')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.settingsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should send user', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
opts.user.should.equal(this.user)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.settingsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it("should set 'shouldAllowEditingDetails' to true", function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
opts.shouldAllowEditingDetails.should.equal(true)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.settingsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should restructure thirdPartyIdentifiers data for template use', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const expectedResult = {
|
2021-04-27 03:52:58 -04:00
|
|
|
google: 'testId',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
expect(opts.thirdPartyIds).to.include(expectedResult)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.settingsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when ldap.updateUserDetailsOnLogin is true', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.settings.ldap = { updateUserDetailsOnLogin: true })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return delete this.settings.ldap
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set "shouldAllowEditingDetails" to false', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
opts.shouldAllowEditingDetails.should.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.settingsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when saml.updateUserDetailsOnLogin is true', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return (this.settings.saml = { updateUserDetailsOnLogin: true })
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return delete this.settings.saml
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set "shouldAllowEditingDetails" to false', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res.render = (page, opts) => {
|
|
|
|
opts.shouldAllowEditingDetails.should.equal(false)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
return this.UserPagesController.settingsPage(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|