2019-05-29 05:21:06 -04:00
|
|
|
const sinon = require('sinon')
|
2019-08-27 06:30:15 -04:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const modulePath =
|
|
|
|
'../../../../app/src/Features/Project/ProjectCreationHandler.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const Path = require('path')
|
|
|
|
|
|
|
|
describe('ProjectCreationHandler', function() {
|
|
|
|
const ownerId = '4eecb1c1bffa66588e0000a1'
|
|
|
|
const projectName = 'project name goes here'
|
2019-08-27 06:30:15 -04:00
|
|
|
const projectId = '4eecaffcbffa66588e000008'
|
2019-05-29 05:21:06 -04:00
|
|
|
const docId = '4eecb17ebffa66588e00003f'
|
|
|
|
const rootFolderId = '234adfa3r2afe'
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.ProjectModel = class Project {
|
|
|
|
constructor(options) {
|
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
this._id = projectId
|
|
|
|
this.owner_ref = options.owner_ref
|
|
|
|
this.name = options.name
|
|
|
|
this.overleaf = { history: {} }
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
}
|
|
|
|
this.ProjectModel.prototype.save = sinon.stub().callsArg(0)
|
|
|
|
this.ProjectModel.prototype.rootFolder = [
|
|
|
|
{
|
|
|
|
_id: rootFolderId,
|
|
|
|
docs: []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
this.FolderModel = class Folder {
|
2019-05-29 05:21:06 -04:00
|
|
|
constructor(options) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.name = options.name
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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() }
|
|
|
|
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler = SandboxedModule.require(modulePath, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
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() {}
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('Creating a Blank project', function() {
|
|
|
|
beforeEach(function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.overleafId = 1234
|
2019-05-29 05:21:06 -04:00
|
|
|
this.HistoryManager.initializeProject = sinon
|
|
|
|
.stub()
|
2019-08-27 06:30:15 -04:00
|
|
|
.callsArgWith(0, null, { overleaf_id: this.overleafId })
|
|
|
|
this.ProjectModel.prototype.save = sinon.stub().callsArg(0)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function() {
|
|
|
|
it('should save the project', function(done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(ownerId, projectName, () => {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectModel.prototype.save.called.should.equal(true)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the project in the callback', function(done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-08-07 10:04:04 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
project.name.should.equal(projectName)
|
2019-08-27 06:30:15 -04:00
|
|
|
expect(project.owner_ref + '').to.equal(ownerId)
|
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should initialize the project overleaf if history id not provided', function(done) {
|
|
|
|
this.handler.createBlankProject(ownerId, projectName, done)
|
2019-08-27 06:30:15 -04:00
|
|
|
this.HistoryManager.initializeProject.calledWith().should.equal(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the overleaf id if overleaf id not provided', function(done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
project.overleaf.history.id.should.equal(this.overleafId)
|
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the overleaf id if overleaf id provided', function(done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
const overleafId = 2345
|
2019-05-29 05:21:06 -04:00
|
|
|
const attributes = {
|
|
|
|
overleaf: {
|
|
|
|
history: {
|
2019-08-27 06:30:15 -04:00
|
|
|
id: overleafId
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
attributes,
|
2019-08-07 10:04:04 -04:00
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
project.overleaf.history.id.should.equal(overleafId)
|
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the language from the user', function(done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-08-07 10:04:04 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-08-07 10:04:04 -04:00
|
|
|
project.spellCheckLanguage.should.equal('de')
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-08-07 10:04:04 -04:00
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the imageName to currentImageName if set and no imageName attribute', function(done) {
|
|
|
|
this.Settings.currentImageName = 'mock-image-name'
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
project.imageName.should.equal(this.Settings.currentImageName)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not set the imageName if no currentImageName', function(done) {
|
|
|
|
this.Settings.currentImageName = null
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(project.imageName).to.not.exist
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
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' }
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
attributes,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
project.imageName.should.equal(attributes.imageName)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not set the overleaf.history.display if not configured in settings', function(done) {
|
|
|
|
this.Settings.apis.project_history.displayHistoryForNewProjects = false
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(project.overleaf.history.display).to.not.exist
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should set the overleaf.history.display if configured in settings', function(done) {
|
|
|
|
this.Settings.apis.project_history.displayHistoryForNewProjects = true
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(project.overleaf.history.display).to.equal(true)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should send a project-created event to analytics', function(done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.AnalyticsManager.recordEvent.calledWith(
|
|
|
|
ownerId,
|
|
|
|
'project-created'
|
|
|
|
)
|
|
|
|
).to.equal(true)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-04 07:08:04 -04:00
|
|
|
it('should send a project-created event with template information if provided', function(done) {
|
|
|
|
const attributes = {
|
|
|
|
fromV1TemplateId: 100
|
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-06-04 07:08:04 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
attributes,
|
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-06-04 07:08:04 -04:00
|
|
|
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.AnalyticsManager.recordEvent.calledWith(
|
|
|
|
ownerId,
|
|
|
|
'project-created',
|
|
|
|
{ projectId: project._id, attributes }
|
|
|
|
)
|
|
|
|
).to.equal(true)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-06-04 07:08:04 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should send a project-imported event when importing a project', function(done) {
|
2019-06-04 07:08:04 -04:00
|
|
|
const attributes = {
|
|
|
|
overleaf: {
|
|
|
|
history: {
|
|
|
|
id: 100
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
2019-06-04 07:08:04 -04:00
|
|
|
attributes,
|
2019-05-29 05:21:06 -04:00
|
|
|
(err, project) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
|
|
|
|
expect(
|
|
|
|
this.AnalyticsManager.recordEvent.calledWith(
|
|
|
|
ownerId,
|
|
|
|
'project-imported'
|
|
|
|
)
|
|
|
|
).to.equal(true)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with an error', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.ProjectModel.prototype.save = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, new Error('something went wrong'))
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(ownerId, projectName, this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should return the error to the callback', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
expect(this.callback.args[0][0]).to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('with an invalid name', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
this.ProjectDetailsHandler.validateProjectName = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(new Error('bad name'))
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject(ownerId, projectName, this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the error to the callback', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
expect(this.callback.args[0][0]).to.exist
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should not try to create the project', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.ProjectModel.prototype.save.called.should.equal(false)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Creating a basic project', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.project = new this.ProjectModel()
|
|
|
|
this.handler._buildTemplate = function(
|
2019-08-27 06:30:15 -04:00
|
|
|
templateName,
|
2019-05-29 05:21:06 -04:00
|
|
|
user,
|
2019-08-27 06:30:15 -04:00
|
|
|
projectName,
|
2019-05-29 05:21:06 -04:00
|
|
|
callback
|
|
|
|
) {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (templateName === 'mainbasic.tex') {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(null, ['mainbasic.tex', 'lines'])
|
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
throw new Error(`unknown template: ${templateName}`)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
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)
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBasicProject(ownerId, projectName, this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should create a blank project first', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(ownerId, projectName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create the root document', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._createRootDoc
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.project, ownerId, ['mainbasic.tex', 'lines'])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should build the mainbasic.tex template', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._buildTemplate
|
2019-05-29 05:21:06 -04:00
|
|
|
.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)
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createProjectFromSnippet(
|
2019-05-29 05:21:06 -04:00
|
|
|
ownerId,
|
|
|
|
projectName,
|
|
|
|
['snippet line 1', 'snippet line 2'],
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create a blank project first', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(ownerId, projectName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should create the root document', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._createRootDoc
|
2019-05-29 05:21:06 -04:00
|
|
|
.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(
|
2019-08-27 06:30:15 -04:00
|
|
|
templateName,
|
2019-05-29 05:21:06 -04:00
|
|
|
user,
|
2019-08-27 06:30:15 -04:00
|
|
|
projectName,
|
2019-05-29 05:21:06 -04:00
|
|
|
callback
|
|
|
|
) {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (templateName === 'main.tex') {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(null, ['main.tex', 'lines'])
|
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
if (templateName === 'references.bib') {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(null, ['references.bib', 'lines'])
|
|
|
|
}
|
2019-08-27 06:30:15 -04:00
|
|
|
throw new Error(`unknown template: ${templateName}`)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
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)
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createExampleProject(ownerId, projectName, this.callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should create a blank project first', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler.createBlankProject
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(ownerId, projectName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create the root document', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._createRootDoc
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.project, ownerId, ['main.tex', 'lines'])
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert references.bib', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.ProjectEntityUpdateHandler.addDoc
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
2019-08-27 06:30:15 -04:00
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolderId,
|
|
|
|
'references.bib',
|
|
|
|
['references.bib', 'lines'],
|
|
|
|
ownerId
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert universe.jpg', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.ProjectEntityUpdateHandler.addFile
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
2019-08-27 06:30:15 -04:00
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolderId,
|
|
|
|
'universe.jpg',
|
|
|
|
Path.resolve(
|
2019-08-27 06:30:15 -04:00
|
|
|
Path.join(
|
|
|
|
__dirname,
|
|
|
|
'../../../../app/templates/project_files/universe.jpg'
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
),
|
|
|
|
null,
|
|
|
|
ownerId
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should build the main.tex template', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._buildTemplate
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith('main.tex', ownerId, projectName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should build the references.bib template', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._buildTemplate
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith('references.bib', ownerId, projectName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('_buildTemplate', function() {
|
|
|
|
beforeEach(function(done) {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._buildTemplate(
|
2019-05-29 05:21:06 -04:00
|
|
|
'main.tex',
|
|
|
|
this.user_id,
|
|
|
|
projectName,
|
|
|
|
(err, templateLines) => {
|
2019-08-27 06:30:15 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.template = templateLines.reduce(
|
|
|
|
(singleLine, line) => `${singleLine}\n${line}`
|
|
|
|
)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert the project name into the template', function(done) {
|
|
|
|
this.template.indexOf(projectName).should.not.equal(-1)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not have undefined in the template', function(done) {
|
|
|
|
this.template.indexOf('undefined').should.equal(-1)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should not have any underscore brackets in the output', function(done) {
|
|
|
|
this.template.indexOf('{{').should.equal(-1)
|
|
|
|
this.template.indexOf('<%=').should.equal(-1)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should put the year in', function(done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.template.indexOf(new Date().getUTCFullYear()).should.not.equal(-1)
|
2019-08-27 06:30:15 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('_createRootDoc', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function(done) {
|
|
|
|
this.project = new this.ProjectModel()
|
|
|
|
|
2019-08-27 06:30:15 -04:00
|
|
|
this.handler._createRootDoc(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project,
|
|
|
|
ownerId,
|
|
|
|
['line 1', 'line 2'],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert main.tex', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.ProjectEntityUpdateHandler.addDoc
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
2019-08-27 06:30:15 -04:00
|
|
|
projectId,
|
2019-05-29 05:21:06 -04:00
|
|
|
rootFolderId,
|
|
|
|
'main.tex',
|
|
|
|
['line 1', 'line 2'],
|
|
|
|
ownerId
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
it('should set the main doc id', function() {
|
2019-08-27 06:30:15 -04:00
|
|
|
this.ProjectEntityUpdateHandler.setRootDoc
|
|
|
|
.calledWith(projectId, docId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|