ProjectEntityHandlerTests.coffee 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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().callsArgWith(3, 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. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  503. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  504. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  505. @projectUpdater.markAsUpdated = sinon.stub()
  506. @callback = sinon.stub()
  507. describe "when the doc has been modified", ->
  508. beforeEach ->
  509. @DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, true, @rev = 5)
  510. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  511. it "should get the project without doc lines", ->
  512. @ProjectGetter.getProjectWithoutDocLines
  513. .calledWith(project_id)
  514. .should.equal true
  515. it "should find the doc", ->
  516. @projectLocator.findElement
  517. .calledWith({
  518. project: @project
  519. type: "docs"
  520. element_id: doc_id
  521. })
  522. .should.equal true
  523. it "should update the doc in the docstore", ->
  524. @DocstoreManager.updateDoc
  525. .calledWith(project_id, doc_id, @lines)
  526. .should.equal true
  527. it "should mark the project as updated", ->
  528. @projectUpdater.markAsUpdated
  529. .calledWith(project_id)
  530. .should.equal true
  531. it "should send the doc the to the TPDS", ->
  532. @tpdsUpdateSender.addDoc
  533. .calledWith({
  534. project_id: project_id
  535. project_name: @project.name
  536. doc_id: doc_id
  537. rev: @rev
  538. path: @path
  539. })
  540. .should.equal true
  541. it "should call the callback", ->
  542. @callback.called.should.equal true
  543. describe "when the doc has not been modified", ->
  544. beforeEach ->
  545. @DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, false, @rev = 5)
  546. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  547. it "should not mark the project as updated", ->
  548. @projectUpdater.markAsUpdated.called.should.equal false
  549. it "should not send the doc the to the TPDS", ->
  550. @tpdsUpdateSender.addDoc.called.should.equal false
  551. it "should call the callback", ->
  552. @callback.called.should.equal true
  553. describe "when the project is not found", ->
  554. beforeEach ->
  555. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  556. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  557. it "should return a not found error", ->
  558. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  559. describe "when the doc is not found", ->
  560. beforeEach ->
  561. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  562. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  563. it "should log out the error", ->
  564. @logger.error
  565. .calledWith(
  566. project_id: project_id
  567. doc_id: doc_id
  568. lines: @lines
  569. err: new Errors.NotFoundError("doc not found")
  570. "doc not found while updating doc lines"
  571. )
  572. .should.equal true
  573. it "should return a not found error", ->
  574. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  575. describe "getting folders, docs and files", ->
  576. beforeEach ->
  577. @project.rootFolder = [
  578. docs: [@doc1 = {
  579. name : "doc1"
  580. _id : "doc1_id"
  581. }]
  582. fileRefs: [@file1 = {
  583. rev : 1
  584. _id : "file1_id"
  585. name : "file1"
  586. }]
  587. folders: [@folder1 = {
  588. name : "folder1"
  589. docs : [@doc2 = {
  590. name : "doc2"
  591. _id : "doc2_id"
  592. }]
  593. fileRefs : [@file2 = {
  594. rev : 2
  595. name : "file2"
  596. _id : "file2_id"
  597. }]
  598. folders : []
  599. }]
  600. ]
  601. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  602. describe "getAllFolders", ->
  603. beforeEach ->
  604. @callback = sinon.stub()
  605. @ProjectEntityHandler.getAllFolders project_id, @callback
  606. it "should get the project without the docs lines", ->
  607. @ProjectGetter.getProjectWithoutDocLines
  608. .calledWith(project_id)
  609. .should.equal true
  610. it "should call the callback with the folders", ->
  611. @callback
  612. .calledWith(null, {
  613. "/": @project.rootFolder[0]
  614. "/folder1": @folder1
  615. })
  616. .should.equal true
  617. describe "getAllFiles", ->
  618. beforeEach ->
  619. @callback = sinon.stub()
  620. @ProjectEntityHandler.getAllFiles project_id, @callback
  621. it "should call the callback with the files", ->
  622. @callback
  623. .calledWith(null, {
  624. "/file1": @file1
  625. "/folder1/file2": @file2
  626. })
  627. .should.equal true
  628. describe "getAllDocs", ->
  629. beforeEach ->
  630. @docs = [{
  631. _id: @doc1._id
  632. lines: @lines1 = ["one"]
  633. rev: @rev1 = 1
  634. }, {
  635. _id: @doc2._id
  636. lines: @lines2 = ["two"]
  637. rev: @rev2 = 2
  638. }]
  639. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  640. @ProjectEntityHandler.getAllDocs project_id, @callback
  641. it "should get the doc lines and rev from the docstore", ->
  642. @DocstoreManager.getAllDocs
  643. .calledWith(project_id)
  644. .should.equal true
  645. it "should call the callback with the docs with the lines and rev included", ->
  646. @callback
  647. .calledWith(null, {
  648. "/doc1": {
  649. _id: @doc1._id
  650. lines: @lines1
  651. name: @doc1.name
  652. rev: @rev1
  653. }
  654. "/folder1/doc2": {
  655. _id: @doc2._id
  656. lines: @lines2
  657. name: @doc2.name
  658. rev: @rev2
  659. }
  660. })
  661. .should.equal true
  662. describe "flushProjectToThirdPartyDataStore", ->
  663. beforeEach (done) ->
  664. @project = {
  665. _id: project_id
  666. name: "Mock project name"
  667. }
  668. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  669. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(1)
  670. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  671. @tpdsUpdateSender.addFile = sinon.stub().callsArg(1)
  672. @docs = {
  673. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  674. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  675. }
  676. @files = {
  677. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  678. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  679. }
  680. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  681. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  682. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  683. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  684. it "should flush the project from the doc updater", ->
  685. @documentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  686. it "should look up the project in mongo", ->
  687. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  688. it "should get all the docs in the project", ->
  689. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  690. it "should get all the files in the project", ->
  691. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  692. it "should flush each doc to the TPDS", ->
  693. for path, doc of @docs
  694. @tpdsUpdateSender.addDoc
  695. .calledWith({
  696. project_id: project_id,
  697. doc_id: doc._id
  698. project_name: @project.name
  699. rev: doc.rev
  700. path: path
  701. })
  702. .should.equal true
  703. it "should flush each file to the TPDS", ->
  704. for path, file of @files
  705. @tpdsUpdateSender.addFile
  706. .calledWith({
  707. project_id: project_id,
  708. file_id: file._id
  709. project_name: @project.name
  710. rev: file.rev
  711. path: path
  712. })
  713. .should.equal true
  714. describe "setRootDoc", ->
  715. it "should call Project.update", ->
  716. @project_id = "project-id-123234adfs"
  717. @rootDoc_id = "root-doc-id-123123"
  718. @ProjectModel.update = sinon.stub()
  719. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  720. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  721. .should.equal true
  722. describe "unsetRootDoc", ->
  723. it "should call Project.update", ->
  724. @project_id = "project-id-123234adfs"
  725. @rootDoc_id = "root-doc-id-123123"
  726. @ProjectModel.update = sinon.stub()
  727. @ProjectEntityHandler.unsetRootDoc @project_id
  728. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  729. .should.equal true
  730. describe 'copyFileFromExistingProject', ->
  731. fileName = "something.jpg"
  732. filePath = "dumpFolder/somewhere/image.jpeg"
  733. oldProject_id = "123kljadas"
  734. oldFileRef = {name:fileName, _id:"oldFileRef"}
  735. beforeEach ->
  736. @ProjectGetter.getProject = (project_id, fields, callback)=> callback(null, {name:@project.name, _id:@project._id})
  737. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  738. it 'should copy the file in FileStoreHandler', (done)->
  739. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  740. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  741. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  742. done()
  743. it 'should put file into folder by calling put element', (done)->
  744. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  745. passedProject._id.should.equal project_id
  746. passedFolder_id.should.equal folder_id
  747. passedFileRef.name.should.equal fileName
  748. passedType.should.equal 'file'
  749. done()
  750. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  751. it 'should return doc and parent folder', (done)->
  752. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  753. parentFolder.should.equal folder_id
  754. fileRef.name.should.equal fileName
  755. done()
  756. it 'should call third party data store if versioning is enabled', (done)->
  757. @project.existsInVersioningApi = true
  758. opts =
  759. path : "/somehwere/idsadsds"
  760. project_id : project_id
  761. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  762. @tpdsUpdateSender.addFile = (options)=>
  763. options.project_id.should.equal project_id
  764. options.project_name.should.equal @project.name
  765. options.path.should.equal opts.path
  766. options.file_id.should.not.be.null
  767. options.rev.should.equal 0
  768. done()
  769. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  770. describe "renameEntity", ->
  771. beforeEach ->
  772. @entity_id = "4eecaffcbffa66588e000009"
  773. @entityType = "doc"
  774. @newName = "new.tex"
  775. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  776. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  777. @ProjectModel.update = sinon.stub().callsArgWith(3)
  778. @tpdsUpdateSender.moveEntity = sinon.stub()
  779. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  780. it "should update the name in mongo", (done)->
  781. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  782. @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
  783. done()
  784. it "should send the update to the tpds", (done)->
  785. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  786. @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  787. done()
  788. describe "_insertDeletedDocReference", ->
  789. beforeEach ->
  790. @doc =
  791. _id: ObjectId()
  792. name: "test.tex"
  793. @callback = sinon.stub()
  794. @ProjectModel.update = sinon.stub().callsArgWith(3)
  795. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  796. it "should insert the doc into deletedDocs", ->
  797. @ProjectModel.update
  798. .calledWith({
  799. _id: project_id
  800. }, {
  801. $push: {
  802. deletedDocs: {
  803. _id: @doc._id
  804. name: @doc.name
  805. }
  806. }
  807. })
  808. .should.equal true
  809. it "should call the callback", ->
  810. @callback.called.should.equal true
  811. describe "_cleanUpDoc", ->
  812. beforeEach ->
  813. @project =
  814. _id: ObjectId(project_id)
  815. @doc =
  816. _id: ObjectId()
  817. name: "test.tex"
  818. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  819. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  820. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  821. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  822. @callback = sinon.stub()
  823. describe "when the doc is the root doc", ->
  824. beforeEach ->
  825. @project.rootDoc_id = @doc._id
  826. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  827. it "should unset the root doc", ->
  828. @ProjectEntityHandler.unsetRootDoc
  829. .calledWith(project_id)
  830. .should.equal true
  831. it "should delete the doc in the doc updater", ->
  832. @documentUpdaterHandler.deleteDoc
  833. .calledWith(project_id, @doc._id.toString())
  834. it "should insert the doc into the deletedDocs array", ->
  835. @ProjectEntityHandler._insertDeletedDocReference
  836. .calledWith(@project._id, @doc)
  837. .should.equal true
  838. it "should delete the doc in the doc store", ->
  839. @DocstoreManager.deleteDoc
  840. .calledWith(project_id, @doc._id.toString())
  841. .should.equal true
  842. it "should call the callback", ->
  843. @callback.called.should.equal true
  844. describe "when the doc is not the root doc", ->
  845. beforeEach ->
  846. @project.rootDoc_id = ObjectId()
  847. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  848. it "should not unset the root doc", ->
  849. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  850. it "should call the callback", ->
  851. @callback.called.should.equal true
  852. describe "_putElement", ->
  853. beforeEach ->
  854. @project_id = project_id
  855. @project =
  856. _id: ObjectId(project_id)
  857. rootFolder: [_id:ObjectId()]
  858. @folder =
  859. _id: ObjectId()
  860. name: "someFolder"
  861. @doc =
  862. _id: ObjectId()
  863. name: "new.tex"
  864. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  865. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  866. @projectLocator.findElement.callsArgWith(1, null, @folder, @path)
  867. @ProjectUpdateStub.callsArgWith(3)
  868. describe "updating the project", ->
  869. it "should use the correct mongo path", (done)->
  870. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
  871. @ProjectModel.update.args[0][0]._id.should.equal @project._id
  872. assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
  873. done()
  874. it "should add an s onto the type if not included", (done)->
  875. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
  876. assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
  877. done()
  878. it "should not call update if elemenet is null", (done)->
  879. @ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
  880. @ProjectModel.update.called.should.equal false
  881. done()
  882. it "should default to root folder insert", (done)->
  883. @ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
  884. @projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  885. done()
  886. it "should error if the element has no _id", (done)->
  887. doc =
  888. name:"something"
  889. @ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
  890. @ProjectModel.update.called.should.equal false
  891. done()
  892. describe "_countElements", ->
  893. beforeEach ->
  894. @project.rootFolder[0].docs = [{_id:123}, {_id:345}]
  895. @project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
  896. @project.rootFolder[0].folders = [
  897. {
  898. docs:
  899. [{_id:123}, {_id:345}, {_id:456}]
  900. fileRefs:{}
  901. folders: [
  902. {
  903. docs:[_id:1234],
  904. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  905. folders:[
  906. {
  907. docs:[{_id:321321}, {_id:123213}]
  908. fileRefs:[{_id:312321}]
  909. folders:[]
  910. }
  911. ]
  912. }
  913. ]
  914. },{
  915. docs:[{_id:123}, {_id:32131}]
  916. fileRefs:[]
  917. folders:[
  918. {
  919. docs:[{_id:3123}]
  920. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  921. folders:0
  922. }
  923. ]
  924. }
  925. ]
  926. it "should return the correct number", (done)->
  927. @ProjectEntityHandler._countElements @project, (err, count)->
  928. count.should.equal 26
  929. done()
  930. it "should deal with null folders", (done)->
  931. @project.rootFolder[0].folders[0].folders = undefined
  932. @ProjectEntityHandler._countElements @project, (err, count)->
  933. count.should.equal 17
  934. done()
  935. it "should deal with null docs", (done)->
  936. @project.rootFolder[0].folders[0].docs = undefined
  937. @ProjectEntityHandler._countElements @project, (err, count)->
  938. count.should.equal 23
  939. done()
  940. it "should deal with null fileRefs", (done)->
  941. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  942. @ProjectEntityHandler._countElements @project, (err, count)->
  943. count.should.equal 23
  944. done()