overleaf/services/web/test/unit/coffee/Uploads/FileSystemImportManagerTests.coffee

201 lines
8.7 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../../app/js/Features/Uploads/FileSystemImportManager.js"
SandboxedModule = require('sandboxed-module')
describe "FileSystemImportManager", ->
beforeEach ->
@project_id = "project-id-123"
@folder_id = "folder-id-123"
@name = "test-file.tex"
@path_on_disk = "/path/to/file/#{@name}"
@replace = "replace-boolean-flag-mock"
2016-02-23 11:46:14 -05:00
@user_id = "mock-user-123"
@callback = sinon.stub()
2014-02-12 05:23:40 -05:00
@FileSystemImportManager = SandboxedModule.require modulePath, requires:
"fs" : @fs = {}
"../Editor/EditorController": @EditorController = {}
"./FileTypeManager": @FileTypeManager = {}
"../Project/ProjectLocator": @ProjectLocator = {}
"logger-sharelatex":
log:->
err:->
2016-02-23 11:46:14 -05:00
2014-02-12 05:23:40 -05:00
describe "addDoc", ->
beforeEach ->
@docContent = "one\ntwo\nthree"
@docLines = @docContent.split("\n")
@fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
describe "when path is symlink", ->
beforeEach ->
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
@EditorController.addDoc = sinon.stub()
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
it "should not read the file from disk", ->
@fs.readFile.called.should.equal false
it "should not insert the doc", ->
@EditorController.addDoc.called.should.equal false
2014-02-12 05:23:40 -05:00
2016-02-23 11:46:14 -05:00
describe "with replace set to false", ->
beforeEach ->
@EditorController.addDoc = sinon.stub().callsArg(6)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
2014-02-12 05:23:40 -05:00
2016-02-23 11:46:14 -05:00
it "should read the file from disk", ->
@fs.readFile.calledWith(@path_on_disk, "utf8").should.equal true
2014-02-12 05:23:40 -05:00
2016-02-23 11:46:14 -05:00
it "should insert the doc", ->
@EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
2016-02-23 11:46:14 -05:00
.should.equal true
2014-02-12 05:23:40 -05:00
2016-02-23 11:46:14 -05:00
describe "with windows line ending", ->
beforeEach ->
@docContent = "one\r\ntwo\r\nthree"
@docLines = ["one", "two", "three"]
@fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
@EditorController.addDoc = sinon.stub().callsArg(6)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
it "should strip the \\r characters before adding", ->
@EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
2016-02-23 11:46:14 -05:00
.should.equal true
describe "with replace set to true", ->
beforeEach ->
@EditorController.upsertDoc = sinon.stub().yields()
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
it "should upsert the doc", ->
@EditorController.upsertDoc
.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
.should.equal true
2014-02-12 05:23:40 -05:00
describe "addFile with replace set to false", ->
beforeEach ->
2018-02-20 07:52:57 -05:00
@EditorController.addFile = sinon.stub().yields()
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
2014-02-12 05:23:40 -05:00
it "should add the file", ->
2018-02-20 07:52:57 -05:00
@EditorController.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, null, "upload", @user_id)
2014-02-12 05:23:40 -05:00
.should.equal true
describe "addFile with symlink", ->
beforeEach ->
@EditorController.addFile = sinon.stub()
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
@EditorController.replaceFile = sinon.stub()
@FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
it "should node add the file", ->
@EditorController.addFile.called.should.equal false
@EditorController.replaceFile.called.should.equal false
2014-02-12 05:23:40 -05:00
describe "addFile with replace set to true", ->
beforeEach ->
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
@EditorController.upsertFile = sinon.stub().yields()
@FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
2014-02-12 05:23:40 -05:00
it "should add the file", ->
@EditorController.upsertFile
2018-02-20 07:52:57 -05:00
.calledWith(@project_id, @folder_id, @name, @path_on_disk, null, "upload", @user_id)
.should.equal true
2014-02-12 05:23:40 -05:00
describe "addFolder", ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@new_folder_id = "new-folder-id"
@EditorController.addFolder = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
2014-02-12 05:23:40 -05:00
describe "successfully", ->
beforeEach ->
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
@FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
2014-02-12 05:23:40 -05:00
it "should add a folder to the project", ->
@EditorController.addFolder.calledWith(@project_id, @folder_id, @name, "upload")
.should.equal true
it "should add the folders contents", ->
@FileSystemImportManager.addFolderContents.calledWith(@user_id, @project_id, @new_folder_id, @path_on_disk, @replace)
.should.equal true
describe "with symlink", ->
beforeEach ->
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
@FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
2014-02-12 05:23:40 -05:00
it "should not add a folder to the project", ->
@EditorController.addFolder.called.should.equal false
@FileSystemImportManager.addFolderContents.called.should.equal false
2014-02-12 05:23:40 -05:00
describe "addFolderContents", ->
beforeEach ->
@folderEntries = ["path1", "path2", "path3"]
@ignoredEntries = [".DS_Store"]
@fs.readdir = sinon.stub().callsArgWith(1, null, @folderEntries.concat @ignoredEntries)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addEntity = sinon.stub().callsArg(6)
2014-02-12 05:23:40 -05:00
@FileTypeManager.shouldIgnore = (path, callback) =>
callback null, @ignoredEntries.indexOf(require("path").basename(path)) != -1
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addFolderContents @user_id, @project_id, @folder_id, @path_on_disk, @replace, @callback
2014-02-12 05:23:40 -05:00
it "should call addEntity for each file in the folder which is not ignored", ->
for name in @folderEntries
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
2014-02-12 05:23:40 -05:00
.should.equal true
it "should not call addEntity for the ignored files", ->
for name in @ignoredEntries
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addEntity.calledWith(@user_id, @project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
2014-02-12 05:23:40 -05:00
.should.equal false
it "should look in the correct directory", ->
@fs.readdir.calledWith(@path_on_disk).should.equal true
describe "addEntity", ->
2014-02-12 05:23:40 -05:00
describe "with directory", ->
beforeEach ->
@FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, true)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addFolder = sinon.stub().callsArg(6)
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
2014-02-12 05:23:40 -05:00
it "should call addFolder", ->
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addFolder.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
2014-02-12 05:23:40 -05:00
.should.equal true
describe "with binary file", ->
beforeEach ->
@FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
2014-09-04 08:00:41 -04:00
@FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, true)
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addFile = sinon.stub().callsArg(6)
@FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
2014-02-12 05:23:40 -05:00
it "should call addFile", ->
2017-06-12 05:09:33 -04:00
@FileSystemImportManager.addFile.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
2014-02-12 05:23:40 -05:00
.should.equal true
describe "with text file", ->
beforeEach ->
@FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
2014-09-04 08:00:41 -04:00
@FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, false)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addDoc = sinon.stub().callsArg(6)
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addEntity @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
2014-02-12 05:23:40 -05:00
it "should call addFile", ->
2017-06-12 05:09:33 -04:00
@FileSystemImportManager.addDoc.calledWith(@user_id, @project_id, @folder_id, @name, @path_on_disk, @replace)
2014-02-12 05:23:40 -05:00
.should.equal true