ProjectDuplicatorTests.coffee 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. sinon = require('sinon')
  2. chai = require('chai').should()
  3. modulePath = "../../../../app/js/Features/Project/ProjectDuplicator.js"
  4. SandboxedModule = require('sandboxed-module')
  5. describe 'ProjectDuplicator', ->
  6. beforeEach ->
  7. @level2folder =
  8. name: "level2folderName"
  9. _id:"level2folderId"
  10. docs:[@doc2 = {_id: "doc2_id", name:"level2folderDocName"}, undefined]
  11. folders:[]
  12. fileRefs:[{name:"file2", _id:"file2"}]
  13. @level1folder =
  14. name:"level1folder"
  15. _id:"level1folderId"
  16. docs:[@doc1 = {_id: "doc1_id", name:"level1folderDocName"}]
  17. folders:[@level2folder]
  18. fileRefs:[{name:"file1", _id:"file1"}, null]
  19. @rootFolder =
  20. name:"rootFolder"
  21. _id:"rootFolderId"
  22. docs:[@doc0 = {_id: "doc0_id", name:"rootDocHere"}]
  23. folders:[@level1folder, {}]
  24. fileRefs:[{name:"file0", _id:"file0"}]
  25. @project =
  26. _id: @old_project_id = "this_is_the_old_project_id"
  27. rootDoc_id: "rootDoc_id"
  28. rootFolder:[@rootFolder]
  29. compiler: "this_is_a_Compiler"
  30. @docContents = [{
  31. _id: @doc0._id
  32. lines: @doc0_lines = ["zero"]
  33. }, {
  34. _id: @doc1._id
  35. lines: @doc1_lines = ["one"]
  36. }, {
  37. _id: @doc2._id
  38. lines: @doc2_lines = ["two"]
  39. }]
  40. @DocstoreManager =
  41. getAllDocs: sinon.stub().callsArgWith(1, null, @docContents)
  42. @owner = {_id:"this_is_the_owner"}
  43. @stubbedNewProject =
  44. _id: @new_project_id = "new_project_id"
  45. readOnly_refs:[]
  46. collaberator_refs:[]
  47. rootFolder:[
  48. {_id:"new_root_folder_id"}
  49. ]
  50. @foundRootDoc = {_id:"rootDocId", name:"rootDocHere"}
  51. @creationHandler =
  52. createBlankProject : sinon.stub().callsArgWith(2, null, @stubbedNewProject)
  53. @newFolder = {_id: "newFolderId"}
  54. @locator =
  55. findRootDoc : sinon.stub().callsArgWith(1, null, @foundRootDoc, {})
  56. @projectOptionsHandler =
  57. setCompiler : sinon.stub()
  58. @entityHandler =
  59. addDocWithProject: sinon.stub().callsArgWith(4, null, {name:"somDoc"})
  60. copyFileFromExistingProjectWithProject: sinon.stub().callsArgWith(4)
  61. setRootDoc: sinon.stub()
  62. addFolderWithProject: sinon.stub().callsArgWith(3, null, @newFolder)
  63. @DocumentUpdaterHandler =
  64. flushProjectToMongo: sinon.stub().callsArg(1)
  65. @Project =
  66. findById: sinon.stub().callsArgWith(1, null, @project)
  67. @ProjectGetter =
  68. getProject: sinon.stub()
  69. @ProjectGetter.getProject.withArgs(@old_project_id, sinon.match.any).callsArgWith(2, null, @project)
  70. @ProjectGetter.getProject.withArgs(@new_project_id, sinon.match.any).callsArgWith(2, null, @stubbedNewProject)
  71. @duplicator = SandboxedModule.require modulePath, requires:
  72. '../../models/Project':{Project:@Project}
  73. "../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler
  74. './ProjectCreationHandler': @creationHandler
  75. './ProjectEntityHandler': @entityHandler
  76. './ProjectLocator': @locator
  77. './ProjectOptionsHandler': @projectOptionsHandler
  78. "../Docstore/DocstoreManager": @DocstoreManager
  79. "./ProjectGetter":@ProjectGetter
  80. 'logger-sharelatex':{log:->}
  81. it "should look up the original project", (done) ->
  82. newProjectName = "someProj"
  83. @duplicator.duplicate @owner, @old_project_id, newProjectName, (err, newProject)=>
  84. @ProjectGetter.getProject.calledWith(@old_project_id).should.equal true
  85. done()
  86. it "should flush the original project to mongo", (done) ->
  87. newProjectName = "someProj"
  88. @duplicator.duplicate @owner, @old_project_id, newProjectName, (err, newProject)=>
  89. @DocumentUpdaterHandler.flushProjectToMongo.calledWith(@old_project_id).should.equal true
  90. done()
  91. it 'should create a blank project', (done)->
  92. newProjectName = "someProj"
  93. @duplicator.duplicate @owner, @old_project_id, newProjectName, (err, newProject)=>
  94. newProject._id.should.equal @stubbedNewProject._id
  95. @creationHandler.createBlankProject.calledWith(@owner._id, newProjectName).should.equal true
  96. done()
  97. it 'should use the same compiler', (done)->
  98. @entityHandler.addDocWithProject.callsArgWith(4, null, @rootFolder.docs[0])
  99. @duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
  100. @projectOptionsHandler.setCompiler.calledWith(@stubbedNewProject._id, @project.compiler).should.equal true
  101. done()
  102. it 'should use the same root doc', (done)->
  103. @entityHandler.addDocWithProject.callsArgWith(4, null, @rootFolder.docs[0])
  104. @duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
  105. @entityHandler.setRootDoc.calledWith(@stubbedNewProject._id, @rootFolder.docs[0]._id).should.equal true
  106. done()
  107. it 'should not copy the collaberators or read only refs', (done)->
  108. @duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
  109. newProject.collaberator_refs.length.should.equal 0
  110. newProject.readOnly_refs.length.should.equal 0
  111. done()
  112. it 'should copy all the folders', (done)->
  113. @duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
  114. @entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @level1folder.name).should.equal true
  115. @entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @newFolder._id, @level2folder.name).should.equal true
  116. @entityHandler.addFolderWithProject.callCount.should.equal 2
  117. done()
  118. it 'should copy all the docs', (done)->
  119. @duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
  120. @DocstoreManager.getAllDocs.calledWith(@old_project_id).should.equal true
  121. @entityHandler.addDocWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @doc0.name, @doc0_lines).should.equal true
  122. @entityHandler.addDocWithProject.calledWith(@stubbedNewProject, @newFolder._id, @doc1.name, @doc1_lines).should.equal true
  123. @entityHandler.addDocWithProject.calledWith(@stubbedNewProject, @newFolder._id, @doc2.name, @doc2_lines).should.equal true
  124. done()
  125. it 'should copy all the files', (done)->
  126. @duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
  127. @entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @project._id, @rootFolder.fileRefs[0]).should.equal true
  128. @entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level1folder.fileRefs[0]).should.equal true
  129. @entityHandler.copyFileFromExistingProjectWithProject.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level2folder.fileRefs[0]).should.equal true
  130. done()