ProjectCreationHandlerTests.coffee 8.0 KB

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