ProjectEntityHandlerTests.coffee 52 KB

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