ProjectUploadManagerTests.coffee 3.5 KB

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