EditorControllerTests.coffee 28 KB

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