2014-02-12 05:23:40 -05:00
|
|
|
should = require('chai').should()
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
assert = require('assert')
|
|
|
|
path = require('path')
|
|
|
|
sinon = require('sinon')
|
|
|
|
modulePath = path.join __dirname, '../../../../app/js/Features/Templates/TemplatesController'
|
|
|
|
|
|
|
|
|
2014-04-09 03:24:23 -04:00
|
|
|
describe 'TemplatesController', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
project_id = "213432"
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
@request = sinon.stub()
|
2014-11-24 09:26:51 -05:00
|
|
|
@request.returns {
|
|
|
|
pipe:->
|
|
|
|
on:->
|
|
|
|
}
|
2014-02-12 05:23:40 -05:00
|
|
|
@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)}
|
|
|
|
@uuid = "1234"
|
|
|
|
@TemplatesPublisher =
|
|
|
|
publish: sinon.stub()
|
|
|
|
unpublish:sinon.stub()
|
2014-02-27 12:35:35 -05:00
|
|
|
getTemplateDetails: sinon.stub()
|
2014-07-16 06:27:47 -04:00
|
|
|
@ProjectDetailsHandler =
|
|
|
|
getProjectDescription:sinon.stub()
|
2014-02-12 05:23:40 -05:00
|
|
|
@controller = SandboxedModule.require modulePath, requires:
|
|
|
|
'../Uploads/ProjectUploadManager':@ProjectUploadManager
|
|
|
|
'../Project/ProjectOptionsHandler':@ProjectOptionsHandler
|
2014-07-16 06:27:47 -04:00
|
|
|
'../Project/ProjectDetailsHandler':@ProjectDetailsHandler
|
2014-11-06 09:39:25 -05:00
|
|
|
'../Project/ProjectGetter':@ProjectGetter = {}
|
|
|
|
'../Editor/EditorController': @EditorController = {}
|
2014-02-12 05:23:40 -05:00
|
|
|
'./TemplatesPublisher':@TemplatesPublisher
|
2014-03-20 08:53:23 -04:00
|
|
|
"logger-sharelatex":
|
|
|
|
log:->
|
|
|
|
err:->
|
2014-02-12 05:23:40 -05:00
|
|
|
"settings-sharelatex":
|
|
|
|
path:
|
|
|
|
dumpFolder:@dumpFolder
|
|
|
|
siteUrl: "http://localhost:3000"
|
2014-04-09 03:24:23 -04:00
|
|
|
apis:
|
2014-08-20 09:47:27 -04:00
|
|
|
templates:
|
2014-04-09 03:24:23 -04:00
|
|
|
url: @templateApiUrl="http://templates.sharelatex.env"
|
2014-04-11 10:15:32 -04:00
|
|
|
web:
|
|
|
|
url: @webApiUrl="http://web-api.sharelatex.env"
|
2014-02-12 05:23:40 -05:00
|
|
|
"node-uuid":v4:=>@uuid
|
|
|
|
"request": @request
|
|
|
|
"fs":@fs
|
2014-04-11 10:15:32 -04:00
|
|
|
@zipUrl = "%2Ftemplates%2F52fb86a81ae1e566597a25f6%2Fv%2F4%2Fzip&templateName=Moderncv%20Banking&compiler=pdflatex"
|
2014-02-12 05:23:40 -05:00
|
|
|
@templateName = "project name here"
|
|
|
|
@user_id = "1234"
|
|
|
|
@req =
|
|
|
|
session:
|
|
|
|
user: _id:@user_id
|
2014-04-11 10:15:32 -04:00
|
|
|
templateData:
|
|
|
|
zipUrl: @zipUrl
|
|
|
|
templateName: @templateName
|
2014-02-12 05:23:40 -05:00
|
|
|
@redirect = {}
|
|
|
|
|
|
|
|
describe 'reciving a request to create project from templates.sharelatex.com', ->
|
|
|
|
|
|
|
|
it 'should take the zip url and write it to disk', (done)->
|
|
|
|
redirect = =>
|
|
|
|
@ProjectUploadManager.createProjectFromZipArchive.calledWith(@user_id, @templateName, "#{@dumpFolder}/#{@uuid}").should.equal true
|
2014-04-09 03:24:23 -04:00
|
|
|
@request.calledWith("#{@templateApiUrl}#{@zipUrl}").should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
@fs.unlink.calledWith("#{@dumpFolder}/#{@uuid}").should.equal true
|
|
|
|
done()
|
|
|
|
res = redirect:redirect
|
|
|
|
@controller.createProjectFromZipTemplate @req, res
|
|
|
|
|
2014-04-11 10:15:32 -04:00
|
|
|
|
|
|
|
it "should go to the web api if the url does not contain templates", (done)->
|
|
|
|
@req.session.templateData.zipUrl = @zipUrl = "/project/52fd24abf080d80a22000fbd/download/zip&templateName=Example_Project&compiler=xelatex"
|
|
|
|
redirect = =>
|
|
|
|
@request.calledWith("#{@webApiUrl}#{@zipUrl}").should.equal true
|
|
|
|
done()
|
|
|
|
res = redirect:redirect
|
|
|
|
@controller.createProjectFromZipTemplate @req, res
|
|
|
|
|
|
|
|
it "should go to the web api if the url has template futher down the string", (done)->
|
|
|
|
@req.session.templateData.zipUrl = @zipUrl = "/project/52fd24abf080d80a22000fbd/download/zip&templateName=templates&compiler=xelatex"
|
|
|
|
redirect = =>
|
|
|
|
@request.calledWith("#{@webApiUrl}#{@zipUrl}").should.equal true
|
|
|
|
done()
|
|
|
|
res = redirect:redirect
|
|
|
|
@controller.createProjectFromZipTemplate @req, res
|
|
|
|
|
2014-11-06 09:39:25 -05:00
|
|
|
describe 'publishProject', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2014-11-06 09:39:25 -05:00
|
|
|
@user_id = "user-id-123"
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
|
|
|
|
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
|
|
|
|
@TemplatesPublisher.publish = sinon.stub().callsArgWith(2)
|
|
|
|
@controller.publishProject @req, @res
|
|
|
|
|
|
|
|
it "should look up the project owner", ->
|
|
|
|
@ProjectGetter.getProject
|
|
|
|
.calledWith(@project_id, { owner_ref: 1 })
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should publish the template", ->
|
|
|
|
@TemplatesPublisher.publish
|
|
|
|
.calledWith(@user_id, @project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a success status", ->
|
|
|
|
@res.send.calledWith(204).should.equal true
|
|
|
|
|
|
|
|
describe 'unpublishProject', ->
|
2014-02-12 05:23:40 -05:00
|
|
|
beforeEach ->
|
2014-11-06 09:39:25 -05:00
|
|
|
@user_id = "user-id-123"
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
|
|
|
|
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
|
|
|
|
@TemplatesPublisher.unpublish = sinon.stub().callsArgWith(2)
|
|
|
|
@controller.unpublishProject @req, @res
|
|
|
|
|
|
|
|
it "should look up the project owner", ->
|
|
|
|
@ProjectGetter.getProject
|
|
|
|
.calledWith(@project_id, { owner_ref: 1 })
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should publish the template", ->
|
|
|
|
@TemplatesPublisher.unpublish
|
|
|
|
.calledWith(@user_id, @project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a success status", ->
|
|
|
|
@res.send.calledWith(204).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
describe 'settings the compiler from the query string', ->
|
|
|
|
it 'should use the said compiler', (done)->
|
|
|
|
@req.session.templateData.compiler = "xelatex"
|
|
|
|
redirect = =>
|
|
|
|
@ProjectOptionsHandler.setCompiler.calledWith(project_id, "xelatex").should.equal true
|
|
|
|
done()
|
|
|
|
res = redirect:redirect
|
|
|
|
@controller.createProjectFromZipTemplate @req, res
|
|
|
|
|
|
|
|
it 'should not call the options handler if there is not set compiler', (done)->
|
|
|
|
redirect = =>
|
|
|
|
@ProjectOptionsHandler.setCompiler.called.should.equal false
|
|
|
|
done()
|
|
|
|
res = redirect:redirect
|
|
|
|
@controller.createProjectFromZipTemplate @req, res
|
|
|
|
|
2014-11-06 09:39:25 -05:00
|
|
|
describe "updateProjectDescription", ->
|
|
|
|
beforeEach ->
|
|
|
|
@EditorController.updateProjectDescription = sinon.stub().callsArg(2)
|
|
|
|
@res =
|
|
|
|
send: sinon.stub()
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id = "project-id-123"
|
|
|
|
@req.body =
|
|
|
|
description: @description = "test description"
|
|
|
|
|
|
|
|
@controller.updateProjectDescription @req, @res
|
|
|
|
|
|
|
|
it "should update the project description", ->
|
|
|
|
@EditorController.updateProjectDescription
|
|
|
|
.calledWith(@project_id, @description)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return a success code", ->
|
|
|
|
@res.send.calledWith(204).should.equal true
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2014-02-27 12:35:35 -05:00
|
|
|
describe 'getTemplateDetails', ->
|
2014-07-16 06:27:47 -04:00
|
|
|
beforeEach ->
|
2014-11-06 09:39:25 -05:00
|
|
|
@user_id = "user-id-123"
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@res =
|
|
|
|
json: sinon.stub()
|
|
|
|
@req.params =
|
|
|
|
Project_id: @project_id
|
|
|
|
|
|
|
|
@ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, {owner_ref: @user_id})
|
|
|
|
@TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, @details = {exists: true})
|
|
|
|
@ProjectDetailsHandler.getProjectDescription.callsArgWith(1, null, @description = "test description")
|
|
|
|
|
|
|
|
@controller.getTemplateDetails @req, @res
|
|
|
|
|
|
|
|
it "should get the template details for the user_id and project_id", ->
|
|
|
|
@TemplatesPublisher.getTemplateDetails
|
|
|
|
.calledWith(@user_id, @project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the details and description", ->
|
|
|
|
@res.json
|
|
|
|
.calledWith({
|
|
|
|
exists: @details.exists
|
|
|
|
description: @description
|
|
|
|
})
|
|
|
|
.should.equal true
|
2014-07-16 06:27:47 -04:00
|
|
|
|