EditorControllerTests.coffee 23 KB

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