ProjectCreationHandlerTests.coffee 9.8 KB

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