ProjectCreationHandlerTests.coffee 8.3 KB

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