ProjectCreationHandlerTests.coffee 9.8 KB

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