ProjectEntityHandlerTests.coffee 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. "../Cooldown/CooldownManager": @CooldownManager = {}
  79. describe 'mkdirp', ->
  80. beforeEach ->
  81. @parentFolder_id = "1jnjknjk"
  82. @newFolder = {_id:"newFolder_id_here"}
  83. @lastFolder = {_id:"123das", folders:[]}
  84. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  85. @projectLocator.findElementByPath = (project_id, path, cb)=>
  86. @parentFolder = {_id:"parentFolder_id_here"}
  87. lastFolder = path.substring(path.lastIndexOf("/"))
  88. if lastFolder.indexOf("level1") == -1
  89. cb "level1 is not the last foler "
  90. else
  91. cb null, @parentFolder
  92. @ProjectEntityHandler.addFolder = (project_id, parentFolder_id, folderName, callback)=>
  93. callback null, {name:folderName}, @parentFolder_id
  94. it 'should return the root folder if the path is just a slash', (done)->
  95. path = "/"
  96. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  97. lastFolder.should.deep.equal @rootFolder
  98. assert.equal lastFolder.parentFolder_id, undefined
  99. done()
  100. it 'should make just one folder', (done)->
  101. path = "/differentFolder/"
  102. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  103. folders.length.should.equal 1
  104. lastFolder.name.should.equal "differentFolder"
  105. lastFolder.parentFolder_id.should.equal @parentFolder_id
  106. done()
  107. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  108. path = "level1/level2"
  109. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  110. folders.length.should.equal 1
  111. lastFolder.name.should.equal "level2"
  112. lastFolder.parentFolder_id.should.equal @parentFolder_id
  113. done()
  114. it 'should make the final folder in path if it doesnt exist with mutliple levels', (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. it 'should work with slashes either side', (done)->
  124. path = "/level1/level2/level3/"
  125. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  126. folders.length.should.equal 2
  127. folders[0].name.should.equal "level2"
  128. folders[0].parentFolder_id.should.equal @parentFolder_id
  129. lastFolder.name.should.equal "level3"
  130. lastFolder.parentFolder_id.should.equal @parentFolder_id
  131. done()
  132. describe 'deleteEntity', ->
  133. entity_id = "4eecaffcbffa66588e000009"
  134. beforeEach ->
  135. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  136. @tpdsUpdateSender.deleteEntity = sinon.stub().callsArg(1)
  137. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
  138. @ProjectEntityHandler._cleanUpEntity = sinon.stub().callsArg(3)
  139. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  140. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: entity_id }, @path)
  141. describe "deleting from Mongo", ->
  142. beforeEach (done) ->
  143. @ProjectEntityHandler.deleteEntity project_id, entity_id, @type = 'file', done
  144. it "should retreive the path", ->
  145. @projectLocator.findElement.called.should.equal true
  146. options = @projectLocator.findElement.args[0][0]
  147. options.type.should.equal @type
  148. options.project.should.equal @project
  149. options.element_id.should.equal entity_id
  150. it "should remove the element from the database", ->
  151. @ProjectEntityHandler._removeElementFromMongoArray.calledWith(@ProjectModel, project_id, @path.mongo).should.equal true
  152. it "should call the third party data store", ->
  153. options = @tpdsUpdateSender.deleteEntity.args[0][0]
  154. options.project_id.should.equal project_id
  155. options.path.should.equal @path.fileSystem
  156. it "should clean up the entity from the rest of the system", ->
  157. @ProjectEntityHandler._cleanUpEntity
  158. .calledWith(@project, @entity, @type)
  159. .should.equal true
  160. describe "_cleanUpEntity", ->
  161. beforeEach ->
  162. @entity_id = "4eecaffcbffa66588e000009"
  163. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  164. @FileStoreHandler.deleteFile = sinon.stub().callsArg(2)
  165. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  166. describe "a file", ->
  167. beforeEach (done) ->
  168. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'file', done
  169. it "should delete the file from FileStoreHandler", ->
  170. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal true
  171. it "should not attempt to delete from the document updater", ->
  172. @documentUpdaterHandler.deleteDoc.called.should.equal false
  173. describe "a doc", ->
  174. beforeEach (done) ->
  175. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(2)
  176. @ProjectEntityHandler._cleanUpEntity @project, @entity = {_id: @entity_id}, 'doc', done
  177. it "should clean up the doc", ->
  178. @ProjectEntityHandler._cleanUpDoc
  179. .calledWith(@project, @entity)
  180. .should.equal true
  181. describe "a folder", ->
  182. beforeEach (done) ->
  183. @folder =
  184. folders: [
  185. fileRefs: [ @file1 = {_id: "file-id-1" } ]
  186. docs: [ @doc1 = { _id: "doc-id-1" } ]
  187. folders: []
  188. ]
  189. fileRefs: [ @file2 = { _id: "file-id-2" } ]
  190. docs: [ @doc2 = { _id: "doc-id-2" } ]
  191. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(2)
  192. @ProjectEntityHandler._cleanUpFile = sinon.stub().callsArg(2)
  193. @ProjectEntityHandler._cleanUpEntity @project, @folder, "folder", done
  194. it "should clean up all sub files", ->
  195. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file1).should.equal true
  196. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file2).should.equal true
  197. it "should clean up all sub docs", ->
  198. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc1).should.equal true
  199. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc2).should.equal true
  200. describe 'moveEntity', ->
  201. beforeEach ->
  202. @pathAfterMove = {
  203. fileSystem: "/somewhere/else.txt"
  204. }
  205. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
  206. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, path: @pathAfterMove)
  207. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  208. @tpdsUpdateSender.moveEntity = sinon.stub().callsArg(1)
  209. describe "moving a doc", ->
  210. beforeEach (done) ->
  211. @docId = "4eecaffcbffa66588e000009"
  212. @doc = {lines:["1234","312343d"], rev: "1234"}
  213. @path = {
  214. mongo:"folders[0]"
  215. fileSystem:"/somewhere.txt"
  216. }
  217. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, @path)
  218. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", done
  219. it 'should find the project then element', ->
  220. @projectLocator.findElement.calledWith({element_id: @docId, type: "docs", project: @project }).should.equal true
  221. it 'should remove the element from its current position', ->
  222. @ProjectEntityHandler._removeElementFromMongoArray
  223. .calledWith(@ProjectModel, project_id, @path.mongo ).should.equal true
  224. it "should put the element back in the new folder", ->
  225. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc, "docs").should.equal true
  226. it 'should tell the third party data store', ->
  227. @tpdsUpdateSender.moveEntity
  228. .calledWith({
  229. project_id: project_id,
  230. startPath: @path.fileSystem
  231. endPath: @pathAfterMove.fileSystem
  232. project_name: @project.name
  233. rev: @doc.rev
  234. })
  235. .should.equal true
  236. describe "moving a folder", ->
  237. beforeEach ->
  238. @folder_id = "folder-to-move"
  239. @move_to_folder_id = "folder-to-move-to"
  240. @folder = { name: "folder" }
  241. @folder_to_move_to = { name: "folder to move to" }
  242. @path = {
  243. mongo: "folders[0]"
  244. fileSystem: "/somewhere.txt"
  245. }
  246. @pathToMoveTo = {
  247. mongo: "folders[0]"
  248. fileSystem: "/somewhere.txt"
  249. }
  250. @projectLocator.findElement = (options, callback) =>
  251. if options.element_id == @folder_id
  252. callback(null, @folder, @path)
  253. else if options.element_id == @move_to_folder_id
  254. callback(null, @folder_to_move_to, @pathToMoveTo)
  255. else
  256. console.log "UNKNOWN ID", options
  257. sinon.spy @projectLocator, "findElement"
  258. describe "when the destination folder is outside the moving folder", ->
  259. beforeEach (done) ->
  260. @path.fileSystem = "/one/directory"
  261. @pathToMoveTo.fileSystem = "/another/directory"
  262. @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", done
  263. it 'should find the project then element', ->
  264. @projectLocator.findElement
  265. .calledWith({
  266. element_id: @folder_id,
  267. type: "folder",
  268. project: @project
  269. })
  270. .should.equal true
  271. it 'should remove the element from its current position', ->
  272. @ProjectEntityHandler._removeElementFromMongoArray
  273. .calledWith(
  274. @ProjectModel,
  275. project_id,
  276. @path.mongo
  277. )
  278. .should.equal true
  279. it "should put the element back in the new folder", ->
  280. @ProjectEntityHandler._putElement
  281. .calledWith(
  282. @project,
  283. @move_to_folder_id,
  284. @folder,
  285. "folder"
  286. )
  287. .should.equal true
  288. it 'should tell the third party data store', ->
  289. @tpdsUpdateSender.moveEntity
  290. .calledWith({
  291. project_id: project_id,
  292. startPath: @path.fileSystem
  293. endPath: @pathAfterMove.fileSystem
  294. project_name: @project.name,
  295. rev: @folder.rev
  296. })
  297. .should.equal true
  298. describe "when the destination folder is inside the moving folder", ->
  299. beforeEach ->
  300. @path.fileSystem = "/one/two"
  301. @pathToMoveTo.fileSystem = "/one/two/three"
  302. @callback = sinon.stub()
  303. @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", @callback
  304. it 'should find the folder we are moving to as well element', ->
  305. @projectLocator.findElement
  306. .calledWith({
  307. element_id: @move_to_folder_id,
  308. type: "folder",
  309. project: @project
  310. })
  311. .should.equal true
  312. it "should return an error", ->
  313. @callback
  314. .calledWith(new Error("destination folder is a child folder of me"))
  315. .should.equal true
  316. describe 'removing element from mongo array', ->
  317. it 'should call update with log the path', (done)->
  318. mongoPath = "folders[0].folders[5]"
  319. id = "12344"
  320. firstUpdate = true
  321. model =
  322. update: (conditions, update, opts, callback)->
  323. if firstUpdate
  324. conditions._id.should.equal id
  325. update.$unset[mongoPath].should.equal 1
  326. firstUpdate = false
  327. callback()
  328. else
  329. conditions._id.should.equal id
  330. assert.deepEqual update, { '$pull': { 'folders[0]': null } }
  331. done()
  332. @ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, ->
  333. describe 'getDoc', ->
  334. beforeEach ->
  335. @lines = ["mock", "doc", "lines"]
  336. @rev = 5
  337. @version = 42
  338. @ranges = {"mock": "ranges"}
  339. @DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
  340. @ProjectEntityHandler.getDoc project_id, doc_id, @callback
  341. it "should call the docstore", ->
  342. @DocstoreManager.getDoc
  343. .calledWith(project_id, doc_id)
  344. .should.equal true
  345. it "should call the callback with the lines, version and rev", ->
  346. @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
  347. describe 'addDoc', ->
  348. beforeEach ->
  349. @name = "some new doc"
  350. @lines = ['1234','abc']
  351. @path = "/path/to/doc"
  352. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
  353. @callback = sinon.stub()
  354. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  355. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, 0)
  356. @ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, @callback
  357. # Created doc
  358. @doc = @ProjectEntityHandler._putElement.args[0][2]
  359. @doc.name.should.equal @name
  360. expect(@doc.lines).to.be.undefined
  361. it 'should call put element', ->
  362. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc).should.equal true
  363. it 'should return doc and parent folder', ->
  364. @callback.calledWith(null, @doc, folder_id).should.equal true
  365. it 'should call third party data store', ->
  366. @tpdsUpdateSender.addDoc
  367. .calledWith({
  368. project_id: project_id
  369. doc_id: doc_id
  370. path: @path
  371. project_name: @project.name
  372. rev: 0
  373. })
  374. .should.equal true
  375. it "should send the doc lines to the doc store", ->
  376. @DocstoreManager.updateDoc
  377. .calledWith(project_id, @doc._id.toString(), @lines)
  378. .should.equal true
  379. describe "restoreDoc", ->
  380. beforeEach ->
  381. @name = "doc-name"
  382. @lines = ['1234','abc']
  383. @doc = { "mock": "doc" }
  384. @folder_id = "mock-folder-id"
  385. @callback = sinon.stub()
  386. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, null, @lines)
  387. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  388. @ProjectEntityHandler.restoreDoc project_id, doc_id, @name, @callback
  389. it 'should get the doc lines', ->
  390. @ProjectEntityHandler.getDoc
  391. .calledWith(project_id, doc_id, include_deleted: true)
  392. .should.equal true
  393. it "should add a new doc with these doc lines", ->
  394. @ProjectEntityHandler.addDoc
  395. .calledWith(project_id, null, @name, @lines)
  396. .should.equal true
  397. it "should call the callback with the new folder and doc", ->
  398. @callback.calledWith(null, @doc, @folder_id).should.equal true
  399. describe 'addFile', ->
  400. fileName = "something.jpg"
  401. beforeEach ->
  402. @filePath = "somewhere"
  403. it 'should upload it via the FileStoreHandler', (done)->
  404. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  405. file_id.should.equal "file_id"
  406. passedProject_id.should.equal project_id
  407. filePath.should.equal @filePath
  408. done()
  409. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
  410. it 'should put file into folder by calling put element', (done)->
  411. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  412. passedProject._id.should.equal project_id
  413. passedFolder_id.should.equal folder_id
  414. passedFileRef.name.should.equal fileName
  415. passedType.should.equal 'file'
  416. done()
  417. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  418. it 'should return doc and parent folder', (done)->
  419. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  420. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  421. parentFolder.should.equal folder_id
  422. fileRef.name.should.equal fileName
  423. done()
  424. it 'should call third party data store', (done)->
  425. @project.existsInVersioningApi = true
  426. opts =
  427. path : "/somehwere/idsadsds"
  428. project_id : project_id
  429. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  430. @tpdsUpdateSender.addFile = (options)=>
  431. options.project_id.should.equal project_id
  432. options.path.should.equal opts.path
  433. options.project_name.should.equal @project.name
  434. options.file_id.should.not.be.null
  435. options.rev.should.equal 0
  436. done()
  437. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  438. describe 'replacing a file', ->
  439. beforeEach ->
  440. @projectLocator
  441. @file_id = "file_id_here"
  442. @fsPath = "fs_path_here.png"
  443. @fileRef = {rev:3, _id:@file_id}
  444. @filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
  445. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  446. @ProjectModel.update = (_, __, ___, cb)-> cb()
  447. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  448. it 'should find the file', (done)->
  449. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  450. @projectLocator.findElement.calledWith({element_id:@file_id, type:"file", project_id:project_id}).should.equal true
  451. done()
  452. it 'should tell the file store handler to upload the file from disk', (done)->
  453. @FileStoreHandler.uploadFileFromDisk = sinon.stub().callsArgWith(3)
  454. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  455. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  456. done()
  457. it 'should send the file to the tpds with an incremented rev', (done)->
  458. @tpdsUpdateSender.addFile = (options)=>
  459. options.project_id.should.equal project_id
  460. options.path.should.equal @filePaths.fileSystem
  461. options.project_name.should.equal @project.name
  462. options.file_id.should.equal @file_id
  463. options.rev.should.equal @fileRef.rev + 1
  464. done()
  465. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  466. it 'should inc the rev id', (done)->
  467. @ProjectModel.update = (conditions, update, options, callback)=>
  468. conditions._id.should.equal project_id
  469. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  470. done()
  471. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  472. it 'should update the created at date', (done)->
  473. d = new Date()
  474. @ProjectModel.update = (conditions, update, options, callback)=>
  475. conditions._id.should.equal project_id
  476. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  477. differenceInMs.should.be.below(20)
  478. done()
  479. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  480. describe 'addFolder', ->
  481. folderName = "folder1234"
  482. beforeEach ->
  483. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  484. it 'should call put element', (done)->
  485. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFolder, passedType, callback)->
  486. passedProject._id.should.equal project_id
  487. passedFolder_id.should.equal folder_id
  488. passedFolder.name.should.equal folderName
  489. passedType.should.equal 'folder'
  490. done()
  491. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  492. it 'should return the folder and parent folder', (done)->
  493. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4)
  494. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  495. parentFolder.should.equal folder_id
  496. folder.name.should.equal folderName
  497. done()
  498. describe 'updateDocLines', ->
  499. beforeEach ->
  500. @lines = ['mock', 'doc', 'lines']
  501. @path = "/somewhere/something.tex"
  502. @doc = {
  503. _id: doc_id
  504. }
  505. @version = 42
  506. @ranges = {"mock":"ranges"}
  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().yields(null, true, @rev = 5)
  515. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @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, @version, @ranges)
  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().yields(null, false, @rev = 5)
  551. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @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, @ranges, @version, @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, @ranges, @version, @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. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  688. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  689. it "should flush the project from the doc updater", ->
  690. @documentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  691. it "should look up the project in mongo", ->
  692. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  693. it "should get all the docs in the project", ->
  694. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  695. it "should get all the files in the project", ->
  696. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  697. it "should flush each doc to the TPDS", ->
  698. for path, doc of @docs
  699. @tpdsUpdateSender.addDoc
  700. .calledWith({
  701. project_id: project_id,
  702. doc_id: doc._id
  703. project_name: @project.name
  704. rev: doc.rev
  705. path: path
  706. })
  707. .should.equal true
  708. it "should flush each file to the TPDS", ->
  709. for path, file of @files
  710. @tpdsUpdateSender.addFile
  711. .calledWith({
  712. project_id: project_id,
  713. file_id: file._id
  714. project_name: @project.name
  715. rev: file.rev
  716. path: path
  717. })
  718. .should.equal true
  719. describe "setRootDoc", ->
  720. it "should call Project.update", ->
  721. @project_id = "project-id-123234adfs"
  722. @rootDoc_id = "root-doc-id-123123"
  723. @ProjectModel.update = sinon.stub()
  724. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  725. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  726. .should.equal true
  727. describe "unsetRootDoc", ->
  728. it "should call Project.update", ->
  729. @project_id = "project-id-123234adfs"
  730. @rootDoc_id = "root-doc-id-123123"
  731. @ProjectModel.update = sinon.stub()
  732. @ProjectEntityHandler.unsetRootDoc @project_id
  733. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  734. .should.equal true
  735. describe 'copyFileFromExistingProject', ->
  736. fileName = "something.jpg"
  737. filePath = "dumpFolder/somewhere/image.jpeg"
  738. oldProject_id = "123kljadas"
  739. oldFileRef = {name:fileName, _id:"oldFileRef"}
  740. beforeEach ->
  741. @ProjectGetter.getProject = (project_id, fields, callback)=> callback(null, {name:@project.name, _id:@project._id})
  742. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  743. it 'should copy the file in FileStoreHandler', (done)->
  744. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  745. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  746. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  747. done()
  748. it 'should put file into folder by calling put element', (done)->
  749. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  750. passedProject._id.should.equal project_id
  751. passedFolder_id.should.equal folder_id
  752. passedFileRef.name.should.equal fileName
  753. passedType.should.equal 'file'
  754. done()
  755. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  756. it 'should return doc and parent folder', (done)->
  757. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  758. parentFolder.should.equal folder_id
  759. fileRef.name.should.equal fileName
  760. done()
  761. it 'should call third party data store if versioning is enabled', (done)->
  762. @project.existsInVersioningApi = true
  763. opts =
  764. path : "/somehwere/idsadsds"
  765. project_id : project_id
  766. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  767. @tpdsUpdateSender.addFile = (options)=>
  768. options.project_id.should.equal project_id
  769. options.project_name.should.equal @project.name
  770. options.path.should.equal opts.path
  771. options.file_id.should.not.be.null
  772. options.rev.should.equal 0
  773. done()
  774. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  775. describe "renameEntity", ->
  776. beforeEach ->
  777. @entity_id = "4eecaffcbffa66588e000009"
  778. @entityType = "doc"
  779. @newName = "new.tex"
  780. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  781. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  782. @ProjectModel.update = sinon.stub().callsArgWith(3)
  783. @tpdsUpdateSender.moveEntity = sinon.stub()
  784. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  785. it "should update the name in mongo", (done)->
  786. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  787. @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
  788. done()
  789. it "should send the update to the tpds", (done)->
  790. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  791. @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  792. done()
  793. describe "_insertDeletedDocReference", ->
  794. beforeEach ->
  795. @doc =
  796. _id: ObjectId()
  797. name: "test.tex"
  798. @callback = sinon.stub()
  799. @ProjectModel.update = sinon.stub().callsArgWith(3)
  800. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  801. it "should insert the doc into deletedDocs", ->
  802. @ProjectModel.update
  803. .calledWith({
  804. _id: project_id
  805. }, {
  806. $push: {
  807. deletedDocs: {
  808. _id: @doc._id
  809. name: @doc.name
  810. }
  811. }
  812. })
  813. .should.equal true
  814. it "should call the callback", ->
  815. @callback.called.should.equal true
  816. describe "_cleanUpDoc", ->
  817. beforeEach ->
  818. @project =
  819. _id: ObjectId(project_id)
  820. @doc =
  821. _id: ObjectId()
  822. name: "test.tex"
  823. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  824. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  825. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  826. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  827. @callback = sinon.stub()
  828. describe "when the doc is the root doc", ->
  829. beforeEach ->
  830. @project.rootDoc_id = @doc._id
  831. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  832. it "should unset the root doc", ->
  833. @ProjectEntityHandler.unsetRootDoc
  834. .calledWith(project_id)
  835. .should.equal true
  836. it "should delete the doc in the doc updater", ->
  837. @documentUpdaterHandler.deleteDoc
  838. .calledWith(project_id, @doc._id.toString())
  839. it "should insert the doc into the deletedDocs array", ->
  840. @ProjectEntityHandler._insertDeletedDocReference
  841. .calledWith(@project._id, @doc)
  842. .should.equal true
  843. it "should delete the doc in the doc store", ->
  844. @DocstoreManager.deleteDoc
  845. .calledWith(project_id, @doc._id.toString())
  846. .should.equal true
  847. it "should call the callback", ->
  848. @callback.called.should.equal true
  849. describe "when the doc is not the root doc", ->
  850. beforeEach ->
  851. @project.rootDoc_id = ObjectId()
  852. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  853. it "should not unset the root doc", ->
  854. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  855. it "should call the callback", ->
  856. @callback.called.should.equal true
  857. describe "_putElement", ->
  858. beforeEach ->
  859. @project_id = project_id
  860. @project =
  861. _id: ObjectId(project_id)
  862. rootFolder: [_id:ObjectId()]
  863. @folder =
  864. _id: ObjectId()
  865. name: "someFolder"
  866. @doc =
  867. _id: ObjectId()
  868. name: "new.tex"
  869. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  870. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  871. @projectLocator.findElement.callsArgWith(1, null, @folder, @path)
  872. @ProjectUpdateStub.callsArgWith(3)
  873. describe "updating the project", ->
  874. it "should use the correct mongo path", (done)->
  875. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
  876. @ProjectModel.update.args[0][0]._id.should.equal @project._id
  877. assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
  878. done()
  879. it "should add an s onto the type if not included", (done)->
  880. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
  881. assert.deepEqual @ProjectModel.update.args[0][1].$push[@path.mongo+".docs"], @doc
  882. done()
  883. it "should not call update if elemenet is null", (done)->
  884. @ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
  885. @ProjectModel.update.called.should.equal false
  886. done()
  887. it "should default to root folder insert", (done)->
  888. @ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
  889. @projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  890. done()
  891. it "should error if the element has no _id", (done)->
  892. doc =
  893. name:"something"
  894. @ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
  895. @ProjectModel.update.called.should.equal false
  896. done()
  897. describe "_countElements", ->
  898. beforeEach ->
  899. @project.rootFolder[0].docs = [{_id:123}, {_id:345}]
  900. @project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
  901. @project.rootFolder[0].folders = [
  902. {
  903. docs:
  904. [{_id:123}, {_id:345}, {_id:456}]
  905. fileRefs:{}
  906. folders: [
  907. {
  908. docs:[_id:1234],
  909. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  910. folders:[
  911. {
  912. docs:[{_id:321321}, {_id:123213}]
  913. fileRefs:[{_id:312321}]
  914. folders:[]
  915. }
  916. ]
  917. }
  918. ]
  919. },{
  920. docs:[{_id:123}, {_id:32131}]
  921. fileRefs:[]
  922. folders:[
  923. {
  924. docs:[{_id:3123}]
  925. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  926. folders:0
  927. }
  928. ]
  929. }
  930. ]
  931. it "should return the correct number", (done)->
  932. @ProjectEntityHandler._countElements @project, (err, count)->
  933. count.should.equal 26
  934. done()
  935. it "should deal with null folders", (done)->
  936. @project.rootFolder[0].folders[0].folders = undefined
  937. @ProjectEntityHandler._countElements @project, (err, count)->
  938. count.should.equal 17
  939. done()
  940. it "should deal with null docs", (done)->
  941. @project.rootFolder[0].folders[0].docs = undefined
  942. @ProjectEntityHandler._countElements @project, (err, count)->
  943. count.should.equal 23
  944. done()
  945. it "should deal with null fileRefs", (done)->
  946. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  947. @ProjectEntityHandler._countElements @project, (err, count)->
  948. count.should.equal 23
  949. done()