ProjectUploadManagerTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. @ProjectUploadManager = SandboxedModule.require modulePath, requires:
  13. "./FileSystemImportManager" : @FileSystemImportManager = {}
  14. "./ArchiveManager" : @ArchiveManager = {}
  15. "../Project/ProjectCreationHandler" : @ProjectCreationHandler = {}
  16. "../Project/ProjectRootDocManager" : @ProjectRootDocManager = {}
  17. "rimraf" : @rimraf = sinon.stub().callsArg(1)
  18. describe "createProjectFromZipArchive", ->
  19. beforeEach ->
  20. @source = "/path/to/zip/file-name.zip"
  21. @root_folder_id = @folder_id
  22. @owner_id = "owner-id-123"
  23. @name = "Project name"
  24. @project =
  25. _id: @project_id
  26. rootFolder: [ _id: @root_folder_id ]
  27. @ProjectCreationHandler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
  28. @ProjectUploadManager.insertZipArchiveIntoFolder = sinon.stub().callsArg(4)
  29. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArg(1)
  30. @ProjectUploadManager.createProjectFromZipArchive @owner_id, @name, @source, @callback
  31. it "should create a project owned by the owner_id", ->
  32. @ProjectCreationHandler
  33. .createBlankProject
  34. .calledWith(@owner_id)
  35. .should.equal true
  36. it "should create a project with the correct name", ->
  37. @ProjectCreationHandler
  38. .createBlankProject
  39. .calledWith(sinon.match.any, @name)
  40. .should.equal true
  41. it "should insert the zip file contents into the root folder", ->
  42. @ProjectUploadManager
  43. .insertZipArchiveIntoFolder
  44. .calledWith(@owner_id, @project_id, @root_folder_id, @source)
  45. .should.equal true
  46. it "should automatically set the root doc", ->
  47. @ProjectRootDocManager
  48. .setRootDocAutomatically
  49. .calledWith(@project_id)
  50. .should.equal true
  51. it "should call the callback", ->
  52. @callback.calledWith(sinon.match.falsy, @project).should.equal true
  53. describe "insertZipArchiveIntoFolder", ->
  54. beforeEach ->
  55. @source = "/path/to/zile/file.zip"
  56. @destination = "/path/to/zile/file-extracted"
  57. @ProjectUploadManager._getDestinationDirectory = sinon.stub().returns @destination
  58. @ArchiveManager.extractZipArchive = sinon.stub().callsArg(2)
  59. @ArchiveManager.findTopLevelDirectory = sinon.stub().callsArgWith(1, null, @topLevelDestination = "/path/to/zip/file-extracted/nested")
  60. @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
  61. @ProjectUploadManager.insertZipArchiveIntoFolder @owner_id, @project_id, @folder_id, @source, @callback
  62. it "should set up the directory to extract the archive to", ->
  63. @ProjectUploadManager._getDestinationDirectory.calledWith(@source).should.equal true
  64. it "should extract the archive", ->
  65. @ArchiveManager.extractZipArchive.calledWith(@source, @destination).should.equal true
  66. it "should find the top level directory", ->
  67. @ArchiveManager.findTopLevelDirectory.calledWith(@destination).should.equal true
  68. it "should insert the extracted archive into the folder", ->
  69. @FileSystemImportManager.addFolderContents.calledWith(@owner_id, @project_id, @folder_id, @topLevelDestination, false)
  70. .should.equal true
  71. it "should return the callback", ->
  72. @callback.called.should.equal true
  73. it "should remove the desintation directory afterwards", ->
  74. @rimraf.calledWith(@destination).should.equal true
  75. describe "_getDestinationDirectory", ->
  76. it "should return the path with the time appended", ->
  77. date = Date.now()
  78. sinon.stub Date, "now", () -> date
  79. @ProjectUploadManager
  80. ._getDestinationDirectory("/path/to/zip/file.zip")
  81. .should.equal "/path/to/zip/file-#{date}"
  82. Date.now.restore()