ProjectEntityHandlerTests.coffee 34 KB

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