ProjectEntityHandlerTests.coffee 41 KB

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