EditorControllerTests.coffee 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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(4)
  343. it 'should send the document to the documentUpdaterHandler', (done)->
  344. @DocumentUpdaterHandler.setDocument = sinon.stub().withArgs(@project_id, @doc_id, @docLines, @source).callsArg(4)
  345. @EditorController.setDoc @project_id, @doc_id, @docLines, @source, (err)->
  346. done()
  347. it 'should send the new doc lines to the doucment updater', (done)->
  348. @DocumentUpdaterHandler.setDocument = ->
  349. mock = sinon.mock(@DocumentUpdaterHandler).expects("setDocument").withArgs(@project_id, @doc_id, @docLines, @source).once().callsArg(4)
  350. @EditorController.setDoc @project_id, @doc_id, @docLines, @source, (err)=>
  351. mock.verify()
  352. done()
  353. it 'should flush the doc to mongo', (done)->
  354. @EditorController.setDoc @project_id, @doc_id, @docLines, @source, (err)=>
  355. @DocumentUpdaterHandler.flushDocToMongo.calledWith(@project_id, @doc_id).should.equal true
  356. done()
  357. describe 'addDoc', ->
  358. beforeEach ->
  359. @ProjectEntityHandler.addDoc = ()->
  360. @EditorRealTimeController.emitToRoom = sinon.stub()
  361. @project_id = "12dsankj"
  362. @folder_id = "213kjd"
  363. @doc = {_id:"123ds"}
  364. @folder_id = "123ksajdn"
  365. @docName = "doc.tex"
  366. @docLines = ["1234","dskl"]
  367. it 'should add the doc using the project entity handler', (done)->
  368. mock = sinon.mock(@ProjectEntityHandler).expects("addDoc").withArgs(@project_id, @folder_id, @docName, @docLines).callsArg(4)
  369. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, ->
  370. mock.verify()
  371. done()
  372. it 'should send the update out to the users in the project', (done)->
  373. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  374. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, =>
  375. @EditorRealTimeController.emitToRoom
  376. .calledWith(@project_id, "reciveNewDoc", @folder_id, @doc, @source)
  377. .should.equal true
  378. done()
  379. it 'should return the doc to the callback', (done) ->
  380. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  381. @EditorController.addDoc @project_id, @folder_id, @docName, @docLines, @source, (error, doc) =>
  382. doc.should.equal @doc
  383. done()
  384. describe 'addFile :', ->
  385. beforeEach ->
  386. @ProjectEntityHandler.addFile = ->
  387. @EditorRealTimeController.emitToRoom = sinon.stub()
  388. @project_id = "12dsankj"
  389. @folder_id = "213kjd"
  390. @fileName = "file.png"
  391. @folder_id = "123ksajdn"
  392. @file = {_id:"dasdkjk"}
  393. @stream = new ArrayBuffer()
  394. it 'should add the folder using the project entity handler', (done)->
  395. mock = sinon.mock(@ProjectEntityHandler).expects("addFile").withArgs(@project_id).callsArg(4)
  396. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, =>
  397. mock.verify()
  398. done()
  399. it 'should send the update of a new folder out to the users in the project', (done)->
  400. @ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
  401. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, =>
  402. @EditorRealTimeController.emitToRoom
  403. .calledWith(@project_id, "reciveNewFile", @folder_id, @file, @source)
  404. .should.equal true
  405. done()
  406. it "should return the file in the callback", (done) ->
  407. @ProjectEntityHandler.addFile = sinon.stub().callsArgWith(4, null, @file, @folder_id)
  408. @EditorController.addFile @project_id, @folder_id, @fileName, @stream, @source, (error, file) =>
  409. file.should.equal @file
  410. done()
  411. describe "replaceFile", ->
  412. beforeEach ->
  413. @project_id = "12dsankj"
  414. @file_id = "file_id_here"
  415. @fsPath = "/folder/file.png"
  416. it 'should send the replace file message to the editor controller', (done)->
  417. @ProjectEntityHandler.replaceFile = sinon.stub().callsArgWith(3)
  418. @EditorController.replaceFile @project_id, @file_id, @fsPath, @source, =>
  419. @ProjectEntityHandler.replaceFile.calledWith(@project_id, @file_id, @fsPath).should.equal true
  420. done()
  421. describe 'addFolder :', ->
  422. beforeEach ->
  423. @ProjectEntityHandler.addFolder = ->
  424. @EditorRealTimeController.emitToRoom = sinon.stub()
  425. @project_id = "12dsankj"
  426. @folder_id = "213kjd"
  427. @folderName = "folder"
  428. @folder = {_id:"123ds"}
  429. it 'should add the folder using the project entity handler', (done)->
  430. mock = sinon.mock(@ProjectEntityHandler).expects("addFolder").withArgs(@project_id, @folder_id, @folderName).callsArg(3)
  431. @EditorController.addFolder @project_id, @folder_id, @folderName, @source, ->
  432. mock.verify()
  433. done()
  434. it 'should notifyProjectUsersOfNewFolder', (done)->
  435. @ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
  436. mock = sinon.mock(@EditorController.p).expects('notifyProjectUsersOfNewFolder').withArgs(@project_id, @folder_id, @folder).callsArg(3)
  437. @EditorController.addFolder @project_id, @folder_id, @folderName, @source, ->
  438. mock.verify()
  439. done()
  440. it 'notifyProjectUsersOfNewFolder should send update out to all users', (done)->
  441. @EditorController.p.notifyProjectUsersOfNewFolder @project_id, @folder_id, @folder, =>
  442. @EditorRealTimeController.emitToRoom
  443. .calledWith(@project_id, "reciveNewFolder", @folder_id, @folder)
  444. .should.equal true
  445. done()
  446. it 'should return the folder in the callback', (done) ->
  447. @ProjectEntityHandler.addFolder = (project_id, folder_id, folderName, callback)=> callback(null, @folder, @folder_id)
  448. @EditorController.addFolder @project_id, @folder_id, @folderName, @source, (error, folder) =>
  449. folder.should.equal @folder
  450. done()
  451. describe 'mkdirp :', ->
  452. it 'should make the dirs and notifyProjectUsersOfNewFolder', (done)->
  453. path = "folder1/folder2"
  454. @folder1 = {_id:"folder_1_id_here"}
  455. @folder2 = {_id:"folder_2_id_here", parentFolder_id:@folder1._id}
  456. @folder3 = {_id:"folder_3_id_here", parentFolder_id:@folder2._id}
  457. @ProjectEntityHandler.mkdirp = sinon.stub().withArgs(@project_id, path).callsArgWith(2, null, [@folder1, @folder2, @folder3], @folder3)
  458. @EditorController.p.notifyProjectUsersOfNewFolder = sinon.stub().callsArg(3)
  459. @EditorController.mkdirp @project_id, path, (err, newFolders, lastFolder)=>
  460. @EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder1._id, @folder2).should.equal true
  461. @EditorController.p.notifyProjectUsersOfNewFolder.calledWith(@project_id, @folder2._id, @folder3).should.equal true
  462. newFolders.should.deep.equal [@folder1, @folder2, @folder3]
  463. lastFolder.should.equal @folder3
  464. done()
  465. describe 'deleteEntity', ->
  466. beforeEach ->
  467. @ProjectEntityHandler.deleteEntity = (project_id, entity_id, type, callback)-> callback()
  468. @entity_id = "entity_id_here"
  469. @type = "doc"
  470. @EditorRealTimeController.emitToRoom = sinon.stub()
  471. it 'should delete the folder using the project entity handler', (done)->
  472. mock = sinon.mock(@ProjectEntityHandler).expects("deleteEntity").withArgs(@project_id, @entity_id, @type).callsArg(3)
  473. @EditorController.deleteEntity @project_id, @entity_id, @type, @source, ->
  474. mock.verify()
  475. done()
  476. it 'notify users an entity has been deleted', (done)->
  477. @EditorController.deleteEntity @project_id, @entity_id, @type, @source, =>
  478. @EditorRealTimeController.emitToRoom
  479. .calledWith(@project_id, "removeEntity", @entity_id, @source)
  480. .should.equal true
  481. done()
  482. describe "getting a list of project paths", ->
  483. it 'should call the project entity handler to get an array of docs', (done)->
  484. fullDocsHash =
  485. "/doc1.tex":{lines:["das"], _id:"1234"}
  486. "/doc2.tex":{lines:["dshajkh"]}
  487. project_id = "d312nkjnajn"
  488. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, fullDocsHash)
  489. @EditorController.getListOfDocPaths project_id, (err, returnedDocs)->
  490. returnedDocs.length.should.equal 2
  491. returnedDocs[0]._id.should.equal "1234"
  492. assert.equal returnedDocs[0].lines, undefined
  493. returnedDocs[1].path.should.equal "doc2.tex"
  494. done()
  495. describe "forceResyncOfDropbox", ->
  496. it 'should tell the project entity handler to flush to tpds', (done)->
  497. @ProjectEntityHandler.flushProjectToThirdPartyDataStore = sinon.stub().callsArgWith(1)
  498. @EditorController.forceResyncOfDropbox @project_id, (err)=>
  499. @ProjectEntityHandler.flushProjectToThirdPartyDataStore.calledWith(@project_id).should.equal true
  500. done()
  501. describe "notifyUsersProjectHasBeenDeletedOrRenamed", ->
  502. it 'should emmit a message to all users in a project', (done)->
  503. @EditorRealTimeController.emitToRoom = sinon.stub()
  504. @EditorController.notifyUsersProjectHasBeenDeletedOrRenamed @project_id, (err)=>
  505. @EditorRealTimeController.emitToRoom
  506. .calledWith(@project_id, "projectRenamedOrDeletedByExternalSource")
  507. .should.equal true
  508. done()
  509. describe "updateProjectDescription", ->
  510. beforeEach ->
  511. @description = "new description"
  512. @EditorRealTimeController.emitToRoom = sinon.stub()
  513. it "should send the new description to the project details handler", (done)->
  514. @ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
  515. @EditorController.updateProjectDescription @project_id, @description, =>
  516. @ProjectDetailsHandler.setProjectDescription.calledWith(@project_id, @description).should.equal true
  517. done()
  518. it "should notify the other clients about the updated description", (done)->
  519. @ProjectDetailsHandler.setProjectDescription.callsArgWith(2)
  520. @EditorController.updateProjectDescription @project_id, @description, =>
  521. @EditorRealTimeController.emitToRoom.calledWith(@project_id, "projectDescriptionUpdated", @description).should.equal true
  522. done()
  523. describe "deleteProject", ->
  524. beforeEach ->
  525. @err = "errro"
  526. @ProjectDeleter.deleteProject = sinon.stub().callsArgWith(1, @err)
  527. it "should call the project handler", (done)->
  528. @EditorController.deleteProject @project_id, (err)=>
  529. err.should.equal @err
  530. @ProjectDeleter.deleteProject.calledWith(@project_id).should.equal true
  531. done()
  532. describe "renameEntity", ->
  533. beforeEach ->
  534. @err = "errro"
  535. @entity_id = "entity_id_here"
  536. @entityType = "doc"
  537. @newName = "bobsfile.tex"
  538. @ProjectEntityHandler.renameEntity = sinon.stub().callsArgWith(4, @err)
  539. @EditorRealTimeController.emitToRoom = sinon.stub()
  540. it "should call the project handler", (done)->
  541. @EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  542. @ProjectEntityHandler.renameEntity.calledWith(@project_id, @entity_id, @entityType, @newName).should.equal true
  543. done()
  544. it "should emit the update to the room", (done)->
  545. @EditorController.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  546. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityRename', @entity_id, @newName).should.equal true
  547. done()
  548. describe "moveEntity", ->
  549. beforeEach ->
  550. @err = "errro"
  551. @entity_id = "entity_id_here"
  552. @entityType = "doc"
  553. @folder_id = "313dasd21dasdsa"
  554. @ProjectEntityHandler.moveEntity = sinon.stub().callsArgWith(4, @err)
  555. @EditorRealTimeController.emitToRoom = sinon.stub()
  556. it "should call the ProjectEntityHandler", (done)->
  557. @EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, =>
  558. @ProjectEntityHandler.moveEntity.calledWith(@project_id, @entity_id, @folder_id, @entityType).should.equal true
  559. done()
  560. it "should emit the update to the room", (done)->
  561. @EditorController.moveEntity @project_id, @entity_id, @folder_id, @entityType, =>
  562. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'reciveEntityMove', @entity_id, @folder_id).should.equal true
  563. done()
  564. describe "renameProject", ->
  565. beforeEach ->
  566. @err = "errro"
  567. @window_id = "kdsjklj290jlk"
  568. @newName = "new name here"
  569. @ProjectDetailsHandler.renameProject = sinon.stub().callsArgWith(2, @err)
  570. @EditorRealTimeController.emitToRoom = sinon.stub()
  571. it "should call the EditorController", (done)->
  572. @EditorController.renameProject @project_id, @newName, =>
  573. @ProjectDetailsHandler.renameProject.calledWith(@project_id, @newName).should.equal true
  574. done()
  575. it "should emit the update to the room", (done)->
  576. @EditorController.renameProject @project_id, @newName, =>
  577. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'projectNameUpdated', @newName).should.equal true
  578. done()
  579. describe "setPublicAccessLevel", ->
  580. beforeEach ->
  581. @newAccessLevel = "public"
  582. @ProjectDetailsHandler.setPublicAccessLevel = sinon.stub().callsArgWith(2, null)
  583. @EditorRealTimeController.emitToRoom = sinon.stub()
  584. it "should call the EditorController", (done)->
  585. @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
  586. @ProjectDetailsHandler.setPublicAccessLevel.calledWith(@project_id, @newAccessLevel).should.equal true
  587. done()
  588. it "should emit the update to the room", (done)->
  589. @EditorController.setPublicAccessLevel @project_id, @newAccessLevel, =>
  590. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'publicAccessLevelUpdated', @newAccessLevel).should.equal true
  591. done()
  592. describe "setRootDoc", ->
  593. beforeEach ->
  594. @newRootDocID = "21312321321"
  595. @ProjectEntityHandler.setRootDoc = sinon.stub().callsArgWith(2, null)
  596. @EditorRealTimeController.emitToRoom = sinon.stub()
  597. it "should call the ProjectEntityHandler", (done)->
  598. @EditorController.setRootDoc @project_id, @newRootDocID, =>
  599. @ProjectEntityHandler.setRootDoc.calledWith(@project_id, @newRootDocID).should.equal true
  600. done()
  601. it "should emit the update to the room", (done)->
  602. @EditorController.setRootDoc @project_id, @newRootDocID, =>
  603. @EditorRealTimeController.emitToRoom.calledWith(@project_id, 'rootDocUpdated', @newRootDocID).should.equal true
  604. done()