overleaf/services/web/test/UnitTests/coffee/Editor/EditorControllerTests.coffee

791 lines
30 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
SandboxedModule = require('sandboxed-module')
sinon = require('sinon')
require('chai').should()
modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorController'
MockClient = require "../helpers/MockClient"
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"
2014-02-12 05:23:40 -05:00
@projectModelView =
_id: @project_id
owner:{_id:"something"}
2014-02-12 05:23:40 -05:00
@user =
_id: @user_id = "user-id"
projects: {}
@rooms = {}
@io =
sockets :
clients : (room_id) =>
@rooms[room_id]
@DocumentUpdaterHandler = {}
@ProjectOptionsHandler =
setCompiler : sinon.spy()
setSpellCheckLanguage: sinon.spy()
2014-04-07 15:46:58 -04:00
@ProjectEntityHandler =
flushProjectToThirdPartyDataStore:sinon.stub()
2014-02-12 05:23:40 -05:00
@ProjectEditorHandler =
buildProjectModelView : sinon.stub().returns(@projectModelView)
@Project =
findPopulatedById: sinon.stub().callsArgWith(1, null, @project)
@LimitationsManager = {}
@AuthorizationManager = {}
@client = new MockClient()
@settings =
apis:{thirdPartyDataStore:{emptyProjectFlushDelayMiliseconds:0.5}}
redis: web:{}
@dropboxProjectLinker = {}
@callback = sinon.stub()
@ProjectDetailsHandler =
setProjectDescription:sinon.stub()
2014-04-07 15:46:58 -04:00
@CollaboratorsHandler =
removeUserFromProject: sinon.stub().callsArgWith(2)
addUserToProject: sinon.stub().callsArgWith(3)
@ProjectDeleter =
deleteProject: sinon.stub()
@ConnectedUsersManager =
markUserAsDisconnected:sinon.stub()
updateUserPosition:sinon.stub()
2014-04-07 15:46:58 -04:00
2014-02-12 05:23:40 -05:00
@EditorController = SandboxedModule.require modulePath, requires:
"../../infrastructure/Server" : io : @io
'../Project/ProjectEditorHandler' : @ProjectEditorHandler
'../Project/ProjectEntityHandler' : @ProjectEntityHandler
'../Project/ProjectOptionsHandler' : @ProjectOptionsHandler
'../Project/ProjectDetailsHandler': @ProjectDetailsHandler
2014-04-07 15:46:58 -04:00
'../Project/ProjectDeleter' : @ProjectDeleter
2014-02-12 05:23:40 -05:00
'../Project/ProjectGetter' : @ProjectGetter = {}
'../User/UserGetter': @UserGetter = {}
2014-04-07 15:46:58 -04:00
'../Collaborators/CollaboratorsHandler': @CollaboratorsHandler
2014-02-12 05:23:40 -05:00
'../DocumentUpdater/DocumentUpdaterHandler' : @DocumentUpdaterHandler
'../Subscription/LimitationsManager' : @LimitationsManager
'../Security/AuthorizationManager' : @AuthorizationManager
'../../models/Project' : Project: @Project
"settings-sharelatex":@settings
'../Dropbox/DropboxProjectLinker':@dropboxProjectLinker
'./EditorRealTimeController':@EditorRealTimeController = {}
"../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
"../TrackChanges/TrackChangesManager": @TrackChangesManager = {}
"../ConnectedUsers/ConnectedUsersManager":@ConnectedUsersManager
2014-09-26 12:49:31 -04:00
'redis-sharelatex':createClient:-> auth:->
2014-02-12 05:23:40 -05:00
"logger-sharelatex": @logger =
log: sinon.stub()
err: sinon.stub()
describe "joinProject", ->
beforeEach ->
sinon.spy(@client, "set")
sinon.spy(@client, "get")
@AuthorizationManager.setPrivilegeLevelOnClient = sinon.stub()
@EditorRealTimeController.emitToRoom = sinon.stub()
@ConnectedUsersManager.updateUserPosition.callsArgWith(4)
@ProjectDeleter.unmarkAsDeletedByExternalSource = sinon.stub()
2014-02-12 05:23:40 -05:00
describe "when authorized", ->
beforeEach ->
@EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectModelView, "owner")
2014-02-12 05:23:40 -05:00
@EditorController.joinProject(@client, @user, @project_id, @callback)
it "should set the privilege level on the client", ->
@AuthorizationManager.setPrivilegeLevelOnClient
.calledWith(@client, "owner")
.should.equal.true
it "should add the client to the project channel", ->
@client.join.calledWith(@project_id).should.equal true
it "should set the project_id of the client", ->
@client.set.calledWith("project_id", @project_id).should.equal true
it "should mark the user as connected with the ConnectedUsersManager", ->
@ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, @user, null).should.equal true
it "should return the project model view, privilege level and protocol version", ->
@callback.calledWith(null, @projectModelView, "owner", @EditorController.protocolVersion).should.equal true
2014-02-12 05:23:40 -05:00
describe "when not authorized", ->
beforeEach ->
@EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, null, false)
2014-02-12 05:23:40 -05:00
@EditorController.joinProject(@client, @user, @project_id, @callback)
it "should not set the privilege level on the client", ->
@AuthorizationManager.setPrivilegeLevelOnClient
.called.should.equal false
it "should not add the client to the project channel", ->
@client.join.called.should.equal false
it "should not set the project_id of the client", ->
@client.set.called.should.equal false
it "should return an error", ->
@callback.calledWith(sinon.match.truthy).should.equal true
describe "when the project is marked as deleted", ->
beforeEach ->
@projectModelView.deletedByExternalDataSource = true
@EditorController.buildJoinProjectView = sinon.stub().callsArgWith(2, null, @projectModelView, "owner")
@EditorController.joinProject(@client, @user, @project_id, @callback)
it "should remove the flag to send a user a message about the project being deleted", ->
@ProjectDeleter.unmarkAsDeletedByExternalSource
.calledWith(@project_id)
.should.equal true
describe "buildJoinProjectView", ->
beforeEach ->
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
@ProjectGetter.populateProjectWithUsers = sinon.stub().callsArgWith(1, null, @project)
@UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
describe "when authorized", ->
beforeEach ->
@AuthorizationManager.getPrivilegeLevelForProject =
sinon.stub().callsArgWith(2, null, true, "owner")
@EditorController.buildJoinProjectView(@project_id, @user_id, @callback)
it "should find the project without doc lines", ->
@ProjectGetter.getProjectWithoutDocLines
.calledWith(@project_id)
.should.equal true
it "should populate the user references in the project", ->
@ProjectGetter.populateProjectWithUsers
.calledWith(@project)
.should.equal true
it "should look up the user", ->
@UserGetter.getUser
.calledWith(@user_id, { isAdmin: true })
.should.equal true
it "should check the privilege level", ->
@AuthorizationManager.getPrivilegeLevelForProject
.calledWith(@project, @user)
.should.equal true
it "should return the project model view, privilege level and protocol version", ->
2014-11-07 07:35:10 -05:00
@callback.calledWith(null, @projectModelView, "owner").should.equal true
describe "when not authorized", ->
beforeEach ->
@AuthorizationManager.getPrivilegeLevelForProject =
sinon.stub().callsArgWith(2, null, false, null)
@EditorController.buildJoinProjectView(@project_id, @user_id, @callback)
it "should return false in the callback", ->
2014-11-07 07:35:10 -05:00
@callback.calledWith(null, null, false).should.equal true
2014-02-12 05:23:40 -05:00
describe "leaveProject", ->
beforeEach ->
sinon.stub(@client, "set")
sinon.stub(@client, "get").callsArgWith(1, null, @project_id)
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.flushProjectIfEmpty = sinon.stub()
@EditorController.leaveProject @client, @user
@ConnectedUsersManager.markUserAsDisconnected.callsArgWith(2)
2014-02-12 05:23:40 -05:00
it "should call the flush project if empty function", ->
@EditorController.flushProjectIfEmpty
.calledWith(@project_id)
.should.equal true
it "should emit a clientDisconnect to the project room", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
.should.equal true
it "should mark the user as connected with the ConnectedUsersManager", ->
2014-07-17 10:25:22 -04:00
@ConnectedUsersManager.markUserAsDisconnected.calledWith(@project_id, @client.id).should.equal true
2014-02-12 05:23:40 -05:00
describe "joinDoc", ->
beforeEach ->
@client.join = sinon.stub()
@client.set("user_id", @user_id)
@fromVersion = 40
@docLines = ["foo", "bar"]
@ops = ["mock-op-1", "mock-op-2"]
@version = 42
@DocumentUpdaterHandler.getDocument = sinon.stub().callsArgWith(3, null, @docLines, @version, @ops)
describe "with a fromVersion", ->
beforeEach ->
@EditorController.joinDoc @client, @project_id, @doc_id, @fromVersion, @callback
it "should add the client to the socket.io room for the doc", ->
@client.join.calledWith(@doc_id).should.equal true
it "should get the document", ->
@DocumentUpdaterHandler.getDocument
.calledWith(@project_id, @doc_id, @fromVersion)
.should.equal true
it "should return the doclines and version and ops", ->
@callback.calledWith(null, @docLines, @version, @ops).should.equal true
it "should increment the join-doc metric", ->
@Metrics.inc.calledWith("editor.join-doc").should.equal true
it "should log out the request", ->
@logger.log
.calledWith(user_id: @user_id, project_id: @project_id, doc_id: @doc_id, "user joining doc")
.should.equal true
describe "without a fromVersion", ->
beforeEach ->
@EditorController.joinDoc @client, @project_id, @doc_id, @callback
it "should get the document with fromVersion=-1", ->
@DocumentUpdaterHandler.getDocument
.calledWith(@project_id, @doc_id, -1)
.should.equal true
it "should return the doclines and version and ops", ->
@callback.calledWith(null, @docLines, @version, @ops).should.equal true
describe "leaveDoc", ->
beforeEach ->
@client.leave = sinon.stub()
@client.set("user_id", @user_id)
@EditorController.leaveDoc @client, @project_id, @doc_id, @callback
it "should remove the client from the socket.io room for the doc", ->
@client.leave.calledWith(@doc_id).should.equal true
it "should increment the leave-doc metric", ->
@Metrics.inc.calledWith("editor.leave-doc").should.equal true
it "should log out the request", ->
@logger.log
.calledWith(user_id: @user_id, project_id: @project_id, doc_id: @doc_id, "user leaving doc")
.should.equal true
describe "flushProjectIfEmpty", ->
beforeEach ->
@DocumentUpdaterHandler.flushProjectToMongoAndDelete = sinon.stub()
@TrackChangesManager.flushProject = sinon.stub()
2014-02-12 05:23:40 -05:00
describe "when a project has no more users", ->
it "should do the flush after the config set timeout to ensure that a reconect didn't just happen", (done)->
@rooms[@project_id] = []
@EditorController.flushProjectIfEmpty @project_id, =>
@DocumentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal(true)
@TrackChangesManager.flushProject.calledWith(@project_id).should.equal true
2014-02-12 05:23:40 -05:00
done()
describe "when a project still has connected users", ->
it "should not flush the project", (done)->
@rooms[@project_id] = ["socket-id-1", "socket-id-2"]
@EditorController.flushProjectIfEmpty @project_id, =>
@DocumentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal(false)
@TrackChangesManager.flushProject.calledWith(@project_id).should.equal false
2014-02-12 05:23:40 -05:00
done()
describe "updateClientPosition", ->
beforeEach ->
@EditorRealTimeController.emitToRoom = sinon.stub()
@ConnectedUsersManager.updateUserPosition.callsArgWith(4)
2014-02-12 05:23:40 -05:00
@update = {
doc_id: @doc_id = "doc-id-123"
row: @row = 42
column: @column = 37
}
2014-07-14 10:30:55 -04:00
2014-02-12 05:23:40 -05:00
describe "with a logged in user", ->
beforeEach ->
2014-06-26 06:19:05 -04:00
@clientParams = {
project_id: @project_id
first_name: @first_name = "Douglas"
last_name: @last_name = "Adams"
email: @email = "joe@example.com"
user_id: @user_id = "user-id-123"
}
@client.get = (param, callback) => callback null, @clientParams[param]
2014-02-12 05:23:40 -05:00
@EditorController.updateClientPosition @client, @update
2014-07-14 10:30:55 -04:00
@populatedCursorData =
doc_id: @doc_id,
id: @client.id
name: "#{@first_name} #{@last_name}"
row: @row
column: @column
email: @email
user_id: @user_id
2014-02-12 05:23:40 -05:00
it "should send the update to the project room with the user's name", ->
2014-07-14 10:30:55 -04:00
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "clientTracking.clientUpdated", @populatedCursorData).should.equal true
it "should send the cursor data to the connected user manager", (done)->
@ConnectedUsersManager.updateUserPosition.calledWith(@project_id, @client.id, {
user_id: @user_id,
email: @email,
first_name: @first_name,
last_name: @last_name
}, {
2014-07-17 10:25:22 -04:00
row: @row
column: @column
doc_id: @doc_id
}).should.equal true
2014-07-14 10:30:55 -04:00
done()
2014-02-12 05:23:40 -05:00
describe "with an anonymous user", ->
beforeEach ->
2014-06-26 06:19:05 -04:00
@clientParams = {
project_id: @project_id
}
@client.get = (param, callback) => callback null, @clientParams[param]
2014-02-12 05:23:40 -05:00
@EditorController.updateClientPosition @client, @update
it "should send the update to the project room with an anonymous name", ->
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "clientTracking.clientUpdated", {
doc_id: @doc_id,
id: @client.id
name: "Anonymous"
row: @row
column: @column
})
.should.equal true
2014-07-14 10:30:55 -04:00
it "should not send cursor data to the connected user manager", (done)->
@ConnectedUsersManager.updateUserPosition.called.should.equal false
2014-07-14 10:30:55 -04:00
done()
2014-02-12 05:23:40 -05:00
describe "addUserToProject", ->
beforeEach ->
@email = "Jane.Doe@example.com"
@priveleges = "readOnly"
@addedUser = { _id: "added-user" }
2014-07-01 09:19:52 -04:00
@ProjectEditorHandler.buildUserModelView = sinon.stub().returns(@addedUser)
2014-04-07 15:46:58 -04:00
@CollaboratorsHandler.addUserToProject = sinon.stub().callsArgWith(3, null, @addedUser)
2014-02-12 05:23:40 -05:00
@EditorRealTimeController.emitToRoom = sinon.stub()
@callback = sinon.stub()
describe "when the project can accept more collaborators", ->
beforeEach ->
@LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, false)
2014-04-07 15:46:58 -04:00
it "should add the user to the project", (done)->
@EditorController.addUserToProject @project_id, @email, @priveleges, =>
@CollaboratorsHandler.addUserToProject.calledWith(@project_id, @email.toLowerCase(), @priveleges).should.equal true
done()
2014-02-12 05:23:40 -05:00
2014-04-07 15:46:58 -04:00
it "should emit a userAddedToProject event", (done)->
@EditorController.addUserToProject @project_id, @email, @priveleges, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "userAddedToProject", @addedUser).should.equal true
done()
2014-07-01 09:19:52 -04:00
it "should return the user to the callback", (done)->
2014-04-07 15:46:58 -04:00
@EditorController.addUserToProject @project_id, @email, @priveleges, (err, result)=>
2014-07-01 09:19:52 -04:00
result.should.equal @addedUser
2014-04-07 15:46:58 -04:00
done()
2014-02-12 05:23:40 -05:00
describe "when the project cannot accept more collaborators", ->
beforeEach ->
@LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, true)
@EditorController.addUserToProject(@project_id, @email, @priveleges, @callback)
it "should not add the user to the project", ->
2014-04-07 15:46:58 -04:00
@CollaboratorsHandler.addUserToProject.called.should.equal false
2014-02-12 05:23:40 -05:00
it "should not emit a userAddedToProject event", ->
@EditorRealTimeController.emitToRoom.called.should.equal false
it "should return false to the callback", ->
@callback.calledWith(null, false).should.equal true
describe "removeUserFromProject", ->
beforeEach ->
@removed_user_id = "removed-user-id"
2014-04-07 15:46:58 -04:00
@CollaboratorsHandler.removeUserFromProject = sinon.stub().callsArgWith(2)
2014-02-12 05:23:40 -05:00
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.removeUserFromProject(@project_id, @removed_user_id)
it "remove the user from the project", ->
2014-04-07 15:46:58 -04:00
@CollaboratorsHandler.removeUserFromProject
2014-02-12 05:23:40 -05:00
.calledWith(@project_id, @removed_user_id)
.should.equal true
it "should emit a userRemovedFromProject event", ->
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "userRemovedFromProject", @removed_user_id).should.equal true
describe "updating compiler used for project", ->
it "should send the new compiler and project id to the project options handler", (done)->
compiler = "latex"
2014-06-25 08:51:02 -04:00
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.setCompiler @project_id, compiler, (err) =>
2014-02-12 05:23:40 -05:00
@ProjectOptionsHandler.setCompiler.calledWith(@project_id, compiler).should.equal true
2014-06-25 08:51:02 -04:00
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "compilerUpdated", compiler).should.equal true
2014-02-12 05:23:40 -05:00
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"
2014-06-25 08:51:02 -04:00
@EditorRealTimeController.emitToRoom = sinon.stub()
@EditorController.setSpellCheckLanguage @project_id, languageCode, (err) =>
2014-02-12 05:23:40 -05:00
@ProjectOptionsHandler.setSpellCheckLanguage.calledWith(@project_id, languageCode).should.equal true
2014-06-25 08:51:02 -04:00
@EditorRealTimeController.emitToRoom.calledWith(@project_id, "spellCheckLanguageUpdated", languageCode).should.equal true
2014-02-12 05:23:40 -05:00
done()
@ProjectOptionsHandler.setSpellCheckLanguage.args[0][2]()
2014-10-15 09:11:02 -04:00
describe 'setDoc', ->
2014-02-12 05:23:40 -05:00
beforeEach ->
@docLines = ["foo", "bar"]
2014-05-08 10:47:50 -04:00
@DocumentUpdaterHandler.flushDocToMongo = sinon.stub().callsArg(2)
@DocumentUpdaterHandler.setDocument = sinon.stub().callsArg(4)
2014-02-12 05:23:40 -05:00
it 'should send the document to the documentUpdaterHandler', (done)->
@DocumentUpdaterHandler.setDocument = sinon.stub().withArgs(@project_id, @doc_id, @docLines, @source).callsArg(4)
@EditorController.setDoc @project_id, @doc_id, @docLines, @source, (err)->
2014-02-12 05:23:40 -05:00
done()
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, @docLines, @source).once().callsArg(4)
2014-02-12 05:23:40 -05:00
@EditorController.setDoc @project_id, @doc_id, @docLines, @source, (err)=>
2014-02-12 05:23:40 -05:00
mock.verify()
done()
2014-05-08 10:47:50 -04:00
it 'should flush the doc to mongo', (done)->
@EditorController.setDoc @project_id, @doc_id, @docLines, @source, (err)=>
2014-05-08 10:47:50 -04:00
@DocumentUpdaterHandler.flushDocToMongo.calledWith(@project_id, @doc_id).should.equal true
2014-02-12 05:23:40 -05:00
done()
2014-10-15 09:11:02 -04:00
describe 'addDoc', ->
2014-02-12 05:23:40 -05:00
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"]
it 'should add the doc using the project entity handler', (done)->
2014-10-15 09:11:02 -04:00
mock = sinon.mock(@ProjectEntityHandler).expects("addDoc").withArgs(@project_id, @folder_id, @docName, @docLines).callsArg(4)
2014-02-12 05:23:40 -05:00
@EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, ->
2014-02-12 05:23:40 -05:00
mock.verify()
done()
it 'should send the update out to the users in the project', (done)->
2014-10-15 09:11:02 -04:00
@ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
2014-02-12 05:23:40 -05:00
@EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, =>
2014-02-12 05:23:40 -05:00
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
2014-02-12 05:23:40 -05:00
.should.equal true
done()
it 'should return the doc to the callback', (done) ->
2014-10-15 09:11:02 -04:00
@ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
@EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, (error, doc) =>
2014-02-12 05:23:40 -05:00
doc.should.equal @doc
done()
describe 'addFile :', ->
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)->
mock = sinon.mock(@ProjectEntityHandler).expects("addFile").withArgs(@project_id).callsArg(4)
@EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, =>
2014-02-12 05:23:40 -05:00
mock.verify()
done()
it 'should send the update of a new folder out to the users in the project', (done)->
@ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
@EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, =>
2014-02-12 05:23:40 -05:00
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source)
2014-02-12 05:23:40 -05:00
.should.equal true
done()
it "should return the file in the callback", (done) ->
@ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
@EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, (error, file) =>
2014-02-12 05:23:40 -05:00
file.should.equal @file
done()
describe "replaceFile", ->
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(3)
@EditorController.replaceFile @project_id, @file_id, @fsPath, @source, =>
2014-02-12 05:23:40 -05:00
@ProjectEntityHandler.replaceFile.calledWith(@project_id, @file_id, @fsPath).should.equal true
done()
describe 'addFolder :', ->
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.addFolder @project_id, @folder_id, @folderName, @source, ->
2014-02-12 05:23:40 -05:00
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.addFolder @project_id, @folder_id, @folderName, @source, ->
2014-02-12 05:23:40 -05:00
mock.verify()
done()
it 'notifyProjectUsersOfNewFolder should send update out to all users', (done)->
@EditorController.p.notifyProjectUsersOfNewFolder @project_id, @folder_id, @folder, =>
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "reciveNewFolder", @folder_id, @folder)
.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.addFolder @project_id, @folder_id, @folderName, @source, (error, folder) =>
2014-02-12 05:23:40 -05:00
folder.should.equal @folder
done()
describe 'mkdirp :', ->
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}
@ProjectEntityHandler.mkdirp = sinon.stub().withArgs(@project_id, path).callsArgWith(2, null, [@folder1, @folder2, @folder3], @folder3)
@EditorController.p.notifyProjectUsersOfNewFolder = sinon.stub().callsArg(3)
@EditorController.mkdirp @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()
describe 'deleteEntity', ->
beforeEach ->
@ProjectEntityHandler.deleteEntity = (project_id, entity_id, type, callback)-> callback()
@entity_id = "entity_id_here"
@type = "doc"
@EditorRealTimeController.emitToRoom = sinon.stub()
it 'should delete the folder using the project entity handler', (done)->
mock = sinon.mock(@ProjectEntityHandler).expects("deleteEntity").withArgs(@project_id, @entity_id, @type).callsArg(3)
@EditorController.deleteEntity @project_id, @entity_id, @type, @source, ->
2014-02-12 05:23:40 -05:00
mock.verify()
done()
it 'notify users an entity has been deleted', (done)->
@EditorController.deleteEntity @project_id, @entity_id, @type, @source, =>
2014-02-12 05:23:40 -05:00
@EditorRealTimeController.emitToRoom
.calledWith(@project_id, "removeEntity", @entity_id, @source)
2014-02-12 05:23:40 -05:00
.should.equal true
done()
describe "getting a list of project paths", ->
it 'should call the project entity handler to get an array of docs', (done)->
fullDocsHash =
"/doc1.tex":{lines:["das"], _id:"1234"}
"/doc2.tex":{lines:["dshajkh"]}
project_id = "d312nkjnajn"
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, fullDocsHash)
@EditorController.getListOfDocPaths project_id, (err, returnedDocs)->
returnedDocs.length.should.equal 2
returnedDocs[0]._id.should.equal "1234"
assert.equal returnedDocs[0].lines, undefined
returnedDocs[1].path.should.equal "doc2.tex"
done()
describe "forceResyncOfDropbox", ->
it 'should tell the project entity handler to flush to tpds', (done)->
@ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArgWith(1)
@EditorController.forceResyncOfDropbox @project_id, (err)=>
@ProjectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(@project_id).should.equal true
done()
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")
.should.equal true
done()
describe "updateProjectDescription", ->
beforeEach ->
@description = "new description"
@EditorRealTimeController.emitToRoom = sinon.stub()
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()
describe "deleteProject", ->
beforeEach ->
@err = "errro"
2014-04-07 15:46:58 -04:00
@ProjectDeleter.deleteProject = sinon.stub().callsArgWith(1, @err)
it "should call the project handler", (done)->
@EditorController.deleteProject @project_id, (err)=>
err.should.equal @err
2014-04-07 15:46:58 -04:00
@ProjectDeleter.deleteProject.calledWith(@project_id).should.equal true
done()
describe "renameEntity", ->
beforeEach ->
@err = "errro"
@entity_id = "entity_id_here"
@entityType = "doc"
@newName = "bobsfile.tex"
@ProjectEntityHandler.renameEntity = sinon.stub().callsArgWith(4, @err)
@EditorRealTimeController.emitToRoom = sinon.stub()
it "should call the project handler", (done)->
@EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
@ProjectEntityHandler.renameEntity.calledWith(@project_id, @entity_id, @entityType, @newName).should.equal true
done()
it "should emit the update to the room", (done)->
@EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityRename', @entity_id, @newName).should.equal true
done()
describe "moveEntity", ->
beforeEach ->
@err = "errro"
@entity_id = "entity_id_here"
@entityType = "doc"
@folder_id = "313dasd21dasdsa"
@ProjectEntityHandler.moveEntity = sinon.stub().callsArgWith(4, @err)
@EditorRealTimeController.emitToRoom = sinon.stub()
it "should call the ProjectEntityHandler", (done)->
@EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, =>
@ProjectEntityHandler.moveEntity.calledWith(@project_id, @entity_id, @folder_id, @entityType).should.equal true
done()
it "should emit the update to the room", (done)->
@EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, =>
@EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityMove', @entity_id, @folder_id).should.equal true
done()
describe "renameProject", ->
beforeEach ->
@err = "errro"
@window_id = "kdsjklj290jlk"
@newName = "new name here"
@ProjectDetailsHandler.renameProject = sinon.stub().callsArgWith(2, @err)
@EditorRealTimeController.emitToRoom = sinon.stub()
2014-04-07 15:46:58 -04:00
it "should call the EditorController", (done)->
@EditorController.renameProject @project_id, @newName, =>
@ProjectDetailsHandler.renameProject.calledWith(@project_id, @newName).should.equal true
done()
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 "setPublicAccessLevel", ->
beforeEach ->
@newAccessLevel = "public"
2014-06-25 08:51:02 -04:00
@ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, null)
@EditorRealTimeController.emitToRoom = sinon.stub()
2014-04-07 15:46:58 -04:00
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()
describe "setRootDoc", ->
beforeEach ->
@newRootDocID = "21312321321"
2014-06-25 08:51:02 -04:00
@ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2, null)
@EditorRealTimeController.emitToRoom = sinon.stub()
it "should call the ProjectEntityHandler", (done)->
@EditorController.setRootDoc @project_id, @newRootDocID, =>
@ProjectEntityHandler.setRootDoc.calledWith(@project_id, @newRootDocID).should.equal true
done()
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()