EditorControllerTests.coffee 29 KB

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