EditorControllerTests.coffee 30 KB

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