EditorControllerTests.coffee 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Editor/EditorController'
  5. MockClient = require "../helpers/MockClient"
  6. assert = require('assert')
  7. describe "EditorController", ->
  8. beforeEach ->
  9. @project_id = "test-project-id"
  10. @project =
  11. _id: @project_id
  12. owner_ref:{_id:"something"}
  13. @doc_id = "test-doc-id"
  14. @projectModelView = "projectModelView"
  15. @user =
  16. _id: @user_id = "user-id"
  17. projects: {}
  18. @rooms = {}
  19. @io =
  20. sockets :
  21. clients : (room_id) =>
  22. @rooms[room_id]
  23. @DocumentUpdaterHandler = {}
  24. @ProjectOptionsHandler =
  25. setCompiler : sinon.spy()
  26. setSpellCheckLanguage: sinon.spy()
  27. @ProjectEntityHandler =
  28. flushProjectToThirdPartyDataStore:sinon.stub()
  29. @ProjectEditorHandler =
  30. buildProjectModelView : sinon.stub().returns(@projectModelView)
  31. @Project =
  32. findPopulatedById: sinon.stub().callsArgWith(1, null, @project)
  33. @LimitationsManager = {}
  34. @AuthorizationManager = {}
  35. @AutomaticSnapshotManager = {}
  36. @VersioningApiHandler =
  37. enableVersioning : sinon.stub().callsArg(1)
  38. @client = new MockClient()
  39. @settings =
  40. apis:{thirdPartyDataStore:{emptyProjectFlushDelayMiliseconds:0.5}}
  41. redis: web:{}
  42. @dropboxProjectLinker = {}
  43. @callback = sinon.stub()
  44. @TpdsPollingBackgroundTasks = {}
  45. @ProjectDetailsHandler =
  46. setProjectDescription:sinon.stub()
  47. @CollaboratorsHandler =
  48. removeUserFromProject: sinon.stub().callsArgWith(2)
  49. addUserToProject: sinon.stub().callsArgWith(3)
  50. @ProjectDeleter =
  51. deleteProject: sinon.stub()
  52. @EditorController = SandboxedModule.require modulePath, requires:
  53. "../../infrastructure/Server" : io : @io
  54. '../Project/ProjectEditorHandler' : @ProjectEditorHandler
  55. '../Project/ProjectEntityHandler' : @ProjectEntityHandler
  56. '../Project/ProjectOptionsHandler' : @ProjectOptionsHandler
  57. '../Project/ProjectDetailsHandler': @ProjectDetailsHandler
  58. '../Project/ProjectDeleter' : @ProjectDeleter
  59. '../Project/ProjectGetter' : @ProjectGetter = {}
  60. '../Collaborators/CollaboratorsHandler': @CollaboratorsHandler
  61. '../DocumentUpdater/DocumentUpdaterHandler' : @DocumentUpdaterHandler
  62. '../Subscription/LimitationsManager' : @LimitationsManager
  63. '../Security/AuthorizationManager' : @AuthorizationManager
  64. "../Versioning/AutomaticSnapshotManager" : @AutomaticSnapshotManager
  65. "../Versioning/VersioningApiHandler" : @VersioningApiHandler
  66. '../../models/Project' : Project: @Project
  67. "settings-sharelatex":@settings
  68. '../Dropbox/DropboxProjectLinker':@dropboxProjectLinker
  69. '../ThirdPartyDataStore/TpdsPollingBackgroundTasks':@TpdsPollingBackgroundTasks
  70. './EditorRealTimeController':@EditorRealTimeController = {}
  71. "../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() }
  72. "../TrackChanges/TrackChangesManager": @TrackChangesManager = {}
  73. 'redis':createClient:-> auth:->
  74. "logger-sharelatex": @logger =
  75. log: sinon.stub()
  76. err: sinon.stub()
  77. describe "joinProject", ->
  78. beforeEach ->
  79. sinon.spy(@client, "set")
  80. sinon.spy(@client, "get")
  81. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  82. @ProjectGetter.populateProjectWithUsers = sinon.stub().callsArgWith(1, null, @project)
  83. @AuthorizationManager.setPrivilegeLevelOnClient = sinon.stub()
  84. describe "when authorized", ->
  85. beforeEach ->
  86. @AuthorizationManager.getPrivilegeLevelForProject =
  87. sinon.stub().callsArgWith(2, null, true, "owner")
  88. @EditorController.joinProject(@client, @user, @project_id, @callback)
  89. it "should find the project without doc lines", ->
  90. @ProjectGetter.getProjectWithoutDocLines
  91. .calledWith(@project_id)
  92. .should.equal true
  93. it "should populate the user references in the project", ->
  94. @ProjectGetter.populateProjectWithUsers
  95. .calledWith(@project)
  96. .should.equal true
  97. it "should set the privilege level on the client", ->
  98. @AuthorizationManager.setPrivilegeLevelOnClient
  99. .calledWith(@client, "owner")
  100. .should.equal.true
  101. it "should add the client to the project channel", ->
  102. @client.join.calledWith(@project_id).should.equal true
  103. it "should set the project_id of the client", ->
  104. @client.set.calledWith("project_id", @project_id).should.equal true
  105. it "should return the project model view, privilege level and protocol version", ->
  106. @callback.calledWith(null, @projectModelView, "owner", @EditorController.protocolVersion).should.equal true
  107. it "should enable versioning", ->
  108. @VersioningApiHandler.enableVersioning.calledWith(@project)
  109. .should.equal true
  110. describe "when not authorized", ->
  111. beforeEach ->
  112. @AuthorizationManager.getPrivilegeLevelForProject =
  113. sinon.stub().callsArgWith(2, null, false)
  114. @EditorController.joinProject(@client, @user, @project_id, @callback)
  115. it "should not set the privilege level on the client", ->
  116. @AuthorizationManager.setPrivilegeLevelOnClient
  117. .called.should.equal false
  118. it "should not add the client to the project channel", ->
  119. @client.join.called.should.equal false
  120. it "should not set the project_id of the client", ->
  121. @client.set.called.should.equal false
  122. it "should return an error", ->
  123. @callback.calledWith(sinon.match.truthy).should.equal true
  124. describe "leaveProject", ->
  125. beforeEach ->
  126. sinon.stub(@client, "set")
  127. sinon.stub(@client, "get").callsArgWith(1, null, @project_id)
  128. @EditorRealTimeController.emitToRoom = sinon.stub()
  129. @EditorController.flushProjectIfEmpty = sinon.stub()
  130. @EditorController.leaveProject @client, @user
  131. it "should call the flush project if empty function", ->
  132. @EditorController.flushProjectIfEmpty
  133. .calledWith(@project_id)
  134. .should.equal true
  135. it "should emit a clientDisconnect to the project room", ->
  136. @EditorRealTimeController.emitToRoom
  137. .calledWith(@project_id, "clientTracking.clientDisconnected", @client.id)
  138. .should.equal true
  139. describe "joinDoc", ->
  140. beforeEach ->
  141. @client.join = sinon.stub()
  142. @client.set("user_id", @user_id)
  143. @fromVersion = 40
  144. @docLines = ["foo", "bar"]
  145. @ops = ["mock-op-1", "mock-op-2"]
  146. @version = 42
  147. @DocumentUpdaterHandler.getDocument = sinon.stub().callsArgWith(3, null, @docLines, @version, @ops)
  148. describe "with a fromVersion", ->
  149. beforeEach ->
  150. @EditorController.joinDoc @client, @project_id, @doc_id, @fromVersion, @callback
  151. it "should add the client to the socket.io room for the doc", ->
  152. @client.join.calledWith(@doc_id).should.equal true
  153. it "should get the document", ->
  154. @DocumentUpdaterHandler.getDocument
  155. .calledWith(@project_id, @doc_id, @fromVersion)
  156. .should.equal true
  157. it "should return the doclines and version and ops", ->
  158. @callback.calledWith(null, @docLines, @version, @ops).should.equal true
  159. it "should increment the join-doc metric", ->
  160. @Metrics.inc.calledWith("editor.join-doc").should.equal true
  161. it "should log out the request", ->
  162. @logger.log
  163. .calledWith(user_id: @user_id, project_id: @project_id, doc_id: @doc_id, "user joining doc")
  164. .should.equal true
  165. describe "without a fromVersion", ->
  166. beforeEach ->
  167. @EditorController.joinDoc @client, @project_id, @doc_id, @callback
  168. it "should get the document with fromVersion=-1", ->
  169. @DocumentUpdaterHandler.getDocument
  170. .calledWith(@project_id, @doc_id, -1)
  171. .should.equal true
  172. it "should return the doclines and version and ops", ->
  173. @callback.calledWith(null, @docLines, @version, @ops).should.equal true
  174. describe "leaveDoc", ->
  175. beforeEach ->
  176. @client.leave = sinon.stub()
  177. @client.set("user_id", @user_id)
  178. @EditorController.leaveDoc @client, @project_id, @doc_id, @callback
  179. it "should remove the client from the socket.io room for the doc", ->
  180. @client.leave.calledWith(@doc_id).should.equal true
  181. it "should increment the leave-doc metric", ->
  182. @Metrics.inc.calledWith("editor.leave-doc").should.equal true
  183. it "should log out the request", ->
  184. @logger.log
  185. .calledWith(user_id: @user_id, project_id: @project_id, doc_id: @doc_id, "user leaving doc")
  186. .should.equal true
  187. describe "flushProjectIfEmpty", ->
  188. beforeEach ->
  189. @DocumentUpdaterHandler.flushProjectToMongoAndDelete = sinon.stub()
  190. @TrackChangesManager.flushProject = sinon.stub()
  191. describe "when a project has no more users", ->
  192. it "should do the flush after the config set timeout to ensure that a reconect didn't just happen", (done)->
  193. @rooms[@project_id] = []
  194. @EditorController.flushProjectIfEmpty @project_id, =>
  195. @DocumentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal(true)
  196. @TrackChangesManager.flushProject.calledWith(@project_id).should.equal true
  197. done()
  198. describe "when a project still has connected users", ->
  199. it "should not flush the project", (done)->
  200. @rooms[@project_id] = ["socket-id-1", "socket-id-2"]
  201. @EditorController.flushProjectIfEmpty @project_id, =>
  202. @DocumentUpdaterHandler.flushProjectToMongoAndDelete.calledWith(@project_id).should.equal(false)
  203. @TrackChangesManager.flushProject.calledWith(@project_id).should.equal false
  204. done()
  205. describe "updateClientPosition", ->
  206. beforeEach ->
  207. @EditorRealTimeController.emitToRoom = sinon.stub()
  208. @update = {
  209. doc_id: @doc_id = "doc-id-123"
  210. row: @row = 42
  211. column: @column = 37
  212. }
  213. @clientParams = {
  214. project_id: @project_id
  215. first_name: @first_name = "Douglas"
  216. last_name: @last_name = "Adams"
  217. }
  218. @client.get = (param, callback) => callback null, @clientParams[param]
  219. describe "with a logged in user", ->
  220. beforeEach ->
  221. @EditorController.updateClientPosition @client, @update
  222. it "should send the update to the project room with the user's name", ->
  223. @EditorRealTimeController.emitToRoom
  224. .calledWith(@project_id, "clientTracking.clientUpdated", {
  225. doc_id: @doc_id,
  226. id: @client.id
  227. name: "#{@first_name} #{@last_name}"
  228. row: @row
  229. column: @column
  230. })
  231. .should.equal true
  232. describe "with an anonymous user", ->
  233. beforeEach ->
  234. @clientParams.first_name = null
  235. @clientParams.last_name = null
  236. @EditorController.updateClientPosition @client, @update
  237. it "should send the update to the project room with an anonymous name", ->
  238. @EditorRealTimeController.emitToRoom
  239. .calledWith(@project_id, "clientTracking.clientUpdated", {
  240. doc_id: @doc_id,
  241. id: @client.id
  242. name: "Anonymous"
  243. row: @row
  244. column: @column
  245. })
  246. .should.equal true
  247. describe "addUserToProject", ->
  248. beforeEach ->
  249. @email = "Jane.Doe@example.com"
  250. @priveleges = "readOnly"
  251. @addedUser = { _id: "added-user" }
  252. @CollaboratorsHandler.addUserToProject = sinon.stub().callsArgWith(3, null, @addedUser)
  253. @EditorRealTimeController.emitToRoom = sinon.stub()
  254. @callback = sinon.stub()
  255. describe "when the project can accept more collaborators", ->
  256. beforeEach ->
  257. @LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, false)
  258. it "should add the user to the project", (done)->
  259. @EditorController.addUserToProject @project_id, @email, @priveleges, =>
  260. @CollaboratorsHandler.addUserToProject.calledWith(@project_id, @email.toLowerCase(), @priveleges).should.equal true
  261. done()
  262. it "should emit a userAddedToProject event", (done)->
  263. @EditorController.addUserToProject @project_id, @email, @priveleges, =>
  264. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "userAddedToProject", @addedUser).should.equal true
  265. done()
  266. it "should return true to the callback", (done)->
  267. @EditorController.addUserToProject @project_id, @email, @priveleges, (err, result)=>
  268. result.should.equal true
  269. done()
  270. describe "when the project cannot accept more collaborators", ->
  271. beforeEach ->
  272. @LimitationsManager.isCollaboratorLimitReached = sinon.stub().callsArgWith(1, null, true)
  273. @EditorController.addUserToProject(@project_id, @email, @priveleges, @callback)
  274. it "should not add the user to the project", ->
  275. @CollaboratorsHandler.addUserToProject.called.should.equal false
  276. it "should not emit a userAddedToProject event", ->
  277. @EditorRealTimeController.emitToRoom.called.should.equal false
  278. it "should return false to the callback", ->
  279. @callback.calledWith(null, false).should.equal true
  280. describe "removeUserFromProject", ->
  281. beforeEach ->
  282. @removed_user_id = "removed-user-id"
  283. @CollaboratorsHandler.removeUserFromProject = sinon.stub().callsArgWith(2)
  284. @EditorRealTimeController.emitToRoom = sinon.stub()
  285. @EditorController.removeUserFromProject(@project_id, @removed_user_id)
  286. it "remove the user from the project", ->
  287. @CollaboratorsHandler.removeUserFromProject
  288. .calledWith(@project_id, @removed_user_id)
  289. .should.equal true
  290. it "should emit a userRemovedFromProject event", ->
  291. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "userRemovedFromProject", @removed_user_id).should.equal true
  292. describe "updating compiler used for project", ->
  293. it "should send the new compiler and project id to the project options handler", (done)->
  294. compiler = "latex"
  295. @EditorController.setCompiler @project_id, compiler, (err)=>
  296. @ProjectOptionsHandler.setCompiler.calledWith(@project_id, compiler).should.equal true
  297. done()
  298. @ProjectOptionsHandler.setCompiler.args[0][2]()
  299. describe "updating language code used for project", ->
  300. it "should send the new languageCode and project id to the project options handler", (done)->
  301. languageCode = "fr"
  302. @EditorController.setSpellCheckLanguage @project_id, languageCode, (err)=>
  303. @ProjectOptionsHandler.setSpellCheckLanguage.calledWith(@project_id, languageCode).should.equal true
  304. done()
  305. @ProjectOptionsHandler.setSpellCheckLanguage.args[0][2]()
  306. describe 'set document', ->
  307. beforeEach ->
  308. @docLines = ["foo", "bar"]
  309. @ProjectEntityHandler.updateDocLines = sinon.stub().callsArg(4)
  310. @DocumentUpdaterHandler.setDocument = sinon.stub().callsArg(3)
  311. @EditorRealTimeController.emitToRoom = sinon.stub()
  312. it 'should send the document to the documentUpdaterHandler', (done)->
  313. @DocumentUpdaterHandler.setDocument = sinon.stub().withArgs(@project_id, @doc_id, @docLines).callsArg(3)
  314. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)->
  315. done()
  316. it 'should send the update to the connected users', (done)->
  317. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)=>
  318. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "entireDocUpdate", @doc_id).should.equal true
  319. done()
  320. it 'should send the new doc lines to the doucment updater', (done)->
  321. @DocumentUpdaterHandler.setDocument = ->
  322. mock = sinon.mock(@DocumentUpdaterHandler).expects("setDocument").withArgs(@project_id, @doc_id, @docLines).once().callsArg(3)
  323. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)=>
  324. mock.verify()
  325. done()
  326. it 'should update the document lines', (done)->
  327. @ProjectEntityHandler.updateDocLines = ->
  328. mock = sinon.mock(@ProjectEntityHandler).expects("updateDocLines").withArgs(@project_id, @doc_id, @docLines).once().callsArg(4)
  329. @EditorController.setDoc @project_id, @doc_id, @docLines, (err)->
  330. mock.verify()
  331. done()
  332. describe 'add doc', ->
  333. beforeEach ->
  334. @ProjectEntityHandler.addDoc = ()->
  335. @EditorRealTimeController.emitToRoom = sinon.stub()
  336. @project_id = "12dsankj"
  337. @folder_id = "213kjd"
  338. @doc = {_id:"123ds"}
  339. @folder_id = "123ksajdn"
  340. @docName = "doc.tex"
  341. @docLines = ["1234","dskl"]
  342. it 'should add the doc using the project entity handler', (done)->
  343. mock = sinon.mock(@ProjectEntityHandler).expects("addDoc").withArgs(@project_id, @folder_id, @docName, @docLines).callsArg(5)
  344. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, "", ->
  345. mock.verify()
  346. done()
  347. it 'should send the update out to the users in the project', (done)->
  348. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(5, null, @doc, @folder_id)
  349. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, "", =>
  350. @EditorRealTimeController.emitToRoom
  351. .calledWith(@project_id, "reciveNewDoc", @folder_id, @doc)
  352. .should.equal true
  353. done()
  354. it 'should return the doc to the callback', (done) ->
  355. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(5, null, @doc, @folder_id)
  356. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, "", (error, doc) =>
  357. doc.should.equal @doc
  358. done()
  359. describe 'addFile :', ->
  360. beforeEach ->
  361. @ProjectEntityHandler.addFile = ->
  362. @EditorRealTimeController.emitToRoom = sinon.stub()
  363. @project_id = "12dsankj"
  364. @folder_id = "213kjd"
  365. @fileName = "file.png"
  366. @folder_id = "123ksajdn"
  367. @file = {_id:"dasdkjk"}
  368. @stream = new ArrayBuffer()
  369. it 'should add the folder using the project entity handler', (done)->
  370. mock = sinon.mock(@ProjectEntityHandler).expects("addFile").withArgs(@project_id).callsArg(4)
  371. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, =>
  372. mock.verify()
  373. done()
  374. it 'should send the update of a new folder out to the users in the project', (done)->
  375. @ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
  376. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, =>
  377. @EditorRealTimeController.emitToRoom
  378. .calledWith(@project_id, "reciveNewFile", @folder_id, @file)
  379. .should.equal true
  380. done()
  381. it "should return the file in the callback", (done) ->
  382. @ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
  383. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, (error, file) =>
  384. file.should.equal @file
  385. done()
  386. describe "replaceFile", ->
  387. beforeEach ->
  388. @project_id = "12dsankj"
  389. @file_id = "file_id_here"
  390. @fsPath = "/folder/file.png"
  391. it 'should send the replace file message to the editor controller', (done)->
  392. @ProjectEntityHandler.replaceFile = sinon.stub().callsArgWith(3)
  393. @EditorController.replaceFile @project_id, @file_id, @fsPath, =>
  394. @ProjectEntityHandler.replaceFile.calledWith(@project_id, @file_id, @fsPath).should.equal true
  395. done()
  396. describe 'addFolder :', ->
  397. beforeEach ->
  398. @ProjectEntityHandler.addFolder = ->
  399. @EditorRealTimeController.emitToRoom = sinon.stub()
  400. @project_id = "12dsankj"
  401. @folder_id = "213kjd"
  402. @folderName = "folder"
  403. @folder = {_id:"123ds"}
  404. it 'should add the folder using the project entity handler', (done)->
  405. mock = sinon.mock(@ProjectEntityHandler).expects("addFolder").withArgs(@project_id, @folder_id, @folderName).callsArg(3)
  406. @EditorController.addFolder @project_id, @folder_id, @folderName, ->
  407. mock.verify()
  408. done()
  409. it 'should notifyProjectUsersOfNewFolder', (done)->
  410. @ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
  411. mock = sinon.mock(@EditorController.p).expects('notifyProjectUsersOfNewFolder').withArgs(@project_id, @folder_id, @folder).callsArg(3)
  412. @EditorController.addFolder @project_id, @folder_id, @folderName, ->
  413. mock.verify()
  414. done()
  415. it 'notifyProjectUsersOfNewFolder should send update out to all users', (done)->
  416. @EditorController.p.notifyProjectUsersOfNewFolder @project_id, @folder_id, @folder, =>
  417. @EditorRealTimeController.emitToRoom
  418. .calledWith(@project_id, "reciveNewFolder", @folder_id, @folder)
  419. .should.equal true
  420. done()
  421. it 'should return the folder in the callback', (done) ->
  422. @ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
  423. @EditorController.addFolder @project_id, @folder_id, @folderName, (error, folder) =>
  424. folder.should.equal @folder
  425. done()
  426. describe 'mkdirp :', ->
  427. it 'should make the dirs and notifyProjectUsersOfNewFolder', (done)->
  428. path = "folder1/folder2"
  429. @folder1 = {_id:"folder_1_id_here"}
  430. @folder2 = {_id:"folder_2_id_here", parentFolder_id:@folder1._id}
  431. @folder3 = {_id:"folder_3_id_here", parentFolder_id:@folder2._id}
  432. @ProjectEntityHandler.mkdirp = sinon.stub().withArgs(@project_id, path).callsArgWith(2, null, [@folder1, @folder2, @folder3], @folder3)
  433. @EditorController.p.notifyProjectUsersOfNewFolder = sinon.stub().callsArg(3)
  434. @EditorController.mkdirp @project_id, path, (err, newFolders, lastFolder)=>
  435. @EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder1._id, @folder2).should.equal true
  436. @EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder2._id, @folder3).should.equal true
  437. newFolders.should.deep.equal [@folder1, @folder2, @folder3]
  438. lastFolder.should.equal @folder3
  439. done()
  440. describe 'deleteEntity', ->
  441. beforeEach ->
  442. @ProjectEntityHandler.deleteEntity = (project_id, entity_id, type, callback)-> callback()
  443. @entity_id = "entity_id_here"
  444. @type = "doc"
  445. @EditorRealTimeController.emitToRoom = sinon.stub()
  446. it 'should delete the folder using the project entity handler', (done)->
  447. mock = sinon.mock(@ProjectEntityHandler).expects("deleteEntity").withArgs(@project_id, @entity_id, @type).callsArg(3)
  448. @EditorController.deleteEntity @project_id, @entity_id, @type, ->
  449. mock.verify()
  450. done()
  451. it 'notify users an entity has been deleted', (done)->
  452. @EditorController.deleteEntity @project_id, @entity_id, @type, =>
  453. @EditorRealTimeController.emitToRoom
  454. .calledWith(@project_id, "removeEntity", @entity_id)
  455. .should.equal true
  456. done()
  457. describe "getting a list of project paths", ->
  458. it 'should call the project entity handler to get an array of docs', (done)->
  459. fullDocsHash =
  460. "/doc1.tex":{lines:["das"], _id:"1234"}
  461. "/doc2.tex":{lines:["dshajkh"]}
  462. project_id = "d312nkjnajn"
  463. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, fullDocsHash)
  464. @EditorController.getListOfDocPaths project_id, (err, returnedDocs)->
  465. returnedDocs.length.should.equal 2
  466. returnedDocs[0]._id.should.equal "1234"
  467. assert.equal returnedDocs[0].lines, undefined
  468. returnedDocs[1].path.should.equal "doc2.tex"
  469. done()
  470. describe "forceResyncOfDropbox", ->
  471. it 'should tell the project entity handler to flush to tpds', (done)->
  472. @ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArgWith(1)
  473. @EditorController.forceResyncOfDropbox @project_id, (err)=>
  474. @ProjectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(@project_id).should.equal true
  475. done()
  476. describe "notifyUsersProjectHasBeenDeletedOrRenamed", ->
  477. it 'should emmit a message to all users in a project', (done)->
  478. @EditorRealTimeController.emitToRoom = sinon.stub()
  479. @EditorController.notifyUsersProjectHasBeenDeletedOrRenamed @project_id, (err)=>
  480. @EditorRealTimeController.emitToRoom
  481. .calledWith(@project_id, "projectRenamedOrDeletedByExternalSource")
  482. .should.equal true
  483. done()
  484. describe "gettingTimeOfLastTpdsPoll", ->
  485. it "should ask the tpdsPollingBackgroundTask", (done)->
  486. date = new Date()
  487. @TpdsPollingBackgroundTasks.getLastTimePollHappned = sinon.stub().callsArgWith(0, null, date)
  488. @EditorController.getLastTimePollHappned (err, lastTimePollHappened)->
  489. lastTimePollHappened.should.equal date
  490. done()
  491. describe "updateProjectDescription", ->
  492. beforeEach ->
  493. @description = "new description"
  494. @EditorRealTimeController.emitToRoom = sinon.stub()
  495. it "should send the new description to the project details handler", (done)->
  496. @ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
  497. @EditorController.updateProjectDescription @project_id, @description, =>
  498. @ProjectDetailsHandler.setProjectDescription.calledWith(@project_id, @description).should.equal true
  499. done()
  500. it "should notify the other clients about the updated description", (done)->
  501. @ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
  502. @EditorController.updateProjectDescription @project_id, @description, =>
  503. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "projectDescriptionUpdated", @description).should.equal true
  504. done()
  505. describe "deleteProject", ->
  506. beforeEach ->
  507. @err = "errro"
  508. @ProjectDeleter.deleteProject = sinon.stub().callsArgWith(1, @err)
  509. it "should call the project handler", (done)->
  510. @EditorController.deleteProject @project_id, (err)=>
  511. err.should.equal @err
  512. @ProjectDeleter.deleteProject.calledWith(@project_id).should.equal true
  513. done()
  514. describe "renameEntity", ->
  515. beforeEach ->
  516. @err = "errro"
  517. @entity_id = "entity_id_here"
  518. @entityType = "doc"
  519. @newName = "bobsfile.tex"
  520. @ProjectEntityHandler.renameEntity = sinon.stub().callsArgWith(4, @err)
  521. @EditorRealTimeController.emitToRoom = sinon.stub()
  522. it "should call the project handler", (done)->
  523. @EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  524. @ProjectEntityHandler.renameEntity.calledWith(@project_id, @entity_id, @entityType, @newName).should.equal true
  525. done()
  526. it "should emit the update to the room", (done)->
  527. @EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  528. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityRename', @entity_id, @newName).should.equal true
  529. done()
  530. describe "moveEntity", ->
  531. beforeEach ->
  532. @err = "errro"
  533. @entity_id = "entity_id_here"
  534. @entityType = "doc"
  535. @folder_id = "313dasd21dasdsa"
  536. @ProjectEntityHandler.moveEntity = sinon.stub().callsArgWith(4, @err)
  537. @EditorRealTimeController.emitToRoom = sinon.stub()
  538. it "should call the ProjectEntityHandler", (done)->
  539. @EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, =>
  540. @ProjectEntityHandler.moveEntity.calledWith(@project_id, @entity_id, @folder_id, @entityType).should.equal true
  541. done()
  542. it "should emit the update to the room", (done)->
  543. @EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, =>
  544. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityMove', @entity_id, @folder_id).should.equal true
  545. done()
  546. describe "renameProject", ->
  547. beforeEach ->
  548. @err = "errro"
  549. @window_id = "kdsjklj290jlk"
  550. @newName = "new name here"
  551. @ProjectDetailsHandler.renameProject = sinon.stub().callsArgWith(2, @err)
  552. @EditorRealTimeController.emitToRoom = sinon.stub()
  553. it "should call the EditorController", (done)->
  554. @EditorController.renameProject @project_id, @newName, =>
  555. @ProjectDetailsHandler.renameProject.calledWith(@project_id, @newName).should.equal true
  556. done()
  557. it "should emit the update to the room", (done)->
  558. @EditorController.renameProject @project_id, @newName, =>
  559. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'projectNameUpdated', @newName).should.equal true
  560. done()
  561. describe "setPublicAccessLevel", ->
  562. beforeEach ->
  563. @err = "errro"
  564. @newAccessLevel = "public"
  565. @ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, @err)
  566. @EditorRealTimeController.emitToRoom = sinon.stub()
  567. it "should call the EditorController", (done)->
  568. @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
  569. @ProjectDetailsHandler.setPublicAccessLevel.calledWith(@project_id, @newAccessLevel).should.equal true
  570. done()
  571. it "should emit the update to the room", (done)->
  572. @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
  573. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'publicAccessLevelUpdated', @newAccessLevel).should.equal true
  574. done()
  575. describe "setRootDoc", ->
  576. beforeEach ->
  577. @err = "errro"
  578. @newRootDocID = "21312321321"
  579. @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2, @err)
  580. @EditorRealTimeController.emitToRoom = sinon.stub()
  581. it "should call the ProjectEntityHandler", (done)->
  582. @EditorController.setRootDoc @project_id, @newRootDocID, =>
  583. @ProjectEntityHandler.setRootDoc.calledWith(@project_id, @newRootDocID).should.equal true
  584. done()
  585. it "should emit the update to the room", (done)->
  586. @EditorController.setRootDoc @project_id, @newRootDocID, =>
  587. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'rootDocUpdated', @newRootDocID).should.equal true
  588. done()