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