ProjectEntityHandlerTests.coffee 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. describe 'without pathname option', ->
  341. beforeEach ->
  342. @ProjectEntityHandler.getDoc project_id, doc_id, @callback
  343. it "should call the docstore", ->
  344. @DocstoreManager.getDoc
  345. .calledWith(project_id, doc_id)
  346. .should.equal true
  347. it "should call the callback with the lines, version and rev", ->
  348. @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
  349. describe 'with pathname option', ->
  350. beforeEach ->
  351. @project = 'a project'
  352. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  353. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, {}, @path)
  354. @ProjectEntityHandler.getDoc project_id, doc_id, {pathname: true}, @callback
  355. it "should call the project locator", ->
  356. @projectLocator.findElement
  357. .calledWith({project_id: project_id, element_id: doc_id, type: 'doc'})
  358. .should.equal true
  359. it "should call the docstore", ->
  360. @DocstoreManager.getDoc
  361. .calledWith(project_id, doc_id)
  362. .should.equal true
  363. it "should return the pathname if option given", ->
  364. @callback.calledWith(null, @lines, @rev, @version, @ranges, @path.fileSystem).should.equal true
  365. describe 'addDoc', ->
  366. beforeEach ->
  367. @name = "some new doc"
  368. @lines = ['1234','abc']
  369. @path = "/path/to/doc"
  370. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
  371. @callback = sinon.stub()
  372. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  373. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, 0)
  374. @ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, @callback
  375. # Created doc
  376. @doc = @ProjectEntityHandler._putElement.args[0][2]
  377. @doc.name.should.equal @name
  378. expect(@doc.lines).to.be.undefined
  379. it 'should call put element', ->
  380. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc).should.equal true
  381. it 'should return doc and parent folder', ->
  382. @callback.calledWith(null, @doc, folder_id).should.equal true
  383. it 'should call third party data store', ->
  384. @tpdsUpdateSender.addDoc
  385. .calledWith({
  386. project_id: project_id
  387. doc_id: doc_id
  388. path: @path
  389. project_name: @project.name
  390. rev: 0
  391. })
  392. .should.equal true
  393. it "should send the doc lines to the doc store", ->
  394. @DocstoreManager.updateDoc
  395. .calledWith(project_id, @doc._id.toString(), @lines)
  396. .should.equal true
  397. describe "restoreDoc", ->
  398. beforeEach ->
  399. @name = "doc-name"
  400. @lines = ['1234','abc']
  401. @doc = { "mock": "doc" }
  402. @folder_id = "mock-folder-id"
  403. @callback = sinon.stub()
  404. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, null, @lines)
  405. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  406. @ProjectEntityHandler.restoreDoc project_id, doc_id, @name, @callback
  407. it 'should get the doc lines', ->
  408. @ProjectEntityHandler.getDoc
  409. .calledWith(project_id, doc_id, include_deleted: true)
  410. .should.equal true
  411. it "should add a new doc with these doc lines", ->
  412. @ProjectEntityHandler.addDoc
  413. .calledWith(project_id, null, @name, @lines)
  414. .should.equal true
  415. it "should call the callback with the new folder and doc", ->
  416. @callback.calledWith(null, @doc, @folder_id).should.equal true
  417. describe 'addFile', ->
  418. fileName = "something.jpg"
  419. beforeEach ->
  420. @filePath = "somewhere"
  421. it 'should upload it via the FileStoreHandler', (done)->
  422. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  423. file_id.should.equal "file_id"
  424. passedProject_id.should.equal project_id
  425. filePath.should.equal @filePath
  426. done()
  427. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
  428. it 'should put file into folder by calling put element', (done)->
  429. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  430. passedProject._id.should.equal project_id
  431. passedFolder_id.should.equal folder_id
  432. passedFileRef.name.should.equal fileName
  433. passedType.should.equal 'file'
  434. done()
  435. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  436. it 'should return doc and parent folder', (done)->
  437. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  438. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  439. parentFolder.should.equal folder_id
  440. fileRef.name.should.equal fileName
  441. done()
  442. it 'should call third party data store', (done)->
  443. @project.existsInVersioningApi = true
  444. opts =
  445. path : "/somehwere/idsadsds"
  446. project_id : project_id
  447. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  448. @tpdsUpdateSender.addFile = (options)=>
  449. options.project_id.should.equal project_id
  450. options.path.should.equal opts.path
  451. options.project_name.should.equal @project.name
  452. options.file_id.should.not.be.null
  453. options.rev.should.equal 0
  454. done()
  455. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  456. describe 'replacing a file', ->
  457. beforeEach ->
  458. @projectLocator
  459. @file_id = "file_id_here"
  460. @fsPath = "fs_path_here.png"
  461. @fileRef = {rev:3, _id:@file_id}
  462. @filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
  463. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  464. @ProjectModel.update = (_, __, ___, cb)-> cb()
  465. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  466. it 'should find the file', (done)->
  467. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  468. @projectLocator.findElement.calledWith({element_id:@file_id, type:"file", project_id:project_id}).should.equal true
  469. done()
  470. it 'should tell the file store handler to upload the file from disk', (done)->
  471. @FileStoreHandler.uploadFileFromDisk = sinon.stub().callsArgWith(3)
  472. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  473. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  474. done()
  475. it 'should send the file to the tpds with an incremented rev', (done)->
  476. @tpdsUpdateSender.addFile = (options)=>
  477. options.project_id.should.equal project_id
  478. options.path.should.equal @filePaths.fileSystem
  479. options.project_name.should.equal @project.name
  480. options.file_id.should.equal @file_id
  481. options.rev.should.equal @fileRef.rev + 1
  482. done()
  483. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  484. it 'should inc the rev id', (done)->
  485. @ProjectModel.update = (conditions, update, options, callback)=>
  486. conditions._id.should.equal project_id
  487. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  488. done()
  489. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  490. it 'should update the created at date', (done)->
  491. d = new Date()
  492. @ProjectModel.update = (conditions, update, options, callback)=>
  493. conditions._id.should.equal project_id
  494. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  495. differenceInMs.should.be.below(20)
  496. done()
  497. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  498. describe 'addFolder', ->
  499. folderName = "folder1234"
  500. beforeEach ->
  501. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  502. it 'should call put element', (done)->
  503. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFolder, passedType, callback)->
  504. passedProject._id.should.equal project_id
  505. passedFolder_id.should.equal folder_id
  506. passedFolder.name.should.equal folderName
  507. passedType.should.equal 'folder'
  508. done()
  509. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  510. it 'should return the folder and parent folder', (done)->
  511. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4)
  512. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  513. parentFolder.should.equal folder_id
  514. folder.name.should.equal folderName
  515. done()
  516. describe 'updateDocLines', ->
  517. beforeEach ->
  518. @lines = ['mock', 'doc', 'lines']
  519. @path = "/somewhere/something.tex"
  520. @doc = {
  521. _id: doc_id
  522. }
  523. @version = 42
  524. @ranges = {"mock":"ranges"}
  525. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  526. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  527. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  528. @projectUpdater.markAsUpdated = sinon.stub()
  529. @callback = sinon.stub()
  530. describe "when the doc has been modified", ->
  531. beforeEach ->
  532. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
  533. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  534. it "should get the project without doc lines", ->
  535. @ProjectGetter.getProjectWithoutDocLines
  536. .calledWith(project_id)
  537. .should.equal true
  538. it "should find the doc", ->
  539. @projectLocator.findElement
  540. .calledWith({
  541. project: @project
  542. type: "docs"
  543. element_id: doc_id
  544. })
  545. .should.equal true
  546. it "should update the doc in the docstore", ->
  547. @DocstoreManager.updateDoc
  548. .calledWith(project_id, doc_id, @lines, @version, @ranges)
  549. .should.equal true
  550. it "should mark the project as updated", ->
  551. @projectUpdater.markAsUpdated
  552. .calledWith(project_id)
  553. .should.equal true
  554. it "should send the doc the to the TPDS", ->
  555. @tpdsUpdateSender.addDoc
  556. .calledWith({
  557. project_id: project_id
  558. project_name: @project.name
  559. doc_id: doc_id
  560. rev: @rev
  561. path: @path
  562. })
  563. .should.equal true
  564. it "should call the callback", ->
  565. @callback.called.should.equal true
  566. describe "when the doc has not been modified", ->
  567. beforeEach ->
  568. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  569. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  570. it "should not mark the project as updated", ->
  571. @projectUpdater.markAsUpdated.called.should.equal false
  572. it "should not send the doc the to the TPDS", ->
  573. @tpdsUpdateSender.addDoc.called.should.equal false
  574. it "should call the callback", ->
  575. @callback.called.should.equal true
  576. describe "when the project is not found", ->
  577. beforeEach ->
  578. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  579. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  580. it "should return a not found error", ->
  581. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  582. describe "when the doc is not found", ->
  583. beforeEach ->
  584. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  585. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  586. it "should log out the error", ->
  587. @logger.error
  588. .calledWith(
  589. project_id: project_id
  590. doc_id: doc_id
  591. lines: @lines
  592. err: new Errors.NotFoundError("doc not found")
  593. "doc not found while updating doc lines"
  594. )
  595. .should.equal true
  596. it "should return a not found error", ->
  597. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  598. describe "getting folders, docs and files", ->
  599. beforeEach ->
  600. @project.rootFolder = [
  601. docs: [@doc1 = {
  602. name : "doc1"
  603. _id : "doc1_id"
  604. }]
  605. fileRefs: [@file1 = {
  606. rev : 1
  607. _id : "file1_id"
  608. name : "file1"
  609. }]
  610. folders: [@folder1 = {
  611. name : "folder1"
  612. docs : [@doc2 = {
  613. name : "doc2"
  614. _id : "doc2_id"
  615. }]
  616. fileRefs : [@file2 = {
  617. rev : 2
  618. name : "file2"
  619. _id : "file2_id"
  620. }]
  621. folders : []
  622. }]
  623. ]
  624. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  625. describe "getAllFolders", ->
  626. beforeEach ->
  627. @callback = sinon.stub()
  628. @ProjectEntityHandler.getAllFolders project_id, @callback
  629. it "should get the project without the docs lines", ->
  630. @ProjectGetter.getProjectWithoutDocLines
  631. .calledWith(project_id)
  632. .should.equal true
  633. it "should call the callback with the folders", ->
  634. @callback
  635. .calledWith(null, {
  636. "/": @project.rootFolder[0]
  637. "/folder1": @folder1
  638. })
  639. .should.equal true
  640. describe "getAllFiles", ->
  641. beforeEach ->
  642. @callback = sinon.stub()
  643. @ProjectEntityHandler.getAllFiles project_id, @callback
  644. it "should call the callback with the files", ->
  645. @callback
  646. .calledWith(null, {
  647. "/file1": @file1
  648. "/folder1/file2": @file2
  649. })
  650. .should.equal true
  651. describe "getAllDocs", ->
  652. beforeEach ->
  653. @docs = [{
  654. _id: @doc1._id
  655. lines: @lines1 = ["one"]
  656. rev: @rev1 = 1
  657. }, {
  658. _id: @doc2._id
  659. lines: @lines2 = ["two"]
  660. rev: @rev2 = 2
  661. }]
  662. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  663. @ProjectEntityHandler.getAllDocs project_id, @callback
  664. it "should get the doc lines and rev from the docstore", ->
  665. @DocstoreManager.getAllDocs
  666. .calledWith(project_id)
  667. .should.equal true
  668. it "should call the callback with the docs with the lines and rev included", ->
  669. @callback
  670. .calledWith(null, {
  671. "/doc1": {
  672. _id: @doc1._id
  673. lines: @lines1
  674. name: @doc1.name
  675. rev: @rev1
  676. }
  677. "/folder1/doc2": {
  678. _id: @doc2._id
  679. lines: @lines2
  680. name: @doc2.name
  681. rev: @rev2
  682. }
  683. })
  684. .should.equal true
  685. describe "getAllFoldersFromProject", ->
  686. beforeEach ->
  687. @callback = sinon.stub()
  688. @ProjectEntityHandler.getAllFoldersFromProject @project, @callback
  689. it "should call the callback with the folders", ->
  690. @callback
  691. .calledWith(null, {
  692. "/": @project.rootFolder[0]
  693. "/folder1": @folder1
  694. })
  695. .should.equal true
  696. describe "getAllDocPathsFromProject", ->
  697. beforeEach ->
  698. @docs = [{
  699. _id: @doc1._id
  700. lines: @lines1 = ["one"]
  701. rev: @rev1 = 1
  702. }, {
  703. _id: @doc2._id
  704. lines: @lines2 = ["two"]
  705. rev: @rev2 = 2
  706. }]
  707. @callback = sinon.stub()
  708. @ProjectEntityHandler.getAllDocPathsFromProject @project, @callback
  709. it "should call the callback with the path for each doc_id", ->
  710. @expected = {}
  711. @expected[@doc1._id] = "/#{@doc1.name}"
  712. @expected[@doc2._id] = "/folder1/#{@doc2.name}"
  713. @callback
  714. .calledWith(null, @expected)
  715. .should.equal true
  716. describe "flushProjectToThirdPartyDataStore", ->
  717. beforeEach (done) ->
  718. @project = {
  719. _id: project_id
  720. name: "Mock project name"
  721. }
  722. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  723. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(1)
  724. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  725. @tpdsUpdateSender.addFile = sinon.stub().callsArg(1)
  726. @docs = {
  727. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  728. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  729. }
  730. @files = {
  731. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  732. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  733. }
  734. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  735. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  736. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  737. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  738. it "should flush the project from the doc updater", ->
  739. @documentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  740. it "should look up the project in mongo", ->
  741. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  742. it "should get all the docs in the project", ->
  743. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  744. it "should get all the files in the project", ->
  745. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  746. it "should flush each doc to the TPDS", ->
  747. for path, doc of @docs
  748. @tpdsUpdateSender.addDoc
  749. .calledWith({
  750. project_id: project_id,
  751. doc_id: doc._id
  752. project_name: @project.name
  753. rev: doc.rev
  754. path: path
  755. })
  756. .should.equal true
  757. it "should flush each file to the TPDS", ->
  758. for path, file of @files
  759. @tpdsUpdateSender.addFile
  760. .calledWith({
  761. project_id: project_id,
  762. file_id: file._id
  763. project_name: @project.name
  764. rev: file.rev
  765. path: path
  766. })
  767. .should.equal true
  768. describe "setRootDoc", ->
  769. it "should call Project.update", ->
  770. @project_id = "project-id-123234adfs"
  771. @rootDoc_id = "root-doc-id-123123"
  772. @ProjectModel.update = sinon.stub()
  773. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  774. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  775. .should.equal true
  776. describe "unsetRootDoc", ->
  777. it "should call Project.update", ->
  778. @project_id = "project-id-123234adfs"
  779. @rootDoc_id = "root-doc-id-123123"
  780. @ProjectModel.update = sinon.stub()
  781. @ProjectEntityHandler.unsetRootDoc @project_id
  782. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  783. .should.equal true
  784. describe 'copyFileFromExistingProject', ->
  785. fileName = "something.jpg"
  786. filePath = "dumpFolder/somewhere/image.jpeg"
  787. oldProject_id = "123kljadas"
  788. oldFileRef = {name:fileName, _id:"oldFileRef"}
  789. beforeEach ->
  790. @ProjectGetter.getProject = (project_id, fields, callback)=> callback(null, {name:@project.name, _id:@project._id})
  791. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  792. it 'should copy the file in FileStoreHandler', (done)->
  793. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  794. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  795. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  796. done()
  797. it 'should put file into folder by calling put element', (done)->
  798. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  799. passedProject._id.should.equal project_id
  800. passedFolder_id.should.equal folder_id
  801. passedFileRef.name.should.equal fileName
  802. passedType.should.equal 'file'
  803. done()
  804. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  805. it 'should return doc and parent folder', (done)->
  806. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  807. parentFolder.should.equal folder_id
  808. fileRef.name.should.equal fileName
  809. done()
  810. it 'should call third party data store if versioning is enabled', (done)->
  811. @project.existsInVersioningApi = true
  812. opts =
  813. path : "/somehwere/idsadsds"
  814. project_id : project_id
  815. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  816. @tpdsUpdateSender.addFile = (options)=>
  817. options.project_id.should.equal project_id
  818. options.project_name.should.equal @project.name
  819. options.path.should.equal opts.path
  820. options.file_id.should.not.be.null
  821. options.rev.should.equal 0
  822. done()
  823. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  824. describe "renameEntity", ->
  825. beforeEach ->
  826. @entity_id = "4eecaffcbffa66588e000009"
  827. @entityType = "doc"
  828. @newName = "new.tex"
  829. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  830. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  831. @ProjectModel.update = sinon.stub().callsArgWith(3)
  832. @tpdsUpdateSender.moveEntity = sinon.stub()
  833. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  834. it "should update the name in mongo", (done)->
  835. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  836. @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
  837. done()
  838. it "should send the update to the tpds", (done)->
  839. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  840. @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  841. done()
  842. describe "_insertDeletedDocReference", ->
  843. beforeEach ->
  844. @doc =
  845. _id: ObjectId()
  846. name: "test.tex"
  847. @callback = sinon.stub()
  848. @ProjectModel.update = sinon.stub().callsArgWith(3)
  849. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  850. it "should insert the doc into deletedDocs", ->
  851. @ProjectModel.update
  852. .calledWith({
  853. _id: project_id
  854. }, {
  855. $push: {
  856. deletedDocs: {
  857. _id: @doc._id
  858. name: @doc.name
  859. }
  860. }
  861. })
  862. .should.equal true
  863. it "should call the callback", ->
  864. @callback.called.should.equal true
  865. describe "_cleanUpDoc", ->
  866. beforeEach ->
  867. @project =
  868. _id: ObjectId(project_id)
  869. @doc =
  870. _id: ObjectId()
  871. name: "test.tex"
  872. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  873. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  874. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  875. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  876. @callback = sinon.stub()
  877. describe "when the doc is the root doc", ->
  878. beforeEach ->
  879. @project.rootDoc_id = @doc._id
  880. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  881. it "should unset the root doc", ->
  882. @ProjectEntityHandler.unsetRootDoc
  883. .calledWith(project_id)
  884. .should.equal true
  885. it "should delete the doc in the doc updater", ->
  886. @documentUpdaterHandler.deleteDoc
  887. .calledWith(project_id, @doc._id.toString())
  888. it "should insert the doc into the deletedDocs array", ->
  889. @ProjectEntityHandler._insertDeletedDocReference
  890. .calledWith(@project._id, @doc)
  891. .should.equal true
  892. it "should delete the doc in the doc store", ->
  893. @DocstoreManager.deleteDoc
  894. .calledWith(project_id, @doc._id.toString())
  895. .should.equal true
  896. it "should call the callback", ->
  897. @callback.called.should.equal true
  898. describe "when the doc is not the root doc", ->
  899. beforeEach ->
  900. @project.rootDoc_id = ObjectId()
  901. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  902. it "should not unset the root doc", ->
  903. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  904. it "should call the callback", ->
  905. @callback.called.should.equal true
  906. describe "_putElement", ->
  907. beforeEach ->
  908. @project_id = project_id
  909. @project =
  910. _id: ObjectId(project_id)
  911. rootFolder: [_id:ObjectId()]
  912. @folder =
  913. _id: ObjectId()
  914. name: "someFolder"
  915. @doc =
  916. _id: ObjectId()
  917. name: "new.tex"
  918. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  919. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  920. @projectLocator.findElement.callsArgWith(1, null, @folder, @path)
  921. @ProjectUpdateStub.callsArgWith(3)
  922. describe "updating the project", ->
  923. it "should use the correct mongo path", (done)->
  924. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
  925. @ProjectModel.update.args[0][0]._id.should.equal @project._id
  926. assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
  927. done()
  928. it "should add an s onto the type if not included", (done)->
  929. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
  930. assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
  931. done()
  932. it "should not call update if elemenet is null", (done)->
  933. @ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
  934. @ProjectModel.update.called.should.equal false
  935. done()
  936. it "should default to root folder insert", (done)->
  937. @ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
  938. @projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  939. done()
  940. it "should error if the element has no _id", (done)->
  941. doc =
  942. name:"something"
  943. @ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
  944. @ProjectModel.update.called.should.equal false
  945. done()
  946. describe "_countElements", ->
  947. beforeEach ->
  948. @project.rootFolder[0].docs = [{_id:123}, {_id:345}]
  949. @project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
  950. @project.rootFolder[0].folders = [
  951. {
  952. docs:
  953. [{_id:123}, {_id:345}, {_id:456}]
  954. fileRefs:{}
  955. folders: [
  956. {
  957. docs:[_id:1234],
  958. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  959. folders:[
  960. {
  961. docs:[{_id:321321}, {_id:123213}]
  962. fileRefs:[{_id:312321}]
  963. folders:[]
  964. }
  965. ]
  966. }
  967. ]
  968. },{
  969. docs:[{_id:123}, {_id:32131}]
  970. fileRefs:[]
  971. folders:[
  972. {
  973. docs:[{_id:3123}]
  974. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  975. folders:0
  976. }
  977. ]
  978. }
  979. ]
  980. it "should return the correct number", (done)->
  981. @ProjectEntityHandler._countElements @project, (err, count)->
  982. count.should.equal 26
  983. done()
  984. it "should deal with null folders", (done)->
  985. @project.rootFolder[0].folders[0].folders = undefined
  986. @ProjectEntityHandler._countElements @project, (err, count)->
  987. count.should.equal 17
  988. done()
  989. it "should deal with null docs", (done)->
  990. @project.rootFolder[0].folders[0].docs = undefined
  991. @ProjectEntityHandler._countElements @project, (err, count)->
  992. count.should.equal 23
  993. done()
  994. it "should deal with null fileRefs", (done)->
  995. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  996. @ProjectEntityHandler._countElements @project, (err, count)->
  997. count.should.equal 23
  998. done()