EditorControllerTests.coffee 29 KB

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