| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- /* 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')
- describe('UserPagesController', function () {
- beforeEach(function () {
- this.settings = {
- apis: {
- v1: {
- url: 'some.host',
- user: 'one',
- pass: 'two',
- },
- },
- }
- this.user = {
- _id: (this.user_id = 'kwjewkl'),
- features: {},
- email: 'joe@example.com',
- ip_address: '1.1.1.1',
- session_created: 'timestamp',
- thirdPartyIdentifiers: [
- {
- providerId: 'google',
- externalUserId: 'testId',
- },
- ],
- refProviders: {
- mendeley: true,
- zotero: true,
- },
- }
- this.UserGetter = {
- getUser: sinon.stub(),
- promises: { getUser: sinon.stub() },
- }
- this.UserSessionsManager = { getAllUserSessions: sinon.stub() }
- this.dropboxStatus = {}
- this.ErrorController = { notFound: sinon.stub() }
- this.SessionManager = {
- getLoggedInUserId: sinon.stub().returns(this.user._id),
- getSessionUser: sinon.stub().returns(this.user),
- }
- this.NewsletterManager = {
- subscribed: sinon.stub().yields(),
- }
- this.AuthenticationController = {
- _getRedirectFromSession: sinon.stub(),
- setRedirectInSession: sinon.stub(),
- }
- this.UserPagesController = SandboxedModule.require(modulePath, {
- requires: {
- '@overleaf/settings': this.settings,
- './UserGetter': this.UserGetter,
- './UserSessionsManager': this.UserSessionsManager,
- '../Newsletter/NewsletterManager': this.NewsletterManager,
- '../Errors/ErrorController': this.ErrorController,
- '../Authentication/AuthenticationController':
- this.AuthenticationController,
- '../Authentication/SessionManager': this.SessionManager,
- request: (this.request = sinon.stub()),
- },
- })
- this.req = {
- query: {},
- session: {
- user: this.user,
- },
- }
- return (this.res = {})
- })
- describe('registerPage', function () {
- it('should render the register page', function (done) {
- this.res.render = page => {
- page.should.equal('user/register')
- return done()
- }
- return this.UserPagesController.registerPage(this.req, this.res)
- })
- it('should set sharedProjectData', function (done) {
- 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)
- })
- it('should set newTemplateData', function (done) {
- 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)
- })
- it('should not set the newTemplateData if there is nothing in the session', function (done) {
- this.res.render = (page, opts) => {
- assert.equal(opts.newTemplateData.templateName, undefined)
- return done()
- }
- return this.UserPagesController.registerPage(this.req, this.res)
- })
- })
- describe('loginForm', function () {
- it('should render the login page', function (done) {
- this.res.render = page => {
- page.should.equal('user/login')
- return done()
- }
- return this.UserPagesController.loginPage(this.req, this.res)
- })
- describe('when an explicit redirect is set via query string', function () {
- beforeEach(function () {
- this.AuthenticationController._getRedirectFromSession = sinon
- .stub()
- .returns(null)
- this.AuthenticationController.setRedirectInSession = sinon.stub()
- return (this.req.query.redir = '/somewhere/in/particular')
- })
- it('should set a redirect', function (done) {
- 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)
- })
- })
- })
- describe('sessionsPage', function () {
- beforeEach(function () {
- return this.UserSessionsManager.getAllUserSessions.callsArgWith(
- 2,
- null,
- []
- )
- })
- it('should render user/sessions', function (done) {
- this.res.render = function (page) {
- page.should.equal('user/sessions')
- return done()
- }
- return this.UserPagesController.sessionsPage(this.req, this.res)
- })
- it('should include current session data in the view', function (done) {
- this.res.render = (page, opts) => {
- expect(opts.currentSession).to.deep.equal({
- ip_address: '1.1.1.1',
- session_created: 'timestamp',
- })
- return done()
- }
- return this.UserPagesController.sessionsPage(this.req, this.res)
- })
- it('should have called getAllUserSessions', function (done) {
- this.res.render = page => {
- this.UserSessionsManager.getAllUserSessions.callCount.should.equal(1)
- return done()
- }
- return this.UserPagesController.sessionsPage(this.req, this.res)
- })
- describe('when getAllUserSessions produces an error', function () {
- beforeEach(function () {
- return this.UserSessionsManager.getAllUserSessions.callsArgWith(
- 2,
- new Error('woops')
- )
- })
- it('should call next with an error', function (done) {
- this.next = err => {
- assert(err !== null)
- assert(err instanceof Error)
- return done()
- }
- return this.UserPagesController.sessionsPage(
- this.req,
- this.res,
- this.next
- )
- })
- })
- })
- describe('emailPreferencesPage', function () {
- beforeEach(function () {
- this.UserGetter.getUser = sinon.stub().yields(null, this.user)
- })
- it('render page with subscribed status', function (done) {
- this.NewsletterManager.subscribed.yields(null, true)
- this.res.render = function (page, data) {
- page.should.equal('user/email-preferences')
- data.title.should.equal('newsletter_info_title')
- data.subscribed.should.equal(true)
- return done()
- }
- return this.UserPagesController.emailPreferencesPage(this.req, this.res)
- })
- it('render page with unsubscribed status', function (done) {
- this.NewsletterManager.subscribed.yields(null, false)
- this.res.render = function (page, data) {
- page.should.equal('user/email-preferences')
- data.title.should.equal('newsletter_info_title')
- data.subscribed.should.equal(false)
- return done()
- }
- return this.UserPagesController.emailPreferencesPage(this.req, this.res)
- })
- })
- describe('settingsPage', function () {
- beforeEach(function () {
- this.request.get = sinon
- .stub()
- .callsArgWith(1, null, { statusCode: 200 }, { has_password: true })
- this.UserGetter.promises.getUser = sinon.stub().resolves(this.user)
- })
- it('should render user/settings', function (done) {
- this.res.render = function (page) {
- page.should.equal('user/settings')
- return done()
- }
- return this.UserPagesController.settingsPage(this.req, this.res)
- })
- it('should send user', function (done) {
- this.res.render = (page, opts) => {
- opts.user.id.should.equal(this.user._id)
- opts.user.email.should.equal(this.user.email)
- return done()
- }
- return this.UserPagesController.settingsPage(this.req, this.res)
- })
- it("should set 'shouldAllowEditingDetails' to true", function (done) {
- this.res.render = (page, opts) => {
- opts.shouldAllowEditingDetails.should.equal(true)
- return done()
- }
- return this.UserPagesController.settingsPage(this.req, this.res)
- })
- it('should restructure thirdPartyIdentifiers data for template use', function (done) {
- const expectedResult = {
- google: 'testId',
- }
- this.res.render = (page, opts) => {
- expect(opts.thirdPartyIds).to.include(expectedResult)
- return done()
- }
- return this.UserPagesController.settingsPage(this.req, this.res)
- })
- it("should set and clear 'projectSyncSuccessMessage'", function (done) {
- this.req.session.projectSyncSuccessMessage = 'Some Sync Success'
- this.res.render = (page, opts) => {
- opts.projectSyncSuccessMessage.should.equal('Some Sync Success')
- expect(this.req.session.projectSyncSuccessMessage).to.not.exist
- return done()
- }
- return this.UserPagesController.settingsPage(this.req, this.res)
- })
- describe('when ldap.updateUserDetailsOnLogin is true', function () {
- beforeEach(function () {
- return (this.settings.ldap = { updateUserDetailsOnLogin: true })
- })
- afterEach(function () {
- return delete this.settings.ldap
- })
- it('should set "shouldAllowEditingDetails" to false', function (done) {
- this.res.render = (page, opts) => {
- opts.shouldAllowEditingDetails.should.equal(false)
- return done()
- }
- return this.UserPagesController.settingsPage(this.req, this.res)
- })
- })
- describe('when saml.updateUserDetailsOnLogin is true', function () {
- beforeEach(function () {
- return (this.settings.saml = { updateUserDetailsOnLogin: true })
- })
- afterEach(function () {
- return delete this.settings.saml
- })
- it('should set "shouldAllowEditingDetails" to false', function (done) {
- this.res.render = (page, opts) => {
- opts.shouldAllowEditingDetails.should.equal(false)
- return done()
- }
- return this.UserPagesController.settingsPage(this.req, this.res)
- })
- })
- })
- })
|