ProjectCreationHandlerTests.coffee 6.2 KB

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