EditorControllerTests.coffee 23 KB

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