ProjectEntityHandlerTests.coffee 38 KB

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