ProjectEntityHandlerTests.coffee 36 KB

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