ProjectEntityHandlerTests.coffee 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. chai = require('chai')
  2. assert = require('chai').assert
  3. should = chai.should()
  4. expect = chai.expect
  5. sinon = require 'sinon'
  6. modulePath = "../../../../app/js/Features/Project/ProjectEntityHandler"
  7. SandboxedModule = require('sandboxed-module')
  8. ObjectId = require("mongoose").Types.ObjectId
  9. tk = require 'timekeeper'
  10. Errors = require "../../../../app/js/Features/Errors/Errors"
  11. describe 'ProjectEntityHandler', ->
  12. project_id = '4eecb1c1bffa66588e0000a1'
  13. doc_id = '4eecb1c1bffa66588e0000a2'
  14. folder_id = "4eecaffcbffa66588e000008"
  15. rootFolderId = "4eecaffcbffa66588e000007"
  16. userId = 1234
  17. beforeEach ->
  18. @fileUrl = 'filestore.example.com/file'
  19. @FileStoreHandler =
  20. uploadFileFromDisk: sinon.stub().callsArgWith(3, null, @fileUrl)
  21. copyFile: sinon.stub().callsArgWith(4, null, @fileUrl)
  22. @tpdsUpdateSender =
  23. addDoc:sinon.stub().callsArg(1)
  24. addFile:sinon.stub().callsArg(1)
  25. addFolder:sinon.stub().callsArg(1)
  26. @rootFolder =
  27. _id:rootFolderId,
  28. folders:[
  29. {name:"level1", folders:[]}
  30. ]
  31. @ProjectUpdateStub = sinon.stub()
  32. @ProjectModel = class Project
  33. constructor:(options)->
  34. @._id = project_id
  35. @name = "project_name_here"
  36. @rev = 0
  37. save:(callback)->callback()
  38. rootFolder:[@rootFolder]
  39. @ProjectModel.update = @ProjectUpdateStub
  40. @DocModel = class Doc
  41. constructor:(options)->
  42. {@name, @lines} = options
  43. @_id = doc_id
  44. @rev = 0
  45. @FileModel = class File
  46. constructor:(options)->
  47. {@name} = options
  48. @._id = "file_id"
  49. @rev = 0
  50. @FolderModel = class Folder
  51. constructor:(options)->
  52. {@name} = options
  53. @project = new @ProjectModel()
  54. @project.rootFolder = [@rootFolder]
  55. @ProjectModel.findById = (project_id, callback)=> callback(null, @project)
  56. @ProjectModel.getProject = (project_id, fields, callback)=> callback(null, @project)
  57. @ProjectGetter =
  58. getProjectWithOnlyFolders : (project_id, callback)=> callback(null, @project)
  59. getProjectWithoutDocLines : (project_id, callback)=> callback(null, @project)
  60. getProject:sinon.stub()
  61. @projectUpdater = markAsUpdated:sinon.stub()
  62. @projectLocator =
  63. findElement : sinon.stub()
  64. @settings =
  65. maxEntitiesPerProject:200
  66. @documentUpdaterHandler =
  67. updateProjectStructure: sinon.stub().callsArg(6)
  68. deleteDoc: sinon.stub().callsArg(2)
  69. @ProjectEntityHandler = SandboxedModule.require modulePath, requires:
  70. '../../models/Project': Project:@ProjectModel
  71. '../../models/Doc': Doc:@DocModel
  72. '../../models/Folder': Folder:@FolderModel
  73. '../../models/File': File:@FileModel
  74. '../FileStore/FileStoreHandler':@FileStoreHandler
  75. '../ThirdPartyDataStore/TpdsUpdateSender':@tpdsUpdateSender
  76. './ProjectLocator': @projectLocator
  77. '../../Features/DocumentUpdater/DocumentUpdaterHandler':@documentUpdaterHandler
  78. '../Docstore/DocstoreManager': @DocstoreManager = {}
  79. 'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
  80. './ProjectUpdateHandler': @projectUpdater
  81. "./ProjectGetter": @ProjectGetter
  82. "settings-sharelatex":@settings
  83. "../Cooldown/CooldownManager": @CooldownManager = {}
  84. describe 'mkdirp', ->
  85. beforeEach ->
  86. @parentFolder_id = "1jnjknjk"
  87. @newFolder = {_id:"newFolder_id_here"}
  88. @lastFolder = {_id:"123das", folders:[]}
  89. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  90. @projectLocator.findElementByPath = (project_id, path, cb)=>
  91. @parentFolder = {_id:"parentFolder_id_here"}
  92. lastFolder = path.substring(path.lastIndexOf("/"))
  93. if lastFolder.indexOf("level1") == -1
  94. cb "level1 is not the last foler "
  95. else
  96. cb null, @parentFolder
  97. @ProjectEntityHandler.addFolder = (project_id, parentFolder_id, folderName, callback)=>
  98. callback null, {name:folderName}, @parentFolder_id
  99. it 'should return the root folder if the path is just a slash', (done)->
  100. path = "/"
  101. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  102. lastFolder.should.deep.equal @rootFolder
  103. assert.equal lastFolder.parentFolder_id, undefined
  104. done()
  105. it 'should make just one folder', (done)->
  106. path = "/differentFolder/"
  107. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  108. folders.length.should.equal 1
  109. lastFolder.name.should.equal "differentFolder"
  110. lastFolder.parentFolder_id.should.equal @parentFolder_id
  111. done()
  112. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  113. path = "level1/level2"
  114. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  115. folders.length.should.equal 1
  116. lastFolder.name.should.equal "level2"
  117. lastFolder.parentFolder_id.should.equal @parentFolder_id
  118. done()
  119. it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
  120. path = "level1/level2/level3"
  121. @ProjectEntityHandler.mkdirp project_id, path,(err, folders, lastFolder) =>
  122. folders.length.should.equal 2
  123. folders[0].name.should.equal "level2"
  124. folders[0].parentFolder_id.should.equal @parentFolder_id
  125. lastFolder.name.should.equal "level3"
  126. lastFolder.parentFolder_id.should.equal @parentFolder_id
  127. done()
  128. it 'should work with slashes either side', (done)->
  129. path = "/level1/level2/level3/"
  130. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  131. folders.length.should.equal 2
  132. folders[0].name.should.equal "level2"
  133. folders[0].parentFolder_id.should.equal @parentFolder_id
  134. lastFolder.name.should.equal "level3"
  135. lastFolder.parentFolder_id.should.equal @parentFolder_id
  136. done()
  137. describe 'deleteEntity', ->
  138. entity_id = "4eecaffcbffa66588e000009"
  139. beforeEach ->
  140. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  141. @tpdsUpdateSender.deleteEntity = sinon.stub().callsArg(1)
  142. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
  143. @ProjectEntityHandler._cleanUpEntity = sinon.stub().callsArg(3)
  144. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  145. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: entity_id }, @path)
  146. describe "deleting from Mongo", ->
  147. beforeEach (done) ->
  148. @ProjectEntityHandler.deleteEntity project_id, entity_id, @type = 'file', done
  149. it "should retreive the path", ->
  150. @projectLocator.findElement.called.should.equal true
  151. options = @projectLocator.findElement.args[0][0]
  152. options.type.should.equal @type
  153. options.project.should.equal @project
  154. options.element_id.should.equal entity_id
  155. it "should remove the element from the database", ->
  156. @ProjectEntityHandler._removeElementFromMongoArray.calledWith(@ProjectModel, project_id, @path.mongo).should.equal true
  157. it "should call the third party data store", ->
  158. options = @tpdsUpdateSender.deleteEntity.args[0][0]
  159. options.project_id.should.equal project_id
  160. options.path.should.equal @path.fileSystem
  161. it "should clean up the entity from the rest of the system", ->
  162. @ProjectEntityHandler._cleanUpEntity
  163. .calledWith(@project, @entity, @type)
  164. .should.equal true
  165. describe "_cleanUpEntity", ->
  166. beforeEach ->
  167. @entity_id = "4eecaffcbffa66588e000009"
  168. @FileStoreHandler.deleteFile = sinon.stub().callsArg(2)
  169. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  170. describe "a file", ->
  171. beforeEach (done) ->
  172. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'file', done
  173. it "should delete the file from FileStoreHandler", ->
  174. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal true
  175. it "should not attempt to delete from the document updater", ->
  176. @documentUpdaterHandler.deleteDoc.called.should.equal false
  177. describe "a doc", ->
  178. beforeEach (done) ->
  179. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(2)
  180. @ProjectEntityHandler._cleanUpEntity @project, @entity = {_id: @entity_id}, 'doc', done
  181. it "should clean up the doc", ->
  182. @ProjectEntityHandler._cleanUpDoc
  183. .calledWith(@project, @entity)
  184. .should.equal true
  185. describe "a folder", ->
  186. beforeEach (done) ->
  187. @folder =
  188. folders: [
  189. fileRefs: [ @file1 = {_id: "file-id-1" } ]
  190. docs: [ @doc1 = { _id: "doc-id-1" } ]
  191. folders: []
  192. ]
  193. fileRefs: [ @file2 = { _id: "file-id-2" } ]
  194. docs: [ @doc2 = { _id: "doc-id-2" } ]
  195. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(2)
  196. @ProjectEntityHandler._cleanUpFile = sinon.stub().callsArg(2)
  197. @ProjectEntityHandler._cleanUpEntity @project, @folder, "folder", done
  198. it "should clean up all sub files", ->
  199. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file1).should.equal true
  200. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file2).should.equal true
  201. it "should clean up all sub docs", ->
  202. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc1).should.equal true
  203. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc2).should.equal true
  204. describe 'moveEntity', ->
  205. beforeEach ->
  206. @pathAfterMove = {
  207. fileSystem: "/somewhere/else.txt"
  208. }
  209. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArgWith(3, null, @project)
  210. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, path: @pathAfterMove)
  211. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  212. @tpdsUpdateSender.moveEntity = sinon.stub()
  213. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  214. @ProjectEntityHandler.getAllEntitiesFromProject
  215. .onFirstCall()
  216. .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  217. @ProjectEntityHandler.getAllEntitiesFromProject
  218. .onSecondCall()
  219. .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  220. describe "moving a doc", ->
  221. beforeEach (done) ->
  222. @docId = "4eecaffcbffa66588e000009"
  223. @doc = {lines:["1234","312343d"], rev: "1234"}
  224. @path = {
  225. mongo:"folders[0]"
  226. fileSystem:"/old_folder/somewhere.txt"
  227. }
  228. @destFolder = { name: "folder" }
  229. @destFolderPath = {
  230. mongo: "folders[0]"
  231. fileSystem: "/dest_folder"
  232. }
  233. @projectLocator.findElement = sinon.stub()
  234. @projectLocator.findElement.withArgs({project: @project, element_id: @docId, type: 'docs'})
  235. .callsArgWith(1, null, @doc, @path)
  236. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", userId, done
  237. it 'should find the doc to move', ->
  238. @projectLocator.findElement.calledWith({element_id: @docId, type: "docs", project: @project }).should.equal true
  239. it "should should send the update to the doc updater", ->
  240. @documentUpdaterHandler.updateProjectStructure
  241. .calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
  242. .should.equal true
  243. it 'should remove the element from its current position', ->
  244. @ProjectEntityHandler._removeElementFromMongoArray
  245. .calledWith(@ProjectModel, project_id, @path.mongo ).should.equal true
  246. it "should put the element back in the new folder", ->
  247. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc, "docs").should.equal true
  248. it 'should tell the third party data store', ->
  249. @tpdsUpdateSender.moveEntity
  250. .calledWith({
  251. project_id: project_id,
  252. startPath: @path.fileSystem
  253. endPath: @pathAfterMove.fileSystem
  254. project_name: @project.name
  255. rev: @doc.rev
  256. })
  257. .should.equal true
  258. describe "moving a folder", ->
  259. beforeEach ->
  260. @folder_id = "folder-to-move"
  261. @move_to_folder_id = "folder-to-move-to"
  262. @folder = { name: "folder" }
  263. @folder_to_move_to = { name: "folder to move to" }
  264. @path = { mongo:"folders[0]" }
  265. @pathToMoveTo = { mongo: "folders[0]" }
  266. @projectLocator.findElement = sinon.stub()
  267. @projectLocator.findElement.withArgs({project: @project, element_id: @folder_id, type: 'folder'})
  268. .callsArgWith(1, null, @folder, @path)
  269. @projectLocator.findElement.withArgs({project: @project, element_id: @move_to_folder_id, type: 'folder'})
  270. .callsArgWith(1, null, @folder_to_move_to, @pathToMoveTo)
  271. describe "when the destination folder is outside the moving folder", ->
  272. beforeEach (done) ->
  273. @path.fileSystem = "/one/src_dir"
  274. @pathToMoveTo.fileSystem = "/two/dest_dir"
  275. @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", userId, done
  276. it 'should find the project then element', ->
  277. @projectLocator.findElement
  278. .calledWith({
  279. element_id: @folder_id,
  280. type: "folder",
  281. project: @project
  282. })
  283. .should.equal true
  284. it "should should send the update to the doc updater", ->
  285. @documentUpdaterHandler.updateProjectStructure
  286. .calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
  287. .should.equal true
  288. it 'should remove the element from its current position', ->
  289. @ProjectEntityHandler._removeElementFromMongoArray
  290. .calledWith(
  291. @ProjectModel,
  292. project_id,
  293. @path.mongo
  294. )
  295. .should.equal true
  296. it "should put the element back in the new folder", ->
  297. @ProjectEntityHandler._putElement
  298. .calledWith(
  299. @project,
  300. @move_to_folder_id,
  301. @folder,
  302. "folder"
  303. )
  304. .should.equal true
  305. it 'should tell the third party data store', ->
  306. @tpdsUpdateSender.moveEntity
  307. .calledWith({
  308. project_id: project_id,
  309. startPath: @path.fileSystem
  310. endPath: @pathAfterMove.fileSystem
  311. project_name: @project.name,
  312. rev: @folder.rev
  313. })
  314. .should.equal true
  315. describe "when the destination folder is inside the moving folder", ->
  316. beforeEach ->
  317. @path.fileSystem = "/one/two"
  318. @pathToMoveTo.fileSystem = "/one/two/three"
  319. @callback = sinon.stub()
  320. @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", userId, @callback
  321. it 'should find the folder we are moving to element', ->
  322. @projectLocator.findElement
  323. .calledWith({
  324. element_id: @move_to_folder_id,
  325. type: "folder",
  326. project: @project
  327. })
  328. .should.equal true
  329. it "should return an error", ->
  330. @callback
  331. .calledWith(new Error("destination folder is a child folder of me"))
  332. .should.equal true
  333. describe '_removeElementFromMongoArray ', ->
  334. beforeEach ->
  335. @mongoPath = "folders[0].folders[5]"
  336. @id = "12344"
  337. @project = 'a project'
  338. @ProjectModel.update = sinon.stub().callsArg(3)
  339. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  340. @ProjectEntityHandler._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @callback
  341. it 'should unset', ->
  342. update = { '$unset': { } }
  343. update['$unset'][@mongoPath] = 1
  344. @ProjectModel.update
  345. .calledWith({ _id: @id }, update, {})
  346. .should.equal true
  347. it 'should pull', ->
  348. @ProjectModel.findOneAndUpdate
  349. .calledWith({ _id: @id }, { '$pull': { 'folders[0]': null } }, {'new': true})
  350. .should.equal true
  351. it 'should call the callback', ->
  352. @callback.calledWith(null, @project).should.equal true
  353. describe 'getDoc', ->
  354. beforeEach ->
  355. @lines = ["mock", "doc", "lines"]
  356. @rev = 5
  357. @version = 42
  358. @ranges = {"mock": "ranges"}
  359. @DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
  360. describe 'without pathname option', ->
  361. beforeEach ->
  362. @ProjectEntityHandler.getDoc project_id, doc_id, @callback
  363. it "should call the docstore", ->
  364. @DocstoreManager.getDoc
  365. .calledWith(project_id, doc_id)
  366. .should.equal true
  367. it "should call the callback with the lines, version and rev", ->
  368. @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
  369. describe 'with pathname option', ->
  370. beforeEach ->
  371. @project = 'a project'
  372. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  373. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, {}, @path)
  374. @ProjectEntityHandler.getDoc project_id, doc_id, {pathname: true}, @callback
  375. it "should call the project locator", ->
  376. @projectLocator.findElement
  377. .calledWith({project_id: project_id, element_id: doc_id, type: 'doc'})
  378. .should.equal true
  379. it "should call the docstore", ->
  380. @DocstoreManager.getDoc
  381. .calledWith(project_id, doc_id)
  382. .should.equal true
  383. it "should return the pathname if option given", ->
  384. @callback.calledWith(null, @lines, @rev, @version, @ranges, @path.fileSystem).should.equal true
  385. describe 'addDoc', ->
  386. beforeEach ->
  387. @name = "some new doc"
  388. @lines = ['1234','abc']
  389. @path = "/path/to/doc"
  390. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
  391. @callback = sinon.stub()
  392. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  393. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, 0)
  394. @ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, userId, @callback
  395. # Created doc
  396. @doc = @ProjectEntityHandler._putElement.args[0][2]
  397. @doc.name.should.equal @name
  398. expect(@doc.lines).to.be.undefined
  399. it 'should call put element', ->
  400. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc).should.equal true
  401. it 'should return doc and parent folder', ->
  402. @callback.calledWith(null, @doc, folder_id).should.equal true
  403. it 'should call third party data store', ->
  404. @tpdsUpdateSender.addDoc
  405. .calledWith({
  406. project_id: project_id
  407. doc_id: doc_id
  408. path: @path
  409. project_name: @project.name
  410. rev: 0
  411. })
  412. .should.equal true
  413. it "should send the doc lines to the doc store", ->
  414. @DocstoreManager.updateDoc
  415. .calledWith(project_id, @doc._id.toString(), @lines)
  416. .should.equal true
  417. it "should should send the change in project structure to the doc updater", () ->
  418. newDoc =
  419. doc: @doc
  420. path: @path
  421. docLines: @lines.join('\n')
  422. @documentUpdaterHandler.updateProjectStructure
  423. .calledWith(project_id, userId, [], [newDoc], [], [])
  424. .should.equal true
  425. describe "restoreDoc", ->
  426. beforeEach ->
  427. @name = "doc-name"
  428. @lines = ['1234','abc']
  429. @doc = { "mock": "doc" }
  430. @folder_id = "mock-folder-id"
  431. @callback = sinon.stub()
  432. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, null, @lines)
  433. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  434. @ProjectEntityHandler.restoreDoc project_id, doc_id, @name, @callback
  435. it 'should get the doc lines', ->
  436. @ProjectEntityHandler.getDoc
  437. .calledWith(project_id, doc_id, include_deleted: true)
  438. .should.equal true
  439. it "should add a new doc with these doc lines", ->
  440. @ProjectEntityHandler.addDoc
  441. .calledWith(project_id, null, @name, @lines)
  442. .should.equal true
  443. it "should call the callback with the new folder and doc", ->
  444. @callback.calledWith(null, @doc, @folder_id).should.equal true
  445. describe 'addFile', ->
  446. fileName = "something.jpg"
  447. beforeEach ->
  448. @fileSystemPath = "somehintg"
  449. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem: @fileSystemPath}})
  450. @filePath = "somewhere"
  451. it 'should upload it via the FileStoreHandler', (done)->
  452. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  453. file_id.should.equal "file_id"
  454. passedProject_id.should.equal project_id
  455. filePath.should.equal @filePath
  456. done()
  457. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, userId, (err, fileRef, parentFolder)->
  458. it 'should put file into folder by calling put element', (done)->
  459. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  460. passedProject._id.should.equal project_id
  461. passedFolder_id.should.equal folder_id
  462. passedFileRef.name.should.equal fileName
  463. passedType.should.equal 'file'
  464. done()
  465. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  466. it 'should return doc and parent folder', (done)->
  467. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  468. parentFolder.should.equal folder_id
  469. fileRef.name.should.equal fileName
  470. done()
  471. it 'should call third party data store', (done)->
  472. @project.existsInVersioningApi = true
  473. opts =
  474. path : "/somehwere/idsadsds"
  475. project_id : project_id
  476. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  477. @tpdsUpdateSender.addFile = (options)=>
  478. options.project_id.should.equal project_id
  479. options.path.should.equal opts.path
  480. options.project_name.should.equal @project.name
  481. options.file_id.should.not.be.null
  482. options.rev.should.equal 0
  483. done()
  484. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  485. it "should should send the change in project structure to the doc updater", (done) ->
  486. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
  487. passed_project_id.should.equal project_id
  488. passed_user_id.should.equal userId
  489. newFiles.length.should.equal 1
  490. newFile = newFiles[0]
  491. newFile.file.name.should.equal fileName
  492. newFile.path.should.equal @fileSystemPath
  493. newFile.url.should.equal @fileUrl
  494. done()
  495. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, () ->
  496. describe 'replacing a file', ->
  497. beforeEach ->
  498. @projectLocator
  499. @file_id = "file_id_here"
  500. @fsPath = "fs_path_here.png"
  501. @fileRef = {rev:3, _id:@file_id}
  502. @filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
  503. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  504. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  505. @ProjectEntityHandler.getAllEntitiesFromProject
  506. .onFirstCall()
  507. .callsArgWith(1, null, [], @oldFiles = ['old-file'])
  508. @ProjectEntityHandler.getAllEntitiesFromProject
  509. .onSecondCall()
  510. .callsArgWith(1, null, [], @newFiles = ['new-file'])
  511. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  512. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  513. it 'should find the file', (done)->
  514. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  515. @projectLocator.findElement
  516. .calledWith({element_id:@file_id, type:"file", project: @project})
  517. .should.equal true
  518. done()
  519. it 'should tell the file store handler to upload the file from disk', (done)->
  520. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  521. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  522. done()
  523. it 'should send the file to the tpds with an incremented rev', (done)->
  524. @tpdsUpdateSender.addFile = (options)=>
  525. options.project_id.should.equal project_id
  526. options.path.should.equal @filePaths.fileSystem
  527. options.project_name.should.equal @project.name
  528. options.file_id.should.equal @file_id
  529. options.rev.should.equal @fileRef.rev + 1
  530. done()
  531. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  532. it 'should inc the rev id', (done)->
  533. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  534. conditions._id.should.equal project_id
  535. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  536. done()
  537. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  538. it 'should update the created at date', (done)->
  539. d = new Date()
  540. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  541. conditions._id.should.equal project_id
  542. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  543. differenceInMs.should.be.below(20)
  544. done()
  545. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  546. it "should should send the old and new project structure to the doc updater", (done) ->
  547. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  548. @documentUpdaterHandler.updateProjectStructure
  549. .calledWith(project_id, null, [], [], @oldFiles, @newFiles)
  550. .should.equal true
  551. done()
  552. describe 'addFolder', ->
  553. folderName = "folder1234"
  554. beforeEach ->
  555. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  556. it 'should call put element', (done)->
  557. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFolder, passedType, callback)->
  558. passedProject._id.should.equal project_id
  559. passedFolder_id.should.equal folder_id
  560. passedFolder.name.should.equal folderName
  561. passedType.should.equal 'folder'
  562. done()
  563. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  564. it 'should return the folder and parent folder', (done)->
  565. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4)
  566. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  567. parentFolder.should.equal folder_id
  568. folder.name.should.equal folderName
  569. done()
  570. describe 'updateDocLines', ->
  571. beforeEach ->
  572. @lines = ['mock', 'doc', 'lines']
  573. @path = "/somewhere/something.tex"
  574. @doc = {
  575. _id: doc_id
  576. }
  577. @version = 42
  578. @ranges = {"mock":"ranges"}
  579. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  580. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  581. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  582. @projectUpdater.markAsUpdated = sinon.stub()
  583. @callback = sinon.stub()
  584. describe "when the doc has been modified", ->
  585. beforeEach ->
  586. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
  587. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  588. it "should get the project without doc lines", ->
  589. @ProjectGetter.getProjectWithoutDocLines
  590. .calledWith(project_id)
  591. .should.equal true
  592. it "should find the doc", ->
  593. @projectLocator.findElement
  594. .calledWith({
  595. project: @project
  596. type: "docs"
  597. element_id: doc_id
  598. })
  599. .should.equal true
  600. it "should update the doc in the docstore", ->
  601. @DocstoreManager.updateDoc
  602. .calledWith(project_id, doc_id, @lines, @version, @ranges)
  603. .should.equal true
  604. it "should mark the project as updated", ->
  605. @projectUpdater.markAsUpdated
  606. .calledWith(project_id)
  607. .should.equal true
  608. it "should send the doc the to the TPDS", ->
  609. @tpdsUpdateSender.addDoc
  610. .calledWith({
  611. project_id: project_id
  612. project_name: @project.name
  613. doc_id: doc_id
  614. rev: @rev
  615. path: @path
  616. })
  617. .should.equal true
  618. it "should call the callback", ->
  619. @callback.called.should.equal true
  620. describe "when the doc has not been modified", ->
  621. beforeEach ->
  622. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  623. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  624. it "should not mark the project as updated", ->
  625. @projectUpdater.markAsUpdated.called.should.equal false
  626. it "should not send the doc the to the TPDS", ->
  627. @tpdsUpdateSender.addDoc.called.should.equal false
  628. it "should call the callback", ->
  629. @callback.called.should.equal true
  630. describe "when the project is not found", ->
  631. beforeEach ->
  632. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  633. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  634. it "should return a not found error", ->
  635. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  636. describe "when the doc is not found", ->
  637. beforeEach ->
  638. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  639. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  640. it "should log out the error", ->
  641. @logger.error
  642. .calledWith(
  643. project_id: project_id
  644. doc_id: doc_id
  645. lines: @lines
  646. err: new Errors.NotFoundError("doc not found")
  647. "doc not found while updating doc lines"
  648. )
  649. .should.equal true
  650. it "should return a not found error", ->
  651. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  652. describe "getting folders, docs and files", ->
  653. beforeEach ->
  654. @project.rootFolder = [
  655. docs: [@doc1 = {
  656. name : "doc1"
  657. _id : "doc1_id"
  658. }]
  659. fileRefs: [@file1 = {
  660. rev : 1
  661. _id : "file1_id"
  662. name : "file1"
  663. }]
  664. folders: [@folder1 = {
  665. name : "folder1"
  666. docs : [@doc2 = {
  667. name : "doc2"
  668. _id : "doc2_id"
  669. }]
  670. fileRefs : [@file2 = {
  671. rev : 2
  672. name : "file2"
  673. _id : "file2_id"
  674. }]
  675. folders : []
  676. }]
  677. ]
  678. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  679. describe "getAllFolders", ->
  680. beforeEach ->
  681. @callback = sinon.stub()
  682. @ProjectEntityHandler.getAllFolders project_id, @callback
  683. it "should get the project without the docs lines", ->
  684. @ProjectGetter.getProjectWithoutDocLines
  685. .calledWith(project_id)
  686. .should.equal true
  687. it "should call the callback with the folders", ->
  688. @callback
  689. .calledWith(null, {
  690. "/": @project.rootFolder[0]
  691. "/folder1": @folder1
  692. })
  693. .should.equal true
  694. describe "getAllFiles", ->
  695. beforeEach ->
  696. @callback = sinon.stub()
  697. @ProjectEntityHandler.getAllFiles project_id, @callback
  698. it "should call the callback with the files", ->
  699. @callback
  700. .calledWith(null, {
  701. "/file1": @file1
  702. "/folder1/file2": @file2
  703. })
  704. .should.equal true
  705. describe "getAllDocs", ->
  706. beforeEach ->
  707. @docs = [{
  708. _id: @doc1._id
  709. lines: @lines1 = ["one"]
  710. rev: @rev1 = 1
  711. }, {
  712. _id: @doc2._id
  713. lines: @lines2 = ["two"]
  714. rev: @rev2 = 2
  715. }]
  716. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  717. @ProjectEntityHandler.getAllDocs project_id, @callback
  718. it "should get the doc lines and rev from the docstore", ->
  719. @DocstoreManager.getAllDocs
  720. .calledWith(project_id)
  721. .should.equal true
  722. it "should call the callback with the docs with the lines and rev included", ->
  723. @callback
  724. .calledWith(null, {
  725. "/doc1": {
  726. _id: @doc1._id
  727. lines: @lines1
  728. name: @doc1.name
  729. rev: @rev1
  730. }
  731. "/folder1/doc2": {
  732. _id: @doc2._id
  733. lines: @lines2
  734. name: @doc2.name
  735. rev: @rev2
  736. }
  737. })
  738. .should.equal true
  739. describe "getAllFoldersFromProject", ->
  740. beforeEach ->
  741. @callback = sinon.stub()
  742. @ProjectEntityHandler.getAllFoldersFromProject @project, @callback
  743. it "should call the callback with the folders", ->
  744. @callback
  745. .calledWith(null, {
  746. "/": @project.rootFolder[0]
  747. "/folder1": @folder1
  748. })
  749. .should.equal true
  750. describe "getAllDocPathsFromProject", ->
  751. beforeEach ->
  752. @docs = [{
  753. _id: @doc1._id
  754. lines: @lines1 = ["one"]
  755. rev: @rev1 = 1
  756. }, {
  757. _id: @doc2._id
  758. lines: @lines2 = ["two"]
  759. rev: @rev2 = 2
  760. }]
  761. @callback = sinon.stub()
  762. @ProjectEntityHandler.getAllDocPathsFromProject @project, @callback
  763. it "should call the callback with the path for each doc_id", ->
  764. @expected = {}
  765. @expected[@doc1._id] = "/#{@doc1.name}"
  766. @expected[@doc2._id] = "/folder1/#{@doc2.name}"
  767. @callback
  768. .calledWith(null, @expected)
  769. .should.equal true
  770. describe "flushProjectToThirdPartyDataStore", ->
  771. beforeEach (done) ->
  772. @project = {
  773. _id: project_id
  774. name: "Mock project name"
  775. }
  776. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  777. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(1)
  778. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  779. @tpdsUpdateSender.addFile = sinon.stub().callsArg(1)
  780. @docs = {
  781. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  782. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  783. }
  784. @files = {
  785. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  786. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  787. }
  788. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  789. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  790. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  791. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  792. it "should flush the project from the doc updater", ->
  793. @documentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  794. it "should look up the project in mongo", ->
  795. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  796. it "should get all the docs in the project", ->
  797. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  798. it "should get all the files in the project", ->
  799. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  800. it "should flush each doc to the TPDS", ->
  801. for path, doc of @docs
  802. @tpdsUpdateSender.addDoc
  803. .calledWith({
  804. project_id: project_id,
  805. doc_id: doc._id
  806. project_name: @project.name
  807. rev: doc.rev
  808. path: path
  809. })
  810. .should.equal true
  811. it "should flush each file to the TPDS", ->
  812. for path, file of @files
  813. @tpdsUpdateSender.addFile
  814. .calledWith({
  815. project_id: project_id,
  816. file_id: file._id
  817. project_name: @project.name
  818. rev: file.rev
  819. path: path
  820. })
  821. .should.equal true
  822. describe "setRootDoc", ->
  823. it "should call Project.update", ->
  824. @project_id = "project-id-123234adfs"
  825. @rootDoc_id = "root-doc-id-123123"
  826. @ProjectModel.update = sinon.stub()
  827. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  828. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  829. .should.equal true
  830. describe "unsetRootDoc", ->
  831. it "should call Project.update", ->
  832. @project_id = "project-id-123234adfs"
  833. @rootDoc_id = "root-doc-id-123123"
  834. @ProjectModel.update = sinon.stub()
  835. @ProjectEntityHandler.unsetRootDoc @project_id
  836. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  837. .should.equal true
  838. describe 'copyFileFromExistingProjectWithProject', ->
  839. fileName = "something.jpg"
  840. filePath = "dumpFolder/somewhere/image.jpeg"
  841. oldProject_id = "123kljadas"
  842. oldFileRef = {name:fileName, _id:"oldFileRef"}
  843. beforeEach ->
  844. @fileSystemPath = "somehintg"
  845. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem: @fileSystemPath}})
  846. it 'should copy the file in FileStoreHandler', (done)->
  847. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  848. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  849. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  850. done()
  851. it 'should put file into folder by calling put element', (done)->
  852. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  853. passedProject._id.should.equal project_id
  854. passedFolder_id.should.equal folder_id
  855. passedFileRef.name.should.equal fileName
  856. passedType.should.equal 'file'
  857. done()
  858. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  859. it 'should return doc and parent folder', (done)->
  860. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  861. parentFolder.should.equal folder_id
  862. fileRef.name.should.equal fileName
  863. done()
  864. it 'should call third party data store if versioning is enabled', (done)->
  865. @project.existsInVersioningApi = true
  866. opts =
  867. path : "/somehwere/idsadsds"
  868. project_id : project_id
  869. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  870. @tpdsUpdateSender.addFile = (options)=>
  871. options.project_id.should.equal project_id
  872. options.project_name.should.equal @project.name
  873. options.path.should.equal opts.path
  874. options.file_id.should.not.be.null
  875. options.rev.should.equal 0
  876. done()
  877. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  878. it "should should send the change in project structure to the doc updater", (done) ->
  879. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
  880. passed_project_id.should.equal project_id
  881. #passed_user_id.should.equal userId
  882. newFiles.length.should.equal 1
  883. newFile = newFiles[0]
  884. newFile.file.name.should.equal fileName
  885. newFile.path.should.equal @fileSystemPath
  886. newFile.url.should.equal @fileUrl
  887. done()
  888. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  889. describe "renameEntity", ->
  890. beforeEach ->
  891. @entity_id = "4eecaffcbffa66588e000009"
  892. @entityType = "doc"
  893. @newName = "new.tex"
  894. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  895. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  896. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  897. @ProjectEntityHandler.getAllEntitiesFromProject
  898. .onFirstCall()
  899. .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  900. @ProjectEntityHandler.getAllEntitiesFromProject
  901. .onSecondCall()
  902. .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  903. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  904. @tpdsUpdateSender.moveEntity = sinon.stub()
  905. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  906. @documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(6)
  907. it "should should send the old and new project structure to the doc updater", (done) ->
  908. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  909. @documentUpdaterHandler.updateProjectStructure
  910. .calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
  911. .should.equal true
  912. done()
  913. it "should update the name in mongo", (done)->
  914. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  915. @ProjectModel.findOneAndUpdate.calledWith({_id: project_id}, {"$set":{"mongo.path.name": @newName}}, {"new": true}).should.equal true
  916. done()
  917. it "should send the update to the tpds", (done)->
  918. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  919. @tpdsUpdateSender.moveEntity.calledWith({project_id:project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  920. done()
  921. describe "_insertDeletedDocReference", ->
  922. beforeEach ->
  923. @doc =
  924. _id: ObjectId()
  925. name: "test.tex"
  926. @callback = sinon.stub()
  927. @ProjectModel.update = sinon.stub().callsArgWith(3)
  928. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  929. it "should insert the doc into deletedDocs", ->
  930. @ProjectModel.update
  931. .calledWith({
  932. _id: project_id
  933. }, {
  934. $push: {
  935. deletedDocs: {
  936. _id: @doc._id
  937. name: @doc.name
  938. }
  939. }
  940. })
  941. .should.equal true
  942. it "should call the callback", ->
  943. @callback.called.should.equal true
  944. describe "_cleanUpDoc", ->
  945. beforeEach ->
  946. @project =
  947. _id: ObjectId(project_id)
  948. @doc =
  949. _id: ObjectId()
  950. name: "test.tex"
  951. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  952. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  953. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  954. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  955. @callback = sinon.stub()
  956. describe "when the doc is the root doc", ->
  957. beforeEach ->
  958. @project.rootDoc_id = @doc._id
  959. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  960. it "should unset the root doc", ->
  961. @ProjectEntityHandler.unsetRootDoc
  962. .calledWith(project_id)
  963. .should.equal true
  964. it "should delete the doc in the doc updater", ->
  965. @documentUpdaterHandler.deleteDoc
  966. .calledWith(project_id, @doc._id.toString())
  967. it "should insert the doc into the deletedDocs array", ->
  968. @ProjectEntityHandler._insertDeletedDocReference
  969. .calledWith(@project._id, @doc)
  970. .should.equal true
  971. it "should delete the doc in the doc store", ->
  972. @DocstoreManager.deleteDoc
  973. .calledWith(project_id, @doc._id.toString())
  974. .should.equal true
  975. it "should call the callback", ->
  976. @callback.called.should.equal true
  977. describe "when the doc is not the root doc", ->
  978. beforeEach ->
  979. @project.rootDoc_id = ObjectId()
  980. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  981. it "should not unset the root doc", ->
  982. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  983. it "should call the callback", ->
  984. @callback.called.should.equal true
  985. describe "_putElement", ->
  986. beforeEach ->
  987. @project_id = project_id
  988. @project =
  989. _id: ObjectId(project_id)
  990. rootFolder: [_id:ObjectId()]
  991. @folder =
  992. _id: ObjectId()
  993. name: "someFolder"
  994. @doc =
  995. _id: ObjectId()
  996. name: "new.tex"
  997. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  998. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  999. @projectLocator.findElement.callsArgWith(1, null, @folder, @path)
  1000. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  1001. describe "updating the project", ->
  1002. it "should use the correct mongo path", (done)->
  1003. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
  1004. @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
  1005. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1006. done()
  1007. it "should return the project in the callback", (done)->
  1008. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
  1009. expect(project).to.equal @project
  1010. done()
  1011. it "should add an s onto the type if not included", (done)->
  1012. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
  1013. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1014. done()
  1015. it "should not call update if element is null", (done)->
  1016. @ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
  1017. @ProjectModel.findOneAndUpdate.called.should.equal false
  1018. done()
  1019. it "should default to root folder insert", (done)->
  1020. @ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
  1021. @projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  1022. done()
  1023. it "should error if the element has no _id", (done)->
  1024. doc =
  1025. name:"something"
  1026. @ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
  1027. @ProjectModel.findOneAndUpdate.called.should.equal false
  1028. done()
  1029. describe "_countElements", ->
  1030. beforeEach ->
  1031. @project.rootFolder[0].docs = [{_id:123}, {_id:345}]
  1032. @project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
  1033. @project.rootFolder[0].folders = [
  1034. {
  1035. docs:
  1036. [{_id:123}, {_id:345}, {_id:456}]
  1037. fileRefs:{}
  1038. folders: [
  1039. {
  1040. docs:[_id:1234],
  1041. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  1042. folders:[
  1043. {
  1044. docs:[{_id:321321}, {_id:123213}]
  1045. fileRefs:[{_id:312321}]
  1046. folders:[]
  1047. }
  1048. ]
  1049. }
  1050. ]
  1051. },{
  1052. docs:[{_id:123}, {_id:32131}]
  1053. fileRefs:[]
  1054. folders:[
  1055. {
  1056. docs:[{_id:3123}]
  1057. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  1058. folders:0
  1059. }
  1060. ]
  1061. }
  1062. ]
  1063. it "should return the correct number", (done)->
  1064. @ProjectEntityHandler._countElements @project, (err, count)->
  1065. count.should.equal 26
  1066. done()
  1067. it "should deal with null folders", (done)->
  1068. @project.rootFolder[0].folders[0].folders = undefined
  1069. @ProjectEntityHandler._countElements @project, (err, count)->
  1070. count.should.equal 17
  1071. done()
  1072. it "should deal with null docs", (done)->
  1073. @project.rootFolder[0].folders[0].docs = undefined
  1074. @ProjectEntityHandler._countElements @project, (err, count)->
  1075. count.should.equal 23
  1076. done()
  1077. it "should deal with null fileRefs", (done)->
  1078. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  1079. @ProjectEntityHandler._countElements @project, (err, count)->
  1080. count.should.equal 23
  1081. done()