EditorControllerTests.coffee 24 KB

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