ProjectEntityHandlerTests.coffee 43 KB

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