EditorControllerTests.coffee 18 KB

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