EditorControllerTests.coffee 18 KB

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