EditorControllerTests.coffee 29 KB

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