ProjectCreationHandlerTests.coffee 6.9 KB

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