ProjectEntityHandlerTests.coffee 42 KB

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