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