2014-02-12 05:23:40 -05:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
modulePath = "../../../../app/js/Features/Uploads/ProjectUploadManager.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
|
|
|
describe "ProjectUploadManager", ->
|
|
|
|
beforeEach ->
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@folder_id = "folder-id-123"
|
2016-02-23 11:46:14 -05:00
|
|
|
@owner_id = "onwer-id-123"
|
2014-02-12 05:23:40 -05:00
|
|
|
@callback = sinon.stub()
|
|
|
|
@ProjectUploadManager = SandboxedModule.require modulePath, requires:
|
|
|
|
"./FileSystemImportManager" : @FileSystemImportManager = {}
|
|
|
|
"./ArchiveManager" : @ArchiveManager = {}
|
|
|
|
"../Project/ProjectCreationHandler" : @ProjectCreationHandler = {}
|
|
|
|
"../Project/ProjectRootDocManager" : @ProjectRootDocManager = {}
|
|
|
|
"rimraf" : @rimraf = sinon.stub().callsArg(1)
|
|
|
|
|
|
|
|
describe "createProjectFromZipArchive", ->
|
|
|
|
beforeEach ->
|
|
|
|
@source = "/path/to/zip/file-name.zip"
|
|
|
|
@root_folder_id = @folder_id
|
|
|
|
@owner_id = "owner-id-123"
|
|
|
|
@name = "Project name"
|
|
|
|
@project =
|
|
|
|
_id: @project_id
|
|
|
|
rootFolder: [ _id: @root_folder_id ]
|
|
|
|
@ProjectCreationHandler.createBlankProject = sinon.stub().callsArgWith(2, null, @project)
|
2016-02-23 11:46:14 -05:00
|
|
|
@ProjectUploadManager.insertZipArchiveIntoFolder = sinon.stub().callsArg(4)
|
2014-02-12 05:23:40 -05:00
|
|
|
@ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArg(1)
|
|
|
|
@ProjectUploadManager.createProjectFromZipArchive @owner_id, @name, @source, @callback
|
|
|
|
|
|
|
|
it "should create a project owned by the owner_id", ->
|
|
|
|
@ProjectCreationHandler
|
|
|
|
.createBlankProject
|
|
|
|
.calledWith(@owner_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should create a project with the correct name", ->
|
|
|
|
@ProjectCreationHandler
|
|
|
|
.createBlankProject
|
|
|
|
.calledWith(sinon.match.any, @name)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should insert the zip file contents into the root folder", ->
|
|
|
|
@ProjectUploadManager
|
|
|
|
.insertZipArchiveIntoFolder
|
2016-02-23 11:46:14 -05:00
|
|
|
.calledWith(@owner_id, @project_id, @root_folder_id, @source)
|
2014-02-12 05:23:40 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should automatically set the root doc", ->
|
|
|
|
@ProjectRootDocManager
|
|
|
|
.setRootDocAutomatically
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.calledWith(sinon.match.falsy, @project).should.equal true
|
|
|
|
|
|
|
|
describe "insertZipArchiveIntoFolder", ->
|
|
|
|
beforeEach ->
|
|
|
|
@source = "/path/to/zile/file.zip"
|
|
|
|
@destination = "/path/to/zile/file-extracted"
|
|
|
|
@ProjectUploadManager._getDestinationDirectory = sinon.stub().returns @destination
|
|
|
|
@ArchiveManager.extractZipArchive = sinon.stub().callsArg(2)
|
2016-02-23 11:46:14 -05:00
|
|
|
@FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
|
2014-02-12 05:23:40 -05:00
|
|
|
|
2016-02-23 11:46:14 -05:00
|
|
|
@ProjectUploadManager.insertZipArchiveIntoFolder @owner_id, @project_id, @folder_id, @source, @callback
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
it "should set up the directory to extract the archive to", ->
|
|
|
|
@ProjectUploadManager._getDestinationDirectory.calledWith(@source).should.equal true
|
|
|
|
|
|
|
|
it "should extract the archive", ->
|
|
|
|
@ArchiveManager.extractZipArchive.calledWith(@source, @destination).should.equal true
|
|
|
|
|
|
|
|
it "should insert the extracted archive into the folder", ->
|
2016-02-23 11:46:14 -05:00
|
|
|
@FileSystemImportManager.addFolderContents.calledWith(@owner_id, @project_id, @folder_id, @destination, false)
|
2014-02-12 05:23:40 -05:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
it "should remove the desintation directory afterwards", ->
|
|
|
|
@rimraf.calledWith(@destination).should.equal true
|
|
|
|
|
|
|
|
describe "_getDestinationDirectory", ->
|
|
|
|
it "should return the path with the time appended", ->
|
|
|
|
date = Date.now()
|
|
|
|
sinon.stub Date, "now", () -> date
|
|
|
|
@ProjectUploadManager
|
|
|
|
._getDestinationDirectory("/path/to/zip/file.zip")
|
|
|
|
.should.equal "/path/to/zip/file-#{date}"
|
|
|
|
Date.now.restore()
|
|
|
|
|