refactor ProjectEntityHandler

- moves project locking into ProjectEntityHandler
- splits ProjectEntityHandler into ProjectEntityHandler,
  ProjectEntityUpdateHandler and ProjectEntityMongoUpdateHandler
- adds upsertDoc/upsertFile and upsertDocWithPath/upsertFileWithPath to
  EditorController and ProjectEntiyUpdateHandler
This commit is contained in:
Hayden Faulds
2018-02-12 13:33:59 +00:00
parent 5fef846a6c
commit 458bbc7cfd
27 changed files with 2877 additions and 2876 deletions
@@ -288,6 +288,74 @@ describe "ProjectStructureChanges", ->
done()
describe "renaming entities", ->
beforeEach () ->
MockDocUpdaterApi.clearProjectStructureUpdates()
it "should version renaming a doc", (done) ->
@owner.request.post {
uri: "project/#{@example_project_id}/Doc/#{@example_doc_id}/rename",
json:
name: 'new_renamed.tex'
}, (error, res, body) =>
throw error if error?
if res.statusCode < 200 || res.statusCode >= 300
throw new Error("failed to move doc #{res.statusCode}")
updates = MockDocUpdaterApi.getProjectStructureUpdates(@example_project_id).docUpdates
expect(updates.length).to.equal(1)
update = updates[0]
expect(update.userId).to.equal(@owner._id)
expect(update.pathname).to.equal("/bar/foo/new.tex")
expect(update.newPathname).to.equal("/bar/foo/new_renamed.tex")
done()
it "should version renaming a file", (done) ->
@owner.request.post {
uri: "project/#{@example_project_id}/File/#{@example_file_id}/rename",
json:
name: '1pixel_renamed.png'
}, (error, res, body) =>
throw error if error?
if res.statusCode < 200 || res.statusCode >= 300
throw new Error("failed to move file #{res.statusCode}")
updates = MockDocUpdaterApi.getProjectStructureUpdates(@example_project_id).fileUpdates
expect(updates.length).to.equal(1)
update = updates[0]
expect(update.userId).to.equal(@owner._id)
expect(update.pathname).to.equal("/bar/foo/1pixel.png")
expect(update.newPathname).to.equal("/bar/foo/1pixel_renamed.png")
done()
it "should version renaming a folder", (done) ->
@owner.request.post {
uri: "project/#{@example_project_id}/Folder/#{@example_folder_id_1}/rename",
json:
name: 'foo_renamed'
}, (error, res, body) =>
throw error if error?
if res.statusCode < 200 || res.statusCode >= 300
throw new Error("failed to move folder #{res.statusCode}")
updates = MockDocUpdaterApi.getProjectStructureUpdates(@example_project_id).docUpdates
expect(updates.length).to.equal(1)
update = updates[0]
expect(update.userId).to.equal(@owner._id)
expect(update.pathname).to.equal("/bar/foo/new_renamed.tex")
expect(update.newPathname).to.equal("/bar/foo_renamed/new_renamed.tex")
updates = MockDocUpdaterApi.getProjectStructureUpdates(@example_project_id).fileUpdates
expect(updates.length).to.equal(1)
update = updates[0]
expect(update.userId).to.equal(@owner._id)
expect(update.pathname).to.equal("/bar/foo/1pixel_renamed.png")
expect(update.newPathname).to.equal("/bar/foo_renamed/1pixel_renamed.png")
done()
describe "deleting entities", ->
beforeEach () ->
MockDocUpdaterApi.clearProjectStructureUpdates()
@@ -304,14 +372,14 @@ describe "ProjectStructureChanges", ->
expect(updates.length).to.equal(1)
update = updates[0]
expect(update.userId).to.equal(@owner._id)
expect(update.pathname).to.equal("/bar/foo/new.tex")
expect(update.pathname).to.equal("/bar/foo_renamed/new_renamed.tex")
expect(update.newPathname).to.equal("")
updates = MockDocUpdaterApi.getProjectStructureUpdates(@example_project_id).fileUpdates
expect(updates.length).to.equal(1)
update = updates[0]
expect(update.userId).to.equal(@owner._id)
expect(update.pathname).to.equal("/bar/foo/1pixel.png")
expect(update.pathname).to.equal("/bar/foo_renamed/1pixel_renamed.png")
expect(update.newPathname).to.equal("")
done()
@@ -16,6 +16,7 @@ describe "DocumentController", ->
log:->
err:->
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
"../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
@res = new MockResponse()
@req = new MockRequest()
@next = sinon.stub()
@@ -68,7 +69,7 @@ describe "DocumentController", ->
describe "when the document exists", ->
beforeEach ->
@ProjectEntityHandler.updateDocLines = sinon.stub().yields()
@ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields()
@req.body =
lines: @doc_lines
version: @version
@@ -76,7 +77,7 @@ describe "DocumentController", ->
@DocumentController.setDocument(@req, @res, @next)
it "should update the document in Mongo", ->
@ProjectEntityHandler.updateDocLines
@ProjectEntityUpdateHandler.updateDocLines
.calledWith(@project_id, @doc_id, @doc_lines, @version, @ranges)
.should.equal true
@@ -85,7 +86,7 @@ describe "DocumentController", ->
describe "when the document doesn't exist", ->
beforeEach ->
@ProjectEntityHandler.updateDocLines = sinon.stub().yields(new Errors.NotFoundError("document does not exist"))
@ProjectEntityUpdateHandler.updateDocLines = sinon.stub().yields(new Errors.NotFoundError("document does not exist"))
@req.body =
lines: @doc_lines
@DocumentController.setDocument(@req, @res, @next)
@@ -10,373 +10,251 @@ assert = require('assert')
describe "EditorController", ->
beforeEach ->
@project_id = "test-project-id"
@project =
_id: @project_id
owner_ref:{_id:"something"}
@doc_id = "test-doc-id"
@source = "dropbox"
@user =
_id: @user_id = "user-id"
projects: {}
@doc = _id: @doc_id = "test-doc-id"
@docName = "doc.tex"
@docLines = ["1234","dskl"]
@file = _id: @file_id ="dasdkjk"
@fileName = "file.png"
@fsPath = "/folder/file.png"
@rooms = {}
@io =
sockets :
clients : (room_id) =>
@rooms[room_id]
@DocumentUpdaterHandler = {}
@ProjectOptionsHandler =
setCompiler : sinon.spy()
setSpellCheckLanguage: sinon.spy()
@ProjectEntityHandler =
flushProjectToThirdPartyDataStore:sinon.stub()
@Project =
findPopulatedById: sinon.stub().callsArgWith(1, null, @project)
@client = new MockClient()
@folder_id = "123ksajdn"
@folder = _id: @folder_id
@folderName = "folder"
@settings =
apis:{thirdPartyDataStore:{emptyProjectFlushDelayMiliseconds:0.5}}
redis: web:{}
@dropboxProjectLinker = {}
@callback = sinon.stub()
@ProjectDetailsHandler =
setProjectDescription:sinon.stub()
@CollaboratorsHandler =
removeUserFromProject: sinon.stub().callsArgWith(2)
addUserToProject: sinon.stub().callsArgWith(3)
@ProjectDeleter =
deleteProject: sinon.stub()
@LockManager =
runWithLock : sinon.spy((key, runner, callback) -> runner(callback))
@EditorController = SandboxedModule.require modulePath, requires:
"../../infrastructure/Server" : io : @io
'../Project/ProjectEntityHandler' : @ProjectEntityHandler
'../Project/ProjectOptionsHandler' : @ProjectOptionsHandler
'../Project/ProjectDetailsHandler': @ProjectDetailsHandler
'../Project/ProjectDeleter' : @ProjectDeleter
'../Collaborators/CollaboratorsHandler': @CollaboratorsHandler
'../DocumentUpdater/DocumentUpdaterHandler' : @DocumentUpdaterHandler
'../../models/Project' : Project: @Project
"settings-sharelatex":@settings
'../Dropbox/DropboxProjectLinker':@dropboxProjectLinker
'./EditorRealTimeController':@EditorRealTimeController = {}
"metrics-sharelatex": @Metrics = { inc: sinon.stub() }
"../TrackChanges/TrackChangesManager": @TrackChangesManager = {}
"../../infrastructure/LockManager":@LockManager
'redis-sharelatex':createClient:-> auth:->
'../Project/ProjectEntityUpdateHandler' : @ProjectEntityUpdateHandler = {}
'../Project/ProjectOptionsHandler' : @ProjectOptionsHandler =
setCompiler: sinon.stub().yields()
setSpellCheckLanguage: sinon.stub().yields()
'../Project/ProjectDetailsHandler': @ProjectDetailsHandler =
setProjectDescription: sinon.stub().yields()
renameProject: sinon.stub().yields()
setPublicAccessLevel: sinon.stub().yields()
'../Project/ProjectDeleter' : @ProjectDeleter = {}
'../DocumentUpdater/DocumentUpdaterHandler' : @DocumentUpdaterHandler =
flushDocToMongo: sinon.stub().yields()
setDocument: sinon.stub().yields()
'./EditorRealTimeController':@EditorRealTimeController =
emitToRoom: sinon.stub()
"metrics-sharelatex": @Metrics = inc: sinon.stub()
"logger-sharelatex": @logger =
log: sinon.stub()
err: sinon.stub()
describe "updating compiler used for project", ->
it "should send the new compiler and project id to the project options handler", (done)->
compiler = "latex"
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.setCompiler @project_id, compiler, (err) =>
@ProjectOptionsHandler.setCompiler.calledWith(@project_id, compiler).should.equal true
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "compilerUpdated", compiler).should.equal true
done()
@ProjectOptionsHandler.setCompiler.args[0][2]()
describe "updating language code used for project", ->
it "should send the new languageCode and project id to the project options handler", (done)->
languageCode = "fr"
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.setSpellCheckLanguage @project_id, languageCode, (err) =>
@ProjectOptionsHandler.setSpellCheckLanguage.calledWith(@project_id, languageCode).should.equal true
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "spellCheckLanguageUpdated", languageCode).should.equal true
done()
@ProjectOptionsHandler.setSpellCheckLanguage.args[0][2]()
describe 'setDoc', ->
describe 'addDoc', ->
beforeEach ->
@docLines = ["foo", "bar"]
@DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArg(2)
@DocumentUpdaterHandler.setDocument = sinon.stub().callsArg(5)
@ProjectEntityUpdateHandler.addDoc = sinon.stub().yields(null, @doc, @folder_id)
@EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, @callback
it 'should send the document to the documentUpdaterHandler', (done)->
@DocumentUpdaterHandler.setDocument = sinon.stub().withArgs(@project_id, @doc_id, @user_id, @docLines, @source).callsArg(5)
@EditorController.setDoc @project_id, @doc_id, @user_id, @docLines, @source, (err)->
done()
it 'should add the doc using the project entity handler', ->
@ProjectEntityUpdateHandler.addDoc
.calledWith(@project_id, @folder_id, @docName, @docLines)
.should.equal true
it 'should send the new doc lines to the doucment updater', (done)->
@DocumentUpdaterHandler.setDocument = ->
mock = sinon.mock(@DocumentUpdaterHandler).expects("setDocument").withArgs(@project_id, @doc_id, @user_id, @docLines, @source).once().callsArg(5)
it 'should send the update out to the users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
.should.equal true
@EditorController.setDoc @project_id, @doc_id, @user_id, @docLines, @source, (err)=>
mock.verify()
done()
it 'calls the callback', ->
@callback.calledWith(null, @doc).should.equal true
it 'should flush the doc to mongo', (done)->
@EditorController.setDoc @project_id, @doc_id, @user_id, @docLines, @source, (err)=>
@DocumentUpdaterHandler.flushDocToMongo.calledWith(@project_id, @doc_id).should.equal true
done()
describe 'addDocWithoutLock', ->
describe 'addFile', ->
beforeEach ->
@ProjectEntityHandler.addDoc = ()->
@EditorRealTimeController.emitToRoom = sinon.stub()
@project_id = "12dsankj"
@folder_id = "213kjd"
@doc = {_id:"123ds"}
@folder_id = "123ksajdn"
@docName = "doc.tex"
@docLines = ["1234","dskl"]
@ProjectEntityUpdateHandler.addFile = sinon.stub().yields(null, @file, @folder_id)
@EditorController.addFile @project_id, @folder_id, @fileName, @fsPath, @source, @user_id, @callback
it 'should add the doc using the project entity handler', (done)->
mock = sinon.mock(@ProjectEntityHandler).expects("addDoc").withArgs(@project_id, @folder_id, @docName, @docLines).callsArg(5)
it 'should add the folder using the project entity handler', ->
@ProjectEntityUpdateHandler.addFile
.calledWith(@project_id, @folder_id, @fileName, @fsPath, @user_id)
.should.equal true
@EditorController.addDocWithoutLock @project_id, @folder_id, @docName, @docLines, @source, @user_id, ->
mock.verify()
done()
it 'should send the update of a new folder out to the users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source)
.should.equal true
it 'should send the update out to the users in the project', (done)->
@ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(5, null, @doc, @folder_id)
it 'calls the callback', ->
@callback.calledWith(null, @file).should.equal true
@EditorController.addDocWithoutLock @project_id, @folder_id, @docName, @docLines, @source, @user_id, =>
describe 'upsertDoc', ->
beforeEach ->
@ProjectEntityUpdateHandler.upsertDoc = sinon.stub().yields(null, @doc, false)
@EditorController.upsertDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, @callback
it 'upserts the doc using the project entity handler', ->
@ProjectEntityUpdateHandler.upsertDoc
.calledWith(@project_id, @folder_id, @docName, @docLines, @source)
.should.equal true
it 'returns the doc', ->
@callback.calledWith(null, @doc).should.equal true
describe 'doc does not exist', ->
beforeEach ->
@ProjectEntityUpdateHandler.upsertDoc = sinon.stub().yields(null, @doc, true)
@EditorController.upsertDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, @callback
it 'sends an update out to users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
.should.equal true
done()
it 'should return the doc to the callback', (done) ->
@ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(5, null, @doc, @folder_id)
@EditorController.addDocWithoutLock @project_id, @folder_id, @docName, @docLines, @source, @user_id, (error, doc) =>
doc.should.equal @doc
done()
describe "addDoc", ->
describe 'upsertFile', ->
beforeEach ->
@EditorController.addDocWithoutLock = sinon.stub().callsArgWith(6)
@ProjectEntityUpdateHandler.upsertFile = sinon.stub().yields(null, @file, false)
@EditorController.upsertFile @project_id, @folder_id, @fileName, @fsPath, @source, @user_id, @callback
it "should call addDocWithoutLock", (done)->
@EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, =>
@EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @docName, @docLines, @source, @user_id).should.equal true
done()
it 'upserts the file using the project entity handler', ->
@ProjectEntityUpdateHandler.upsertFile
.calledWith(@project_id, @folder_id, @fileName, @fsPath, @user_id)
.should.equal true
it "should take the lock", (done)->
@EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it 'returns the file', ->
@callback.calledWith(null, @file).should.equal true
it "should propogate up any errors", (done)->
@LockManager.runWithLock = sinon.stub().callsArgWith(2, "timed out")
@EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, (err) =>
expect(err).to.exist
err.should.equal "timed out"
done()
describe 'file does not exist', ->
beforeEach ->
@ProjectEntityUpdateHandler.upsertFile = sinon.stub().yields(null, @file, true)
@EditorController.upsertFile @project_id, @folder_id, @fileName, @fsPath, @source, @user_id, @callback
describe 'addFileWithoutLock:', ->
beforeEach ->
@ProjectEntityHandler.addFile = ->
@EditorRealTimeController.emitToRoom = sinon.stub()
@project_id = "12dsankj"
@folder_id = "213kjd"
@fileName = "file.png"
@folder_id = "123ksajdn"
@file = {_id:"dasdkjk"}
@stream = new ArrayBuffer()
it 'should add the folder using the project entity handler', (done)->
@ProjectEntityHandler.addFile = sinon.stub().callsArgWith(5)
@EditorController.addFileWithoutLock @project_id, @folder_id, @fileName, @stream, @source, @user_id, =>
@ProjectEntityHandler.addFile.calledWith(@project_id, @folder_id, @fileName, @stream, @user_id).should.equal true
done()
it 'should send the update of a new folder out to the users in the project', (done)->
@ProjectEntityHandler.addFile = sinon.stub().callsArgWith(5, null, @file, @folder_id)
@EditorController.addFileWithoutLock @project_id, @folder_id, @fileName, @stream, @source, @user_id, =>
it 'should send the update out to users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source)
.should.equal true
done()
it "should return the file in the callback", (done) ->
@ProjectEntityHandler.addFile = sinon.stub().callsArgWith(5, null, @file, @folder_id)
@EditorController.addFileWithoutLock @project_id, @folder_id, @fileName, @stream, @source, @user_id, (error, file) =>
file.should.equal @file
done()
describe "addFile", ->
describe "upsertDocWithPath", ->
beforeEach ->
@EditorController.addFileWithoutLock = sinon.stub().callsArgWith(6)
@docPath = '/folder/doc'
it "should call addFileWithoutLock", (done)->
@EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, @user_id, (error, file) =>
@EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @fileName, @stream, @source, @user_id).should.equal true
done()
@ProjectEntityUpdateHandler.upsertDocWithPath = sinon.stub().yields(null, @doc, false, [], @folder)
@EditorController.upsertDocWithPath @project_id, @docPath, @docLines, @source, @user_id, @callback
it "should take the lock", (done)->
@EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, @user_id, (error, file) =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it 'upserts the doc using the project entity handler', ->
@ProjectEntityUpdateHandler.upsertDocWithPath
.calledWith(@project_id, @docPath, @docLines, @source)
.should.equal true
it "should propogate up any errors", (done)->
@LockManager.runWithLock = sinon.stub().callsArgWith(2, "timed out")
@EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, @user_id, (error, file) =>
expect(error).to.exist
error.should.equal "timed out"
done()
describe 'doc does not exist', ->
beforeEach ->
@ProjectEntityUpdateHandler.upsertDocWithPath = sinon.stub().yields(null, @doc, true, [], @folder)
@EditorController.upsertDocWithPath @project_id, @docPath, @docLines, @source, @user_id, @callback
describe "replaceFileWithoutLock", ->
beforeEach ->
@project_id = "12dsankj"
@file_id = "file_id_here"
@fsPath = "/folder/file.png"
it 'should send the replace file message to the editor controller', (done)->
@ProjectEntityHandler.replaceFile = sinon.stub().callsArgWith(4)
@EditorController.replaceFileWithoutLock @project_id, @file_id, @fsPath, @source, @user_id, =>
@ProjectEntityHandler.replaceFile
.calledWith(@project_id, @file_id, @fsPath, @user_id)
.should.equal true
done()
describe 'addFolderWithoutLock :', ->
beforeEach ->
@ProjectEntityHandler.addFolder = ->
@EditorRealTimeController.emitToRoom = sinon.stub()
@project_id = "12dsankj"
@folder_id = "213kjd"
@folderName = "folder"
@folder = {_id:"123ds"}
it 'should add the folder using the project entity handler', (done)->
mock = sinon.mock(@ProjectEntityHandler).expects("addFolder").withArgs(@project_id, @folder_id, @folderName).callsArg(3)
@EditorController.addFolderWithoutLock @project_id, @folder_id, @folderName, @source, ->
mock.verify()
done()
it 'should notifyProjectUsersOfNewFolder', (done)->
@ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
mock = sinon.mock(@EditorController.p).expects('notifyProjectUsersOfNewFolder').withArgs(@project_id, @folder_id, @folder).callsArg(3)
@EditorController.addFolderWithoutLock @project_id, @folder_id, @folderName, @source, ->
mock.verify()
done()
it 'notifyProjectUsersOfNewFolder should send update out to all users', (done)->
@EditorController.p.notifyProjectUsersOfNewFolder @project_id, @folder_id, @folder, =>
it 'should send the update for the doc out to users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFolder", @folder_id, @folder)
.calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
.should.equal true
done()
it 'should return the folder in the callback', (done) ->
@ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
@EditorController.addFolderWithoutLock @project_id, @folder_id, @folderName, @source, (error, folder) =>
folder.should.equal @folder
done()
describe 'folders required for doc do not exist', ->
beforeEach ->
folders = [
@folderA = { _id: 2, parentFolder_id: 1}
@folderB = { _id: 3, parentFolder_id: 2}
]
@ProjectEntityUpdateHandler.upsertDocWithPath = sinon.stub().yields(null, @doc, true, folders, @folderB)
@EditorController.upsertDocWithPath @project_id, @docPath, @docLines, @source, @user_id, @callback
describe "addFolder", ->
it 'should send the update for each folder to users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFolder", @folderA.parentFolder_id, @folderA)
.should.equal true
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFolder", @folderB.parentFolder_id, @folderB)
.should.equal true
describe "upsertFileWithPath", ->
beforeEach ->
@EditorController.addFolderWithoutLock = sinon.stub().callsArgWith(4)
@filePath = '/folder/file'
it "should call addFolderWithoutLock", (done)->
@EditorController.addFolder @project_id, @folder_id, @folderName, @source, (error, file) =>
@EditorController.addFolderWithoutLock.calledWith(@project_id, @folder_id, @folderName, @source).should.equal true
done()
@ProjectEntityUpdateHandler.upsertFileWithPath = sinon.stub().yields(null, @file, false, [], @folder)
@EditorController.upsertFileWithPath @project_id, @filePath, @fsPath, @source, @user_id, @callback
it "should take the lock", (done)->
@EditorController.addFolder @project_id, @folder_id, @folderName, @source, (error, file) =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it 'upserts the file using the project entity handler', ->
@ProjectEntityUpdateHandler.upsertFileWithPath
.calledWith(@project_id, @filePath, @fsPath)
.should.equal true
it "should propogate up any errors", (done)->
@LockManager.runWithLock = sinon.stub().callsArgWith(2, "timed out")
@EditorController.addFolder @project_id, @folder_id, @folderName, @source, (err, file) =>
expect(err).to.exist
err.should.equal "timed out"
done()
describe 'file does not exist', ->
beforeEach ->
@ProjectEntityUpdateHandler.upsertFileWithPath = sinon.stub().yields(null, @file, true, [], @folder)
@EditorController.upsertFileWithPath @project_id, @filePath, @fsPath, @source, @user_id, @callback
it 'should send the update for the file out to users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source)
.should.equal true
describe 'mkdirpWithoutLock :', ->
describe 'folders required for file do not exist', ->
beforeEach ->
folders = [
@folderA = { _id: 2, parentFolder_id: 1}
@folderB = { _id: 3, parentFolder_id: 2}
]
@ProjectEntityUpdateHandler.upsertFileWithPath = sinon.stub().yields(null, @file, true, folders, @folderB)
@EditorController.upsertFileWithPath @project_id, @filePath, @fsPath, @source, @user_id, @callback
it 'should make the dirs and notifyProjectUsersOfNewFolder', (done)->
path = "folder1/folder2"
@folder1 = {_id:"folder_1_id_here"}
@folder2 = {_id:"folder_2_id_here", parentFolder_id:@folder1._id}
@folder3 = {_id:"folder_3_id_here", parentFolder_id:@folder2._id}
it 'should send the update for each folder to users in the project', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFolder", @folderA.parentFolder_id, @folderA)
.should.equal true
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFolder", @folderB.parentFolder_id, @folderB)
.should.equal true
@ProjectEntityHandler.mkdirp = sinon.stub().withArgs(@project_id, path).callsArgWith(2, null, [@folder1, @folder2, @folder3], @folder3)
describe 'addFolder', ->
beforeEach ->
@EditorController._notifyProjectUsersOfNewFolder = sinon.stub().yields()
@ProjectEntityUpdateHandler.addFolder = sinon.stub().yields(null, @folder, @folder_id)
@EditorController.addFolder @project_id, @folder_id, @folderName, @source, @callback
@EditorController.p.notifyProjectUsersOfNewFolder = sinon.stub().callsArg(3)
it 'should add the folder using the project entity handler', ->
@ProjectEntityUpdateHandler.addFolder
.calledWith(@project_id, @folder_id, @folderName)
.should.equal true
@EditorController.mkdirpWithoutLock @project_id, path, (err, newFolders, lastFolder)=>
@EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder1._id, @folder2).should.equal true
@EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder2._id, @folder3).should.equal true
newFolders.should.deep.equal [@folder1, @folder2, @folder3]
lastFolder.should.equal @folder3
done()
it 'should notifyProjectUsersOfNewFolder', ->
@EditorController._notifyProjectUsersOfNewFolder
.calledWith(@project_id, @folder_id, @folder)
it 'should return the folder in the callback', ->
@callback.calledWith(null, @folder).should.equal true
describe "mkdirp", ->
describe 'mkdirp', ->
beforeEach ->
@path = "folder1/folder2"
@EditorController.mkdirpWithoutLock = sinon.stub().callsArgWith(2)
@folders = [
@folderA = { _id: 2, parentFolder_id: 1}
@folderB = { _id: 3, parentFolder_id: 2}
]
@EditorController._notifyProjectUsersOfNewFolders = sinon.stub().yields()
@ProjectEntityUpdateHandler.mkdirp = sinon.stub().yields(null, @folders, @folder)
@EditorController.mkdirp @project_id, @path, @callback
it "should call mkdirpWithoutLock", (done)->
@EditorController.mkdirp @project_id, @path, (error, file) =>
@EditorController.mkdirpWithoutLock.calledWith(@project_id, @path).should.equal true
done()
it 'should create the folder using the project entity handler', ->
@ProjectEntityUpdateHandler.mkdirp
.calledWith(@project_id, @path)
.should.equal true
it "should take the lock", (done)->
@EditorController.mkdirp @project_id, @path, (error, file) =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it 'should notifyProjectUsersOfNewFolder', ->
@EditorController._notifyProjectUsersOfNewFolders
.calledWith(@project_id, @folders)
it "should propogate up any errors", (done)->
@LockManager.runWithLock = sinon.stub().callsArgWith(2, "timed out")
@EditorController.mkdirp @project_id, @path, (err, file) =>
expect(err).to.exist
err.should.equal "timed out"
done()
it 'should return the folder in the callback', ->
@callback.calledWith(null, @folders, @folder).should.equal true
describe "deleteEntity", ->
describe 'deleteEntity', ->
beforeEach ->
@EditorController.deleteEntityWithoutLock = sinon.stub().callsArgWith(5)
it "should call deleteEntityWithoutLock", (done)->
@EditorController.deleteEntity @project_id, @entity_id, @type, @source, @user_id, =>
@EditorController.deleteEntityWithoutLock
.calledWith(@project_id, @entity_id, @type, @source, @user_id)
.should.equal true
done()
it "should take the lock", (done)->
@EditorController.deleteEntity @project_id, @entity_id, @type, @source, @user_id, =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it "should propogate up any errors", (done)->
@LockManager.runWithLock = sinon.stub().callsArgWith(2, "timed out")
@EditorController.deleteEntity @project_id, @entity_id, @type, @source, @user_id, (error) =>
expect(error).to.exist
error.should.equal "timed out"
done()
describe 'deleteEntityWithoutLock', ->
beforeEach (done) ->
@entity_id = "entity_id_here"
@type = "doc"
@EditorRealTimeController.emitToRoom = sinon.stub()
@ProjectEntityHandler.deleteEntity = sinon.stub().callsArg(4)
@EditorController.deleteEntityWithoutLock @project_id, @entity_id, @type, @source, @user_id, done
@ProjectEntityUpdateHandler.deleteEntity = sinon.stub().yields()
@EditorController.deleteEntity @project_id, @entity_id, @type, @source, @user_id, @callback
it 'should delete the folder using the project entity handler', ->
@ProjectEntityHandler.deleteEntity
@ProjectEntityUpdateHandler.deleteEntity
.calledWith(@project_id, @entity_id, @type, @user_id)
.should.equal.true
@@ -385,9 +263,25 @@ describe "EditorController", ->
.calledWith(@project_id, "removeEntity", @entity_id, @source)
.should.equal true
describe "deleteEntityWithPath", ->
beforeEach () ->
@entity_id = "entity_id_here"
@ProjectEntityUpdateHandler.deleteEntityWithPath = sinon.stub().yields(null, @entity_id)
@path = "folder1/folder2"
@EditorController.deleteEntityWithPath @project_id, @path, @source, @user_id, @callback
it 'should delete the folder using the project entity handler', ->
@ProjectEntityUpdateHandler.deleteEntityWithPath
.calledWith(@project_id, @path, @user_id)
.should.equal.true
it 'notify users an entity has been deleted', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "removeEntity", @entity_id, @source)
.should.equal true
describe "notifyUsersProjectHasBeenDeletedOrRenamed", ->
it 'should emmit a message to all users in a project', (done)->
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.notifyUsersProjectHasBeenDeletedOrRenamed @project_id, (err)=>
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "projectRenamedOrDeletedByExternalSource")
@@ -397,24 +291,15 @@ describe "EditorController", ->
describe "updateProjectDescription", ->
beforeEach ->
@description = "new description"
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.updateProjectDescription @project_id, @description, @callback
it "should send the new description to the project details handler", ->
@ProjectDetailsHandler.setProjectDescription.calledWith(@project_id, @description).should.equal true
it "should send the new description to the project details handler", (done)->
@ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
@EditorController.updateProjectDescription @project_id, @description, =>
@ProjectDetailsHandler.setProjectDescription.calledWith(@project_id, @description).should.equal true
done()
it "should notify the other clients about the updated description", (done)->
@ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
@EditorController.updateProjectDescription @project_id, @description, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "projectDescriptionUpdated", @description).should.equal true
done()
it "should notify the other clients about the updated description", ->
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "projectDescriptionUpdated", @description).should.equal true
describe "deleteProject", ->
beforeEach ->
@err = "errro"
@ProjectDeleter.deleteProject = sinon.stub().callsArgWith(1, @err)
@@ -425,27 +310,20 @@ describe "EditorController", ->
@ProjectDeleter.deleteProject.calledWith(@project_id).should.equal true
done()
describe "renameEntity", ->
beforeEach (done) ->
@entity_id = "entity_id_here"
@entityType = "doc"
@newName = "bobsfile.tex"
@ProjectEntityHandler.renameEntity = sinon.stub().callsArg(5)
@EditorRealTimeController.emitToRoom = sinon.stub()
@ProjectEntityUpdateHandler.renameEntity = sinon.stub().yields()
@EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, @user_id, done
it "should call the project handler", ->
@ProjectEntityHandler.renameEntity
@ProjectEntityUpdateHandler.renameEntity
.calledWith(@project_id, @entity_id, @entityType, @newName, @user_id)
.should.equal true
it "should take the lock", ->
@LockManager.runWithLock
.calledWith(@project_id)
.should.equal true
it "should emit the update to the room", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'reciveEntityRename', @entity_id, @newName)
@@ -455,162 +333,126 @@ describe "EditorController", ->
beforeEach ->
@entity_id = "entity_id_here"
@entityType = "doc"
@folder_id = "313dasd21dasdsa"
@ProjectEntityHandler.moveEntity = sinon.stub().callsArg(5)
@EditorRealTimeController.emitToRoom = sinon.stub()
@ProjectEntityUpdateHandler.moveEntity = sinon.stub().yields()
@EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, @user_id, @callback
it "should call the ProjectEntityHandler", (done)->
@EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, @user_id, =>
@ProjectEntityHandler.moveEntity.calledWith(@project_id, @entity_id, @folder_id, @entityType, @user_id).should.equal true
done()
it "should call the ProjectEntityUpdateHandler", ->
@ProjectEntityUpdateHandler.moveEntity
.calledWith(@project_id, @entity_id, @folder_id, @entityType, @user_id)
.should.equal true
it "should take the lock", (done)->
@EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, @user_id, =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it "should emit the update to the room", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'reciveEntityMove', @entity_id, @folder_id)
.should.equal true
it "should propogate up any errors", (done)->
@LockManager.runWithLock = sinon.stub().callsArgWith(2, "timed out")
@EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, @user_id, (error) =>
expect(error).to.exist
error.should.equal "timed out"
done()
it "should emit the update to the room", (done)->
@EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, @user_id, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityMove', @entity_id, @folder_id).should.equal true
done()
it "calls the callback", ->
@callback.called.should.equal true
describe "renameProject", ->
beforeEach ->
@err = "errro"
@window_id = "kdsjklj290jlk"
@newName = "new name here"
@ProjectDetailsHandler.renameProject = sinon.stub().callsArg(2)
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.renameProject @project_id, @newName, @callback
it "should call the EditorController", (done)->
@EditorController.renameProject @project_id, @newName, =>
@ProjectDetailsHandler.renameProject.calledWith(@project_id, @newName).should.equal true
done()
it "should call the EditorController", ->
@ProjectDetailsHandler.renameProject.calledWith(@project_id, @newName).should.equal true
it "should emit the update to the room", ->
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'projectNameUpdated', @newName).should.equal true
it "should emit the update to the room", (done)->
@EditorController.renameProject @project_id, @newName, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'projectNameUpdated', @newName).should.equal true
done()
describe "setCompiler", ->
beforeEach ->
@compiler = "latex"
@EditorController.setCompiler @project_id, @compiler, @callback
it "should send the new compiler and project id to the project options handler", ->
@ProjectOptionsHandler.setCompiler
.calledWith(@project_id, @compiler)
.should.equal true
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "compilerUpdated", @compiler)
.should.equal true
describe "setSpellCheckLanguage", ->
beforeEach ->
@languageCode = "fr"
@EditorController.setSpellCheckLanguage @project_id, @languageCode, @callback
it "should send the new languageCode and project id to the project options handler", ->
@ProjectOptionsHandler.setSpellCheckLanguage
.calledWith(@project_id, @languageCode)
.should.equal true
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "spellCheckLanguageUpdated", @languageCode)
.should.equal true
describe "setPublicAccessLevel", ->
describe 'when setting to private', ->
beforeEach ->
@newAccessLevel = 'private'
@ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, null)
@ProjectDetailsHandler.ensureTokensArePresent = sinon.stub()
.callsArgWith(1, null, @tokens)
@EditorRealTimeController.emitToRoom = sinon.stub()
@ProjectDetailsHandler.ensureTokensArePresent = sinon.stub().yields(null, @tokens)
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, @callback
it 'should set the access level', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
it 'should set the access level', ->
@ProjectDetailsHandler.setPublicAccessLevel
.calledWith(@project_id, @newAccessLevel).should.equal true
done()
.calledWith(@project_id, @newAccessLevel)
.should.equal true
it 'should broadcast the access level change', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:publicAccessLevel:changed').should.equal true
done()
it 'should broadcast the access level change', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:publicAccessLevel:changed')
.should.equal true
it 'should not ensure tokens are present for project', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
@ProjectDetailsHandler.ensureTokensArePresent
.calledWith(@project_id).should.equal false
done()
it 'should not ensure tokens are present for project', ->
@ProjectDetailsHandler.ensureTokensArePresent
.calledWith(@project_id)
.should.equal false
it 'should not broadcast a token change', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
.should.equal false
done()
it 'should not produce an error', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, (err) =>
expect(err).to.not.exist
done()
it 'should not broadcast a token change', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
.should.equal false
describe 'when setting to tokenBased', ->
beforeEach ->
@newAccessLevel = 'tokenBased'
@tokens = {readOnly: 'aaa', readAndWrite: '42bbb'}
@ProjectDetailsHandler.setPublicAccessLevel = sinon.stub()
.callsArgWith(2, null)
@ProjectDetailsHandler.ensureTokensArePresent = sinon.stub()
.callsArgWith(1, null, @tokens)
@EditorRealTimeController.emitToRoom = sinon.stub()
@ProjectDetailsHandler.ensureTokensArePresent = sinon.stub().yields(null, @tokens)
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, @callback
it 'should set the access level', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
@ProjectDetailsHandler.setPublicAccessLevel
.calledWith(@project_id, @newAccessLevel).should.equal true
done()
it 'should set the access level', ->
@ProjectDetailsHandler.setPublicAccessLevel
.calledWith(@project_id, @newAccessLevel)
.should.equal true
it 'should broadcast the access level change', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:publicAccessLevel:changed')
.should.equal true
done()
it 'should broadcast the access level change', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:publicAccessLevel:changed')
.should.equal true
it 'should ensure tokens are present for project', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
@ProjectDetailsHandler.ensureTokensArePresent
.calledWith(@project_id).should.equal true
done()
it 'should ensure tokens are present for project', ->
@ProjectDetailsHandler.ensureTokensArePresent
.calledWith(@project_id)
.should.equal true
it 'should broadcast the token change too', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
.should.equal true
done()
it 'should not produce an error', (done) ->
@EditorController.setPublicAccessLevel @project_id, @newAccessLevel, (err) =>
expect(err).to.not.exist
done()
# beforeEach ->
# @newAccessLevel = "public"
# @ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, null)
# @EditorRealTimeController.emitToRoom = sinon.stub()
# it "should call the EditorController", (done)->
# @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
# @ProjectDetailsHandler.setPublicAccessLevel.calledWith(@project_id, @newAccessLevel).should.equal true
# done()
# it "should emit the update to the room", (done)->
# @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
# @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'publicAccessLevelUpdated', @newAccessLevel).should.equal true
# done()
it 'should broadcast the token change too', ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
.should.equal true
describe "setRootDoc", ->
beforeEach ->
@newRootDocID = "21312321321"
@ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2, null)
@EditorRealTimeController.emitToRoom = sinon.stub()
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().yields()
@EditorController.setRootDoc @project_id, @newRootDocID, @callback
it "should call the ProjectEntityHandler", (done)->
@EditorController.setRootDoc @project_id, @newRootDocID, =>
@ProjectEntityHandler.setRootDoc.calledWith(@project_id, @newRootDocID).should.equal true
done()
it "should call the ProjectEntityUpdateHandler", ->
@ProjectEntityUpdateHandler.setRootDoc
.calledWith(@project_id, @newRootDocID)
.should.equal true
it "should emit the update to the room", (done)->
@EditorController.setRootDoc @project_id, @newRootDocID, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'rootDocUpdated', @newRootDocID).should.equal true
done()
it "should emit the update to the room", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, 'rootDocUpdated', @newRootDocID)
.should.equal true
@@ -6,7 +6,7 @@ modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor
describe "EditorHttpController", ->
beforeEach ->
@EditorHttpController = SandboxedModule.require modulePath, requires:
'../Project/ProjectEntityHandler' : @ProjectEntityHandler = {}
'../Project/ProjectEntityUpdateHandler' : @ProjectEntityUpdateHandler = {}
'../Project/ProjectDeleter' : @ProjectDeleter = {}
'../Project/ProjectGetter' : @ProjectGetter = {}
'../User/UserGetter' : @UserGetter = {}
@@ -171,7 +171,7 @@ describe "EditorHttpController", ->
doc_id: @doc_id
@req.body =
name: @name = "doc-name"
@ProjectEntityHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
@ProjectEntityUpdateHandler.restoreDoc = sinon.stub().callsArgWith(3, null,
@doc = { "mock": "doc", _id: @new_doc_id = "new-doc-id" }
@folder_id = "mock-folder-id"
)
@@ -179,7 +179,7 @@ describe "EditorHttpController", ->
@EditorHttpController.restoreDoc @req, @res
it "should restore the doc", ->
@ProjectEntityHandler.restoreDoc
@ProjectEntityUpdateHandler.restoreDoc
.calledWith(@project_id, @doc_id, @name)
.should.equal true
@@ -32,7 +32,7 @@ describe 'ProjectCreationHandler', ->
@FolderModel = class Folder
constructor:(options)->
{@name} = options
@ProjectEntityHandler =
@ProjectEntityUpdateHandler =
addDoc: sinon.stub().callsArgWith(5, null, {_id: docId})
addFile: sinon.stub().callsArg(5)
setRootDoc: sinon.stub().callsArg(2)
@@ -57,7 +57,7 @@ describe 'ProjectCreationHandler', ->
'../../models/Project':{Project:@ProjectModel}
'../../models/Folder':{Folder:@FolderModel}
'../History/HistoryManager': @HistoryManager
'./ProjectEntityHandler':@ProjectEntityHandler
'./ProjectEntityUpdateHandler':@ProjectEntityUpdateHandler
"./ProjectDetailsHandler":@ProjectDetailsHandler
"settings-sharelatex": @Settings
'logger-sharelatex': {log:->}
@@ -164,11 +164,11 @@ describe 'ProjectCreationHandler', ->
.should.equal true
it 'should insert main.tex', ->
@ProjectEntityHandler.addDoc.calledWith(project_id, rootFolderId, "main.tex", ["mainbasic.tex", "lines"], ownerId)
@ProjectEntityUpdateHandler.addDoc.calledWith(project_id, rootFolderId, "main.tex", ["mainbasic.tex", "lines"], ownerId)
.should.equal true
it 'should set the main doc id', ->
@ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
@ProjectEntityUpdateHandler.setRootDoc.calledWith(project_id, docId).should.equal true
it 'should build the mainbasic.tex template', ->
@handler._buildTemplate
@@ -194,17 +194,17 @@ describe 'ProjectCreationHandler', ->
.should.equal true
it 'should insert main.tex', ->
@ProjectEntityHandler.addDoc
@ProjectEntityUpdateHandler.addDoc
.calledWith(project_id, rootFolderId, "main.tex", ["main.tex", "lines"], ownerId)
.should.equal true
it 'should insert references.bib', ->
@ProjectEntityHandler.addDoc
@ProjectEntityUpdateHandler.addDoc
.calledWith(project_id, rootFolderId, "references.bib", ["references.bib", "lines"], ownerId)
.should.equal true
it 'should insert universe.jpg', ->
@ProjectEntityHandler.addFile
@ProjectEntityUpdateHandler.addFile
.calledWith(
project_id, rootFolderId, "universe.jpg",
Path.resolve(__dirname + "/../../../../app/templates/project_files/universe.jpg"),
@@ -213,7 +213,7 @@ describe 'ProjectCreationHandler', ->
.should.equal true
it 'should set the main doc id', ->
@ProjectEntityHandler.setRootDoc.calledWith(project_id, docId).should.equal true
@ProjectEntityUpdateHandler.setRootDoc.calledWith(project_id, docId).should.equal true
it 'should build the main.tex template', ->
@handler._buildTemplate
@@ -63,11 +63,11 @@ describe 'ProjectDuplicator', ->
@projectOptionsHandler =
setCompiler : sinon.stub()
@entityHandler =
@ProjectEntityUpdateHandler =
addDoc: sinon.stub().callsArgWith(5, null, {name:"somDoc"})
copyFileFromExistingProjectWithProject: sinon.stub().callsArgWith(5)
setRootDoc: sinon.stub()
addFolderWithProject: sinon.stub().callsArgWith(3, null, @newFolder)
addFolder: sinon.stub().callsArgWith(3, null, @newFolder)
@DocumentUpdaterHandler =
flushProjectToMongo: sinon.stub().callsArg(1)
@@ -85,7 +85,7 @@ describe 'ProjectDuplicator', ->
'../../models/Project':{Project:@Project}
"../DocumentUpdater/DocumentUpdaterHandler": @DocumentUpdaterHandler
'./ProjectCreationHandler': @creationHandler
'./ProjectEntityHandler': @entityHandler
'./ProjectEntityUpdateHandler': @ProjectEntityUpdateHandler
'./ProjectLocator': @locator
'./ProjectOptionsHandler': @projectOptionsHandler
"../Docstore/DocstoreManager": @DocstoreManager
@@ -112,15 +112,15 @@ describe 'ProjectDuplicator', ->
done()
it 'should use the same compiler', (done)->
@entityHandler.addDoc.callsArgWith(5, null, @rootFolder.docs[0], @owner._id)
@ProjectEntityUpdateHandler.addDoc.callsArgWith(5, null, @rootFolder.docs[0], @owner._id)
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
@projectOptionsHandler.setCompiler.calledWith(@stubbedNewProject._id, @project.compiler).should.equal true
done()
it 'should use the same root doc', (done)->
@entityHandler.addDoc.callsArgWith(5, null, @rootFolder.docs[0], @owner._id)
@ProjectEntityUpdateHandler.addDoc.callsArgWith(5, null, @rootFolder.docs[0], @owner._id)
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
@entityHandler.setRootDoc.calledWith(@stubbedNewProject._id, @rootFolder.docs[0]._id).should.equal true
@ProjectEntityUpdateHandler.setRootDoc.calledWith(@stubbedNewProject._id, @rootFolder.docs[0]._id).should.equal true
done()
it 'should not copy the collaberators or read only refs', (done)->
@@ -131,34 +131,34 @@ describe 'ProjectDuplicator', ->
it 'should copy all the folders', (done)->
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
@entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @level1folder.name).should.equal true
@entityHandler.addFolderWithProject.calledWith(@stubbedNewProject, @newFolder._id, @level2folder.name).should.equal true
@entityHandler.addFolderWithProject.callCount.should.equal 2
@ProjectEntityUpdateHandler.addFolder.calledWith(@new_project_id, @stubbedNewProject.rootFolder[0]._id, @level1folder.name).should.equal true
@ProjectEntityUpdateHandler.addFolder.calledWith(@new_project_id, @newFolder._id, @level2folder.name).should.equal true
@ProjectEntityUpdateHandler.addFolder.callCount.should.equal 2
done()
it 'should copy all the docs', (done)->
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
@DocstoreManager.getAllDocs.calledWith(@old_project_id).should.equal true
@entityHandler.addDoc
.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @doc0.name, @doc0_lines, @owner._id)
@ProjectEntityUpdateHandler.addDoc
.calledWith(@new_project_id, @stubbedNewProject.rootFolder[0]._id, @doc0.name, @doc0_lines, @owner._id)
.should.equal true
@entityHandler.addDoc
.calledWith(@stubbedNewProject, @newFolder._id, @doc1.name, @doc1_lines, @owner._id)
@ProjectEntityUpdateHandler.addDoc
.calledWith(@new_project_id, @newFolder._id, @doc1.name, @doc1_lines, @owner._id)
.should.equal true
@entityHandler.addDoc
.calledWith(@stubbedNewProject, @newFolder._id, @doc2.name, @doc2_lines, @owner._id)
@ProjectEntityUpdateHandler.addDoc
.calledWith(@new_project_id, @newFolder._id, @doc2.name, @doc2_lines, @owner._id)
.should.equal true
done()
it 'should copy all the files', (done)->
@duplicator.duplicate @owner, @old_project_id, "", (err, newProject)=>
@entityHandler.copyFileFromExistingProjectWithProject
@ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject
.calledWith(@stubbedNewProject, @stubbedNewProject.rootFolder[0]._id, @project._id, @rootFolder.fileRefs[0], @owner._id)
.should.equal true
@entityHandler.copyFileFromExistingProjectWithProject
@ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject
.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level1folder.fileRefs[0], @owner._id)
.should.equal true
@entityHandler.copyFileFromExistingProjectWithProject
@ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject
.calledWith(@stubbedNewProject, @newFolder._id, @project._id, @level2folder.fileRefs[0], @owner._id)
.should.equal true
done()
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,592 @@
chai = require('chai')
expect = chai.expect
assert = require('chai').assert
should = chai.should()
sinon = require 'sinon'
tk = require("timekeeper")
modulePath = "../../../../app/js/Features/Project/ProjectEntityMongoUpdateHandler"
Errors = require "../../../../app/js/Features/Errors/Errors"
ObjectId = require("mongoose").Types.ObjectId
SandboxedModule = require('sandboxed-module')
describe 'ProjectEntityMongoUpdateHandler', ->
project_id = '4eecb1c1bffa66588e0000a1'
doc_id = '4eecb1c1bffa66588e0000a2'
file_id = '4eecb1c1bffa66588e0000a3'
folder_id = "4eecaffcbffa66588e000008"
beforeEach ->
@FolderModel = class Folder
constructor:(options)->
{@name} = options
@._id = "folder_id"
@docName = "doc-name"
@fileName = "something.jpg"
@project = _id: project_id, name: 'project name'
@callback = sinon.stub()
tk.freeze(Date.now())
@subject = SandboxedModule.require modulePath, requires:
'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
"settings-sharelatex":@settings = {
maxEntitiesPerProject: 100
}
"../Cooldown/CooldownManager": @CooldownManager = {}
'../../models/Folder': Folder:@FolderModel
'../../models/Project': Project:@ProjectModel = {}
'./ProjectEntityHandler': @ProjectEntityHandler = {}
'./ProjectLocator': @ProjectLocator = {}
"./ProjectGetter": @ProjectGetter =
getProject: sinon.stub().yields(null, @project)
afterEach ->
tk.reset()
describe 'addDoc', ->
beforeEach ->
@subject._confirmFolder = sinon.stub().yields(folder_id)
@subject._putElement = sinon.stub()
@doc = _id: doc_id
@subject.addDoc project_id, folder_id, @doc, @callback
it 'gets the project', ->
@ProjectGetter.getProject
.calledWith(project_id, {rootFolder:true, name: true})
.should.equal true
it 'checks the folder exists', ->
@subject._confirmFolder
.calledWith(@project, folder_id)
.should.equal true
it 'puts the element in mongo', ->
@subject._putElement
.calledWith(@project, folder_id, @doc, 'doc', @callback)
.should.equal true
describe 'addFile', ->
beforeEach ->
@subject._confirmFolder = sinon.stub().yields(folder_id)
@subject._putElement = sinon.stub()
@file = _id: file_id
@subject.addFile project_id, folder_id, @file, @callback
it 'gets the project', ->
@ProjectGetter.getProject
.calledWith(project_id, {rootFolder:true, name: true})
.should.equal true
it 'checks the folder exists', ->
@subject._confirmFolder
.calledWith(@project, folder_id)
.should.equal true
it 'puts the element in mongo', ->
@subject._putElement
.calledWith(@project, folder_id, @file, 'file', @callback)
.should.equal true
describe 'replaceFile', ->
beforeEach ->
@file = _id: file_id
@path = mongo: 'file.png'
@ProjectLocator.findElement = sinon.stub().yields(null, @file, @path)
@ProjectModel.update = sinon.stub().yields()
@subject.replaceFile project_id, file_id, @callback
it 'gets the project', ->
@ProjectGetter.getProject
.calledWith(project_id, {rootFolder:true, name: true})
.should.equal true
it 'finds the element', ->
@ProjectLocator.findElement
.calledWith({ @project, element_id: file_id, type: 'file' })
.should.equal true
it 'increments the rev and sets the created_at', ->
@ProjectModel.update
.calledWith(
{ _id: project_id },
{
'$inc': { 'file.png.rev': 1 }
'$set': { 'file.png.created': new Date() }
}
{}
)
.should.equal true
it 'calls the callback', ->
@callback.calledWith(null, @file, @project, @path).should.equal true
describe 'mkdirp', ->
beforeEach ->
@parentFolder_id = "1jnjknjk"
@newFolder = {_id:"newFolder_id_here"}
@lastFolder = {_id:"123das", folders:[]}
@rootFolder = {_id: "rootFolderId" }
@project = _id: project_id, rootFolder: [@rootFolder]
@ProjectGetter.getProjectWithOnlyFolders = sinon.stub().yields(null, @project)
@ProjectLocator.findElementByPath = (project_id, path, cb) =>
@parentFolder = {_id:"parentFolder_id_here"}
lastFolder = path.substring(path.lastIndexOf("/"))
if lastFolder.indexOf("level1") == -1
cb "level1 is not the last foler "
else
cb null, @parentFolder
@subject.addFolder = (project_id, parentFolder_id, folderName, callback) =>
callback null, {name:folderName}, @parentFolder_id
it 'should return the root folder if the path is just a slash', (done)->
path = "/"
@subject.mkdirp project_id, path, (err, folders, lastFolder)=>
lastFolder.should.deep.equal @rootFolder
assert.equal lastFolder.parentFolder_id, undefined
done()
it 'should make just one folder', (done)->
path = "/differentFolder/"
@subject.mkdirp project_id, path, (err, folders, lastFolder)=>
folders.length.should.equal 1
lastFolder.name.should.equal "differentFolder"
lastFolder.parentFolder_id.should.equal @parentFolder_id
done()
it 'should make the final folder in path if it doesnt exist with one level', (done)->
path = "level1/level2"
@subject.mkdirp project_id, path, (err, folders, lastFolder)=>
folders.length.should.equal 1
lastFolder.name.should.equal "level2"
lastFolder.parentFolder_id.should.equal @parentFolder_id
done()
it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
path = "level1/level2/level3"
@subject.mkdirp project_id, path,(err, folders, lastFolder) =>
folders.length.should.equal 2
folders[0].name.should.equal "level2"
folders[0].parentFolder_id.should.equal @parentFolder_id
lastFolder.name.should.equal "level3"
lastFolder.parentFolder_id.should.equal @parentFolder_id
done()
it 'should work with slashes either side', (done)->
path = "/level1/level2/level3/"
@subject.mkdirp project_id, path, (err, folders, lastFolder)=>
folders.length.should.equal 2
folders[0].name.should.equal "level2"
folders[0].parentFolder_id.should.equal @parentFolder_id
lastFolder.name.should.equal "level3"
lastFolder.parentFolder_id.should.equal @parentFolder_id
done()
describe 'moveEntity', ->
beforeEach ->
@pathAfterMove = {
fileSystem: "/somewhere/else.txt"
}
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
@ProjectEntityHandler.getAllEntitiesFromProject
.onFirstCall()
.yields(null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
@ProjectEntityHandler.getAllEntitiesFromProject
.onSecondCall()
.yields(null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
@doc = {lines:["1234","312343d"], rev: "1234"}
@path = { mongo:"folders[0]", fileSystem:"/old_folder/somewhere.txt" }
@ProjectLocator.findElement = sinon.stub()
.withArgs({@project, element_id: @docId, type: 'docs'})
.yields(null, @doc, @path)
@subject._checkValidMove = sinon.stub().yields()
@subject._removeElementFromMongoArray = sinon.stub().yields(null, @project)
@subject._putElement = sinon.stub().yields(null, path: @pathAfterMove)
@subject.moveEntity project_id, doc_id, folder_id, "docs", @callback
it 'should get the project', ->
@ProjectGetter.getProject
.calledWith(project_id, {rootFolder:true, name:true})
.should.equal true
it 'should find the doc to move', ->
@ProjectLocator.findElement
.calledWith({element_id: doc_id, type: "docs", project: @project })
.should.equal true
it 'should check this is a valid move', ->
@subject._checkValidMove
.calledWith(@project, 'docs', @doc, @path, folder_id)
.should.equal true
it 'should remove the element from its current position', ->
@subject._removeElementFromMongoArray
.calledWith(@ProjectModel, project_id, @path.mongo)
.should.equal true
it "should put the element back in the new folder", ->
@subject._putElement
.calledWith(@project, folder_id, @doc, "docs")
.should.equal true
it "calls the callback", ->
changes = { @oldDocs, @newDocs, @oldFiles, @newFiles }
@callback.calledWith(
null, @project.name, @path.fileSystem, @pathAfterMove.fileSystem, @doc.rev, changes
).should.equal true
describe 'deleteEntity', ->
beforeEach ->
@path = mongo: "mongo.path", fileSystem: "/file/system/path"
@doc = _id: doc_id
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, @path)
@subject._removeElementFromMongoArray = sinon.stub().yields()
@subject.deleteEntity project_id, doc_id, 'doc', @callback
it "should get the project", ->
@ProjectGetter.getProject
.calledWith(project_id, {name:true, rootFolder:true})
.should.equal true
it "should find the element", ->
@ProjectLocator.findElement
.calledWith({@project, element_id: @doc._id, type: 'doc'})
.should.equal true
it "should remove the element from the database", ->
@subject._removeElementFromMongoArray
.calledWith(@ProjectModel, project_id, @path.mongo)
.should.equal true
it "calls the callbck", ->
@callback.calledWith(null, @doc, @path, @project).should.equal true
describe "renameEntity", ->
beforeEach ->
@newName = "new.tex"
@path = mongo: "mongo.path", fileSystem: "/old.tex"
@project =
_id: ObjectId(project_id)
rootFolder: [_id:ObjectId()]
@doc = _id: doc_id, name: "old.tex", rev: 1
@folder = _id: folder_id
@ProjectGetter.getProject = sinon.stub().yields(null, @project)
@ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
@ProjectEntityHandler.getAllEntitiesFromProject
.onFirstCall()
.yields( null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
@ProjectEntityHandler.getAllEntitiesFromProject
.onSecondCall()
.yields( null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
@ProjectLocator.findElement = sinon.stub().yields(null, @doc, @path, @folder)
@subject._checkValidElementName = sinon.stub().yields()
@ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
@subject.renameEntity project_id, doc_id, 'doc', @newName, @callback
it 'should get the project', ->
@ProjectGetter.getProject
.calledWith(project_id, {rootFolder:true, name:true})
.should.equal true
it 'should find the doc', ->
@ProjectLocator.findElement
.calledWith({element_id: doc_id, type: 'doc', project: @project })
.should.equal true
it 'should check the new name is valid', ->
@subject._checkValidElementName
.calledWith(@folder, @newName)
.should.equal true
it 'should update the doc name', ->
@ProjectModel.findOneAndUpdate
.calledWith(
{ _id: project_id },
{ $set: { "mongo.path.name": @newName } },
{ new: true }
).should.equal true
it 'calls the callback', ->
changes = { @oldDocs, @newDocs, @oldFiles, @newFiles }
@callback.calledWith(
null, @project.name, '/old.tex', '/new.tex', @doc.rev, changes
).should.equal true
describe 'addFolder', ->
beforeEach ->
@folderName = "folder1234"
@ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
@subject._confirmFolder = sinon.stub().yields(folder_id)
@subject._putElement = sinon.stub().yields()
@subject.addFolder project_id, folder_id, @folderName, @callback
it 'gets the project', ->
@ProjectGetter.getProject
.calledWith(project_id, {rootFolder:true, name: true})
.should.equal true
it 'checks the parent folder exists', ->
@subject._confirmFolder
.calledWith(@project, folder_id)
.should.equal true
it 'puts the element in mongo', ->
folderMatcher = sinon.match (folder) =>
folder.name == @folderName
@subject._putElement
.calledWithMatch(@project, folder_id, folderMatcher, 'folder')
.should.equal true
it 'calls the callback', ->
folderMatcher = sinon.match (folder) =>
folder.name == @folderName
@callback.calledWithMatch(null, folderMatcher, folder_id).should.equal true
describe '_removeElementFromMongoArray ', ->
beforeEach ->
@mongoPath = "folders[0].folders[5]"
@id = "12344"
@ProjectModel.update = sinon.stub().yields()
@ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
@subject._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @callback
it 'should unset', ->
update = { '$unset': { } }
update['$unset'][@mongoPath] = 1
@ProjectModel.update
.calledWith({ _id: @id }, update, {})
.should.equal true
it 'should pull', ->
@ProjectModel.findOneAndUpdate
.calledWith({ _id: @id }, { '$pull': { 'folders[0]': null } }, {'new': true})
.should.equal true
it 'should call the callback', ->
@callback.calledWith(null, @project).should.equal true
describe "_countElements", ->
beforeEach ->
@project =
_id: project_id,
rootFolder: [
docs: [{_id:123}, {_id:345}]
fileRefs: [{_id:123}, {_id:345}, {_id:456}]
folders: [
{
docs: [{_id:123}, {_id:345}, {_id:456}]
fileRefs:{}
folders: [
{
docs:[_id:1234],
fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
folders:[
{
docs:[{_id:321321}, {_id:123213}]
fileRefs:[{_id:312321}]
folders:[]
}
]
}
]
},{
docs:[{_id:123}, {_id:32131}]
fileRefs:[]
folders:[
{
docs:[{_id:3123}]
fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
folders:0
}
]
}
]
]
it "should return the correct number", ->
expect(@subject._countElements @project).to.equal(26)
it "should deal with null folders", ->
@project.rootFolder[0].folders[0].folders = undefined
expect(@subject._countElements @project).to.equal(17)
it "should deal with null docs", ->
@project.rootFolder[0].folders[0].docs = undefined
expect(@subject._countElements @project).to.equal(23)
it "should deal with null fileRefs", ->
@project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
expect(@subject._countElements @project).to.equal(23)
describe "_putElement", ->
beforeEach ->
@project =
_id: project_id
rootFolder: [_id:ObjectId()]
@folder =
_id: ObjectId()
name: "someFolder"
docs: [ {name: "another-doc.tex"} ]
fileRefs: [ {name: "another-file.tex"} ]
folders: [ {name: "another-folder"} ]
@doc =
_id: ObjectId()
name: "new.tex"
@path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
@ProjectLocator.findElement = sinon.stub().yields(null, @folder, @path)
@ProjectModel.findOneAndUpdate = sinon.stub().yields(null, @project)
describe "updating the project", ->
it "should use the correct mongo path", (done)->
@subject._putElement @project, @folder._id, @doc, "docs", (err)=>
@ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
done()
it "should return the project in the callback", (done)->
@subject._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
assert.equal project, @project
done()
it "should add an s onto the type if not included", (done)->
@subject._putElement @project, @folder._id, @doc, "doc", (err)=>
assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
done()
it "should not call update if element is null", (done)->
@subject._putElement @project, @folder._id, null, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
done()
it "should default to root folder insert", (done)->
@subject._putElement @project, null, @doc, "doc", (err)=>
@ProjectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
done()
it "should error if the element has no _id", (done)->
doc =
name:"something"
@subject._putElement @project, @folder._id, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
done()
it "should error if element name contains invalid characters", (done)->
doc =
_id: ObjectId()
name: "something*bad"
@subject._putElement @project, @folder._id, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("invalid element name")
done()
it "should error if element name is too long", (done)->
doc =
_id: ObjectId()
name: new Array(200).join("long-") + "something"
@subject._putElement @project, @folder._id, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("invalid element name")
done()
it "should error if the folder name is too long", (done)->
@path =
mongo: "mongo.path",
fileSystem: new Array(200).join("subdir/") + "foo"
@ProjectLocator.findElement.callsArgWith(1, null, @folder, @path)
doc =
_id: ObjectId()
name: "something"
@subject._putElement @project, @folder._id, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("path too long")
done()
it "should error if a document already exists with the same name", (done)->
doc =
_id: ObjectId()
name: "another-doc.tex"
@subject._putElement @project, @folder, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("file already exists")
done()
it "should error if a file already exists with the same name", (done)->
doc =
_id: ObjectId()
name: "another-file.tex"
@subject._putElement @project, @folder, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("file already exists")
done()
it "should error if a folder already exists with the same name", (done)->
doc =
_id: ObjectId()
name: "another-folder"
@subject._putElement @project, @folder, doc, "doc", (err)=>
@ProjectModel.findOneAndUpdate.called.should.equal false
err.should.deep.equal new Errors.InvalidNameError("file already exists")
done()
describe '_checkValidElementName', ->
beforeEach ->
@folder =
docs: [ name: 'doc_name' ]
fileRefs: [ name: 'file_name' ]
folders: [ name: 'folder_name' ]
it 'returns an error if name matches any doc name', ->
@subject._checkValidElementName @folder, 'doc_name', (err) ->
expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
it 'returns an error if name matches any file name', ->
@subject._checkValidElementName @folder, 'file_name', (err) ->
expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
it 'returns an error if name matches any folder name', ->
@subject._checkValidElementName @folder, 'folder_name', (err) ->
expect(err).to.deep.equal new Errors.InvalidNameError("file already exists")
it 'returns nothing if name is valid', ->
@subject._checkValidElementName @folder, 'unique_name', (err) ->
expect(err).to.be.undefined
describe '_checkValidMove', ->
beforeEach ->
@destFolder = _id: folder_id
@destFolderPath = fileSystem: '/foo/bar'
@ProjectLocator.findElement = sinon.stub().yields(null, @destFolder, @destFolderPath)
@subject._checkValidElementName = sinon.stub().yields()
it 'checks the element name is valid', ->
@doc = _id: doc_id, name: 'doc_name'
@subject._checkValidMove @project, 'doc', @doc, fileSystem: '/main.tex', @destFolder._id, (err) =>
expect(err).to.be.undefined
@subject._checkValidElementName
.calledWith(@destFolder, @doc.name)
.should.equal true
it 'returns an error if trying to move a folder inside itself', ->
folder = name: 'folder_name'
@subject._checkValidMove @project, 'folder', folder, fileSystem: '/foo', @destFolder._id, (err) =>
expect(err).to.deep.equal new Errors.InvalidNameError("destination folder is a child folder of me")
@@ -0,0 +1,876 @@
chai = require('chai')
assert = require('chai').assert
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/Features/Project/ProjectEntityUpdateHandler"
sinon = require 'sinon'
Errors = require "../../../../app/js/Features/Errors/Errors"
SandboxedModule = require('sandboxed-module')
ObjectId = require("mongoose").Types.ObjectId
describe 'ProjectEntityUpdateHandler', ->
project_id = '4eecb1c1bffa66588e0000a1'
doc_id = '4eecb1c1bffa66588e0000a2'
file_id = "4eecaffcbffa66588e000009"
folder_id = "4eecaffcbffa66588e000008"
rootFolderId = "4eecaffcbffa66588e000007"
userId = 1234
beforeEach ->
@project = _id: project_id, name: 'project name'
@fileUrl = 'filestore.example.com/file'
@FileStoreHandler =
uploadFileFromDisk: sinon.stub().yields(null, @fileUrl)
copyFile: sinon.stub().yields(null, @fileUrl)
@DocModel = class Doc
constructor:(options)->
{@name, @lines} = options
@_id = doc_id
@rev = 0
@FileModel = class File
constructor:(options)->
{@name} = options
@._id = file_id
@rev = 0
@docName = "doc-name"
@docLines = ['1234','abc']
@fileName = "something.jpg"
@fileSystemPath = "somehintg"
@source = 'editor'
@callback = sinon.stub()
@ProjectEntityUpdateHandler = SandboxedModule.require modulePath, requires:
'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
'../../models/Doc': Doc:@DocModel
'../Docstore/DocstoreManager': @DocstoreManager = {}
'../../Features/DocumentUpdater/DocumentUpdaterHandler':@DocumentUpdaterHandler =
updateProjectStructure: sinon.stub().yields()
'../../models/File': File:@FileModel
'../FileStore/FileStoreHandler':@FileStoreHandler
"../../infrastructure/LockManager":@LockManager =
runWithLock:
sinon.spy((key, runner, callback) -> runner(callback))
'../../models/Project': Project:@ProjectModel = {}
"./ProjectGetter": @ProjectGetter = {}
'./ProjectLocator': @ProjectLocator = {}
'./ProjectUpdateHandler': @ProjectUpdater = {}
'./ProjectEntityHandler': @ProjectEntityHandler = {}
'./ProjectEntityMongoUpdateHandler': @ProjectEntityMongoUpdateHandler = {}
'../ThirdPartyDataStore/TpdsUpdateSender':@TpdsUpdateSender =
addFile: sinon.stub().yields()
describe 'copyFileFromExistingProjectWithProject', ->
beforeEach ->
@oldProject_id = "123kljadas"
@oldFileRef = {name:@fileName, _id:"oldFileRef"}
@ProjectEntityMongoUpdateHandler._confirmFolder = sinon.stub().yields(folder_id)
@ProjectEntityMongoUpdateHandler._putElement = sinon.stub().yields(null, {path:{fileSystem: @fileSystemPath}})
@ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject @project, folder_id, @oldProject_id, @oldFileRef, userId, @callback
it 'should copy the file in FileStoreHandler', ->
@FileStoreHandler.copyFile
.calledWith(@oldProject_id, @oldFileRef._id, project_id, file_id)
.should.equal true
it 'should put file into folder by calling put element', ->
@ProjectEntityMongoUpdateHandler._putElement
.calledWithMatch(@project, folder_id, { _id: file_id, name: @fileName }, "file")
.should.equal true
it 'should return doc and parent folder', ->
@callback.calledWithMatch(null,{ _id: file_id, name: @fileName }, folder_id).should.equal true
it 'should call third party data store if versioning is enabled', ->
@TpdsUpdateSender.addFile.calledWith(
project_id: project_id
file_id: file_id
path: @fileSystemPath
rev: 0
project_name: @project.name
).should.equal true
it "should should send the change in project structure to the doc updater", ->
changesMatcher = sinon.match (changes) =>
{ newFiles } = changes
return false if newFiles.length != 1
newFile = newFiles[0]
newFile.file._id == file_id &&
newFile.path == @fileSystemPath &&
newFile.url == @fileUrl
@DocumentUpdaterHandler.updateProjectStructure
.calledWithMatch(project_id, userId, changesMatcher)
.should.equal true
describe 'updateDocLines', ->
beforeEach ->
@path = "/somewhere/something.tex"
@doc = {
_id: doc_id
}
@version = 42
@ranges = {"mock":"ranges"}
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields(null, @project)
@ProjectLocator.findElement = sinon.stub().yields(null, @doc, {fileSystem: @path})
@TpdsUpdateSender.addDoc = sinon.stub().yields()
@ProjectUpdater.markAsUpdated = sinon.stub()
@callback = sinon.stub()
describe "when the doc has been modified", ->
beforeEach ->
@DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
@ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @version, @ranges, @callback
it "should get the project without doc lines", ->
@ProjectGetter.getProjectWithoutDocLines
.calledWith(project_id)
.should.equal true
it "should find the doc", ->
@ProjectLocator.findElement
.calledWith({
project: @project
type: "docs"
element_id: doc_id
})
.should.equal true
it "should update the doc in the docstore", ->
@DocstoreManager.updateDoc
.calledWith(project_id, doc_id, @docLines, @version, @ranges)
.should.equal true
it "should mark the project as updated", ->
@ProjectUpdater.markAsUpdated
.calledWith(project_id)
.should.equal true
it "should send the doc the to the TPDS", ->
@TpdsUpdateSender.addDoc
.calledWith({
project_id: project_id
project_name: @project.name
doc_id: doc_id
rev: @rev
path: @path
})
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "when the doc has not been modified", ->
beforeEach ->
@DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
@ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @version, @ranges, @callback
it "should not mark the project as updated", ->
@ProjectUpdater.markAsUpdated.called.should.equal false
it "should not send the doc the to the TPDS", ->
@TpdsUpdateSender.addDoc.called.should.equal false
it "should call the callback", ->
@callback.called.should.equal true
describe "when the project is not found", ->
beforeEach ->
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields()
@ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @ranges, @version, @callback
it "should return a not found error", ->
@callback.calledWith(new Errors.NotFoundError()).should.equal true
describe "when the doc is not found", ->
beforeEach ->
@ProjectLocator.findElement = sinon.stub().yields()
@ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @ranges, @version, @callback
it "should log out the error", ->
@logger.error
.calledWith(
project_id: project_id
doc_id: doc_id
lines: @docLines
err: new Errors.NotFoundError("doc not found")
"doc not found while updating doc lines"
)
.should.equal true
it "should return a not found error", ->
@callback.calledWith(new Errors.NotFoundError()).should.equal true
describe "setRootDoc", ->
it "should call Project.update", ->
rootDoc_id = "root-doc-id-123123"
@ProjectModel.update = sinon.stub()
@ProjectEntityUpdateHandler.setRootDoc project_id, rootDoc_id
@ProjectModel.update
.calledWith({_id : project_id}, {rootDoc_id})
.should.equal true
describe "unsetRootDoc", ->
it "should call Project.update", ->
@ProjectModel.update = sinon.stub()
@ProjectEntityUpdateHandler.unsetRootDoc project_id
@ProjectModel.update
.calledWith({_id : project_id}, {$unset : {rootDoc_id: true}})
.should.equal true
describe "restoreDoc", ->
beforeEach ->
@doc = { "mock": "doc" }
@ProjectEntityHandler.getDoc = sinon.stub().yields(null, @docLines)
@ProjectEntityUpdateHandler.addDoc = sinon.stub().yields(null, @doc, folder_id)
@ProjectEntityUpdateHandler.restoreDoc project_id, doc_id, @docName, @callback
it 'should get the doc lines', ->
@ProjectEntityHandler.getDoc
.calledWith(project_id, doc_id, include_deleted: true)
.should.equal true
it "should add a new doc with these doc lines", ->
@ProjectEntityUpdateHandler.addDoc
.calledWith(project_id, null, @docName, @docLines)
.should.equal true
it "should call the callback with the new folder and doc", ->
@callback.calledWith(null, @doc, folder_id).should.equal true
describe 'addDoc', ->
beforeEach ->
@path = "/path/to/doc"
@newDoc = _id: doc_id
@ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory =
withoutLock: sinon.stub().yields(null, @newDoc, folder_id, @path)
@ProjectEntityUpdateHandler.addDoc project_id, folder_id, @docName, @docLines, userId, @callback
it "creates the doc without history", () ->
@ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory.withoutLock
.calledWith(project_id, folder_id, @docName, @docLines, userId)
.should.equal true
it "sends the change in project structure to the doc updater", () ->
newDocs = [
doc: @newDoc
path: @path
docLines: @docLines.join('\n')
]
@DocumentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, {newDocs})
.should.equal true
describe 'addFile', ->
beforeEach ->
@path = "/path/to/file"
@newFile = _id: file_id
@ProjectEntityUpdateHandler.addFileWithoutUpdatingHistory =
withoutLock: sinon.stub().yields(null, @newFile, folder_id, @path, @fileUrl)
@ProjectEntityUpdateHandler.addFile project_id, folder_id, @docName, @fileSystemPath, userId, @callback
it "creates the doc without history", () ->
@ProjectEntityUpdateHandler.addFileWithoutUpdatingHistory.withoutLock
.calledWith(project_id, folder_id, @docName, @fileSystemPath, userId)
.should.equal true
it "sends the change in project structure to the doc updater", () ->
newFiles = [
file: @newFile
path: @path
url: @fileUrl
]
@DocumentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, {newFiles})
.should.equal true
describe 'replaceFile', ->
beforeEach ->
@newFile = _id: file_id, rev: 0
@path = "/path/to/file"
@project = _id: project_id, name: 'some project'
@ProjectEntityMongoUpdateHandler.replaceFile = sinon.stub().yields(null, @newFile, @project, fileSystem: @path)
@ProjectEntityUpdateHandler.replaceFile project_id, file_id, @fileSystemPath, userId, @callback
it 'uploads a new version of the file', ->
@FileStoreHandler.uploadFileFromDisk
.calledWith(project_id, file_id, @fileSystemPath)
.should.equal true
it 'replaces the file in mongo', ->
@ProjectEntityMongoUpdateHandler.replaceFile
.calledWith(project_id, file_id)
.should.equal true
it 'notifies the tpds', ->
@TpdsUpdateSender.addFile
.calledWith({
project_id: project_id
project_name: @project.name
file_id: file_id
rev: @newFile.rev + 1
path: @path
})
.should.equal true
it 'updates the project structure in the doc updater', ->
newFiles = [
file: @newFile
path: @path
url: @fileUrl
]
@DocumentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, {newFiles})
.should.equal true
describe 'addDocWithoutUpdatingHistory', ->
beforeEach ->
@path = "/path/to/doc"
@project = _id: project_id, name: 'some project'
@TpdsUpdateSender.addDoc = sinon.stub().yields()
@DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
@ProjectEntityMongoUpdateHandler.addDoc = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
@ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory project_id, folder_id, @docName, @docLines, userId, @callback
it "updates the doc in the docstore", () ->
@DocstoreManager.updateDoc
.calledWith(project_id, doc_id, @docLines, 0, {})
.should.equal true
it "updates the doc in mongo", () ->
docMatcher = sinon.match (doc) =>
doc.name == @docName
@ProjectEntityMongoUpdateHandler.addDoc
.calledWithMatch(project_id, folder_id, docMatcher)
.should.equal true
it "notifies the tpds", () ->
@TpdsUpdateSender.addDoc
.calledWith({
project_id: project_id
project_name: @project.name
doc_id: doc_id
rev: 0
path: @path
})
.should.equal true
it "should not should send the change in project structure to the doc updater", () ->
@DocumentUpdaterHandler.updateProjectStructure
.called
.should.equal false
describe 'addFileWithoutUpdatingHistory', ->
beforeEach ->
@path = "/path/to/file"
@project = _id: project_id, name: 'some project'
@TpdsUpdateSender.addFile = sinon.stub().yields()
@ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
@ProjectEntityUpdateHandler.addFileWithoutUpdatingHistory project_id, folder_id, @fileName, @fileSystemPath, userId, @callback
it "updates the file in the filestore", () ->
@FileStoreHandler.uploadFileFromDisk
.calledWith(project_id, file_id, @fileSystemPath)
.should.equal true
it "updates the file in mongo", () ->
fileMatcher = sinon.match (file) =>
file.name == @fileName
@ProjectEntityMongoUpdateHandler.addFile
.calledWithMatch(project_id, folder_id, fileMatcher)
.should.equal true
it "notifies the tpds", () ->
@TpdsUpdateSender.addFile
.calledWith({
project_id: project_id
project_name: @project.name
file_id: file_id
rev: 0
path: @path
})
.should.equal true
it "should not should send the change in project structure to the doc updater", () ->
@DocumentUpdaterHandler.updateProjectStructure
.called
.should.equal false
describe 'upsertDoc', ->
describe 'upserting into an invalid folder', ->
beforeEach ->
@ProjectLocator.findElement = sinon.stub().yields()
@ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
it 'returns an error', ->
errorMatcher = sinon.match.instanceOf(Error)
@callback.calledWithMatch(errorMatcher)
.should.equal true
describe 'updating an existing doc', ->
beforeEach ->
@existingDoc = _id: doc_id, name: @docName
@folder = _id: folder_id, docs: [@existingDoc]
@ProjectLocator.findElement = sinon.stub().yields(null, @folder)
@DocumentUpdaterHandler.setDocument = sinon.stub().yields()
@DocumentUpdaterHandler.flushDocToMongo = sinon.stub().yields()
@ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
it 'tries to find the folder', ->
@ProjectLocator.findElement
.calledWith({project_id, element_id: folder_id, type: "folder"})
.should.equal true
it 'updates the doc contents', ->
@DocumentUpdaterHandler.setDocument
.calledWith(project_id, @existingDoc._id, userId, @docLines, @source)
.should.equal true
it 'flushes the doc contents', ->
@DocumentUpdaterHandler.flushDocToMongo
.calledWith(project_id, @existingDoc._id )
.should.equal true
it 'returns the doc', ->
@callback.calledWith(null, @existingDoc, false)
describe 'creating a new doc', ->
beforeEach ->
@folder = _id: folder_id, docs: []
@newDoc = _id: doc_id
@ProjectLocator.findElement = sinon.stub().yields(null, @folder)
@ProjectEntityUpdateHandler.addDoc = withoutLock: sinon.stub().yields(null, @newDoc)
@ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
it 'tries to find the folder', ->
@ProjectLocator.findElement
.calledWith({project_id, element_id: folder_id, type: "folder"})
.should.equal true
it 'adds the doc', ->
@ProjectEntityUpdateHandler.addDoc.withoutLock
.calledWith(project_id, folder_id, @docName, @docLines, userId)
.should.equal true
it 'returns the doc', ->
@callback.calledWith(null, @newDoc, true)
describe 'upsertFile', ->
describe 'upserting into an invalid folder', ->
beforeEach ->
@ProjectLocator.findElement = sinon.stub().yields()
@ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, userId, @callback
it 'returns an error', ->
errorMatcher = sinon.match.instanceOf(Error)
@callback.calledWithMatch(errorMatcher)
.should.equal true
describe 'updating an existing file', ->
beforeEach ->
@existingFile = _id: file_id, name: @fileName
@folder = _id: folder_id, fileRefs: [@existingFile]
@ProjectLocator.findElement = sinon.stub().yields(null, @folder)
@ProjectEntityUpdateHandler.replaceFile = withoutLock: sinon.stub().yields(null, @newFile)
@ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, userId, @callback
it 'replaces the file', ->
@ProjectEntityUpdateHandler.replaceFile.withoutLock
.calledWith(project_id, file_id, @fileSystemPath, userId)
.should.equal true
it 'returns the file', ->
@callback.calledWith(null, @existingFile, false)
describe 'creating a new file', ->
beforeEach ->
@folder = _id: folder_id, fileRefs: []
@newFile = _id: file_id
@ProjectLocator.findElement = sinon.stub().yields(null, @folder)
@ProjectEntityUpdateHandler.addFile = withoutLock: sinon.stub().yields(null, @newFile)
@ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, userId, @callback
it 'tries to find the folder', ->
@ProjectLocator.findElement
.calledWith({project_id, element_id: folder_id, type: "folder"})
.should.equal true
it 'adds the file', ->
@ProjectEntityUpdateHandler.addFile.withoutLock
.calledWith(project_id, folder_id, @fileName, @fileSystemPath, userId)
.should.equal true
it 'returns the file', ->
@callback.calledWith(null, @newFile, true)
describe 'upsertDocWithPath', ->
beforeEach ->
@path = "/folder/doc.tex"
@newFolders = [ 'mock-a', 'mock-b' ]
@folder = _id: folder_id
@doc = _id: doc_id
@isNewDoc = true
@ProjectEntityUpdateHandler.mkdirp =
withoutLock: sinon.stub().yields(null, @newFolders, @folder)
@ProjectEntityUpdateHandler.upsertDoc =
withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
@ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
it 'creates any necessary folders', ->
@ProjectEntityUpdateHandler.mkdirp.withoutLock
.calledWith(project_id, '/folder')
.should.equal true
it 'upserts the doc', ->
@ProjectEntityUpdateHandler.upsertDoc.withoutLock
.calledWith(project_id, @folder._id, 'doc.tex', @docLines, @source, userId)
.should.equal true
it 'calls the callback', ->
@callback
.calledWith(null, @doc, @isNewDoc, @newFolders, @folder)
.should.equal true
describe 'upsertFileWithPath', ->
beforeEach ->
@path = "/folder/file.png"
@newFolders = [ 'mock-a', 'mock-b' ]
@folder = _id: folder_id
@file = _id: file_id
@isNewFile = true
@ProjectEntityUpdateHandler.mkdirp =
withoutLock: sinon.stub().yields(null, @newFolders, @folder)
@ProjectEntityUpdateHandler.upsertFile =
withoutLock: sinon.stub().yields(null, @file, @isNewFile)
@ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, userId, @callback
it 'creates any necessary folders', ->
@ProjectEntityUpdateHandler.mkdirp.withoutLock
.calledWith(project_id, '/folder')
.should.equal true
it 'upserts the file', ->
@ProjectEntityUpdateHandler.upsertFile.withoutLock
.calledWith(project_id, @folder._id, 'file.png', @fileSystemPath, userId)
.should.equal true
it 'calls the callback', ->
@callback
.calledWith(null, @file, @isNewFile, @newFolders, @folder)
.should.equal true
describe 'deleteEntity', ->
beforeEach ->
@path = '/path/to/doc.tex'
@doc = _id: doc_id
@projectBeforeDeletion = _id: project_id, name: 'project'
@ProjectEntityMongoUpdateHandler.deleteEntity = sinon.stub().yields(null, @doc, {fileSystem: @path}, @projectBeforeDeletion)
@ProjectEntityUpdateHandler._cleanUpEntity = sinon.stub().yields()
@TpdsUpdateSender.deleteEntity = sinon.stub().yields()
@ProjectEntityUpdateHandler.deleteEntity project_id, doc_id, 'doc', userId, @callback
it 'deletes the entity in mongo', ->
@ProjectEntityMongoUpdateHandler.deleteEntity
.calledWith(project_id, doc_id, 'doc')
.should.equal true
it 'cleans up the doc in the docstore', ->
@ProjectEntityUpdateHandler._cleanUpEntity
.calledWith(@projectBeforeDeletion, @doc, 'doc', @path, userId)
.should.equal true
it 'it notifies the tpds', ->
@TpdsUpdateSender.deleteEntity
.calledWith({ project_id, @path, project_name: @projectBeforeDeletion.name })
.should.equal true
it 'retuns the entity_id', ->
@callback.calledWith(null, doc_id).should.equal true
describe 'deleteEntityWithPath', ->
describe 'when the entity exists', ->
beforeEach ->
@doc = _id: doc_id
@ProjectLocator.findElementByPath = sinon.stub().yields(null, @doc, 'doc')
@ProjectEntityUpdateHandler.deleteEntity =
withoutLock: sinon.stub().yields()
@path = '/path/to/doc.tex'
@ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
it 'finds the entity', ->
@ProjectLocator.findElementByPath
.calledWith(project_id, @path)
.should.equal true
it 'deletes the entity', ->
@ProjectEntityUpdateHandler.deleteEntity.withoutLock
.calledWith(project_id, @doc._id, 'doc', userId, @callback)
.should.equal true
describe 'when the entity does not exist', ->
beforeEach ->
@ProjectLocator.findElementByPath = sinon.stub().yields()
@path = '/doc.tex'
@ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
it 'returns an error', ->
@callback.calledWith(new Errors.NotFoundError()).should.equal true
describe 'mkdirp', ->
beforeEach ->
@docPath = '/folder/doc.tex'
@ProjectEntityMongoUpdateHandler.mkdirp = sinon.stub().yields()
@ProjectEntityUpdateHandler.mkdirp project_id, @docPath, @callback
it 'calls ProjectEntityMongoUpdateHandler', ->
@ProjectEntityMongoUpdateHandler.mkdirp
.calledWith(project_id, @docPath)
.should.equal true
describe 'addFolder', ->
beforeEach ->
@parentFolder_id = '123asdf'
@folderName = 'new-folder'
@ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
@ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
it 'calls ProjectEntityMongoUpdateHandler', ->
@ProjectEntityMongoUpdateHandler.addFolder
.calledWith(project_id, @parentFolder_id, @folderName)
.should.equal true
describe 'moveEntity', ->
beforeEach ->
@project_name = 'project name'
@startPath = '/a.tex'
@endPath = '/folder/b.tex'
@rev = 2
@changes = newDocs: ['old-doc'], newFiles: ['old-file']
@ProjectEntityMongoUpdateHandler.moveEntity = sinon.stub().yields(
null, @project_name, @startPath, @endPath, @rev, @changes
)
@TpdsUpdateSender.moveEntity = sinon.stub()
@DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
@ProjectEntityUpdateHandler.moveEntity project_id, doc_id, folder_id, 'doc', userId, @callback
it 'moves the entity in mongo', ->
@ProjectEntityMongoUpdateHandler.moveEntity
.calledWith(project_id, doc_id, folder_id, 'doc')
.should.equal true
it 'notifies tpds', ->
@TpdsUpdateSender.moveEntity
.calledWith({project_id, @project_name, @startPath, @endPath, @rev})
.should.equal true
it 'sends the changes in project structure to the doc updater', ->
@DocumentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @changes, @callback)
.should.equal true
describe "renameEntity", ->
beforeEach ->
@project_name = 'project name'
@startPath = '/folder/a.tex'
@endPath = '/folder/b.tex'
@rev = 2
@changes = newDocs: ['old-doc'], newFiles: ['old-file']
@newDocName = 'b.tex'
@ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
null, @project_name, @startPath, @endPath, @rev, @changes
)
@TpdsUpdateSender.moveEntity = sinon.stub()
@DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
@ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
it 'moves the entity in mongo', ->
@ProjectEntityMongoUpdateHandler.renameEntity
.calledWith(project_id, doc_id, 'doc', @newDocName)
.should.equal true
it 'notifies tpds', ->
@TpdsUpdateSender.moveEntity
.calledWith({project_id, @project_name, @startPath, @endPath, @rev})
.should.equal true
it 'sends the changes in project structure to the doc updater', ->
@DocumentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, @changes, @callback)
.should.equal true
describe "_cleanUpEntity", ->
beforeEach ->
@entity_id = "4eecaffcbffa66588e000009"
@FileStoreHandler.deleteFile = sinon.stub().yields()
@DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
@ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
describe "a file", ->
beforeEach (done) ->
@path = "/file/system/path.png"
@entity = _id: @entity_id
@ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'file', @path, userId, done
it "should delete the file from FileStoreHandler", ->
@FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal true
it "should not attempt to delete from the document updater", ->
@DocumentUpdaterHandler.deleteDoc.called.should.equal false
it "should should send the update to the doc updater", ->
oldFiles = [ file: @entity, path: @path ]
@DocumentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, {oldFiles})
.should.equal true
describe "a doc", ->
beforeEach (done) ->
@path = "/file/system/path.tex"
@ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
@entity = {_id: @entity_id}
@ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'doc', @path, userId, done
it "should clean up the doc", ->
@ProjectEntityUpdateHandler._cleanUpDoc
.calledWith(@project, @entity, @path, userId)
.should.equal true
describe "a folder", ->
beforeEach (done) ->
@folder =
folders: [
name: "subfolder"
fileRefs: [ @file1 = { _id: "file-id-1", name: "file-name-1"} ]
docs: [ @doc1 = { _id: "doc-id-1", name: "doc-name-1" } ]
folders: []
]
fileRefs: [ @file2 = { _id: "file-id-2", name: "file-name-2" } ]
docs: [ @doc2 = { _id: "doc-id-2", name: "doc-name-2" } ]
@ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
@ProjectEntityUpdateHandler._cleanUpFile = sinon.stub().yields()
path = "/folder"
@ProjectEntityUpdateHandler._cleanUpEntity @project, @folder, "folder", path, userId, done
it "should clean up all sub files", ->
@ProjectEntityUpdateHandler._cleanUpFile
.calledWith(@project, @file1, "/folder/subfolder/file-name-1", userId)
.should.equal true
@ProjectEntityUpdateHandler._cleanUpFile
.calledWith(@project, @file2, "/folder/file-name-2", userId)
.should.equal true
it "should clean up all sub docs", ->
@ProjectEntityUpdateHandler._cleanUpDoc
.calledWith(@project, @doc1, "/folder/subfolder/doc-name-1", userId)
.should.equal true
@ProjectEntityUpdateHandler._cleanUpDoc
.calledWith(@project, @doc2, "/folder/doc-name-2", userId)
.should.equal true
describe "_cleanUpDoc", ->
beforeEach ->
@project =
_id: ObjectId(project_id)
@doc =
_id: ObjectId()
name: "test.tex"
@path = "/path/to/doc"
@ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
@ProjectEntityUpdateHandler._insertDeletedDocReference = sinon.stub().yields()
@DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
@DocstoreManager.deleteDoc = sinon.stub().yields()
@callback = sinon.stub()
describe "when the doc is the root doc", ->
beforeEach ->
@project.rootDoc_id = @doc._id
@ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
it "should unset the root doc", ->
@ProjectEntityUpdateHandler.unsetRootDoc
.calledWith(project_id)
.should.equal true
it "should delete the doc in the doc updater", ->
@DocumentUpdaterHandler.deleteDoc
.calledWith(project_id, @doc._id.toString())
it "should insert the doc into the deletedDocs array", ->
@ProjectEntityUpdateHandler._insertDeletedDocReference
.calledWith(@project._id, @doc)
.should.equal true
it "should delete the doc in the doc store", ->
@DocstoreManager.deleteDoc
.calledWith(project_id, @doc._id.toString())
.should.equal true
it "should should send the update to the doc updater", ->
oldDocs = [ doc: @doc, path: @path ]
@DocumentUpdaterHandler.updateProjectStructure
.calledWith(project_id, userId, {oldDocs})
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "when the doc is not the root doc", ->
beforeEach ->
@project.rootDoc_id = ObjectId()
@ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
it "should not unset the root doc", ->
@ProjectEntityUpdateHandler.unsetRootDoc.called.should.equal false
it "should call the callback", ->
@callback.called.should.equal true
describe "_insertDeletedDocReference", ->
beforeEach ->
@doc =
_id: ObjectId()
name: "test.tex"
@callback = sinon.stub()
@ProjectModel.update = sinon.stub().yields()
@ProjectEntityUpdateHandler._insertDeletedDocReference project_id, @doc, @callback
it "should insert the doc into deletedDocs", ->
@ProjectModel.update
.calledWith({
_id: project_id
}, {
$push: {
deletedDocs: {
_id: @doc._id
name: @doc.name
}
}
})
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
@@ -19,6 +19,9 @@ describe "ProjectGetter", ->
"metrics-sharelatex": timeAsyncMethod: sinon.stub()
"../../models/Project": Project: @Project = {}
"../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
"../../infrastructure/LockManager": @LockManager =
mongoTransactionLock:
runWithLock : sinon.spy((key, runner, callback) -> runner(callback))
"logger-sharelatex":
err:->
log:->
@@ -61,7 +64,7 @@ describe "ProjectGetter", ->
@project =
_id: @project_id = "56d46b0a1d3422b87c5ebcb1"
@db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
describe "passing an id", ->
beforeEach ->
@ProjectGetter.getProjectWithOnlyFolders @project_id, @callback
@@ -101,7 +104,7 @@ describe "ProjectGetter", ->
@project =
_id: @project_id = "56d46b0a1d3422b87c5ebcb1"
@db.projects.find = sinon.stub().callsArgWith(2, null, [@project])
describe "passing an id", ->
beforeEach ->
@ProjectGetter.getProjectWithOnlyFolders @project_id, @callback
@@ -159,7 +162,7 @@ describe "ProjectGetter", ->
}
)
@ProjectGetter.findAllUsersProjects @user_id, @fields, @callback
it "should call the callback with all the projects", ->
@callback
.calledWith(null, {
@@ -11,6 +11,7 @@ describe 'ProjectRootDocManager', ->
@callback = sinon.stub()
@ProjectRootDocManager = SandboxedModule.require modulePath, requires:
"./ProjectEntityHandler" : @ProjectEntityHandler = {}
"./ProjectEntityUpdateHandler" : @ProjectEntityUpdateHandler = {}
describe "setRootDocAutomatically", ->
describe "when there is a suitable root doc", ->
@@ -30,7 +31,7 @@ describe 'ProjectRootDocManager', ->
lines: ["Hello world"]
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2)
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
@ProjectRootDocManager.setRootDocAutomatically @project_id, done
it "should check the docs of the project", ->
@@ -38,7 +39,7 @@ describe 'ProjectRootDocManager', ->
.should.equal true
it "should set the root doc to the doc containing a documentclass", ->
@ProjectEntityHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
@ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
.should.equal true
describe "when the root doc is an Rtex file", ->
@@ -51,11 +52,11 @@ describe 'ProjectRootDocManager', ->
_id: "doc-id-2"
lines: ["\\documentclass{article}", "\\input{chapter1}"]
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2)
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
@ProjectRootDocManager.setRootDocAutomatically @project_id, @callback
it "should set the root doc to the doc containing a documentclass", ->
@ProjectEntityHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
@ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
.should.equal true
describe "when there is no suitable root doc", ->
@@ -68,9 +69,9 @@ describe 'ProjectRootDocManager', ->
_id: "doc-id-2"
lines: ["%Example: \\documentclass{article}"]
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2)
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
@ProjectRootDocManager.setRootDocAutomatically @project_id, done
it "should not set the root doc to the doc containing a documentclass", ->
@ProjectEntityHandler.setRootDoc.called.should.equal false
@ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
@@ -7,168 +7,92 @@ BufferedStream = require('bufferedstream')
describe 'UpdateMerger :', ->
beforeEach ->
@editorController = {}
@updateReciver = {}
@projectLocator = {}
@projectEntityHandler = {}
@fs =
unlink:sinon.stub().callsArgWith(1)
@FileTypeManager = {}
@LockManager =
runWithLock : sinon.spy((key, runner, callback) -> runner(callback))
@updateMerger = SandboxedModule.require modulePath, requires:
'../Editor/EditorController': @editorController
'../Project/ProjectLocator': @projectLocator
'../Project/ProjectEntityHandler': @projectEntityHandler
'fs': @fs
'../Uploads/FileTypeManager':@FileTypeManager
'settings-sharelatex':{path:{dumpPath:"dump_here"}}
'fs': @fs =
unlink:sinon.stub().callsArgWith(1)
'logger-sharelatex':
log: ->
err: ->
"metrics-sharelatex":
Timer:->
done:->
"../../infrastructure/LockManager":@LockManager
'../Editor/EditorController': @EditorController = {}
'../Uploads/FileTypeManager':@FileTypeManager = {}
'settings-sharelatex':{path:{dumpPath:"dump_here"}}
@project_id = "project_id_here"
@user_id = "mock-user-id"
@docPath = "/folder/doc.tex"
@filePath = "/folder/file.png"
@fsPath = "/tmp/file/path"
@source = "dropbox"
@update = new BufferedStream()
@update.headers = {}
@updateRequest = new BufferedStream()
@updateMerger.p.writeStreamToDisk = sinon.stub().yields(null, @fsPath)
@callback = sinon.stub()
describe 'mergeUpdate', ->
beforeEach ->
@path = "/doc1"
@fsPath = "file/system/path.tex"
@updateMerger.p.writeStreamToDisk = sinon.stub().callsArgWith(2, null, @fsPath)
@FileTypeManager.isBinary = sinon.stub()
describe "doc updates", () ->
describe "doc updates", ->
beforeEach ->
@doc_id = "231312s"
@FileTypeManager.isBinary.callsArgWith(2, null, false)
@projectLocator.findElementByPath = sinon.stub().callsArgWith(2, null, _id: @doc_id)
@updateMerger.p.processDoc = sinon.stub().callsArgWith(6)
@filePath = "/folder/doc.tex"
@FileTypeManager.isBinary = sinon.stub().yields(null, false)
@updateMerger.p.processDoc = sinon.stub().yields()
@updateMerger.mergeUpdate @user_id, @project_id, @docPath, @updateRequest, @source, @callback
it 'should get the element id', (done)->
@updateMerger.mergeUpdate @user_id, @project_id, @path, @update, @source, =>
@projectLocator.findElementByPath.calledWith(@project_id, @path).should.equal true
done()
it 'should take a project lock', (done)->
@updateMerger.mergeUpdate @user_id, @project_id, @path, @update, @source, =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it 'should process update as doc', (done)->
@updateMerger.mergeUpdate @user_id, @project_id, @filePath, @update, @source, =>
@FileTypeManager.isBinary.calledWith(@filePath, @fsPath).should.equal true
@updateMerger.p.processDoc.calledWith(@project_id, @doc_id, @user_id, @fsPath, @filePath, @source).should.equal true
@fs.unlink.calledWith(@fsPath).should.equal true
done()
describe "file updates", () ->
beforeEach ->
@file_id = "1231"
@projectLocator.findElementByPath = sinon.stub().callsArgWith(2, null, _id: @file_id)
@FileTypeManager.isBinary.callsArgWith(2, null, true)
@updateMerger.p.processFile = sinon.stub().callsArgWith(6)
@filePath = "/folder/file1.png"
it 'should process update as file when it is not a doc', (done)->
@updateMerger.mergeUpdate @user_id, @project_id, @filePath, @update, @source, =>
@updateMerger.p.processFile.calledWith(@project_id, @file_id, @fsPath, @filePath, @source, @user_id).should.equal true
@FileTypeManager.isBinary.calledWith(@filePath, @fsPath).should.equal true
@fs.unlink.calledWith(@fsPath).should.equal true
done()
describe 'deleteUpdate', (done)->
beforeEach ->
@path = "folder/doc1"
@type = "mock-type"
@editorController.deleteEntityWithoutLock = ->
@entity_id = "entity_id_here"
@entity = _id:@entity_id
@projectLocator.findElementByPath = sinon.stub().callsArgWith(2, null, @entity, @type)
@editorController.deleteEntityWithoutLock = sinon.stub().callsArg(5)
it 'should get the element id', (done)->
@updateMerger.deleteUpdate @user_id, @project_id, @path, @source, =>
@projectLocator.findElementByPath.calledWith(@project_id, @path).should.equal true
done()
it 'should take a project lock', (done)->
@updateMerger.deleteUpdate @user_id, @project_id, @path, @source, =>
@LockManager.runWithLock.calledWith(@project_id).should.equal true
done()
it 'should delete the entity in the editor controller with the correct type', (done)->
@entity.lines = []
@updateMerger.deleteUpdate @user_id, @project_id, @path, @source, =>
@editorController.deleteEntityWithoutLock
.calledWith(@project_id, @entity_id, @type, @source, @user_id)
it 'should process update as doc', ->
@updateMerger.p.processDoc
.calledWith(@project_id, @user_id, @fsPath, @docPath, @source)
.should.equal true
done()
describe 'private methods', () ->
describe 'processDoc', (done)->
it 'removes the temp file from disk', ->
@fs.unlink.calledWith(@fsPath).should.equal true
describe "file updates", ->
beforeEach ->
@FileTypeManager.isBinary = sinon.stub().yields(null, true)
@updateMerger.p.processFile = sinon.stub().yields()
@updateMerger.mergeUpdate @user_id, @project_id, @filePath, @updateRequest, @source, @callback
it 'should process update as file', ->
@updateMerger.p.processFile
.calledWith(@project_id, @fsPath, @filePath, @source, @user_id)
.should.equal true
it 'removes the temp file from disk', ->
@fs.unlink.calledWith(@fsPath).should.equal true
describe 'deleteUpdate', ->
beforeEach ->
@EditorController.deleteEntityWithPath = sinon.stub().yields()
@updateMerger.deleteUpdate @user_id, @project_id, @docPath, @source, @callback
it 'should delete the entity in the editor controller', ->
@EditorController.deleteEntityWithPath
.calledWith(@project_id, @docPath, @source, @user_id)
.should.equal true
describe 'private methods', ->
describe 'processDoc', ->
beforeEach ->
@doc_id = "312312klnkld"
@docLines = "\\documentclass{article}\n\\usepackage[utf8]{inputenc}\n\n\\title{42}\n\\author{Jane Doe}\n\\date{June 2011}"
@splitDocLines = @docLines.split("\n")
@fs.readFile = sinon.stub().callsArgWith(2, null, @docLines)
@updateMerger.p.readFileIntoTextArray = sinon.stub().yields(null, @docLines)
@EditorController.upsertDocWithPath = sinon.stub().yields()
@editorController.setDoc = sinon.stub().callsArg(5)
@updateMerger.p.processDoc @project_id, @user_id, @fsPath, @docPath, @source, @callback
@update.write(@docLines)
@update.end()
it 'reads the temp file from disk', ->
@updateMerger.p.readFileIntoTextArray
.calledWith(@fsPath)
.should.equal true
it 'should set the doc text in the editor controller', (done)->
@updateMerger.p.processDoc @project_id, @doc_id, @user_id, @update, "path", @source, =>
@editorController.setDoc
.calledWith(@project_id, @doc_id, @user_id, @splitDocLines, @source)
.should.equal true
done()
it 'should upsert the doc in the editor controller', ->
@EditorController.upsertDocWithPath
.calledWith(@project_id, @docPath, @docLines, @source, @user_id)
.should.equal true
it 'should create a new doc when it doesnt exist', (done)->
folder = {_id:"adslkjioj"}
docName = "main.tex"
path = "folder1/folder2/#{docName}"
@editorController.mkdirpWithoutLock = sinon.stub().callsArgWith(2, null, [folder], folder)
@editorController.addDocWithoutLock = sinon.stub().callsArg(6)
@updateMerger.p.processDoc @project_id, undefined, @user_id, @update, path, @source, =>
@editorController.mkdirpWithoutLock
.calledWith(@project_id)
.should.equal true
@editorController.addDocWithoutLock
.calledWith(@project_id, folder._id, docName, @splitDocLines, @source, @user_id)
.should.equal true
done()
describe 'processFile', (done)->
describe 'processFile', ->
beforeEach ->
@file_id = "file_id_here"
@folder_id = "folder_id_here"
@path = "folder/file.png"
@folder = _id: @folder_id
@fileName = "file.png"
@fsPath = "fs/path.tex"
@editorController.addFileWithoutLock = sinon.stub().callsArg(6)
@editorController.replaceFileWithoutLock = sinon.stub().callsArg(5)
@editorController.deleteEntityWithoutLock = sinon.stub()
@editorController.mkdirpWithoutLock = sinon.stub().withArgs(@project_id).callsArgWith(2, null, [@folder], @folder)
@EditorController.upsertFileWithPath = sinon.stub().yields()
@updateMerger.p.processFile @project_id, @fsPath, @filePath, @source, @user_id, @callback
it 'should replace file if the file already exists', (done)->
@updateMerger.p.processFile @project_id, @file_id, @fsPath, @path, @source, @user_id, =>
@editorController.addFileWithoutLock.called.should.equal false
@editorController.replaceFileWithoutLock.calledWith(@project_id, @file_id, @fsPath, @source, @user_id).should.equal true
done()
it 'should call add file if the file does not exist', (done)->
@updateMerger.p.processFile @project_id, undefined, @fsPath, @path, @source, @user_id, =>
@editorController.mkdirpWithoutLock.calledWith(@project_id, "folder/").should.equal true
@editorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @fileName, @fsPath, @source, @user_id).should.equal true
@editorController.replaceFileWithoutLock.called.should.equal false
done()
it 'should upsert the file in the editor controller', ->
@EditorController.upsertFileWithPath
.calledWith(@project_id, @filePath, @fsPath, @source, @user_id)
.should.equal true
@@ -33,25 +33,25 @@ describe "FileSystemImportManager", ->
describe "when path is symlink", ->
beforeEach ->
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
@EditorController.addDocWithoutLock = sinon.stub()
@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.addDocWithoutLock.called.should.equal false
@EditorController.addDoc.called.should.equal false
describe "with replace set to false", ->
beforeEach ->
@EditorController.addDocWithoutLock = sinon.stub().callsArg(6)
@EditorController.addDoc = sinon.stub().callsArg(6)
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
it "should read the file from disk", ->
@fs.readFile.calledWith(@path_on_disk, "utf8").should.equal true
it "should insert the doc", ->
@EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
@EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
.should.equal true
describe "with windows line ending", ->
@@ -59,134 +59,60 @@ describe "FileSystemImportManager", ->
@docContent = "one\r\ntwo\r\nthree"
@docLines = ["one", "two", "three"]
@fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
@EditorController.addDocWithoutLock = sinon.stub().callsArg(6)
@EditorController.addDoc = sinon.stub().callsArg(6)
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
it "should strip the \\r characters before adding", ->
@EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
@EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
.should.equal true
describe "with replace set to true", ->
describe "when the doc doesn't exist", ->
beforeEach ->
@folder = {
docs: [{
_id: "doc-id-2"
name: "not-the-right-file.tex"
}]
}
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
@EditorController.addDocWithoutLock = sinon.stub().callsArg(6)
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
beforeEach ->
@EditorController.upsertDoc = sinon.stub().yields()
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
it "should look up the folder", ->
@ProjectLocator.findElement
.calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
.should.equal true
it "should insert the doc", ->
@EditorController.addDocWithoutLock.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
.should.equal true
describe "when the doc does exist", ->
beforeEach ->
@folder = {
docs: [{
_id: @doc_id = "doc-id-1"
name: @name
}, {
_id: "doc-id-2"
name: "not-the-right-file.tex"
}]
}
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
@EditorController.setDoc = sinon.stub().callsArg(5)
@FileSystemImportManager.addDoc @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
it "should look up the folder", ->
@ProjectLocator.findElement
.calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
.should.equal true
it "should set the doc with the new doc lines", ->
@EditorController.setDoc.calledWith(@project_id, @doc_id, @user_id, @docLines, "upload")
.should.equal true
it "should upsert the doc", ->
@EditorController.upsertDoc
.calledWith(@project_id, @folder_id, @name, @docLines, "upload", @user_id)
.should.equal true
describe "addFile with replace set to false", ->
beforeEach ->
@EditorController.addFileWithoutLock = sinon.stub().callsArg(6)
@EditorController.addFile = sinon.stub().callsArg(6)
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
@FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, false, @callback
it "should add the file", ->
@EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload", @user_id)
@EditorController.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload", @user_id)
.should.equal true
describe "addFile with symlink", ->
beforeEach ->
@EditorController.addFileWithoutLock = sinon.stub()
@EditorController.addFile = sinon.stub()
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, false)
@EditorController.replaceFileWithoutLock = sinon.stub()
@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.addFileWithoutLock.called.should.equal false
@EditorController.replaceFileWithoutLock.called.should.equal false
@EditorController.addFile.called.should.equal false
@EditorController.replaceFile.called.should.equal false
describe "addFile with replace set to true", ->
describe "when the file doesn't exist", ->
beforeEach ->
@folder = {
fileRefs: [{
_id: "file-id-2"
name: "not-the-right-file.tex"
}]
}
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
@EditorController.addFileWithoutLock = sinon.stub().callsArg(6)
@FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
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
it "should look up the folder", ->
@ProjectLocator.findElement
.calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
.should.equal true
it "should add the file", ->
@EditorController.addFileWithoutLock.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload", @user_id)
.should.equal true
describe "when the file does exist", ->
beforeEach ->
@folder = {
fileRefs: [{
_id: @file_id = "file-id-1"
name: @name
}, {
_id: "file-id-2"
name: "not-the-right-file.tex"
}]
}
@FileSystemImportManager._isSafeOnFileSystem = sinon.stub().callsArgWith(1, null, true)
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
@EditorController.replaceFileWithoutLock = sinon.stub().callsArg(5)
@FileSystemImportManager.addFile @user_id, @project_id, @folder_id, @name, @path_on_disk, true, @callback
it "should look up the folder", ->
@ProjectLocator.findElement
.calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
.should.equal true
it "should replace the file", ->
@EditorController.replaceFileWithoutLock
.calledWith(@project_id, @file_id, @path_on_disk, "upload", @user_id)
.should.equal true
it "should add the file", ->
@EditorController.upsertFile
.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload", @user_id)
.should.equal true
describe "addFolder", ->
beforeEach ->
@new_folder_id = "new-folder-id"
@EditorController.addFolderWithoutLock = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
@EditorController.addFolder = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
@FileSystemImportManager.addFolderContents = sinon.stub().callsArg(5)
describe "successfully", ->
@@ -195,7 +121,7 @@ describe "FileSystemImportManager", ->
@FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
it "should add a folder to the project", ->
@EditorController.addFolderWithoutLock.calledWith(@project_id, @folder_id, @name, "upload")
@EditorController.addFolder.calledWith(@project_id, @folder_id, @name, "upload")
.should.equal true
it "should add the folders contents", ->
@@ -208,7 +134,7 @@ describe "FileSystemImportManager", ->
@FileSystemImportManager.addFolder @user_id, @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
it "should not add a folder to the project", ->
@EditorController.addFolderWithoutLock.called.should.equal false
@EditorController.addFolder.called.should.equal false
@FileSystemImportManager.addFolderContents.called.should.equal false
describe "addFolderContents", ->
@@ -17,8 +17,6 @@ describe "ProjectUploadController", ->
done: sinon.stub()
@AuthenticationController =
getLoggedInUserId: sinon.stub().returns(@user_id)
@LockManager =
runWithLock : sinon.spy((key, runner, callback) -> runner(callback))
@ProjectUploadController = SandboxedModule.require modulePath, requires:
"./ProjectUploadManager" : @ProjectUploadManager = {}
@@ -26,7 +24,6 @@ describe "ProjectUploadController", ->
"logger-sharelatex" : @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
"metrics-sharelatex": @metrics
'../Authentication/AuthenticationController': @AuthenticationController
"../../infrastructure/LockManager": @LockManager
"fs" : @fs = {}
describe "uploadProject", ->
@@ -129,9 +126,6 @@ describe "ProjectUploadController", ->
@FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity)
@ProjectUploadController.uploadFile @req, @res
it "should take the lock", ->
@LockManager.runWithLock.calledWith(@project_id).should.equal true
it "should insert the file", ->
@FileSystemImportManager.addEntity
.calledWith(@user_id, @project_id, @folder_id, @name, @path)