ProjectEntityHandlerTests.coffee 38 KB

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