EditorControllerTests.coffee 18 KB

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