ProjectCreationHandlerTests.coffee 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. describe 'ProjectCreationHandler', ->
  10. ownerId = '4eecb1c1bffa66588e0000a1'
  11. projectName = 'project name'
  12. project_id = "4eecaffcbffa66588e000008"
  13. docId = '4eecb17ebffa66588e00003f'
  14. rootFolderId = "234adfa3r2afe"
  15. beforeEach ->
  16. @versioningApiHandler={enableVersioning: sinon.stub().callsArg(1)}
  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. '../Versioning/VersioningApiHandler':@versioningApiHandler
  46. './ProjectEntityHandler':@ProjectEntityHandler
  47. 'logger-sharelatex': {log:->}
  48. describe 'Creating a Blank project', ->
  49. beforeEach ->
  50. @ProjectModel::save = sinon.stub().callsArg(0)
  51. describe "successfully", ->
  52. it "should save the project", (done)->
  53. @handler.createBlankProject ownerId, projectName, =>
  54. @ProjectModel::save.called.should.equal true
  55. done()
  56. it "should return the project in the callback", (done)->
  57. @handler.createBlankProject ownerId, projectName, (err, project)->
  58. project.name.should.equal projectName
  59. (project.owner_ref + "").should.equal ownerId
  60. done()
  61. it 'should enable versioning', (done)->
  62. @handler.createBlankProject ownerId, projectName, =>
  63. @versioningApiHandler.enableVersioning.calledWith(project_id).should.equal true
  64. done()
  65. it "should set the language from the user", (done)->
  66. @handler.createBlankProject ownerId, projectName, (err, project)->
  67. project.spellCheckLanguage.should.equal "de"
  68. done()
  69. describe "with an error", ->
  70. beforeEach ->
  71. @ProjectModel::save = sinon.stub().callsArgWith(0, new Error("something went wrong"))
  72. @handler.createBlankProject ownerId, projectName, @callback
  73. it 'should return the error to the callback', ->
  74. should.exist @callback.args[0][0]
  75. it 'enables versioning', (done)->
  76. @versioningApiHandler.enableVersioning = (enbleVersioningProjectId, callback)->
  77. project_id.should.equal enbleVersioningProjectId
  78. done()
  79. @handler.createBlankProject ownerId, projectName, ->
  80. describe 'Creating a basic project', ->
  81. beforeEach ->
  82. @project = new @ProjectModel()
  83. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  84. if template_name == "mainbasic.tex"
  85. return callback(null, ["mainbasic.tex", "lines"])
  86. throw new Error("unknown template: #{template_name}")
  87. sinon.spy @handler, "_buildTemplate"
  88. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  89. @handler.createBasicProject(ownerId, projectName, @callback)
  90. it "should create a blank project first", ->
  91. @handler.createBlankProject.calledWith(ownerId, projectName)
  92. .should.equal true
  93. it 'should insert main.tex', ->
  94. @ProjectEntityHandler.addDoc.calledWith(project_id, rootFolderId, "main.tex", ["mainbasic.tex", "lines"])
  95. .should.equal true
  96. it 'should set the main doc id', ->
  97. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  98. it 'should build the mainbasic.tex template', ->
  99. @handler._buildTemplate
  100. .calledWith("mainbasic.tex", ownerId, projectName)
  101. .should.equal true
  102. describe 'Creating an example project', ->
  103. beforeEach ->
  104. @project = new @ProjectModel()
  105. @handler._buildTemplate = (template_name, user, project_name, callback) ->
  106. if template_name == "main.tex"
  107. return callback(null, ["main.tex", "lines"])
  108. if template_name == "references.bib"
  109. return callback(null, ["references.bib", "lines"])
  110. throw new Error("unknown template: #{template_name}")
  111. sinon.spy @handler, "_buildTemplate"
  112. @handler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  113. @handler.createExampleProject(ownerId, projectName, @callback)
  114. it "should create a blank project first", ->
  115. @handler.createBlankProject.calledWith(ownerId, projectName)
  116. .should.equal true
  117. it 'should insert main.tex', ->
  118. @ProjectEntityHandler.addDoc
  119. .calledWith(project_id, rootFolderId, "main.tex", ["main.tex", "lines"])
  120. .should.equal true
  121. it 'should insert references.bib', ->
  122. @ProjectEntityHandler.addDoc
  123. .calledWith(project_id, rootFolderId, "references.bib", ["references.bib", "lines"])
  124. .should.equal true
  125. it 'should insert universe.jpg', ->
  126. @ProjectEntityHandler.addFile
  127. .calledWith(
  128. project_id, rootFolderId, "universe.jpg",
  129. Path.resolve(__dirname + "/../../../../app/templates/project_files/universe.jpg")
  130. )
  131. .should.equal true
  132. it 'should set the main doc id', ->
  133. @ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
  134. it 'should build the main.tex template', ->
  135. @handler._buildTemplate
  136. .calledWith("main.tex", ownerId, projectName)
  137. .should.equal true
  138. it 'should build the references.bib template', ->
  139. @handler._buildTemplate
  140. .calledWith("references.bib", ownerId, projectName)
  141. .should.equal true