ProjectCreationHandlerTests.coffee 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. describe 'Creating a Blank project', ->
  50. beforeEach ->
  51. @ProjectModel::save = sinon.stub().callsArg(0)
  52. describe "successfully", ->
  53. it "should save the project", (done)->
  54. @handler.createBlankProject ownerId, projectName, =>
  55. @ProjectModel::save.called.should.equal true
  56. done()
  57. it "should return the project in the callback", (done)->
  58. @handler.createBlankProject ownerId, projectName, (err, project)->
  59. project.name.should.equal projectName
  60. (project.owner_ref + "").should.equal ownerId
  61. done()
  62. it "should set the language from the user", (done)->
  63. @handler.createBlankProject ownerId, projectName, (err, project)->
  64. project.spellCheckLanguage.should.equal "de"
  65. done()
  66. it "should set the imageName to currentImageName if set", (done) ->
  67. @Settings.currentImageName = "mock-image-name"
  68. @handler.createBlankProject ownerId, projectName, (err, project)=>
  69. project.imageName.should.equal @Settings.currentImageName
  70. done()
  71. it "should not set the imageName if no currentImageName", (done) ->
  72. @Settings.currentImageName = null
  73. @handler.createBlankProject ownerId, projectName, (err, project)=>
  74. expect(project.imageName).to.not.exist
  75. done()
  76. describe "with an error", ->
  77. beforeEach ->
  78. @ProjectModel::save = sinon.stub().callsArgWith(0, new Error("something went wrong"))
  79. @handler.createBlankProject ownerId, projectName, @callback
  80. it 'should return the error to the callback', ->
  81. should.exist @callback.args[0][0]
  82. describe 'Creating a basic project', ->
  83. beforeEach ->
  84. @project = new @ProjectModel()
  85. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  86. if template_name == "mainbasic.tex"
  87. return callback(null, ["mainbasic.tex", "lines"])
  88. throw new Error("unknown template: #{template_name}")
  89. sinon.spy @handler, "_buildTemplate"
  90. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  91. @handler.createBasicProject(ownerId, projectName, @callback)
  92. it "should create a blank project first", ->
  93. @handler.createBlankProject.calledWith(ownerId, projectName)
  94. .should.equal true
  95. it 'should insert main.tex', ->
  96. @ProjectEntityHandler.addDoc.calledWith(project_id, rootFolderId, "main.tex", ["mainbasic.tex", "lines"])
  97. .should.equal true
  98. it 'should set the main doc id', ->
  99. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  100. it 'should build the mainbasic.tex template', ->
  101. @handler._buildTemplate
  102. .calledWith("mainbasic.tex", ownerId, projectName)
  103. .should.equal true
  104. describe 'Creating an example project', ->
  105. beforeEach ->
  106. @project = new @ProjectModel()
  107. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  108. if template_name == "main.tex"
  109. return callback(null, ["main.tex", "lines"])
  110. if template_name == "references.bib"
  111. return callback(null, ["references.bib", "lines"])
  112. throw new Error("unknown template: #{template_name}")
  113. sinon.spy @handler, "_buildTemplate"
  114. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  115. @handler.createExampleProject(ownerId, projectName, @callback)
  116. it "should create a blank project first", ->
  117. @handler.createBlankProject.calledWith(ownerId, projectName)
  118. .should.equal true
  119. it 'should insert main.tex', ->
  120. @ProjectEntityHandler.addDoc
  121. .calledWith(project_id, rootFolderId, "main.tex", ["main.tex", "lines"])
  122. .should.equal true
  123. it 'should insert references.bib', ->
  124. @ProjectEntityHandler.addDoc
  125. .calledWith(project_id, rootFolderId, "references.bib", ["references.bib", "lines"])
  126. .should.equal true
  127. it 'should insert universe.jpg', ->
  128. @ProjectEntityHandler.addFile
  129. .calledWith(
  130. project_id, rootFolderId, "universe.jpg",
  131. Path.resolve(__dirname + "/../../../../app/templates/project_files/universe.jpg")
  132. )
  133. .should.equal true
  134. it 'should set the main doc id', ->
  135. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  136. it 'should build the main.tex template', ->
  137. @handler._buildTemplate
  138. .calledWith("main.tex", ownerId, projectName)
  139. .should.equal true
  140. it 'should build the references.bib template', ->
  141. @handler._buildTemplate
  142. .calledWith("references.bib", ownerId, projectName)
  143. .should.equal true
  144. describe "_buildTemplate", ->
  145. beforeEach (done)->
  146. @handler._buildTemplate "main.tex", @user_id, projectName, (err, templateLines)=>
  147. @template = templateLines.reduce (singleLine, line)-> "#{singleLine}\n#{line}"
  148. done()
  149. it "should insert the project name into the template", (done)->
  150. @template.indexOf(projectName).should.not.equal -1
  151. done()
  152. it "should insert the users name into the template", (done)->
  153. @template.indexOf(@user.first_name).should.not.equal -1
  154. @template.indexOf(@user.last_name).should.not.equal -1
  155. done()
  156. it "should not have undefined in the template", (done)->
  157. @template.indexOf("undefined").should.equal -1
  158. done()
  159. it "should not have any underscore brackets in the output", (done)->
  160. @template.indexOf("{{").should.equal -1
  161. @template.indexOf("<%=").should.equal -1
  162. done()
  163. it "should put the year in", (done)->
  164. @template.indexOf(new Date().getUTCFullYear()).should.not.equal -1
  165. done()