EditorControllerTests.coffee 36 KB

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