EditorControllerTests.coffee 25 KB

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