| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- max-len,
- no-path-concat,
- 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
- * DS206: Consider reworking classes to avoid initClass
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const spies = require('chai-spies')
- const chai = require('chai').use(spies)
- const sinon = require('sinon')
- const should = chai.should()
- const { expect } = chai
- const modulePath =
- '../../../../app/src/Features/Project/ProjectCreationHandler.js'
- const SandboxedModule = require('sandboxed-module')
- const Settings = require('settings-sharelatex')
- const Path = require('path')
- const _ = require('underscore')
- describe('ProjectCreationHandler', function() {
- const ownerId = '4eecb1c1bffa66588e0000a1'
- const projectName = 'project name goes here'
- const project_id = '4eecaffcbffa66588e000008'
- const docId = '4eecb17ebffa66588e00003f'
- const rootFolderId = '234adfa3r2afe'
- beforeEach(function() {
- let Folder, Project
- this.ProjectModel = Project = (function() {
- Project = class Project {
- static initClass() {
- this.prototype.save = sinon.stub().callsArg(0)
- this.prototype.rootFolder = [
- {
- _id: rootFolderId,
- docs: []
- }
- ]
- }
- constructor(options) {
- if (options == null) {
- options = {}
- }
- this._id = project_id
- this.owner_ref = options.owner_ref
- this.name = options.name
- this.overleaf = { history: {} }
- }
- }
- Project.initClass()
- return Project
- })()
- this.FolderModel = Folder = class Folder {
- constructor(options) {
- ;({ name: this.name } = options)
- }
- }
- this.ProjectEntityUpdateHandler = {
- addDoc: sinon.stub().callsArgWith(5, null, { _id: docId }),
- addFile: sinon.stub().callsArg(6),
- setRootDoc: sinon.stub().callsArg(2)
- }
- this.ProjectDetailsHandler = { validateProjectName: sinon.stub().yields() }
- this.HistoryManager = { initializeProject: sinon.stub().callsArg(0) }
- this.user = {
- first_name: 'first name here',
- last_name: 'last name here',
- ace: {
- spellCheckLanguage: 'de'
- }
- }
- this.User = { findById: sinon.stub().callsArgWith(2, null, this.user) }
- this.callback = sinon.stub()
- this.Settings = { apis: { project_history: {} } }
- this.AnalyticsManager = { recordEvent: sinon.stub() }
- return (this.handler = SandboxedModule.require(modulePath, {
- requires: {
- '../../models/User': {
- User: this.User
- },
- '../../models/Project': { Project: this.ProjectModel },
- '../../models/Folder': { Folder: this.FolderModel },
- '../History/HistoryManager': this.HistoryManager,
- './ProjectEntityUpdateHandler': this.ProjectEntityUpdateHandler,
- './ProjectDetailsHandler': this.ProjectDetailsHandler,
- 'settings-sharelatex': this.Settings,
- '../Analytics/AnalyticsManager': this.AnalyticsManager,
- 'logger-sharelatex': { log() {} },
- 'metrics-sharelatex': {
- inc() {},
- timeAsyncMethod() {}
- }
- }
- }))
- })
- describe('Creating a Blank project', function() {
- beforeEach(function() {
- this.overleaf_id = 1234
- this.HistoryManager.initializeProject = sinon
- .stub()
- .callsArgWith(0, null, { overleaf_id: this.overleaf_id })
- return (this.ProjectModel.prototype.save = sinon.stub().callsArg(0))
- })
- describe('successfully', function() {
- it('should save the project', function(done) {
- return this.handler.createBlankProject(ownerId, projectName, () => {
- this.ProjectModel.prototype.save.called.should.equal(true)
- return done()
- })
- })
- it('should return the project in the callback', function(done) {
- return this.handler.createBlankProject(ownerId, projectName, function(
- err,
- project
- ) {
- project.name.should.equal(projectName)
- ;(project.owner_ref + '').should.equal(ownerId)
- return done()
- })
- })
- it('should initialize the project overleaf if history id not provided', function(done) {
- this.handler.createBlankProject(ownerId, projectName, done)
- return this.HistoryManager.initializeProject
- .calledWith()
- .should.equal(true)
- })
- it('should set the overleaf id if overleaf id not provided', function(done) {
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- (err, project) => {
- project.overleaf.history.id.should.equal(this.overleaf_id)
- return done()
- }
- )
- })
- it('should set the overleaf id if overleaf id provided', function(done) {
- const overleaf_id = 2345
- const attributes = {
- overleaf: {
- history: {
- id: overleaf_id
- }
- }
- }
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- attributes,
- function(err, project) {
- project.overleaf.history.id.should.equal(overleaf_id)
- return done()
- }
- )
- })
- it('should set the language from the user', function(done) {
- return this.handler.createBlankProject(ownerId, projectName, function(
- err,
- project
- ) {
- project.spellCheckLanguage.should.equal('de')
- return done()
- })
- })
- it('should set the imageName to currentImageName if set and no imageName attribute', function(done) {
- this.Settings.currentImageName = 'mock-image-name'
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- (err, project) => {
- project.imageName.should.equal(this.Settings.currentImageName)
- return done()
- }
- )
- })
- it('should not set the imageName if no currentImageName', function(done) {
- this.Settings.currentImageName = null
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- (err, project) => {
- expect(project.imageName).to.not.exist
- return done()
- }
- )
- })
- it('should set the imageName to the attribute value if set and not overwrite it with the currentImageName', function(done) {
- this.Settings.currentImageName = 'mock-image-name'
- const attributes = { imageName: 'attribute-image-name' }
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- attributes,
- (err, project) => {
- project.imageName.should.equal(attributes.imageName)
- return done()
- }
- )
- })
- it('should not set the overleaf.history.display if not configured in settings', function(done) {
- this.Settings.apis.project_history.displayHistoryForNewProjects = false
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- (err, project) => {
- expect(project.overleaf.history.display).to.not.exist
- return done()
- }
- )
- })
- it('should set the overleaf.history.display if configured in settings', function(done) {
- this.Settings.apis.project_history.displayHistoryForNewProjects = true
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- (err, project) => {
- expect(project.overleaf.history.display).to.equal(true)
- return done()
- }
- )
- })
- it('should send a project-created event to analytics', function(done) {
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- (err, project) => {
- expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
- expect(
- this.AnalyticsManager.recordEvent.calledWith(
- ownerId,
- 'project-created'
- )
- ).to.equal(true)
- return done()
- }
- )
- })
- it('should send a project-created event with template information if provided', function(done) {
- const attributes = {
- fromV1TemplateId: 100
- }
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- attributes,
- (err, project) => {
- expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
- expect(
- this.AnalyticsManager.recordEvent.calledWith(
- ownerId,
- 'project-created',
- { projectId: project._id, attributes }
- )
- ).to.equal(true)
- return done()
- }
- )
- })
- it('should send a project-imported event when importing a project', function(done) {
- const attributes = {
- overleaf: {
- history: {
- id: 100
- }
- }
- }
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- attributes,
- (err, project) => {
- expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
- expect(
- this.AnalyticsManager.recordEvent.calledWith(
- ownerId,
- 'project-imported'
- )
- ).to.equal(true)
- return done()
- }
- )
- })
- })
- describe('with an error', function() {
- beforeEach(function() {
- this.ProjectModel.prototype.save = sinon
- .stub()
- .callsArgWith(0, new Error('something went wrong'))
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- this.callback
- )
- })
- it('should return the error to the callback', function() {
- return should.exist(this.callback.args[0][0])
- })
- })
- describe('with an invalid name', function() {
- beforeEach(function() {
- this.ProjectDetailsHandler.validateProjectName = sinon
- .stub()
- .yields(new Error('bad name'))
- return this.handler.createBlankProject(
- ownerId,
- projectName,
- this.callback
- )
- })
- it('should return the error to the callback', function() {
- return should.exist(this.callback.args[0][0])
- })
- it('should not try to create the project', function() {
- return this.ProjectModel.prototype.save.called.should.equal(false)
- })
- })
- })
- describe('Creating a basic project', function() {
- beforeEach(function() {
- this.project = new this.ProjectModel()
- this.handler._buildTemplate = function(
- template_name,
- user,
- project_name,
- callback
- ) {
- if (template_name === 'mainbasic.tex') {
- return callback(null, ['mainbasic.tex', 'lines'])
- }
- throw new Error(`unknown template: ${template_name}`)
- }
- sinon.spy(this.handler, '_buildTemplate')
- this.handler.createBlankProject = sinon
- .stub()
- .callsArgWith(2, null, this.project)
- this.handler._createRootDoc = sinon
- .stub()
- .callsArgWith(3, null, this.project)
- return this.handler.createBasicProject(
- ownerId,
- projectName,
- this.callback
- )
- })
- it('should create a blank project first', function() {
- return this.handler.createBlankProject
- .calledWith(ownerId, projectName)
- .should.equal(true)
- })
- it('should create the root document', function() {
- return this.handler._createRootDoc
- .calledWith(this.project, ownerId, ['mainbasic.tex', 'lines'])
- .should.equal(true)
- })
- it('should build the mainbasic.tex template', function() {
- return this.handler._buildTemplate
- .calledWith('mainbasic.tex', ownerId, projectName)
- .should.equal(true)
- })
- })
- describe('Creating a project from a snippet', function() {
- beforeEach(function() {
- this.project = new this.ProjectModel()
- this.handler.createBlankProject = sinon
- .stub()
- .callsArgWith(2, null, this.project)
- this.handler._createRootDoc = sinon
- .stub()
- .callsArgWith(3, null, this.project)
- return this.handler.createProjectFromSnippet(
- ownerId,
- projectName,
- ['snippet line 1', 'snippet line 2'],
- this.callback
- )
- })
- it('should create a blank project first', function() {
- return this.handler.createBlankProject
- .calledWith(ownerId, projectName)
- .should.equal(true)
- })
- it('should create the root document', function() {
- return this.handler._createRootDoc
- .calledWith(this.project, ownerId, ['snippet line 1', 'snippet line 2'])
- .should.equal(true)
- })
- })
- describe('Creating an example project', function() {
- beforeEach(function() {
- this.project = new this.ProjectModel()
- this.handler._buildTemplate = function(
- template_name,
- user,
- project_name,
- callback
- ) {
- if (template_name === 'main.tex') {
- return callback(null, ['main.tex', 'lines'])
- }
- if (template_name === 'references.bib') {
- return callback(null, ['references.bib', 'lines'])
- }
- throw new Error(`unknown template: ${template_name}`)
- }
- sinon.spy(this.handler, '_buildTemplate')
- this.handler.createBlankProject = sinon
- .stub()
- .callsArgWith(2, null, this.project)
- this.handler._createRootDoc = sinon
- .stub()
- .callsArgWith(3, null, this.project)
- return this.handler.createExampleProject(
- ownerId,
- projectName,
- this.callback
- )
- })
- it('should create a blank project first', function() {
- return this.handler.createBlankProject
- .calledWith(ownerId, projectName)
- .should.equal(true)
- })
- it('should create the root document', function() {
- return this.handler._createRootDoc
- .calledWith(this.project, ownerId, ['main.tex', 'lines'])
- .should.equal(true)
- })
- it('should insert references.bib', function() {
- return this.ProjectEntityUpdateHandler.addDoc
- .calledWith(
- project_id,
- rootFolderId,
- 'references.bib',
- ['references.bib', 'lines'],
- ownerId
- )
- .should.equal(true)
- })
- it('should insert universe.jpg', function() {
- return this.ProjectEntityUpdateHandler.addFile
- .calledWith(
- project_id,
- rootFolderId,
- 'universe.jpg',
- Path.resolve(
- __dirname + '/../../../../app/templates/project_files/universe.jpg'
- ),
- null,
- ownerId
- )
- .should.equal(true)
- })
- it('should build the main.tex template', function() {
- return this.handler._buildTemplate
- .calledWith('main.tex', ownerId, projectName)
- .should.equal(true)
- })
- it('should build the references.bib template', function() {
- return this.handler._buildTemplate
- .calledWith('references.bib', ownerId, projectName)
- .should.equal(true)
- })
- })
- describe('_buildTemplate', function() {
- beforeEach(function(done) {
- return this.handler._buildTemplate(
- 'main.tex',
- this.user_id,
- projectName,
- (err, templateLines) => {
- this.template = templateLines.reduce(
- (singleLine, line) => `${singleLine}\n${line}`
- )
- return done()
- }
- )
- })
- it('should insert the project name into the template', function(done) {
- this.template.indexOf(projectName).should.not.equal(-1)
- return done()
- })
- it('should insert the users name into the template', function(done) {
- this.template.indexOf(this.user.first_name).should.not.equal(-1)
- this.template.indexOf(this.user.last_name).should.not.equal(-1)
- return done()
- })
- it('should not have undefined in the template', function(done) {
- this.template.indexOf('undefined').should.equal(-1)
- return done()
- })
- it('should not have any underscore brackets in the output', function(done) {
- this.template.indexOf('{{').should.equal(-1)
- this.template.indexOf('<%=').should.equal(-1)
- return done()
- })
- it('should put the year in', function(done) {
- this.template.indexOf(new Date().getUTCFullYear()).should.not.equal(-1)
- return done()
- })
- })
- describe('_createRootDoc', function() {
- beforeEach(function(done) {
- this.project = new this.ProjectModel()
- return this.handler._createRootDoc(
- this.project,
- ownerId,
- ['line 1', 'line 2'],
- done
- )
- })
- it('should insert main.tex', function() {
- return this.ProjectEntityUpdateHandler.addDoc
- .calledWith(
- project_id,
- rootFolderId,
- 'main.tex',
- ['line 1', 'line 2'],
- ownerId
- )
- .should.equal(true)
- })
- it('should set the main doc id', function() {
- return this.ProjectEntityUpdateHandler.setRootDoc
- .calledWith(project_id, docId)
- .should.equal(true)
- })
- })
- })
|