ProjectUploadManagerTests.coffee 4.0 KB

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