ProjectCreationHandlerTests.coffee 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. spies = require('chai-spies')
  2. chai = require('chai').use(spies)
  3. sinon = require("sinon")
  4. should = chai.should()
  5. modulePath = "../../../../app/js/Features/Project/ProjectCreationHandler.js"
  6. SandboxedModule = require('sandboxed-module')
  7. Settings = require('settings-sharelatex')
  8. Path = require "path"
  9. _ = require("underscore")
  10. describe 'ProjectCreationHandler', ->
  11. ownerId = '4eecb1c1bffa66588e0000a1'
  12. projectName = 'project name goes here'
  13. project_id = "4eecaffcbffa66588e000008"
  14. docId = '4eecb17ebffa66588e00003f'
  15. rootFolderId = "234adfa3r2afe"
  16. beforeEach ->
  17. @versioningApiHandler={enableVersioning: sinon.stub().callsArg(1)}
  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(5, 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. '../Versioning/VersioningApiHandler':@versioningApiHandler
  47. './ProjectEntityHandler':@ProjectEntityHandler
  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 enable versioning', (done)->
  63. @handler.createBlankProject ownerId, projectName, =>
  64. @versioningApiHandler.enableVersioning.calledWith(project_id).should.equal true
  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. describe "with an error", ->
  71. beforeEach ->
  72. @ProjectModel::save = sinon.stub().callsArgWith(0, new Error("something went wrong"))
  73. @handler.createBlankProject ownerId, projectName, @callback
  74. it 'should return the error to the callback', ->
  75. should.exist @callback.args[0][0]
  76. it 'enables versioning', (done)->
  77. @versioningApiHandler.enableVersioning = (enbleVersioningProjectId, callback)->
  78. project_id.should.equal enbleVersioningProjectId
  79. done()
  80. @handler.createBlankProject ownerId, projectName, ->
  81. describe 'Creating a basic project', ->
  82. beforeEach ->
  83. @project = new @ProjectModel()
  84. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  85. if template_name == "mainbasic.tex"
  86. return callback(null, ["mainbasic.tex", "lines"])
  87. throw new Error("unknown template: #{template_name}")
  88. sinon.spy @handler, "_buildTemplate"
  89. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  90. @handler.createBasicProject(ownerId, projectName, @callback)
  91. it "should create a blank project first", ->
  92. @handler.createBlankProject.calledWith(ownerId, projectName)
  93. .should.equal true
  94. it 'should insert main.tex', ->
  95. @ProjectEntityHandler.addDoc.calledWith(project_id, rootFolderId, "main.tex", ["mainbasic.tex", "lines"])
  96. .should.equal true
  97. it 'should set the main doc id', ->
  98. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  99. it 'should build the mainbasic.tex template', ->
  100. @handler._buildTemplate
  101. .calledWith("mainbasic.tex", ownerId, projectName)
  102. .should.equal true
  103. describe 'Creating an example project', ->
  104. beforeEach ->
  105. @project = new @ProjectModel()
  106. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  107. if template_name == "main.tex"
  108. return callback(null, ["main.tex", "lines"])
  109. if template_name == "references.bib"
  110. return callback(null, ["references.bib", "lines"])
  111. throw new Error("unknown template: #{template_name}")
  112. sinon.spy @handler, "_buildTemplate"
  113. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  114. @handler.createExampleProject(ownerId, projectName, @callback)
  115. it "should create a blank project first", ->
  116. @handler.createBlankProject.calledWith(ownerId, projectName)
  117. .should.equal true
  118. it 'should insert main.tex', ->
  119. @ProjectEntityHandler.addDoc
  120. .calledWith(project_id, rootFolderId, "main.tex", ["main.tex", "lines"])
  121. .should.equal true
  122. it 'should insert references.bib', ->
  123. @ProjectEntityHandler.addDoc
  124. .calledWith(project_id, rootFolderId, "references.bib", ["references.bib", "lines"])
  125. .should.equal true
  126. it 'should insert universe.jpg', ->
  127. @ProjectEntityHandler.addFile
  128. .calledWith(
  129. project_id, rootFolderId, "universe.jpg",
  130. Path.resolve(__dirname + "/../../../../app/templates/project_files/universe.jpg")
  131. )
  132. .should.equal true
  133. it 'should set the main doc id', ->
  134. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  135. it 'should build the main.tex template', ->
  136. @handler._buildTemplate
  137. .calledWith("main.tex", ownerId, projectName)
  138. .should.equal true
  139. it 'should build the references.bib template', ->
  140. @handler._buildTemplate
  141. .calledWith("references.bib", ownerId, projectName)
  142. .should.equal true
  143. describe "_buildTemplate", ->
  144. beforeEach (done)->
  145. @handler._buildTemplate "main.tex", @user_id, projectName, (err, templateLines)=>
  146. @template = templateLines.reduce (singleLine, line)-> "#{singleLine}\n#{line}"
  147. done()
  148. it "should insert the project name into the template", (done)->
  149. @template.indexOf(projectName).should.not.equal -1
  150. done()
  151. it "should insert the users name into the template", (done)->
  152. @template.indexOf(@user.first_name).should.not.equal -1
  153. @template.indexOf(@user.last_name).should.not.equal -1
  154. done()
  155. it "should not have undefined in the template", (done)->
  156. @template.indexOf("undefined").should.equal -1
  157. done()
  158. it "should not have any underscore brackets in the output", (done)->
  159. @template.indexOf("{{").should.equal -1
  160. @template.indexOf("<%=").should.equal -1
  161. done()
  162. it "should put the year in", (done)->
  163. @template.indexOf(new Date().getUTCFullYear()).should.not.equal -1
  164. done()