ProjectEntityHandlerTests.coffee 33 KB

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