ProjectCreationHandlerTests.coffee 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. @ProjectEntityUpdateHandler =
  34. addDoc: sinon.stub().callsArgWith(5, null, {_id: docId})
  35. addFile: sinon.stub().callsArg(5)
  36. setRootDoc: sinon.stub().callsArg(2)
  37. @ProjectDetailsHandler =
  38. validateProjectName: sinon.stub().yields()
  39. @HistoryManager =
  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. @Settings = apis: { project_history: {} }
  49. @handler = SandboxedModule.require modulePath, requires:
  50. '../../models/User': User:@User
  51. '../../models/Project':{Project:@ProjectModel}
  52. '../../models/Folder':{Folder:@FolderModel}
  53. '../History/HistoryManager': @HistoryManager
  54. './ProjectEntityUpdateHandler':@ProjectEntityUpdateHandler
  55. "./ProjectDetailsHandler":@ProjectDetailsHandler
  56. "settings-sharelatex": @Settings
  57. 'logger-sharelatex': {log:->}
  58. "metrics-sharelatex": {
  59. inc: ()->,
  60. timeAsyncMethod: ()->
  61. }
  62. describe 'Creating a Blank project', ->
  63. beforeEach ->
  64. @overleaf_id = 1234
  65. @HistoryManager.initializeProject = sinon.stub().callsArgWith(0, null, { @overleaf_id })
  66. @ProjectModel::save = sinon.stub().callsArg(0)
  67. describe "successfully", ->
  68. it "should save the project", (done)->
  69. @handler.createBlankProject ownerId, projectName, =>
  70. @ProjectModel::save.called.should.equal true
  71. done()
  72. it "should return the project in the callback", (done)->
  73. @handler.createBlankProject ownerId, projectName, (err, project)->
  74. project.name.should.equal projectName
  75. (project.owner_ref + "").should.equal ownerId
  76. done()
  77. it "should initialize the project overleaf if history id not provided", (done)->
  78. @handler.createBlankProject ownerId, projectName, done
  79. @HistoryManager.initializeProject.calledWith().should.equal true
  80. it "should set the overleaf id if overleaf id not provided", (done)->
  81. @handler.createBlankProject ownerId, projectName, (err, project)=>
  82. project.overleaf.history.id.should.equal @overleaf_id
  83. done()
  84. it "should set the overleaf id if overleaf id provided", (done)->
  85. overleaf_id = 2345
  86. @handler.createBlankProject ownerId, projectName, overleaf_id, (err, project)->
  87. project.overleaf.history.id.should.equal overleaf_id
  88. done()
  89. it "should set the language from the user", (done)->
  90. @handler.createBlankProject ownerId, projectName, (err, project)->
  91. project.spellCheckLanguage.should.equal "de"
  92. done()
  93. it "should set the imageName to currentImageName if set", (done) ->
  94. @Settings.currentImageName = "mock-image-name"
  95. @handler.createBlankProject ownerId, projectName, (err, project)=>
  96. project.imageName.should.equal @Settings.currentImageName
  97. done()
  98. it "should not set the imageName if no currentImageName", (done) ->
  99. @Settings.currentImageName = null
  100. @handler.createBlankProject ownerId, projectName, (err, project)=>
  101. expect(project.imageName).to.not.exist
  102. done()
  103. it "should not set the overleaf.history.display if not configured in settings", (done) ->
  104. @Settings.apis.project_history.displayHistoryForNewProjects = false
  105. @handler.createBlankProject ownerId, projectName, (err, project)=>
  106. expect(project.overleaf.history.display).to.not.exist
  107. done()
  108. it "should set the overleaf.history.display if configured in settings", (done) ->
  109. @Settings.apis.project_history.displayHistoryForNewProjects = true
  110. @handler.createBlankProject ownerId, projectName, (err, project)=>
  111. expect(project.overleaf.history.display).to.equal true
  112. done()
  113. describe "with an error", ->
  114. beforeEach ->
  115. @ProjectModel::save = sinon.stub().callsArgWith(0, new Error("something went wrong"))
  116. @handler.createBlankProject ownerId, projectName, @callback
  117. it 'should return the error to the callback', ->
  118. should.exist @callback.args[0][0]
  119. describe "with an invalid name", ->
  120. beforeEach ->
  121. @ProjectDetailsHandler.validateProjectName = sinon.stub().yields(new Error("bad name"))
  122. @handler.createBlankProject ownerId, projectName, @callback
  123. it 'should return the error to the callback', ->
  124. should.exist @callback.args[0][0]
  125. it 'should not try to create the project', ->
  126. @ProjectModel::save.called.should.equal false
  127. describe 'Creating a basic project', ->
  128. beforeEach ->
  129. @project = new @ProjectModel()
  130. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  131. if template_name == "mainbasic.tex"
  132. return callback(null, ["mainbasic.tex", "lines"])
  133. throw new Error("unknown template: #{template_name}")
  134. sinon.spy @handler, "_buildTemplate"
  135. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  136. @handler.createBasicProject(ownerId, projectName, @callback)
  137. it "should create a blank project first", ->
  138. @handler.createBlankProject.calledWith(ownerId, projectName)
  139. .should.equal true
  140. it 'should insert main.tex', ->
  141. @ProjectEntityUpdateHandler.addDoc.calledWith(project_id, rootFolderId, "main.tex", ["mainbasic.tex", "lines"], ownerId)
  142. .should.equal true
  143. it 'should set the main doc id', ->
  144. @ProjectEntityUpdateHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  145. it 'should build the mainbasic.tex template', ->
  146. @handler._buildTemplate
  147. .calledWith("mainbasic.tex", ownerId, projectName)
  148. .should.equal true
  149. describe 'Creating an example project', ->
  150. beforeEach ->
  151. @project = new @ProjectModel()
  152. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  153. if template_name == "main.tex"
  154. return callback(null, ["main.tex", "lines"])
  155. if template_name == "references.bib"
  156. return callback(null, ["references.bib", "lines"])
  157. throw new Error("unknown template: #{template_name}")
  158. sinon.spy @handler, "_buildTemplate"
  159. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  160. @handler.createExampleProject(ownerId, projectName, @callback)
  161. it "should create a blank project first", ->
  162. @handler.createBlankProject.calledWith(ownerId, projectName)
  163. .should.equal true
  164. it 'should insert main.tex', ->
  165. @ProjectEntityUpdateHandler.addDoc
  166. .calledWith(project_id, rootFolderId, "main.tex", ["main.tex", "lines"], ownerId)
  167. .should.equal true
  168. it 'should insert references.bib', ->
  169. @ProjectEntityUpdateHandler.addDoc
  170. .calledWith(project_id, rootFolderId, "references.bib", ["references.bib", "lines"], ownerId)
  171. .should.equal true
  172. it 'should insert universe.jpg', ->
  173. @ProjectEntityUpdateHandler.addFile
  174. .calledWith(
  175. project_id, rootFolderId, "universe.jpg",
  176. Path.resolve(__dirname + "/../../../../app/templates/project_files/universe.jpg"),
  177. ownerId
  178. )
  179. .should.equal true
  180. it 'should set the main doc id', ->
  181. @ProjectEntityUpdateHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  182. it 'should build the main.tex template', ->
  183. @handler._buildTemplate
  184. .calledWith("main.tex", ownerId, projectName)
  185. .should.equal true
  186. it 'should build the references.bib template', ->
  187. @handler._buildTemplate
  188. .calledWith("references.bib", ownerId, projectName)
  189. .should.equal true
  190. describe "_buildTemplate", ->
  191. beforeEach (done)->
  192. @handler._buildTemplate "main.tex", @user_id, projectName, (err, templateLines)=>
  193. @template = templateLines.reduce (singleLine, line)-> "#{singleLine}\n#{line}"
  194. done()
  195. it "should insert the project name into the template", (done)->
  196. @template.indexOf(projectName).should.not.equal -1
  197. done()
  198. it "should insert the users name into the template", (done)->
  199. @template.indexOf(@user.first_name).should.not.equal -1
  200. @template.indexOf(@user.last_name).should.not.equal -1
  201. done()
  202. it "should not have undefined in the template", (done)->
  203. @template.indexOf("undefined").should.equal -1
  204. done()
  205. it "should not have any underscore brackets in the output", (done)->
  206. @template.indexOf("{{").should.equal -1
  207. @template.indexOf("<%=").should.equal -1
  208. done()
  209. it "should put the year in", (done)->
  210. @template.indexOf(new Date().getUTCFullYear()).should.not.equal -1
  211. done()