ProjectUploadManagerTests.coffee 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/Features/Uploads/ProjectUploadManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "ProjectUploadManager", ->
  7. beforeEach ->
  8. @project_id = "project-id-123"
  9. @folder_id = "folder-id-123"
  10. @owner_id = "onwer-id-123"
  11. @callback = sinon.stub()
  12. @source = "/path/to/zip/file-name.zip"
  13. @destination = "/path/to/zile/file-extracted"
  14. @root_folder_id = @folder_id
  15. @owner_id = "owner-id-123"
  16. @name = "Project name"
  17. @othername = "Other name"
  18. @project =
  19. _id: @project_id
  20. rootFolder: [ _id: @root_folder_id ]
  21. @ProjectUploadManager = SandboxedModule.require modulePath, requires:
  22. "./FileSystemImportManager" : @FileSystemImportManager = {}
  23. "./ArchiveManager" : @ArchiveManager = {}
  24. "../Project/ProjectCreationHandler" : @ProjectCreationHandler = {}
  25. "../Project/ProjectRootDocManager" : @ProjectRootDocManager = {}
  26. "../Project/ProjectDetailsHandler" : @ProjectDetailsHandler = {}
  27. "../Documents/DocumentHelper" : @DocumentHelper = {}
  28. "rimraf" : @rimraf = sinon.stub().callsArg(1)
  29. @ArchiveManager.extractZipArchive = sinon.stub().callsArg(2)
  30. @ArchiveManager.findTopLevelDirectory = sinon.stub().callsArgWith(1, null, @topLevelDestination = "/path/to/zip/file-extracted/nested")
  31. @ProjectCreationHandler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  32. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArg(1)
  33. @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
  34. @ProjectRootDocManager.findRootDocFileFromDirectory = sinon.stub().callsArgWith(1, null, 'main.tex', @othername)
  35. @ProjectRootDocManager.setRootDocFromName = sinon.stub().callsArg(2)
  36. @DocumentHelper.getTitleFromTexContent = sinon.stub().returns(@othername)
  37. @ProjectDetailsHandler.fixProjectName = sinon.stub().returnsArg(0)
  38. describe "createProjectFromZipArchive", ->
  39. describe "when the title can be read from the root document", ->
  40. beforeEach (done) ->
  41. @ProjectUploadManager._getDestinationDirectory = sinon.stub().returns @destination
  42. @ProjectDetailsHandler.generateUniqueName = sinon.stub().callsArgWith(2, null, @othername)
  43. @ProjectUploadManager.createProjectFromZipArchive @owner_id, @name, @source, (err, project) =>
  44. @callback(err, project)
  45. done()
  46. it "should set up the directory to extract the archive to", ->
  47. @ProjectUploadManager._getDestinationDirectory.calledWith(@source).should.equal true
  48. it "should extract the archive", ->
  49. @ArchiveManager.extractZipArchive.calledWith(@source, @destination).should.equal true
  50. it "should find the top level directory", ->
  51. @ArchiveManager.findTopLevelDirectory.calledWith(@destination).should.equal true
  52. it "should insert the extracted archive into the folder", ->
  53. @FileSystemImportManager.addFolderContents.calledWith(@owner_id, @project_id, @folder_id, @topLevelDestination, false)
  54. .should.equal true
  55. it "should create a project owned by the owner_id", ->
  56. @ProjectCreationHandler
  57. .createBlankProject
  58. .calledWith(@owner_id)
  59. .should.equal true
  60. it "should create a project with the correct name", ->
  61. @ProjectCreationHandler
  62. .createBlankProject
  63. .calledWith(sinon.match.any, @othername)
  64. .should.equal true
  65. it "should read the title from the tex contents", ->
  66. @DocumentHelper.getTitleFromTexContent.called.should.equal true
  67. it "should set the root document", ->
  68. @ProjectRootDocManager.setRootDocFromName.calledWith(@project_id, 'main.tex').should.equal true
  69. it "should call the callback", ->
  70. @callback.calledWith(sinon.match.falsy, @project).should.equal true
  71. it "should ensure the name is valid", ->
  72. @ProjectDetailsHandler.fixProjectName.called.should.equal true
  73. describe "when the root document can't be determined", ->
  74. beforeEach (done) ->
  75. @ProjectRootDocManager.findRootDocFileFromDirectory = sinon.stub().callsArg(1)
  76. @ProjectUploadManager._getDestinationDirectory = sinon.stub().returns @destination
  77. @ProjectDetailsHandler.generateUniqueName = sinon.stub().callsArgWith(2, null, @name)
  78. @ProjectUploadManager.createProjectFromZipArchive @owner_id, @name, @source, (err, project) =>
  79. @callback(err, project)
  80. done()
  81. it "should not try to set the root doc", ->
  82. @ProjectRootDocManager.setRootDocFromName.called.should.equal false
  83. describe "createProjectFromZipArchiveWithName", ->
  84. beforeEach (done) ->
  85. @ProjectDetailsHandler.generateUniqueName = sinon.stub().callsArgWith(2, null, @name)
  86. @ProjectUploadManager.insertZipArchiveIntoFolder = sinon.stub().callsArg(4)
  87. @ProjectUploadManager.createProjectFromZipArchiveWithName @owner_id, @name, @source, (err, project) =>
  88. @callback(err, project)
  89. done()
  90. it "should create a project owned by the owner_id", ->
  91. @ProjectCreationHandler
  92. .createBlankProject
  93. .calledWith(@owner_id)
  94. .should.equal true
  95. it "should create a project with the correct name", ->
  96. @ProjectCreationHandler
  97. .createBlankProject
  98. .calledWith(sinon.match.any, @name)
  99. .should.equal true
  100. it "should insert the zip file contents into the root folder", ->
  101. @ProjectUploadManager
  102. .insertZipArchiveIntoFolder
  103. .calledWith(@owner_id, @project_id, @root_folder_id, @source)
  104. .should.equal true
  105. it "should automatically set the root doc", ->
  106. @ProjectRootDocManager
  107. .setRootDocAutomatically
  108. .calledWith(@project_id)
  109. .should.equal true
  110. it "should call the callback", ->
  111. @callback.calledWith(sinon.match.falsy, @project).should.equal true
  112. describe "insertZipArchiveIntoFolder", ->
  113. beforeEach (done) ->
  114. @ProjectUploadManager._getDestinationDirectory = sinon.stub().returns @destination
  115. @ProjectUploadManager.insertZipArchiveIntoFolder @owner_id, @project_id, @folder_id, @source, (err) =>
  116. @callback(err)
  117. done()
  118. it "should set up the directory to extract the archive to", ->
  119. @ProjectUploadManager._getDestinationDirectory.calledWith(@source).should.equal true
  120. it "should extract the archive", ->
  121. @ArchiveManager.extractZipArchive.calledWith(@source, @destination).should.equal true
  122. it "should find the top level directory", ->
  123. @ArchiveManager.findTopLevelDirectory.calledWith(@destination).should.equal true
  124. it "should insert the extracted archive into the folder", ->
  125. @FileSystemImportManager.addFolderContents.calledWith(@owner_id, @project_id, @folder_id, @topLevelDestination, false)
  126. .should.equal true
  127. it "should return the callback", ->
  128. @callback.called.should.equal true
  129. it "should remove the desintation directory afterwards", ->
  130. @rimraf.calledWith(@destination).should.equal true
  131. describe "_getDestinationDirectory", ->
  132. it "should return the path with the time appended", ->
  133. date = Date.now()
  134. sinon.stub Date, "now", () -> date
  135. @ProjectUploadManager
  136. ._getDestinationDirectory("/path/to/zip/file.zip")
  137. .should.equal "/path/to/zip/file-#{date}"
  138. Date.now.restore()