Merge pull request #1145 from sharelatex/ew-collabratec-create-project2

Collabratec create project api

GitOrigin-RevId: 5f421625baa5c48f1745a15e15fe3a18fc1a4884
This commit is contained in:
James Allen
2018-11-14 14:44:17 +00:00
committed by sharelatex
parent 95f7d3da24
commit 8c70d680f4
9 changed files with 298 additions and 202 deletions
@@ -0,0 +1,45 @@
ObjectId = require("mongojs").ObjectId
Path = require "path"
SandboxedModule = require "sandboxed-module"
assert = require "assert"
chai = require "chai"
sinon = require "sinon"
sinonChai = require "sinon-chai"
chai.use sinonChai
expect = chai.expect
modulePath = Path.join __dirname, "../../../../app/js/Features/Project/ProjectCollabratecDetailsHandler"
describe "ProjectCollabratecDetailsHandler", ->
beforeEach ->
@ProjectModel = {}
@ProjectCollabratecDetailsHandler = SandboxedModule.require modulePath, requires:
"../../models/Project": { Project: @ProjectModel }
@callback = sinon.stub()
describe "initializeCollabratecProject", ->
describe "when update succeeds", ->
beforeEach ->
@ProjectModel.update = sinon.stub().yields()
@ProjectCollabratecDetailsHandler.initializeCollabratecProject "project-id", "name", "user-id", "collabratec-document-id", "collabratec-private-group-id", @callback
it "should update project model", ->
update = $set: {
name: "name",
collabratecUsers: [ {
user_id: "user-id",
collabratec_document_id: "collabratec-document-id",
collabratec_privategroup_id: "collabratec-private-group-id"
} ]
}
expect(@ProjectModel.update).to.have.been.calledWith { _id: "project-id" }, update, @callback
describe "when update has error", ->
beforeEach ->
@ProjectModel.update = sinon.stub().yields("error")
@ProjectCollabratecDetailsHandler.initializeCollabratecProject "project-id", "name", "user-id", "collabratec-document-id", "collabratec-private-group-id", @callback
it "should callback with error", ->
expect(@callback).to.have.been.calledWith("error")
@@ -1,128 +1,66 @@
should = require('chai').should()
SandboxedModule = require('sandboxed-module')
assert = require('assert')
path = require('path')
chai = require('chai')
sinon = require('sinon')
sinonChai = require('sinon-chai')
chai.should()
chai.use(sinonChai)
expect = chai.expect
modulePath = '../../../../app/js/Features/Templates/TemplatesController'
describe 'TemplatesController', ->
@project_id = "213432"
describe "TemplatesController", ->
beforeEach ->
@request = sinon.stub()
@request.returns {
pipe:->
on:->
}
@fs = {
unlink : sinon.stub()
createWriteStream : sinon.stub().returns(on:(_, cb)->cb())
}
@ProjectUploadManager = {createProjectFromZipArchive : sinon.stub().callsArgWith(3, null, {_id:@project_id})}
@dumpFolder = "dump/path"
@ProjectOptionsHandler = {
setCompiler:sinon.stub().callsArgWith(2)
setImageName:sinon.stub().callsArgWith(2)
setBrandVariationId:sinon.stub().callsArgWith(2)
}
@uuid = "1234"
@ProjectRootDocManager = {
setRootDocFromName: sinon.stub().callsArgWith(2)
}
@ProjectDetailsHandler =
getProjectDescription:sinon.stub()
fixProjectName: sinon.stub().returns(@templateName)
@Project =
update: sinon.stub().callsArgWith(3, null)
@controller = SandboxedModule.require modulePath, requires:
'../../../js/Features/Uploads/ProjectUploadManager':@ProjectUploadManager
'../../../js/Features/Project/ProjectOptionsHandler':@ProjectOptionsHandler
'../../../js/Features/Project/ProjectRootDocManager':@ProjectRootDocManager
'../../../js/Features/Project/ProjectDetailsHandler':@ProjectDetailsHandler
'../../../js/Features/Authentication/AuthenticationController': @AuthenticationController = {getLoggedInUserId: sinon.stub()}
'./TemplatesPublisher':@TemplatesPublisher
"logger-sharelatex":
log:->
err:->
"settings-sharelatex":
path:
dumpFolder:@dumpFolder
siteUrl: @siteUrl = "http://localhost:3000"
apis:
v1:
url: @v1Url="http://overleaf.com"
user: "sharelatex"
pass: "password"
overleaf:
host: @v1Url
"uuid":v4:=>@uuid
"request": @request
"fs":@fs
"../../../js/models/Project": {Project: @Project}
@zipUrl = "%2Ftemplates%2F52fb86a81ae1e566597a25f6%2Fv%2F4%2Fzip&templateName=Moderncv%20Banking&compiler=pdflatex"
@templateName = "project name here"
@user_id = "1234"
@user_id = "user-id"
@TemplatesController = SandboxedModule.require modulePath, requires:
"../../../js/Features/Authentication/AuthenticationController": @AuthenticationController = {
getLoggedInUserId: sinon.stub().returns(@user_id)
}
"./TemplatesManager": @TemplatesManager = {
createProjectFromV1Template: sinon.stub()
}
@next = sinon.stub()
@req =
body:
brandVariationId: "brand-variation-id"
compiler: "compiler"
mainFile: "main-file"
templateId: "template-id"
templateName: "template-name"
templateVersionId: "template-version-id"
session:
user: _id:@user_id
templateData:
zipUrl: @zipUrl
templateName: @templateName
@redirect = {}
@AuthenticationController.getLoggedInUserId.returns(@user_id)
templateData: "template-data"
user: _id: @user_id
@res =
redirect: sinon.stub()
describe 'v1Templates', ->
describe "createProjectFromV1Template", ->
it "should fetch zip from v1 based on template id", (done)->
@templateVersionId = 15
@req.body = {templateVersionId: @templateVersionId}
describe "on success", ->
beforeEach ->
@project =
_id: "project-id"
@TemplatesManager.createProjectFromV1Template.yields null, @project
@TemplatesController.createProjectFromV1Template @req, @res, @next
redirect = =>
@request.calledWith("#{@v1Url}/api/v1/sharelatex/templates/#{@templateVersionId}").should.equal true
done()
res = redirect:redirect
@controller.createProjectFromV1Template @req, res
it "should call TemplatesManager", ->
@TemplatesManager.createProjectFromV1Template.should.have.been.calledWithMatch "brand-variation-id", "compiler", "main-file", "template-id", "template-name", "template-version-id", "user-id"
it "should set project options based on payload data", (done)->
@compiler = "pdflatex"
@mainFile = "main.tex"
@templateVersionId = 15
@brandVariationId = "123"
it "should redirect to project", ->
@res.redirect.should.have.been.calledWith "/project/project-id"
@req.body =
templateVersionId: @templateVersionId
name: @templateName
compiler: @compiler
mainFile: @mainFile
brandVariationId: @brandVariationId
it "should delete session", ->
expect(@req.session.templateData).to.be.undefined
redirect = =>
@ProjectOptionsHandler.setCompiler.calledWith(@project_id, @compiler).should.equal true
@ProjectOptionsHandler.setBrandVariationId.calledWith(@project_id, @brandVariationId).should.equal true
@ProjectRootDocManager.setRootDocFromName.calledWith(@project_id, @mainFile).should.equal true
done()
res = redirect:redirect
@controller.createProjectFromV1Template @req, res
describe "on error", ->
beforeEach ->
@TemplatesManager.createProjectFromV1Template.yields "error"
@TemplatesController.createProjectFromV1Template @req, @res, @next
it "should only set project options which are defined in the payload", (done)->
@compiler = "pdflatex"
@templateVersionId = 15
@brandVariationId = "123"
@req.body =
templateVersionId: @templateVersionId
name: @templateName
compiler: @compiler
brandVariationId: @brandVariationId
redirect = =>
# Payload doesn't refine a main file, so `setRootDocFromName` should not be called
@ProjectOptionsHandler.setCompiler.calledWith(@project_id, @compiler).should.equal true
@ProjectOptionsHandler.setBrandVariationId.calledWith(@project_id, @brandVariationId).should.equal true
@ProjectRootDocManager.setRootDocFromName.called.should.equal false
done()
res = redirect:redirect
@controller.createProjectFromV1Template @req, res
it "should call next with error", ->
@next.should.have.been.calledWith "error"
it "should not redirect", ->
@res.redirect.called.should.equal false
@@ -0,0 +1,111 @@
SandboxedModule = require('sandboxed-module')
assert = require('assert')
chai = require('chai')
sinon = require('sinon')
sinonChai = require('sinon-chai')
should = require('chai').should()
chai.use(sinonChai)
modulePath = '../../../../app/js/Features/Templates/TemplatesManager'
describe 'TemplatesManager', ->
beforeEach ->
@project_id = "project-id"
@brandVariationId = "brand-variation-id"
@compiler = "pdflatex"
@mainFile = "main.tex"
@templateId = "template-id"
@templateName = "template name"
@templateVersionId = "template-version-id"
@user_id = "user-id"
@dumpPath = "#{@dumpFolder}/#{@uuid}"
@callback = sinon.stub()
@request = sinon.stub().returns {
pipe:->
on:->
response: statusCode: 200
}
@fs = {
unlink : sinon.stub()
createWriteStream : sinon.stub().returns(on: sinon.stub().yields())
}
@ProjectUploadManager = {createProjectFromZipArchive : sinon.stub().callsArgWith(3, null, {_id:@project_id})}
@dumpFolder = "dump/path"
@ProjectOptionsHandler = {
setCompiler:sinon.stub().callsArgWith(2)
setImageName:sinon.stub().callsArgWith(2)
setBrandVariationId:sinon.stub().callsArgWith(2)
}
@uuid = "1234"
@ProjectRootDocManager = {
setRootDocFromName: sinon.stub().callsArgWith(2)
}
@ProjectDetailsHandler =
getProjectDescription:sinon.stub()
fixProjectName: sinon.stub().returns(@templateName)
@Project =
update: sinon.stub().callsArgWith(3, null)
@TemplatesManager = SandboxedModule.require modulePath, requires:
'../../../js/Features/Uploads/ProjectUploadManager':@ProjectUploadManager
'../../../js/Features/Project/ProjectOptionsHandler':@ProjectOptionsHandler
'../../../js/Features/Project/ProjectRootDocManager':@ProjectRootDocManager
'../../../js/Features/Project/ProjectDetailsHandler':@ProjectDetailsHandler
'../../../js/Features/Authentication/AuthenticationController': @AuthenticationController = {getLoggedInUserId: sinon.stub()}
'./TemplatesPublisher':@TemplatesPublisher
"logger-sharelatex":
log:->
err:->
"settings-sharelatex":
path:
dumpFolder:@dumpFolder
siteUrl: @siteUrl = "http://localhost:3000"
apis:
v1:
url: @v1Url="http://overleaf.com"
user: "sharelatex"
pass: "password"
overleaf:
host: @v1Url
"uuid":v4:=>@uuid
"request": @request
"fs":@fs
"../../../js/models/Project": {Project: @Project}
@zipUrl = "%2Ftemplates%2F52fb86a81ae1e566597a25f6%2Fv%2F4%2Fzip&templateName=Moderncv%20Banking&compiler=pdflatex"
describe 'createProjectFromV1Template', ->
describe "when all options passed", ->
beforeEach ->
@TemplatesManager.createProjectFromV1Template @brandVariationId, @compiler, @mainFile, @templateId, @templateName, @templateVersionId, @user_id, @callback
it "should fetch zip from v1 based on template id", ->
@request.should.have.been.calledWith "#{@v1Url}/api/v1/sharelatex/templates/#{@templateVersionId}"
it "should save temporary file", ->
@fs.createWriteStream.should.have.been.calledWith @dumpPath
it "should create project", ->
@ProjectUploadManager.createProjectFromZipArchive.should.have.been.calledWithMatch @user_id, @templateName, @dumpPath
it "should unlink file", ->
@fs.unlink.should.have.been.calledWith @dumpPath
it "should set project options when passed", ->
@ProjectOptionsHandler.setCompiler.should.have.been.calledWithMatch @project_id, @compiler
@ProjectOptionsHandler.setImageName.should.have.been.calledWithMatch @project_id, "wl_texlive:2018.1"
@ProjectRootDocManager.setRootDocFromName.should.have.been.calledWithMatch @project_id, @mainFile
@ProjectOptionsHandler.setBrandVariationId.should.have.been.calledWithMatch @project_id, @brandVariationId
it "should update project", ->
@Project.update.should.have.been.calledWithMatch { _id: @project_id }, { fromV1TemplateId: @templateId, fromV1TemplateVersionId: @templateVersionId }
describe "when some options not set", ->
beforeEach ->
@TemplatesManager.createProjectFromV1Template null, null, null, @templateId, @templateName, @templateVersionId, @user_id, @callback
it "should not set missing project options", ->
@ProjectOptionsHandler.setCompiler.called.should.equal false
@ProjectRootDocManager.setRootDocFromName.called.should.equal false
@ProjectOptionsHandler.setBrandVariationId.called.should.equal false