EditorControllerTests.coffee 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorController'
  5. MockClient = require "../helpers/MockClient"
  6. assert = require('assert')
  7. describe "EditorController", ->
  8. beforeEach ->
  9. @project_id = "test-project-id"
  10. @project =
  11. _id: @project_id
  12. owner_ref:{_id:"something"}
  13. @doc_id = "test-doc-id"
  14. @projectModelView = "projectModelView"
  15. @user =
  16. _id: @user_id = "user-id"
  17. projects: {}
  18. @rooms = {}
  19. @io =
  20. sockets :
  21. clients : (room_id) =>
  22. @rooms[room_id]
  23. @DocumentUpdaterHandler = {}
  24. @ProjectOptionsHandler =
  25. setCompiler : sinon.spy()
  26. setSpellCheckLanguage: sinon.spy()
  27. @ProjectEntityHandler = {}
  28. @ProjectEditorHandler =
  29. buildProjectModelView : sinon.stub().returns(@projectModelView)
  30. @ProjectHandler = class ProjectHandler
  31. @Project =
  32. findPopulatedById: sinon.stub().callsArgWith(1, null, @project)
  33. @LimitationsManager = {}
  34. @AuthorizationManager = {}
  35. @AutomaticSnapshotManager = {}
  36. @VersioningApiHandler =
  37. enableVersioning : sinon.stub().callsArg(1)
  38. @client = new MockClient()
  39. @settings =
  40. apis:{thirdPartyDataStore:{emptyProjectFlushDelayMiliseconds:0.5}}
  41. redis: web:{}
  42. @dropboxProjectLinker = {}
  43. @callback = sinon.stub()
  44. @TpdsPollingBackgroundTasks = {}
  45. @ProjectDetailsHandler =
  46. setProjectDescription:sinon.stub()
  47. @EditorController = SandboxedModule.require modulePath, requires:
  48. "../../infrastructure/Server" : io : @io
  49. '../Project/ProjectEditorHandler' : @ProjectEditorHandler
  50. '../Project/ProjectEntityHandler' : @ProjectEntityHandler
  51. '../Project/ProjectOptionsHandler' : @ProjectOptionsHandler
  52. '../Project/ProjectDetailsHandler': @ProjectDetailsHandler
  53. '../Project/ProjectGetter' : @ProjectGetter = {}
  54. '../DocumentUpdater/DocumentUpdaterHandler' : @DocumentUpdaterHandler
  55. '../Subscription/LimitationsManager' : @LimitationsManager
  56. '../Security/AuthorizationManager' : @AuthorizationManager
  57. '../../handlers/ProjectHandler' : @ProjectHandler
  58. "../Versioning/AutomaticSnapshotManager" : @AutomaticSnapshotManager
  59. "../Versioning/VersioningApiHandler" : @VersioningApiHandler
  60. '../../models/Project' : Project: @Project
  61. "settings-sharelatex":@settings
  62. '../Dropbox/DropboxProjectLinker':@dropboxProjectLinker
  63. '../ThirdPartyDataStore/TpdsPollingBackgroundTasks':@TpdsPollingBackgroundTasks
  64. './EditorRealTimeController':@EditorRealTimeController = {}
  65. "../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
  66. "../TrackChanges/TrackChangesManager": @TrackChangesManager = {}
  67. 'redis':createClient:-> auth:->
  68. "logger-sharelatex": @logger =
  69. log: sinon.stub()
  70. err: sinon.stub()
  71. describe "joinProject", ->
  72. beforeEach ->
  73. sinon.spy(@client, "set")
  74. sinon.spy(@client, "get")
  75. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  76. @ProjectGetter.populateProjectWithUsers = sinon.stub().callsArgWith(1, null, @project)
  77. @AuthorizationManager.setPrivilegeLevelOnClient = sinon.stub()
  78. describe "when authorized", ->
  79. beforeEach ->
  80. @AuthorizationManager.getPrivilegeLevelForProject =
  81. sinon.stub().callsArgWith(2, null, true, "owner")
  82. @EditorController.joinProject(@client, @user, @project_id, @callback)
  83. it "should find the project without doc lines", ->
  84. @ProjectGetter.getProjectWithoutDocLines
  85. .calledWith(@project_id)
  86. .should.equal true
  87. it "should populate the user references in the project", ->
  88. @ProjectGetter.populateProjectWithUsers
  89. .calledWith(@project)
  90. .should.equal true
  91. it "should set the privilege level on the client", ->
  92. @AuthorizationManager.setPrivilegeLevelOnClient
  93. .calledWith(@client, "owner")
  94. .should.equal.true
  95. it "should add the client to the project channel", ->
  96. @client.join.calledWith(@project_id).should.equal true
  97. it "should set the project_id of the client", ->
  98. @client.set.calledWith("project_id", @project_id).should.equal true
  99. it "should return the project model view, privilege level and protocol version", ->
  100. @callback.calledWith(null, @projectModelView, "owner", @EditorController.protocolVersion).should.equal true
  101. it "should enable versioning", ->
  102. @VersioningApiHandler.enableVersioning.calledWith(@project)
  103. .should.equal true
  104. describe "when not authorized", ->
  105. beforeEach ->
  106. @AuthorizationManager.getPrivilegeLevelForProject =
  107. sinon.stub().callsArgWith(2, null, false)
  108. @EditorController.joinProject(@client, @user, @project_id, @callback)
  109. it "should not set the privilege level on the client", ->
  110. @AuthorizationManager.setPrivilegeLevelOnClient
  111. .called.should.equal false
  112. it "should not add the client to the project channel", ->
  113. @client.join.called.should.equal false
  114. it "should not set the project_id of the client", ->
  115. @client.set.called.should.equal false
  116. it "should return an error", ->
  117. @callback.calledWith(sinon.match.truthy).should.equal true
  118. describe "leaveProject", ->
  119. beforeEach ->
  120. sinon.stub(@client, "set")
  121. sinon.stub(@client, "get").callsArgWith(1, null, @project_id)
  122. @EditorRealTimeController.emitToRoom = sinon.stub()
  123. @EditorController.flushProjectIfEmpty = sinon.stub()
  124. @EditorController.leaveProject @client, @user
  125. it "should call the flush project if empty function", ->
  126. @EditorController.flushProjectIfEmpty
  127. .calledWith(@project_id)
  128. .should.equal true
  129. it "should emit a clientDisconnect to the project room", ->
  130. @EditorRealTimeController.emitToRoom
  131. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
  132. .should.equal true
  133. describe "joinDoc", ->
  134. beforeEach ->
  135. @client.join = sinon.stub()
  136. @client.set("user_id", @user_id)
  137. @fromVersion = 40
  138. @docLines = ["foo", "bar"]
  139. @ops = ["mock-op-1", "mock-op-2"]
  140. @version = 42
  141. @DocumentUpdaterHandler.getDocument = sinon.stub().callsArgWith(3, null, @docLines, @version, @ops)
  142. describe "with a fromVersion", ->
  143. beforeEach ->
  144. @EditorController.joinDoc @client, @project_id, @doc_id, @fromVersion, @callback
  145. it "should add the client to the socket.io room for the doc", ->
  146. @client.join.calledWith(@doc_id).should.equal true
  147. it "should get the document", ->
  148. @DocumentUpdaterHandler.getDocument
  149. .calledWith(@project_id, @doc_id, @fromVersion)
  150. .should.equal true
  151. it "should return the doclines and version and ops", ->
  152. @callback.calledWith(null, @docLines, @version, @ops).should.equal true
  153. it "should increment the join-doc metric", ->
  154. @Metrics.inc.calledWith("editor.join-doc").should.equal true
  155. it "should log out the request", ->
  156. @logger.log
  157. .calledWith(user_id: @user_id, project_id: @project_id, doc_id: @doc_id, "user joining doc")
  158. .should.equal true
  159. describe "without a fromVersion", ->
  160. beforeEach ->
  161. @EditorController.joinDoc @client, @project_id, @doc_id, @callback
  162. it "should get the document with fromVersion=-1", ->
  163. @DocumentUpdaterHandler.getDocument
  164. .calledWith(@project_id, @doc_id, -1)
  165. .should.equal true
  166. it "should return the doclines and version and ops", ->
  167. @callback.calledWith(null, @docLines, @version, @ops).should.equal true
  168. describe "leaveDoc", ->
  169. beforeEach ->
  170. @client.leave = sinon.stub()
  171. @client.set("user_id", @user_id)
  172. @EditorController.leaveDoc @client, @project_id, @doc_id, @callback
  173. it "should remove the client from the socket.io room for the doc", ->
  174. @client.leave.calledWith(@doc_id).should.equal true
  175. it "should increment the leave-doc metric", ->
  176. @Metrics.inc.calledWith("editor.leave-doc").should.equal true
  177. it "should log out the request", ->
  178. @logger.log
  179. .calledWith(user_id: @user_id, project_id: @project_id, doc_id: @doc_id, "user leaving doc")
  180. .should.equal true
  181. describe "flushProjectIfEmpty", ->
  182. beforeEach ->
  183. @DocumentUpdaterHandler.flushProjectToMongoAndDelete = sinon.stub()
  184. @TrackChangesManager.flushProject = sinon.stub()
  185. describe "when a project has no more users", ->
  186. it "should do the flush after the config set timeout to ensure that a reconect didn't just happen", (done)->
  187. @rooms[@project_id] = []
  188. @EditorController.flushProjectIfEmpty @project_id, =>
  189. @DocumentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal(true)
  190. @TrackChangesManager.flushProject.calledWith(@project_id).should.equal true
  191. done()
  192. describe "when a project still has connected users", ->
  193. it "should not flush the project", (done)->
  194. @rooms[@project_id] = ["socket-id-1", "socket-id-2"]
  195. @EditorController.flushProjectIfEmpty @project_id, =>
  196. @DocumentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal(false)
  197. @TrackChangesManager.flushProject.calledWith(@project_id).should.equal false
  198. done()
  199. describe "updateClientPosition", ->
  200. beforeEach ->
  201. @EditorRealTimeController.emitToRoom = sinon.stub()
  202. @update = {
  203. doc_id: @doc_id = "doc-id-123"
  204. row: @row = 42
  205. column: @column = 37
  206. }
  207. @clientParams = {
  208. project_id: @project_id
  209. first_name: @first_name = "Douglas"
  210. last_name: @last_name = "Adams"
  211. }
  212. @client.get = (param, callback) => callback null, @clientParams[param]
  213. describe "with a logged in user", ->
  214. beforeEach ->
  215. @EditorController.updateClientPosition @client, @update
  216. it "should send the update to the project room with the user's name", ->
  217. @EditorRealTimeController.emitToRoom
  218. .calledWith(@project_id, "clientTracking.clientUpdated", {
  219. doc_id: @doc_id,
  220. id: @client.id
  221. name: "#{@first_name} #{@last_name}"
  222. row: @row
  223. column: @column
  224. })
  225. .should.equal true
  226. describe "with an anonymous user", ->
  227. beforeEach ->
  228. @clientParams.first_name = null
  229. @clientParams.last_name = null
  230. @EditorController.updateClientPosition @client, @update
  231. it "should send the update to the project room with an anonymous name", ->
  232. @EditorRealTimeController.emitToRoom
  233. .calledWith(@project_id, "clientTracking.clientUpdated", {
  234. doc_id: @doc_id,
  235. id: @client.id
  236. name: "Anonymous"
  237. row: @row
  238. column: @column
  239. })
  240. .should.equal true
  241. describe "addUserToProject", ->
  242. beforeEach ->
  243. @email = "Jane.Doe@example.com"
  244. @priveleges = "readOnly"
  245. @addedUser = { _id: "added-user" }
  246. @ProjectHandler::addUserToProject = sinon.stub().callsArgWith(3, @addedUser)
  247. @EditorRealTimeController.emitToRoom = sinon.stub()
  248. @callback = sinon.stub()
  249. describe "when the project can accept more collaborators", ->
  250. beforeEach ->
  251. @LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, false)
  252. @EditorController.addUserToProject(@project_id, @email, @priveleges, @callback)
  253. it "should add the user to the project", ->
  254. @ProjectHandler::addUserToProject
  255. .calledWith(@project_id, @email.toLowerCase(), @priveleges)
  256. .should.equal true
  257. it "should emit a userAddedToProject event", ->
  258. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "userAddedToProject", @addedUser).should.equal true
  259. it "should return true to the callback", ->
  260. @callback.calledWith(null, true).should.equal true
  261. describe "when the project cannot accept more collaborators", ->
  262. beforeEach ->
  263. @LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, true)
  264. @EditorController.addUserToProject(@project_id, @email, @priveleges, @callback)
  265. it "should not add the user to the project", ->
  266. @ProjectHandler::addUserToProject.called.should.equal false
  267. it "should not emit a userAddedToProject event", ->
  268. @EditorRealTimeController.emitToRoom.called.should.equal false
  269. it "should return false to the callback", ->
  270. @callback.calledWith(null, false).should.equal true
  271. describe "removeUserFromProject", ->
  272. beforeEach ->
  273. @removed_user_id = "removed-user-id"
  274. @ProjectHandler::removeUserFromProject = sinon.stub().callsArgWith(2)
  275. @EditorRealTimeController.emitToRoom = sinon.stub()
  276. @EditorController.removeUserFromProject(@project_id, @removed_user_id)
  277. it "remove the user from the project", ->
  278. @ProjectHandler::removeUserFromProject
  279. .calledWith(@project_id, @removed_user_id)
  280. .should.equal true
  281. it "should emit a userRemovedFromProject event", ->
  282. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "userRemovedFromProject", @removed_user_id).should.equal true
  283. describe "updating compiler used for project", ->
  284. it "should send the new compiler and project id to the project options handler", (done)->
  285. compiler = "latex"
  286. @EditorController.setCompiler @project_id, compiler, (err)=>
  287. @ProjectOptionsHandler.setCompiler.calledWith(@project_id, compiler).should.equal true
  288. done()
  289. @ProjectOptionsHandler.setCompiler.args[0][2]()
  290. describe "updating language code used for project", ->
  291. it "should send the new languageCode and project id to the project options handler", (done)->
  292. languageCode = "fr"
  293. @EditorController.setSpellCheckLanguage @project_id, languageCode, (err)=>
  294. @ProjectOptionsHandler.setSpellCheckLanguage.calledWith(@project_id, languageCode).should.equal true
  295. done()
  296. @ProjectOptionsHandler.setSpellCheckLanguage.args[0][2]()
  297. describe 'set document', ->
  298. beforeEach ->
  299. @docLines = ["foo", "bar"]
  300. @ProjectEntityHandler.updateDocLines = sinon.stub().callsArg(4)
  301. @DocumentUpdaterHandler.setDocument = sinon.stub().callsArg(3)
  302. @EditorRealTimeController.emitToRoom = sinon.stub()
  303. it 'should send the document to the documentUpdaterHandler', (done)->
  304. @DocumentUpdaterHandler.setDocument = sinon.stub().withArgs(@project_id, @doc_id, @docLines).callsArg(3)
  305. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)->
  306. done()
  307. it 'should send the update to the connected users', (done)->
  308. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)=>
  309. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "entireDocUpdate", @doc_id).should.equal true
  310. done()
  311. it 'should send the new doc lines to the doucment updater', (done)->
  312. @DocumentUpdaterHandler.setDocument = ->
  313. mock = sinon.mock(@DocumentUpdaterHandler).expects("setDocument").withArgs(@project_id, @doc_id, @docLines).once().callsArg(3)
  314. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)=>
  315. mock.verify()
  316. done()
  317. it 'should update the document lines', (done)->
  318. @ProjectEntityHandler.updateDocLines = ->
  319. mock = sinon.mock(@ProjectEntityHandler).expects("updateDocLines").withArgs(@project_id, @doc_id, @docLines).once().callsArg(4)
  320. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)->
  321. mock.verify()
  322. done()
  323. describe 'add doc', ->
  324. beforeEach ->
  325. @ProjectEntityHandler.addDoc = ()->
  326. @EditorRealTimeController.emitToRoom = sinon.stub()
  327. @project_id = "12dsankj"
  328. @folder_id = "213kjd"
  329. @doc = {_id:"123ds"}
  330. @folder_id = "123ksajdn"
  331. @docName = "doc.tex"
  332. @docLines = ["1234","dskl"]
  333. it 'should add the doc using the project entity handler', (done)->
  334. mock = sinon.mock(@ProjectEntityHandler).expects("addDoc").withArgs(@project_id, @folder_id, @docName, @docLines).callsArg(5)
  335. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, "", ->
  336. mock.verify()
  337. done()
  338. it 'should send the update out to the users in the project', (done)->
  339. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(5, null, @doc, @folder_id)
  340. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, "", =>
  341. @EditorRealTimeController.emitToRoom
  342. .calledWith(@project_id, "reciveNewDoc", @folder_id, @doc)
  343. .should.equal true
  344. done()
  345. it 'should return the doc to the callback', (done) ->
  346. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(5, null, @doc, @folder_id)
  347. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, "", (error, doc) =>
  348. doc.should.equal @doc
  349. done()
  350. describe 'addFile :', ->
  351. beforeEach ->
  352. @ProjectEntityHandler.addFile = ->
  353. @EditorRealTimeController.emitToRoom = sinon.stub()
  354. @project_id = "12dsankj"
  355. @folder_id = "213kjd"
  356. @fileName = "file.png"
  357. @folder_id = "123ksajdn"
  358. @file = {_id:"dasdkjk"}
  359. @stream = new ArrayBuffer()
  360. it 'should add the folder using the project entity handler', (done)->
  361. mock = sinon.mock(@ProjectEntityHandler).expects("addFile").withArgs(@project_id).callsArg(4)
  362. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, =>
  363. mock.verify()
  364. done()
  365. it 'should send the update of a new folder out to the users in the project', (done)->
  366. @ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
  367. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, =>
  368. @EditorRealTimeController.emitToRoom
  369. .calledWith(@project_id, "reciveNewFile", @folder_id, @file)
  370. .should.equal true
  371. done()
  372. it "should return the file in the callback", (done) ->
  373. @ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
  374. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, (error, file) =>
  375. file.should.equal @file
  376. done()
  377. describe "replaceFile", ->
  378. beforeEach ->
  379. @project_id = "12dsankj"
  380. @file_id = "file_id_here"
  381. @fsPath = "/folder/file.png"
  382. it 'should send the replace file message to the editor controller', (done)->
  383. @ProjectEntityHandler.replaceFile = sinon.stub().callsArgWith(3)
  384. @EditorController.replaceFile @project_id, @file_id, @fsPath, =>
  385. @ProjectEntityHandler.replaceFile.calledWith(@project_id, @file_id, @fsPath).should.equal true
  386. done()
  387. describe 'addFolder :', ->
  388. beforeEach ->
  389. @ProjectEntityHandler.addFolder = ->
  390. @EditorRealTimeController.emitToRoom = sinon.stub()
  391. @project_id = "12dsankj"
  392. @folder_id = "213kjd"
  393. @folderName = "folder"
  394. @folder = {_id:"123ds"}
  395. it 'should add the folder using the project entity handler', (done)->
  396. mock = sinon.mock(@ProjectEntityHandler).expects("addFolder").withArgs(@project_id, @folder_id, @folderName).callsArg(3)
  397. @EditorController.addFolder @project_id, @folder_id, @folderName, ->
  398. mock.verify()
  399. done()
  400. it 'should notifyProjectUsersOfNewFolder', (done)->
  401. @ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
  402. mock = sinon.mock(@EditorController.p).expects('notifyProjectUsersOfNewFolder').withArgs(@project_id, @folder_id, @folder).callsArg(3)
  403. @EditorController.addFolder @project_id, @folder_id, @folderName, ->
  404. mock.verify()
  405. done()
  406. it 'notifyProjectUsersOfNewFolder should send update out to all users', (done)->
  407. @EditorController.p.notifyProjectUsersOfNewFolder @project_id, @folder_id, @folder, =>
  408. @EditorRealTimeController.emitToRoom
  409. .calledWith(@project_id, "reciveNewFolder", @folder_id, @folder)
  410. .should.equal true
  411. done()
  412. it 'should return the folder in the callback', (done) ->
  413. @ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
  414. @EditorController.addFolder @project_id, @folder_id, @folderName, (error, folder) =>
  415. folder.should.equal @folder
  416. done()
  417. describe 'mkdirp :', ->
  418. it 'should make the dirs and notifyProjectUsersOfNewFolder', (done)->
  419. path = "folder1/folder2"
  420. @folder1 = {_id:"folder_1_id_here"}
  421. @folder2 = {_id:"folder_2_id_here", parentFolder_id:@folder1._id}
  422. @folder3 = {_id:"folder_3_id_here", parentFolder_id:@folder2._id}
  423. @ProjectEntityHandler.mkdirp = sinon.stub().withArgs(@project_id, path).callsArgWith(2, null, [@folder1, @folder2, @folder3], @folder3)
  424. @EditorController.p.notifyProjectUsersOfNewFolder = sinon.stub().callsArg(3)
  425. @EditorController.mkdirp @project_id, path, (err, newFolders, lastFolder)=>
  426. @EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder1._id, @folder2).should.equal true
  427. @EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder2._id, @folder3).should.equal true
  428. newFolders.should.deep.equal [@folder1, @folder2, @folder3]
  429. lastFolder.should.equal @folder3
  430. done()
  431. describe 'deleteEntity', ->
  432. beforeEach ->
  433. @ProjectEntityHandler.deleteEntity = (project_id, entity_id, type, callback)-> callback()
  434. @entity_id = "entity_id_here"
  435. @type = "doc"
  436. @EditorRealTimeController.emitToRoom = sinon.stub()
  437. it 'should delete the folder using the project entity handler', (done)->
  438. mock = sinon.mock(@ProjectEntityHandler).expects("deleteEntity").withArgs(@project_id, @entity_id, @type).callsArg(3)
  439. @EditorController.deleteEntity @project_id, @entity_id, @type, ->
  440. mock.verify()
  441. done()
  442. it 'notify users an entity has been deleted', (done)->
  443. @EditorController.deleteEntity @project_id, @entity_id, @type, =>
  444. @EditorRealTimeController.emitToRoom
  445. .calledWith(@project_id, "removeEntity", @entity_id)
  446. .should.equal true
  447. done()
  448. describe "getting a list of project paths", ->
  449. it 'should call the project entity handler to get an array of docs', (done)->
  450. fullDocsHash =
  451. "/doc1.tex":{lines:["das"], _id:"1234"}
  452. "/doc2.tex":{lines:["dshajkh"]}
  453. project_id = "d312nkjnajn"
  454. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, fullDocsHash)
  455. @EditorController.getListOfDocPaths project_id, (err, returnedDocs)->
  456. returnedDocs.length.should.equal 2
  457. returnedDocs[0]._id.should.equal "1234"
  458. assert.equal returnedDocs[0].lines, undefined
  459. returnedDocs[1].path.should.equal "doc2.tex"
  460. done()
  461. describe "forceResyncOfDropbox", ->
  462. it 'should tell the project entity handler to flush to tpds', (done)->
  463. @ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArgWith(1)
  464. @EditorController.forceResyncOfDropbox @project_id, (err)=>
  465. @ProjectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(@project_id).should.equal true
  466. done()
  467. describe "notifyUsersProjectHasBeenDeletedOrRenamed", ->
  468. it 'should emmit a message to all users in a project', (done)->
  469. @EditorRealTimeController.emitToRoom = sinon.stub()
  470. @EditorController.notifyUsersProjectHasBeenDeletedOrRenamed @project_id, (err)=>
  471. @EditorRealTimeController.emitToRoom
  472. .calledWith(@project_id, "projectRenamedOrDeletedByExternalSource")
  473. .should.equal true
  474. done()
  475. describe "gettingTimeOfLastTpdsPoll", ->
  476. it "should ask the tpdsPollingBackgroundTask", (done)->
  477. date = new Date()
  478. @TpdsPollingBackgroundTasks.getLastTimePollHappned = sinon.stub().callsArgWith(0, null, date)
  479. @EditorController.getLastTimePollHappned (err, lastTimePollHappened)->
  480. lastTimePollHappened.should.equal date
  481. done()
  482. describe "updateProjectDescription", ->
  483. beforeEach ->
  484. @description = "new description"
  485. @EditorRealTimeController.emitToRoom = sinon.stub()
  486. it "should send the new description to the project details handler", (done)->
  487. @ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
  488. @EditorController.updateProjectDescription @project_id, @description, =>
  489. @ProjectDetailsHandler.setProjectDescription.calledWith(@project_id, @description).should.equal true
  490. done()
  491. it "should notify the other clients about the updated description", (done)->
  492. @ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
  493. @EditorController.updateProjectDescription @project_id, @description, =>
  494. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "projectDescriptionUpdated", @description).should.equal true
  495. done()