ProjectEntityHandlerTests.coffee 39 KB

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