ProjectEntityHandlerTests.coffee 36 KB

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