overleaf/services/web/test/UnitTests/coffee/Uploads/ProjectUploadManagerTests.coffee

100 lines
3.8 KiB
CoffeeScript
Raw Normal View History

2014-02-12 10:23:40 +00: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 16:46:14 +00:00
@owner_id = "onwer-id-123"
2014-02-12 10:23:40 +00: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 16:46:14 +00:00
@ProjectUploadManager.insertZipArchiveIntoFolder = sinon.stub().callsArg(4)
2014-02-12 10:23:40 +00: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 16:46:14 +00:00
.calledWith(@owner_id, @project_id, @root_folder_id, @source)
2014-02-12 10:23:40 +00: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)
@ArchiveManager.findTopLevelDirectory = sinon.stub().callsArgWith(1, null, @topLevelDestination = "/path/to/zip/file-extracted/nested")
2016-02-23 16:46:14 +00:00
@FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
2014-02-12 10:23:40 +00:00
2016-02-23 16:46:14 +00:00
@ProjectUploadManager.insertZipArchiveIntoFolder @owner_id, @project_id, @folder_id, @source, @callback
2014-02-12 10:23:40 +00: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 find the top level directory", ->
@ArchiveManager.findTopLevelDirectory.calledWith(@destination).should.equal true
2014-02-12 10:23:40 +00:00
it "should insert the extracted archive into the folder", ->
@FileSystemImportManager.addFolderContents.calledWith(@owner_id, @project_id, @folder_id, @topLevelDestination, false)
2014-02-12 10:23:40 +00: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()