EditorControllerTests.coffee 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. @source = "dropbox"
  12. @doc = _id: @doc_id = "test-doc-id"
  13. @docName = "doc.tex"
  14. @docLines = ["1234","dskl"]
  15. @file = _id: @file_id ="dasdkjk"
  16. @fileName = "file.png"
  17. @fsPath = "/folder/file.png"
  18. @linkedFileData = {provider: 'url'}
  19. @newFile = _id: "new-file-id"
  20. @folder_id = "123ksajdn"
  21. @folder = _id: @folder_id
  22. @folderName = "folder"
  23. @callback = sinon.stub()
  24. @EditorController = SandboxedModule.require modulePath, requires:
  25. '../Project/ProjectEntityUpdateHandler' : @ProjectEntityUpdateHandler = {}
  26. '../Project/ProjectOptionsHandler' : @ProjectOptionsHandler =
  27. setCompiler: sinon.stub().yields()
  28. setImageName: sinon.stub().yields()
  29. setSpellCheckLanguage: sinon.stub().yields()
  30. '../Project/ProjectDetailsHandler': @ProjectDetailsHandler =
  31. setProjectDescription: sinon.stub().yields()
  32. renameProject: sinon.stub().yields()
  33. setPublicAccessLevel: sinon.stub().yields()
  34. '../Project/ProjectDeleter' : @ProjectDeleter = {}
  35. '../DocumentUpdater/DocumentUpdaterHandler' : @DocumentUpdaterHandler =
  36. flushDocToMongo: sinon.stub().yields()
  37. setDocument: sinon.stub().yields()
  38. './EditorRealTimeController':@EditorRealTimeController =
  39. emitToRoom: sinon.stub()
  40. "metrics-sharelatex": @Metrics = inc: sinon.stub()
  41. "logger-sharelatex": @logger =
  42. log: sinon.stub()
  43. err: sinon.stub()
  44. describe 'addDoc', ->
  45. beforeEach ->
  46. @ProjectEntityUpdateHandler.addDoc = sinon.stub().yields(null, @doc, @folder_id)
  47. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, @callback
  48. it 'should add the doc using the project entity handler', ->
  49. @ProjectEntityUpdateHandler.addDoc
  50. .calledWith(@project_id, @folder_id, @docName, @docLines)
  51. .should.equal true
  52. it 'should send the update out to the users in the project', ->
  53. @EditorRealTimeController.emitToRoom
  54. .calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
  55. .should.equal true
  56. it 'calls the callback', ->
  57. @callback.calledWith(null, @doc).should.equal true
  58. describe 'addFile', ->
  59. beforeEach ->
  60. @ProjectEntityUpdateHandler.addFile = sinon.stub().yields(null, @file, @folder_id)
  61. @EditorController.addFile @project_id, @folder_id, @fileName, @fsPath, @linkedFileData, @source, @user_id, @callback
  62. it 'should add the folder using the project entity handler', ->
  63. @ProjectEntityUpdateHandler.addFile
  64. .calledWith(@project_id, @folder_id, @fileName, @fsPath, @linkedFileData, @user_id)
  65. .should.equal true
  66. it 'should send the update of a new folder out to the users in the project', ->
  67. @EditorRealTimeController.emitToRoom
  68. .calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source, @linkedFileData)
  69. .should.equal true
  70. it 'calls the callback', ->
  71. @callback.calledWith(null, @file).should.equal true
  72. describe 'upsertDoc', ->
  73. beforeEach ->
  74. @ProjectEntityUpdateHandler.upsertDoc = sinon.stub().yields(null, @doc, false)
  75. @EditorController.upsertDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, @callback
  76. it 'upserts the doc using the project entity handler', ->
  77. @ProjectEntityUpdateHandler.upsertDoc
  78. .calledWith(@project_id, @folder_id, @docName, @docLines, @source)
  79. .should.equal true
  80. it 'returns the doc', ->
  81. @callback.calledWith(null, @doc).should.equal true
  82. describe 'doc does not exist', ->
  83. beforeEach ->
  84. @ProjectEntityUpdateHandler.upsertDoc = sinon.stub().yields(null, @doc, true)
  85. @EditorController.upsertDoc @project_id, @folder_id, @docName, @docLines, @source, @user_id, @callback
  86. it 'sends an update out to users in the project', ->
  87. @EditorRealTimeController.emitToRoom
  88. .calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
  89. .should.equal true
  90. describe 'upsertFile', ->
  91. beforeEach ->
  92. @ProjectEntityUpdateHandler.upsertFile = sinon.stub().yields(null, @newFile, false, @file)
  93. @EditorController.upsertFile @project_id, @folder_id, @fileName, @fsPath, @linkedFileData, @source, @user_id, @callback
  94. it 'upserts the file using the project entity handler', ->
  95. @ProjectEntityUpdateHandler.upsertFile
  96. .calledWith(@project_id, @folder_id, @fileName, @fsPath, @linkedFileData, @user_id)
  97. .should.equal true
  98. it 'returns the file', ->
  99. @callback.calledWith(null, @newFile).should.equal true
  100. describe 'file does not exist', ->
  101. beforeEach ->
  102. @ProjectEntityUpdateHandler.upsertFile = sinon.stub().yields(null, @file, true)
  103. @EditorController.upsertFile @project_id, @folder_id, @fileName, @fsPath, @linkedFileData, @source, @user_id, @callback
  104. it 'should send the update out to users in the project', ->
  105. @EditorRealTimeController.emitToRoom
  106. .calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source, @linkedFileData)
  107. .should.equal true
  108. describe "upsertDocWithPath", ->
  109. beforeEach ->
  110. @docPath = '/folder/doc'
  111. @ProjectEntityUpdateHandler.upsertDocWithPath = sinon.stub().yields(null, @doc, false, [], @folder)
  112. @EditorController.upsertDocWithPath @project_id, @docPath, @docLines, @source, @user_id, @callback
  113. it 'upserts the doc using the project entity handler', ->
  114. @ProjectEntityUpdateHandler.upsertDocWithPath
  115. .calledWith(@project_id, @docPath, @docLines, @source)
  116. .should.equal true
  117. describe 'doc does not exist', ->
  118. beforeEach ->
  119. @ProjectEntityUpdateHandler.upsertDocWithPath = sinon.stub().yields(null, @doc, true, [], @folder)
  120. @EditorController.upsertDocWithPath @project_id, @docPath, @docLines, @source, @user_id, @callback
  121. it 'should send the update for the doc out to users in the project', ->
  122. @EditorRealTimeController.emitToRoom
  123. .calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
  124. .should.equal true
  125. describe 'folders required for doc do not exist', ->
  126. beforeEach ->
  127. folders = [
  128. @folderA = { _id: 2, parentFolder_id: 1}
  129. @folderB = { _id: 3, parentFolder_id: 2}
  130. ]
  131. @ProjectEntityUpdateHandler.upsertDocWithPath = sinon.stub().yields(null, @doc, true, folders, @folderB)
  132. @EditorController.upsertDocWithPath @project_id, @docPath, @docLines, @source, @user_id, @callback
  133. it 'should send the update for each folder to users in the project', ->
  134. @EditorRealTimeController.emitToRoom
  135. .calledWith(@project_id, "reciveNewFolder", @folderA.parentFolder_id, @folderA)
  136. .should.equal true
  137. @EditorRealTimeController.emitToRoom
  138. .calledWith(@project_id, "reciveNewFolder", @folderB.parentFolder_id, @folderB)
  139. .should.equal true
  140. describe "upsertFileWithPath", ->
  141. beforeEach ->
  142. @filePath = '/folder/file'
  143. @ProjectEntityUpdateHandler.upsertFileWithPath = sinon.stub().yields(null, @newFile, false, @file, [], @folder)
  144. @EditorController.upsertFileWithPath @project_id, @filePath, @fsPath, @linkedFileData, @source, @user_id, @callback
  145. it 'upserts the file using the project entity handler', ->
  146. @ProjectEntityUpdateHandler.upsertFileWithPath
  147. .calledWith(@project_id, @filePath, @fsPath, @linkedFileData)
  148. .should.equal true
  149. describe 'file does not exist', ->
  150. beforeEach ->
  151. @ProjectEntityUpdateHandler.upsertFileWithPath = sinon.stub().yields(null, @file, true, undefined, [], @folder)
  152. @EditorController.upsertFileWithPath @project_id, @filePath, @fsPath, @linkedFileData, @source, @user_id, @callback
  153. it 'should send the update for the file out to users in the project', ->
  154. @EditorRealTimeController.emitToRoom
  155. .calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source, @linkedFileData)
  156. .should.equal true
  157. describe 'folders required for file do not exist', ->
  158. beforeEach ->
  159. folders = [
  160. @folderA = { _id: 2, parentFolder_id: 1}
  161. @folderB = { _id: 3, parentFolder_id: 2}
  162. ]
  163. @ProjectEntityUpdateHandler.upsertFileWithPath = sinon.stub().yields(null, @file, true, undefined, folders, @folderB)
  164. @EditorController.upsertFileWithPath @project_id, @filePath, @fsPath, @linkedFileData, @source, @user_id, @callback
  165. it 'should send the update for each folder to users in the project', ->
  166. @EditorRealTimeController.emitToRoom
  167. .calledWith(@project_id, "reciveNewFolder", @folderA.parentFolder_id, @folderA)
  168. .should.equal true
  169. @EditorRealTimeController.emitToRoom
  170. .calledWith(@project_id, "reciveNewFolder", @folderB.parentFolder_id, @folderB)
  171. .should.equal true
  172. describe 'addFolder', ->
  173. beforeEach ->
  174. @EditorController._notifyProjectUsersOfNewFolder = sinon.stub().yields()
  175. @ProjectEntityUpdateHandler.addFolder = sinon.stub().yields(null, @folder, @folder_id)
  176. @EditorController.addFolder @project_id, @folder_id, @folderName, @source, @callback
  177. it 'should add the folder using the project entity handler', ->
  178. @ProjectEntityUpdateHandler.addFolder
  179. .calledWith(@project_id, @folder_id, @folderName)
  180. .should.equal true
  181. it 'should notifyProjectUsersOfNewFolder', ->
  182. @EditorController._notifyProjectUsersOfNewFolder
  183. .calledWith(@project_id, @folder_id, @folder)
  184. it 'should return the folder in the callback', ->
  185. @callback.calledWith(null, @folder).should.equal true
  186. describe 'mkdirp', ->
  187. beforeEach ->
  188. @path = "folder1/folder2"
  189. @folders = [
  190. @folderA = { _id: 2, parentFolder_id: 1}
  191. @folderB = { _id: 3, parentFolder_id: 2}
  192. ]
  193. @EditorController._notifyProjectUsersOfNewFolders = sinon.stub().yields()
  194. @ProjectEntityUpdateHandler.mkdirp = sinon.stub().yields(null, @folders, @folder)
  195. @EditorController.mkdirp @project_id, @path, @callback
  196. it 'should create the folder using the project entity handler', ->
  197. @ProjectEntityUpdateHandler.mkdirp
  198. .calledWith(@project_id, @path)
  199. .should.equal true
  200. it 'should notifyProjectUsersOfNewFolder', ->
  201. @EditorController._notifyProjectUsersOfNewFolders
  202. .calledWith(@project_id, @folders)
  203. it 'should return the folder in the callback', ->
  204. @callback.calledWith(null, @folders, @folder).should.equal true
  205. describe 'deleteEntity', ->
  206. beforeEach ->
  207. @entity_id = "entity_id_here"
  208. @type = "doc"
  209. @ProjectEntityUpdateHandler.deleteEntity = sinon.stub().yields()
  210. @EditorController.deleteEntity @project_id, @entity_id, @type, @source, @user_id, @callback
  211. it 'should delete the folder using the project entity handler', ->
  212. @ProjectEntityUpdateHandler.deleteEntity
  213. .calledWith(@project_id, @entity_id, @type, @user_id)
  214. .should.equal.true
  215. it 'notify users an entity has been deleted', ->
  216. @EditorRealTimeController.emitToRoom
  217. .calledWith(@project_id, "removeEntity", @entity_id, @source)
  218. .should.equal true
  219. describe "deleteEntityWithPath", ->
  220. beforeEach () ->
  221. @entity_id = "entity_id_here"
  222. @ProjectEntityUpdateHandler.deleteEntityWithPath = sinon.stub().yields(null, @entity_id)
  223. @path = "folder1/folder2"
  224. @EditorController.deleteEntityWithPath @project_id, @path, @source, @user_id, @callback
  225. it 'should delete the folder using the project entity handler', ->
  226. @ProjectEntityUpdateHandler.deleteEntityWithPath
  227. .calledWith(@project_id, @path, @user_id)
  228. .should.equal.true
  229. it 'notify users an entity has been deleted', ->
  230. @EditorRealTimeController.emitToRoom
  231. .calledWith(@project_id, "removeEntity", @entity_id, @source)
  232. .should.equal true
  233. describe "notifyUsersProjectHasBeenDeletedOrRenamed", ->
  234. it 'should emmit a message to all users in a project', (done)->
  235. @EditorController.notifyUsersProjectHasBeenDeletedOrRenamed @project_id, (err)=>
  236. @EditorRealTimeController.emitToRoom
  237. .calledWith(@project_id, "projectRenamedOrDeletedByExternalSource")
  238. .should.equal true
  239. done()
  240. describe "updateProjectDescription", ->
  241. beforeEach ->
  242. @description = "new description"
  243. @EditorController.updateProjectDescription @project_id, @description, @callback
  244. it "should send the new description to the project details handler", ->
  245. @ProjectDetailsHandler.setProjectDescription.calledWith(@project_id, @description).should.equal true
  246. it "should notify the other clients about the updated description", ->
  247. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "projectDescriptionUpdated", @description).should.equal true
  248. describe "deleteProject", ->
  249. beforeEach ->
  250. @err = "errro"
  251. @ProjectDeleter.deleteProject = sinon.stub().callsArgWith(1, @err)
  252. it "should call the project handler", (done)->
  253. @EditorController.deleteProject @project_id, (err)=>
  254. err.should.equal @err
  255. @ProjectDeleter.deleteProject.calledWith(@project_id).should.equal true
  256. done()
  257. describe "renameEntity", ->
  258. beforeEach (done) ->
  259. @entity_id = "entity_id_here"
  260. @entityType = "doc"
  261. @newName = "bobsfile.tex"
  262. @ProjectEntityUpdateHandler.renameEntity = sinon.stub().yields()
  263. @EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, @user_id, done
  264. it "should call the project handler", ->
  265. @ProjectEntityUpdateHandler.renameEntity
  266. .calledWith(@project_id, @entity_id, @entityType, @newName, @user_id)
  267. .should.equal true
  268. it "should emit the update to the room", ->
  269. @EditorRealTimeController.emitToRoom
  270. .calledWith(@project_id, 'reciveEntityRename', @entity_id, @newName)
  271. .should.equal true
  272. describe "moveEntity", ->
  273. beforeEach ->
  274. @entity_id = "entity_id_here"
  275. @entityType = "doc"
  276. @ProjectEntityUpdateHandler.moveEntity = sinon.stub().yields()
  277. @EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, @user_id, @callback
  278. it "should call the ProjectEntityUpdateHandler", ->
  279. @ProjectEntityUpdateHandler.moveEntity
  280. .calledWith(@project_id, @entity_id, @folder_id, @entityType, @user_id)
  281. .should.equal true
  282. it "should emit the update to the room", ->
  283. @EditorRealTimeController.emitToRoom
  284. .calledWith(@project_id, 'reciveEntityMove', @entity_id, @folder_id)
  285. .should.equal true
  286. it "calls the callback", ->
  287. @callback.called.should.equal true
  288. describe "renameProject", ->
  289. beforeEach ->
  290. @err = "errro"
  291. @newName = "new name here"
  292. @EditorController.renameProject @project_id, @newName, @callback
  293. it "should call the EditorController", ->
  294. @ProjectDetailsHandler.renameProject.calledWith(@project_id, @newName).should.equal true
  295. it "should emit the update to the room", ->
  296. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'projectNameUpdated', @newName).should.equal true
  297. describe "setCompiler", ->
  298. beforeEach ->
  299. @compiler = "latex"
  300. @EditorController.setCompiler @project_id, @compiler, @callback
  301. it "should send the new compiler and project id to the project options handler", ->
  302. @ProjectOptionsHandler.setCompiler
  303. .calledWith(@project_id, @compiler)
  304. .should.equal true
  305. @EditorRealTimeController.emitToRoom
  306. .calledWith(@project_id, "compilerUpdated", @compiler)
  307. .should.equal true
  308. describe "setImageName", ->
  309. beforeEach ->
  310. @imageName = "texlive-1234.5"
  311. @EditorController.setImageName @project_id, @imageName, @callback
  312. it "should send the new imageName and project id to the project options handler", ->
  313. @ProjectOptionsHandler.setImageName
  314. .calledWith(@project_id, @imageName)
  315. .should.equal true
  316. @EditorRealTimeController.emitToRoom
  317. .calledWith(@project_id, "imageNameUpdated", @imageName)
  318. .should.equal true
  319. describe "setSpellCheckLanguage", ->
  320. beforeEach ->
  321. @languageCode = "fr"
  322. @EditorController.setSpellCheckLanguage @project_id, @languageCode, @callback
  323. it "should send the new languageCode and project id to the project options handler", ->
  324. @ProjectOptionsHandler.setSpellCheckLanguage
  325. .calledWith(@project_id, @languageCode)
  326. .should.equal true
  327. @EditorRealTimeController.emitToRoom
  328. .calledWith(@project_id, "spellCheckLanguageUpdated", @languageCode)
  329. .should.equal true
  330. describe "setPublicAccessLevel", ->
  331. describe 'when setting to private', ->
  332. beforeEach ->
  333. @newAccessLevel = 'private'
  334. @ProjectDetailsHandler.ensureTokensArePresent = sinon.stub().yields(null, @tokens)
  335. @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, @callback
  336. it 'should set the access level', ->
  337. @ProjectDetailsHandler.setPublicAccessLevel
  338. .calledWith(@project_id, @newAccessLevel)
  339. .should.equal true
  340. it 'should broadcast the access level change', ->
  341. @EditorRealTimeController.emitToRoom
  342. .calledWith(@project_id, 'project:publicAccessLevel:changed')
  343. .should.equal true
  344. it 'should not ensure tokens are present for project', ->
  345. @ProjectDetailsHandler.ensureTokensArePresent
  346. .calledWith(@project_id)
  347. .should.equal false
  348. it 'should not broadcast a token change', ->
  349. @EditorRealTimeController.emitToRoom
  350. .calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
  351. .should.equal false
  352. describe 'when setting to tokenBased', ->
  353. beforeEach ->
  354. @newAccessLevel = 'tokenBased'
  355. @tokens = {readOnly: 'aaa', readAndWrite: '42bbb'}
  356. @ProjectDetailsHandler.ensureTokensArePresent = sinon.stub().yields(null, @tokens)
  357. @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, @callback
  358. it 'should set the access level', ->
  359. @ProjectDetailsHandler.setPublicAccessLevel
  360. .calledWith(@project_id, @newAccessLevel)
  361. .should.equal true
  362. it 'should broadcast the access level change', ->
  363. @EditorRealTimeController.emitToRoom
  364. .calledWith(@project_id, 'project:publicAccessLevel:changed')
  365. .should.equal true
  366. it 'should ensure tokens are present for project', ->
  367. @ProjectDetailsHandler.ensureTokensArePresent
  368. .calledWith(@project_id)
  369. .should.equal true
  370. it 'should broadcast the token change too', ->
  371. @EditorRealTimeController.emitToRoom
  372. .calledWith(@project_id, 'project:tokens:changed', {tokens: @tokens})
  373. .should.equal true
  374. describe "setRootDoc", ->
  375. beforeEach ->
  376. @newRootDocID = "21312321321"
  377. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().yields()
  378. @EditorController.setRootDoc @project_id, @newRootDocID, @callback
  379. it "should call the ProjectEntityUpdateHandler", ->
  380. @ProjectEntityUpdateHandler.setRootDoc
  381. .calledWith(@project_id, @newRootDocID)
  382. .should.equal true
  383. it "should emit the update to the room", ->
  384. @EditorRealTimeController.emitToRoom
  385. .calledWith(@project_id, 'rootDocUpdated', @newRootDocID)
  386. .should.equal true