ProjectCreationHandlerTests.coffee 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. spies = require('chai-spies')
  2. chai = require('chai').use(spies)
  3. sinon = require("sinon")
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../../app/js/Features/Project/ProjectCreationHandler.js"
  7. SandboxedModule = require('sandboxed-module')
  8. Settings = require('settings-sharelatex')
  9. Path = require "path"
  10. _ = require("underscore")
  11. describe 'ProjectCreationHandler', ->
  12. ownerId = '4eecb1c1bffa66588e0000a1'
  13. projectName = 'project name goes here'
  14. project_id = "4eecaffcbffa66588e000008"
  15. docId = '4eecb17ebffa66588e00003f'
  16. rootFolderId = "234adfa3r2afe"
  17. beforeEach ->
  18. @ProjectModel = class Project
  19. constructor:(options = {})->
  20. @._id = project_id
  21. @owner_ref = options.owner_ref
  22. @name = options.name
  23. @overleaf =
  24. history: {}
  25. save: sinon.stub().callsArg(0)
  26. rootFolder:[{
  27. _id: rootFolderId
  28. docs: []
  29. }]
  30. @FolderModel = class Folder
  31. constructor:(options)->
  32. {@name} = options
  33. @ProjectEntityHandler =
  34. addDoc: sinon.stub().callsArgWith(4, null, {_id: docId})
  35. addFile: sinon.stub().callsArg(4)
  36. setRootDoc: sinon.stub().callsArg(2)
  37. @ProjectDetailsHandler =
  38. validateProjectName: sinon.stub().yields()
  39. @HistoryController =
  40. initializeProject: sinon.stub().callsArg(0)
  41. @user =
  42. first_name:"first name here"
  43. last_name:"last name here"
  44. ace:
  45. spellCheckLanguage:"de"
  46. @User = findById:sinon.stub().callsArgWith(2, null, @user)
  47. @callback = sinon.stub()
  48. @handler = SandboxedModule.require modulePath, requires:
  49. '../../models/User': User:@User
  50. '../../models/Project':{Project:@ProjectModel}
  51. '../../models/Folder':{Folder:@FolderModel}
  52. '../History/HistoryController': @HistoryController
  53. './ProjectEntityHandler':@ProjectEntityHandler
  54. "./ProjectDetailsHandler":@ProjectDetailsHandler
  55. "settings-sharelatex": @Settings = {}
  56. 'logger-sharelatex': {log:->}
  57. "metrics-sharelatex": {
  58. inc: ()->,
  59. timeAsyncMethod: ()->
  60. }
  61. describe 'Creating a Blank project', ->
  62. beforeEach ->
  63. @overleaf_id = 1234
  64. @HistoryController.initializeProject = sinon.stub().callsArgWith(0, null, { @overleaf_id })
  65. @ProjectModel::save = sinon.stub().callsArg(0)
  66. describe "successfully", ->
  67. it "should save the project", (done)->
  68. @handler.createBlankProject ownerId, projectName, =>
  69. @ProjectModel::save.called.should.equal true
  70. done()
  71. it "should return the project in the callback", (done)->
  72. @handler.createBlankProject ownerId, projectName, (err, project)->
  73. project.name.should.equal projectName
  74. (project.owner_ref + "").should.equal ownerId
  75. done()
  76. it "should initialize the project overleaf if history id not provided", (done)->
  77. @handler.createBlankProject ownerId, projectName, done
  78. @HistoryController.initializeProject.calledWith().should.equal true
  79. it "should set the overleaf id if overleaf id not provided", (done)->
  80. @handler.createBlankProject ownerId, projectName, (err, project)=>
  81. project.overleaf.history.id.should.equal @overleaf_id
  82. done()
  83. it "should set the overleaf id if overleaf id provided", (done)->
  84. overleaf_id = 2345
  85. @handler.createBlankProject ownerId, projectName, overleaf_id, (err, project)->
  86. project.overleaf.history.id.should.equal overleaf_id
  87. done()
  88. it "should set the language from the user", (done)->
  89. @handler.createBlankProject ownerId, projectName, (err, project)->
  90. project.spellCheckLanguage.should.equal "de"
  91. done()
  92. it "should set the imageName to currentImageName if set", (done) ->
  93. @Settings.currentImageName = "mock-image-name"
  94. @handler.createBlankProject ownerId, projectName, (err, project)=>
  95. project.imageName.should.equal @Settings.currentImageName
  96. done()
  97. it "should not set the imageName if no currentImageName", (done) ->
  98. @Settings.currentImageName = null
  99. @handler.createBlankProject ownerId, projectName, (err, project)=>
  100. expect(project.imageName).to.not.exist
  101. done()
  102. describe "with an error", ->
  103. beforeEach ->
  104. @ProjectModel::save = sinon.stub().callsArgWith(0, new Error("something went wrong"))
  105. @handler.createBlankProject ownerId, projectName, @callback
  106. it 'should return the error to the callback', ->
  107. should.exist @callback.args[0][0]
  108. describe "with an invalid name", ->
  109. beforeEach ->
  110. @ProjectDetailsHandler.validateProjectName = sinon.stub().yields(new Error("bad name"))
  111. @handler.createBlankProject ownerId, projectName, @callback
  112. it 'should return the error to the callback', ->
  113. should.exist @callback.args[0][0]
  114. it 'should not try to create the project', ->
  115. @ProjectModel::save.called.should.equal false
  116. describe 'Creating a basic project', ->
  117. beforeEach ->
  118. @project = new @ProjectModel()
  119. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  120. if template_name == "mainbasic.tex"
  121. return callback(null, ["mainbasic.tex", "lines"])
  122. throw new Error("unknown template: #{template_name}")
  123. sinon.spy @handler, "_buildTemplate"
  124. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  125. @handler.createBasicProject(ownerId, projectName, @callback)
  126. it "should create a blank project first", ->
  127. @handler.createBlankProject.calledWith(ownerId, projectName)
  128. .should.equal true
  129. it 'should insert main.tex', ->
  130. @ProjectEntityHandler.addDoc.calledWith(project_id, rootFolderId, "main.tex", ["mainbasic.tex", "lines"])
  131. .should.equal true
  132. it 'should set the main doc id', ->
  133. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  134. it 'should build the mainbasic.tex template', ->
  135. @handler._buildTemplate
  136. .calledWith("mainbasic.tex", ownerId, projectName)
  137. .should.equal true
  138. describe 'Creating an example project', ->
  139. beforeEach ->
  140. @project = new @ProjectModel()
  141. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  142. if template_name == "main.tex"
  143. return callback(null, ["main.tex", "lines"])
  144. if template_name == "references.bib"
  145. return callback(null, ["references.bib", "lines"])
  146. throw new Error("unknown template: #{template_name}")
  147. sinon.spy @handler, "_buildTemplate"
  148. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  149. @handler.createExampleProject(ownerId, projectName, @callback)
  150. it "should create a blank project first", ->
  151. @handler.createBlankProject.calledWith(ownerId, projectName)
  152. .should.equal true
  153. it 'should insert main.tex', ->
  154. @ProjectEntityHandler.addDoc
  155. .calledWith(project_id, rootFolderId, "main.tex", ["main.tex", "lines"])
  156. .should.equal true
  157. it 'should insert references.bib', ->
  158. @ProjectEntityHandler.addDoc
  159. .calledWith(project_id, rootFolderId, "references.bib", ["references.bib", "lines"])
  160. .should.equal true
  161. it 'should insert universe.jpg', ->
  162. @ProjectEntityHandler.addFile
  163. .calledWith(
  164. project_id, rootFolderId, "universe.jpg",
  165. Path.resolve(__dirname + "/../../../../app/templates/project_files/universe.jpg")
  166. )
  167. .should.equal true
  168. it 'should set the main doc id', ->
  169. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  170. it 'should build the main.tex template', ->
  171. @handler._buildTemplate
  172. .calledWith("main.tex", ownerId, projectName)
  173. .should.equal true
  174. it 'should build the references.bib template', ->
  175. @handler._buildTemplate
  176. .calledWith("references.bib", ownerId, projectName)
  177. .should.equal true
  178. describe "_buildTemplate", ->
  179. beforeEach (done)->
  180. @handler._buildTemplate "main.tex", @user_id, projectName, (err, templateLines)=>
  181. @template = templateLines.reduce (singleLine, line)-> "#{singleLine}\n#{line}"
  182. done()
  183. it "should insert the project name into the template", (done)->
  184. @template.indexOf(projectName).should.not.equal -1
  185. done()
  186. it "should insert the users name into the template", (done)->
  187. @template.indexOf(@user.first_name).should.not.equal -1
  188. @template.indexOf(@user.last_name).should.not.equal -1
  189. done()
  190. it "should not have undefined in the template", (done)->
  191. @template.indexOf("undefined").should.equal -1
  192. done()
  193. it "should not have any underscore brackets in the output", (done)->
  194. @template.indexOf("{{").should.equal -1
  195. @template.indexOf("<%=").should.equal -1
  196. done()
  197. it "should put the year in", (done)->
  198. @template.indexOf(new Date().getUTCFullYear()).should.not.equal -1
  199. done()