EditorControllerTests.coffee 28 KB

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