ProjectEntityHandlerTests.coffee 33 KB

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