|
|
@@ -1,128 +1,66 @@
|
|
|
-should = require('chai').should()
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
assert = require('assert')
|
|
|
-path = require('path')
|
|
|
+chai = require('chai')
|
|
|
sinon = require('sinon')
|
|
|
-modulePath = '../../../../app/js/Features/Templates/TemplatesController'
|
|
|
+sinonChai = require('sinon-chai')
|
|
|
|
|
|
+chai.should()
|
|
|
+chai.use(sinonChai)
|
|
|
+expect = chai.expect
|
|
|
|
|
|
-describe 'TemplatesController', ->
|
|
|
+modulePath = '../../../../app/js/Features/Templates/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)
|
|
|
-
|
|
|
- describe 'v1Templates', ->
|
|
|
-
|
|
|
- it "should fetch zip from v1 based on template id", (done)->
|
|
|
- @templateVersionId = 15
|
|
|
- @req.body = {templateVersionId: @templateVersionId}
|
|
|
+ templateData: "template-data"
|
|
|
+ user: _id: @user_id
|
|
|
+ @res =
|
|
|
+ redirect: sinon.stub()
|
|
|
|
|
|
- redirect = =>
|
|
|
- @request.calledWith("#{@v1Url}/api/v1/sharelatex/templates/#{@templateVersionId}").should.equal true
|
|
|
- done()
|
|
|
- res = redirect:redirect
|
|
|
- @controller.createProjectFromV1Template @req, res
|
|
|
+ describe "createProjectFromV1Template", ->
|
|
|
|
|
|
- it "should set project options based on payload data", (done)->
|
|
|
- @compiler = "pdflatex"
|
|
|
- @mainFile = "main.tex"
|
|
|
- @templateVersionId = 15
|
|
|
- @brandVariationId = "123"
|
|
|
+ describe "on success", ->
|
|
|
+ beforeEach ->
|
|
|
+ @project =
|
|
|
+ _id: "project-id"
|
|
|
+ @TemplatesManager.createProjectFromV1Template.yields null, @project
|
|
|
+ @TemplatesController.createProjectFromV1Template @req, @res, @next
|
|
|
|
|
|
- @req.body =
|
|
|
- templateVersionId: @templateVersionId
|
|
|
- name: @templateName
|
|
|
- compiler: @compiler
|
|
|
- mainFile: @mainFile
|
|
|
- brandVariationId: @brandVariationId
|
|
|
+ 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"
|
|
|
|
|
|
- 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
|
|
|
+ it "should redirect to project", ->
|
|
|
+ @res.redirect.should.have.been.calledWith "/project/project-id"
|
|
|
|
|
|
- it "should only set project options which are defined in the payload", (done)->
|
|
|
- @compiler = "pdflatex"
|
|
|
- @templateVersionId = 15
|
|
|
- @brandVariationId = "123"
|
|
|
+ it "should delete session", ->
|
|
|
+ expect(@req.session.templateData).to.be.undefined
|
|
|
|
|
|
- @req.body =
|
|
|
- templateVersionId: @templateVersionId
|
|
|
- name: @templateName
|
|
|
- compiler: @compiler
|
|
|
- brandVariationId: @brandVariationId
|
|
|
+ describe "on error", ->
|
|
|
+ beforeEach ->
|
|
|
+ @TemplatesManager.createProjectFromV1Template.yields "error"
|
|
|
+ @TemplatesController.createProjectFromV1Template @req, @res, @next
|
|
|
|
|
|
- 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
|