ProjectEntityHandlerTests.coffee 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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 'replaceFile', ->
  497. beforeEach ->
  498. @projectLocator
  499. @file_id = "file_id_here"
  500. @fsPath = "fs_path_here.png"
  501. @fileRef = {rev:3, _id: @file_id, name: @fileName = "fileName"}
  502. @filePaths = {fileSystem: @fileSystemPath="/folder1/file.png", mongo:"folder.1.files.somewhere"}
  503. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  504. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3)
  505. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  506. it 'should find the file', (done)->
  507. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  508. @projectLocator.findElement
  509. .calledWith({element_id:@file_id, type:"file", project: @project})
  510. .should.equal true
  511. done()
  512. it 'should tell the file store handler to upload the file from disk', (done)->
  513. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  514. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  515. done()
  516. it 'should send the file to the tpds with an incremented rev', (done)->
  517. @tpdsUpdateSender.addFile = (options)=>
  518. options.project_id.should.equal project_id
  519. options.path.should.equal @filePaths.fileSystem
  520. options.project_name.should.equal @project.name
  521. options.file_id.should.equal @file_id
  522. options.rev.should.equal @fileRef.rev + 1
  523. done()
  524. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  525. it 'should inc the rev id', (done)->
  526. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  527. conditions._id.should.equal project_id
  528. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  529. done()
  530. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  531. it 'should update the created at date', (done)->
  532. d = new Date()
  533. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  534. conditions._id.should.equal project_id
  535. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  536. differenceInMs.should.be.below(20)
  537. done()
  538. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  539. it "should should send the old and new project structure to the doc updater", (done) ->
  540. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
  541. passed_project_id.should.equal project_id
  542. passed_user_id.should.equal userId
  543. newFiles.length.should.equal 1
  544. newFile = newFiles[0]
  545. newFile.file.name.should.equal @fileName
  546. newFile.path.should.equal @fileSystemPath
  547. newFile.url.should.equal @fileUrl
  548. done()
  549. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  550. describe 'addFolder', ->
  551. folderName = "folder1234"
  552. beforeEach ->
  553. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  554. it 'should call put element', (done)->
  555. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFolder, passedType, callback)->
  556. passedProject._id.should.equal project_id
  557. passedFolder_id.should.equal folder_id
  558. passedFolder.name.should.equal folderName
  559. passedType.should.equal 'folder'
  560. done()
  561. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  562. it 'should return the folder and parent folder', (done)->
  563. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4)
  564. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  565. parentFolder.should.equal folder_id
  566. folder.name.should.equal folderName
  567. done()
  568. describe 'updateDocLines', ->
  569. beforeEach ->
  570. @lines = ['mock', 'doc', 'lines']
  571. @path = "/somewhere/something.tex"
  572. @doc = {
  573. _id: doc_id
  574. }
  575. @version = 42
  576. @ranges = {"mock":"ranges"}
  577. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  578. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  579. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  580. @projectUpdater.markAsUpdated = sinon.stub()
  581. @callback = sinon.stub()
  582. describe "when the doc has been modified", ->
  583. beforeEach ->
  584. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
  585. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  586. it "should get the project without doc lines", ->
  587. @ProjectGetter.getProjectWithoutDocLines
  588. .calledWith(project_id)
  589. .should.equal true
  590. it "should find the doc", ->
  591. @projectLocator.findElement
  592. .calledWith({
  593. project: @project
  594. type: "docs"
  595. element_id: doc_id
  596. })
  597. .should.equal true
  598. it "should update the doc in the docstore", ->
  599. @DocstoreManager.updateDoc
  600. .calledWith(project_id, doc_id, @lines, @version, @ranges)
  601. .should.equal true
  602. it "should mark the project as updated", ->
  603. @projectUpdater.markAsUpdated
  604. .calledWith(project_id)
  605. .should.equal true
  606. it "should send the doc the to the TPDS", ->
  607. @tpdsUpdateSender.addDoc
  608. .calledWith({
  609. project_id: project_id
  610. project_name: @project.name
  611. doc_id: doc_id
  612. rev: @rev
  613. path: @path
  614. })
  615. .should.equal true
  616. it "should call the callback", ->
  617. @callback.called.should.equal true
  618. describe "when the doc has not been modified", ->
  619. beforeEach ->
  620. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  621. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  622. it "should not mark the project as updated", ->
  623. @projectUpdater.markAsUpdated.called.should.equal false
  624. it "should not send the doc the to the TPDS", ->
  625. @tpdsUpdateSender.addDoc.called.should.equal false
  626. it "should call the callback", ->
  627. @callback.called.should.equal true
  628. describe "when the project is not found", ->
  629. beforeEach ->
  630. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  631. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  632. it "should return a not found error", ->
  633. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  634. describe "when the doc is not found", ->
  635. beforeEach ->
  636. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  637. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  638. it "should log out the error", ->
  639. @logger.error
  640. .calledWith(
  641. project_id: project_id
  642. doc_id: doc_id
  643. lines: @lines
  644. err: new Errors.NotFoundError("doc not found")
  645. "doc not found while updating doc lines"
  646. )
  647. .should.equal true
  648. it "should return a not found error", ->
  649. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  650. describe "getting folders, docs and files", ->
  651. beforeEach ->
  652. @project.rootFolder = [
  653. docs: [@doc1 = {
  654. name : "doc1"
  655. _id : "doc1_id"
  656. }]
  657. fileRefs: [@file1 = {
  658. rev : 1
  659. _id : "file1_id"
  660. name : "file1"
  661. }]
  662. folders: [@folder1 = {
  663. name : "folder1"
  664. docs : [@doc2 = {
  665. name : "doc2"
  666. _id : "doc2_id"
  667. }]
  668. fileRefs : [@file2 = {
  669. rev : 2
  670. name : "file2"
  671. _id : "file2_id"
  672. }]
  673. folders : []
  674. }]
  675. ]
  676. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  677. describe "getAllFolders", ->
  678. beforeEach ->
  679. @callback = sinon.stub()
  680. @ProjectEntityHandler.getAllFolders project_id, @callback
  681. it "should get the project without the docs lines", ->
  682. @ProjectGetter.getProjectWithoutDocLines
  683. .calledWith(project_id)
  684. .should.equal true
  685. it "should call the callback with the folders", ->
  686. @callback
  687. .calledWith(null, {
  688. "/": @project.rootFolder[0]
  689. "/folder1": @folder1
  690. })
  691. .should.equal true
  692. describe "getAllFiles", ->
  693. beforeEach ->
  694. @callback = sinon.stub()
  695. @ProjectEntityHandler.getAllFiles project_id, @callback
  696. it "should call the callback with the files", ->
  697. @callback
  698. .calledWith(null, {
  699. "/file1": @file1
  700. "/folder1/file2": @file2
  701. })
  702. .should.equal true
  703. describe "getAllDocs", ->
  704. beforeEach ->
  705. @docs = [{
  706. _id: @doc1._id
  707. lines: @lines1 = ["one"]
  708. rev: @rev1 = 1
  709. }, {
  710. _id: @doc2._id
  711. lines: @lines2 = ["two"]
  712. rev: @rev2 = 2
  713. }]
  714. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  715. @ProjectEntityHandler.getAllDocs project_id, @callback
  716. it "should get the doc lines and rev from the docstore", ->
  717. @DocstoreManager.getAllDocs
  718. .calledWith(project_id)
  719. .should.equal true
  720. it "should call the callback with the docs with the lines and rev included", ->
  721. @callback
  722. .calledWith(null, {
  723. "/doc1": {
  724. _id: @doc1._id
  725. lines: @lines1
  726. name: @doc1.name
  727. rev: @rev1
  728. }
  729. "/folder1/doc2": {
  730. _id: @doc2._id
  731. lines: @lines2
  732. name: @doc2.name
  733. rev: @rev2
  734. }
  735. })
  736. .should.equal true
  737. describe "getAllFoldersFromProject", ->
  738. beforeEach ->
  739. @callback = sinon.stub()
  740. @ProjectEntityHandler.getAllFoldersFromProject @project, @callback
  741. it "should call the callback with the folders", ->
  742. @callback
  743. .calledWith(null, {
  744. "/": @project.rootFolder[0]
  745. "/folder1": @folder1
  746. })
  747. .should.equal true
  748. describe "getAllDocPathsFromProject", ->
  749. beforeEach ->
  750. @docs = [{
  751. _id: @doc1._id
  752. lines: @lines1 = ["one"]
  753. rev: @rev1 = 1
  754. }, {
  755. _id: @doc2._id
  756. lines: @lines2 = ["two"]
  757. rev: @rev2 = 2
  758. }]
  759. @callback = sinon.stub()
  760. @ProjectEntityHandler.getAllDocPathsFromProject @project, @callback
  761. it "should call the callback with the path for each doc_id", ->
  762. @expected = {}
  763. @expected[@doc1._id] = "/#{@doc1.name}"
  764. @expected[@doc2._id] = "/folder1/#{@doc2.name}"
  765. @callback
  766. .calledWith(null, @expected)
  767. .should.equal true
  768. describe "flushProjectToThirdPartyDataStore", ->
  769. beforeEach (done) ->
  770. @project = {
  771. _id: project_id
  772. name: "Mock project name"
  773. }
  774. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  775. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(1)
  776. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  777. @tpdsUpdateSender.addFile = sinon.stub().callsArg(1)
  778. @docs = {
  779. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  780. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  781. }
  782. @files = {
  783. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  784. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  785. }
  786. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  787. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  788. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  789. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  790. it "should flush the project from the doc updater", ->
  791. @documentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  792. it "should look up the project in mongo", ->
  793. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  794. it "should get all the docs in the project", ->
  795. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  796. it "should get all the files in the project", ->
  797. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  798. it "should flush each doc to the TPDS", ->
  799. for path, doc of @docs
  800. @tpdsUpdateSender.addDoc
  801. .calledWith({
  802. project_id: project_id,
  803. doc_id: doc._id
  804. project_name: @project.name
  805. rev: doc.rev
  806. path: path
  807. })
  808. .should.equal true
  809. it "should flush each file to the TPDS", ->
  810. for path, file of @files
  811. @tpdsUpdateSender.addFile
  812. .calledWith({
  813. project_id: project_id,
  814. file_id: file._id
  815. project_name: @project.name
  816. rev: file.rev
  817. path: path
  818. })
  819. .should.equal true
  820. describe "setRootDoc", ->
  821. it "should call Project.update", ->
  822. @project_id = "project-id-123234adfs"
  823. @rootDoc_id = "root-doc-id-123123"
  824. @ProjectModel.update = sinon.stub()
  825. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  826. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  827. .should.equal true
  828. describe "unsetRootDoc", ->
  829. it "should call Project.update", ->
  830. @project_id = "project-id-123234adfs"
  831. @rootDoc_id = "root-doc-id-123123"
  832. @ProjectModel.update = sinon.stub()
  833. @ProjectEntityHandler.unsetRootDoc @project_id
  834. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  835. .should.equal true
  836. describe 'copyFileFromExistingProjectWithProject', ->
  837. fileName = "something.jpg"
  838. filePath = "dumpFolder/somewhere/image.jpeg"
  839. oldProject_id = "123kljadas"
  840. oldFileRef = {name:fileName, _id:"oldFileRef"}
  841. beforeEach ->
  842. @fileSystemPath = "somehintg"
  843. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem: @fileSystemPath}})
  844. it 'should copy the file in FileStoreHandler', (done)->
  845. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  846. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)=>
  847. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  848. done()
  849. it 'should put file into folder by calling put element', (done)->
  850. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  851. passedProject._id.should.equal project_id
  852. passedFolder_id.should.equal folder_id
  853. passedFileRef.name.should.equal fileName
  854. passedType.should.equal 'file'
  855. done()
  856. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  857. it 'should return doc and parent folder', (done)->
  858. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  859. parentFolder.should.equal folder_id
  860. fileRef.name.should.equal fileName
  861. done()
  862. it 'should call third party data store if versioning is enabled', (done)->
  863. @project.existsInVersioningApi = true
  864. opts =
  865. path : "/somehwere/idsadsds"
  866. project_id : project_id
  867. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  868. @tpdsUpdateSender.addFile = (options)=>
  869. options.project_id.should.equal project_id
  870. options.project_name.should.equal @project.name
  871. options.path.should.equal opts.path
  872. options.file_id.should.not.be.null
  873. options.rev.should.equal 0
  874. done()
  875. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  876. it "should should send the change in project structure to the doc updater", (done) ->
  877. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, oldDocs, newDocs, oldFiles, newFiles) =>
  878. passed_project_id.should.equal project_id
  879. passed_user_id.should.equal userId
  880. newFiles.length.should.equal 1
  881. newFile = newFiles[0]
  882. newFile.file.name.should.equal fileName
  883. newFile.path.should.equal @fileSystemPath
  884. newFile.url.should.equal @fileUrl
  885. done()
  886. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  887. describe "renameEntity", ->
  888. beforeEach ->
  889. @entity_id = "4eecaffcbffa66588e000009"
  890. @entityType = "doc"
  891. @newName = "new.tex"
  892. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  893. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  894. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  895. @ProjectEntityHandler.getAllEntitiesFromProject
  896. .onFirstCall()
  897. .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  898. @ProjectEntityHandler.getAllEntitiesFromProject
  899. .onSecondCall()
  900. .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  901. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  902. @tpdsUpdateSender.moveEntity = sinon.stub()
  903. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  904. @documentUpdaterHandler.updateProjectStructure = sinon.stub().callsArg(6)
  905. it "should should send the old and new project structure to the doc updater", (done) ->
  906. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  907. @documentUpdaterHandler.updateProjectStructure
  908. .calledWith(project_id, userId, @oldDocs, @newDocs, @oldFiles, @newFiles)
  909. .should.equal true
  910. done()
  911. it "should update the name in mongo", (done)->
  912. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  913. @ProjectModel.findOneAndUpdate.calledWith({_id: project_id}, {"$set":{"mongo.path.name": @newName}}, {"new": true}).should.equal true
  914. done()
  915. it "should send the update to the tpds", (done)->
  916. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  917. @tpdsUpdateSender.moveEntity.calledWith({project_id:project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  918. done()
  919. describe "_insertDeletedDocReference", ->
  920. beforeEach ->
  921. @doc =
  922. _id: ObjectId()
  923. name: "test.tex"
  924. @callback = sinon.stub()
  925. @ProjectModel.update = sinon.stub().callsArgWith(3)
  926. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  927. it "should insert the doc into deletedDocs", ->
  928. @ProjectModel.update
  929. .calledWith({
  930. _id: project_id
  931. }, {
  932. $push: {
  933. deletedDocs: {
  934. _id: @doc._id
  935. name: @doc.name
  936. }
  937. }
  938. })
  939. .should.equal true
  940. it "should call the callback", ->
  941. @callback.called.should.equal true
  942. describe "_cleanUpDoc", ->
  943. beforeEach ->
  944. @project =
  945. _id: ObjectId(project_id)
  946. @doc =
  947. _id: ObjectId()
  948. name: "test.tex"
  949. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  950. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  951. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  952. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  953. @callback = sinon.stub()
  954. describe "when the doc is the root doc", ->
  955. beforeEach ->
  956. @project.rootDoc_id = @doc._id
  957. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  958. it "should unset the root doc", ->
  959. @ProjectEntityHandler.unsetRootDoc
  960. .calledWith(project_id)
  961. .should.equal true
  962. it "should delete the doc in the doc updater", ->
  963. @documentUpdaterHandler.deleteDoc
  964. .calledWith(project_id, @doc._id.toString())
  965. it "should insert the doc into the deletedDocs array", ->
  966. @ProjectEntityHandler._insertDeletedDocReference
  967. .calledWith(@project._id, @doc)
  968. .should.equal true
  969. it "should delete the doc in the doc store", ->
  970. @DocstoreManager.deleteDoc
  971. .calledWith(project_id, @doc._id.toString())
  972. .should.equal true
  973. it "should call the callback", ->
  974. @callback.called.should.equal true
  975. describe "when the doc is not the root doc", ->
  976. beforeEach ->
  977. @project.rootDoc_id = ObjectId()
  978. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  979. it "should not unset the root doc", ->
  980. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  981. it "should call the callback", ->
  982. @callback.called.should.equal true
  983. describe "_putElement", ->
  984. beforeEach ->
  985. @project_id = project_id
  986. @project =
  987. _id: ObjectId(project_id)
  988. rootFolder: [_id:ObjectId()]
  989. @folder =
  990. _id: ObjectId()
  991. name: "someFolder"
  992. @doc =
  993. _id: ObjectId()
  994. name: "new.tex"
  995. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  996. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  997. @projectLocator.findElement.callsArgWith(1, null, @folder, @path)
  998. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  999. describe "updating the project", ->
  1000. it "should use the correct mongo path", (done)->
  1001. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
  1002. @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
  1003. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1004. done()
  1005. it "should return the project in the callback", (done)->
  1006. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
  1007. expect(project).to.equal @project
  1008. done()
  1009. it "should add an s onto the type if not included", (done)->
  1010. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
  1011. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1012. done()
  1013. it "should not call update if element is null", (done)->
  1014. @ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
  1015. @ProjectModel.findOneAndUpdate.called.should.equal false
  1016. done()
  1017. it "should default to root folder insert", (done)->
  1018. @ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
  1019. @projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  1020. done()
  1021. it "should error if the element has no _id", (done)->
  1022. doc =
  1023. name:"something"
  1024. @ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
  1025. @ProjectModel.findOneAndUpdate.called.should.equal false
  1026. done()
  1027. describe "_countElements", ->
  1028. beforeEach ->
  1029. @project.rootFolder[0].docs = [{_id:123}, {_id:345}]
  1030. @project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
  1031. @project.rootFolder[0].folders = [
  1032. {
  1033. docs:
  1034. [{_id:123}, {_id:345}, {_id:456}]
  1035. fileRefs:{}
  1036. folders: [
  1037. {
  1038. docs:[_id:1234],
  1039. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  1040. folders:[
  1041. {
  1042. docs:[{_id:321321}, {_id:123213}]
  1043. fileRefs:[{_id:312321}]
  1044. folders:[]
  1045. }
  1046. ]
  1047. }
  1048. ]
  1049. },{
  1050. docs:[{_id:123}, {_id:32131}]
  1051. fileRefs:[]
  1052. folders:[
  1053. {
  1054. docs:[{_id:3123}]
  1055. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  1056. folders:0
  1057. }
  1058. ]
  1059. }
  1060. ]
  1061. it "should return the correct number", (done)->
  1062. @ProjectEntityHandler._countElements @project, (err, count)->
  1063. count.should.equal 26
  1064. done()
  1065. it "should deal with null folders", (done)->
  1066. @project.rootFolder[0].folders[0].folders = undefined
  1067. @ProjectEntityHandler._countElements @project, (err, count)->
  1068. count.should.equal 17
  1069. done()
  1070. it "should deal with null docs", (done)->
  1071. @project.rootFolder[0].folders[0].docs = undefined
  1072. @ProjectEntityHandler._countElements @project, (err, count)->
  1073. count.should.equal 23
  1074. done()
  1075. it "should deal with null fileRefs", (done)->
  1076. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  1077. @ProjectEntityHandler._countElements @project, (err, count)->
  1078. count.should.equal 23
  1079. done()