ProjectEntityHandlerTests.coffee 38 KB

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