ProjectEntityHandlerTests.coffee 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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/Features/Errors/Errors"
  11. describe 'ProjectEntityHandler', ->
  12. project_id = '4eecb1c1bffa66588e0000a1'
  13. doc_id = '4eecb1c1bffa66588e0000a2'
  14. folder_id = "4eecaffcbffa66588e000008"
  15. rootFolderId = "4eecaffcbffa66588e000007"
  16. userId = 1234
  17. beforeEach ->
  18. @fileUrl = 'filestore.example.com/file'
  19. @FileStoreHandler =
  20. uploadFileFromDisk: sinon.stub().callsArgWith(3, null, @fileUrl)
  21. copyFile: sinon.stub().callsArgWith(4, null, @fileUrl)
  22. @tpdsUpdateSender =
  23. addDoc:sinon.stub().callsArg(1)
  24. addFile:sinon.stub().callsArg(1)
  25. addFolder:sinon.stub().callsArg(1)
  26. @rootFolder =
  27. _id:rootFolderId,
  28. folders:[
  29. {name:"level1", folders:[]}
  30. ]
  31. @ProjectUpdateStub = sinon.stub()
  32. @ProjectModel = class Project
  33. constructor:(options)->
  34. @._id = project_id
  35. @name = "project_name_here"
  36. @rev = 0
  37. save:(callback)->callback()
  38. rootFolder:[@rootFolder]
  39. @ProjectModel.update = @ProjectUpdateStub
  40. @DocModel = class Doc
  41. constructor:(options)->
  42. {@name, @lines} = options
  43. @_id = doc_id
  44. @rev = 0
  45. @FileModel = class File
  46. constructor:(options)->
  47. {@name} = options
  48. @._id = "file_id"
  49. @rev = 0
  50. @FolderModel = class Folder
  51. constructor:(options)->
  52. {@name} = options
  53. @project = new @ProjectModel()
  54. @project.rootFolder = [@rootFolder]
  55. @ProjectModel.findById = (project_id, callback)=> callback(null, @project)
  56. @ProjectModel.getProject = (project_id, fields, callback)=> callback(null, @project)
  57. @ProjectGetter =
  58. getProjectWithOnlyFolders : (project_id, callback)=> callback(null, @project)
  59. getProjectWithoutDocLines : (project_id, callback)=> callback(null, @project)
  60. getProject:sinon.stub()
  61. @projectUpdater = markAsUpdated:sinon.stub()
  62. @projectLocator =
  63. findElement : sinon.stub()
  64. @settings =
  65. maxEntitiesPerProject:200
  66. @documentUpdaterHandler =
  67. updateProjectStructure: sinon.stub().yields()
  68. deleteDoc: sinon.stub().callsArg(2)
  69. @ProjectEntityHandler = SandboxedModule.require modulePath, requires:
  70. '../../models/Project': Project:@ProjectModel
  71. '../../models/Doc': Doc:@DocModel
  72. '../../models/Folder': Folder:@FolderModel
  73. '../../models/File': File:@FileModel
  74. '../FileStore/FileStoreHandler':@FileStoreHandler
  75. '../ThirdPartyDataStore/TpdsUpdateSender':@tpdsUpdateSender
  76. './ProjectLocator': @projectLocator
  77. '../../Features/DocumentUpdater/DocumentUpdaterHandler':@documentUpdaterHandler
  78. '../Docstore/DocstoreManager': @DocstoreManager = {}
  79. 'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
  80. './ProjectUpdateHandler': @projectUpdater
  81. "./ProjectGetter": @ProjectGetter
  82. "settings-sharelatex":@settings
  83. "../Cooldown/CooldownManager": @CooldownManager = {}
  84. describe 'mkdirp', ->
  85. beforeEach ->
  86. @parentFolder_id = "1jnjknjk"
  87. @newFolder = {_id:"newFolder_id_here"}
  88. @lastFolder = {_id:"123das", folders:[]}
  89. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  90. @projectLocator.findElementByPath = (project_id, path, cb)=>
  91. @parentFolder = {_id:"parentFolder_id_here"}
  92. lastFolder = path.substring(path.lastIndexOf("/"))
  93. if lastFolder.indexOf("level1") == -1
  94. cb "level1 is not the last foler "
  95. else
  96. cb null, @parentFolder
  97. @ProjectEntityHandler.addFolder = (project_id, parentFolder_id, folderName, callback)=>
  98. callback null, {name:folderName}, @parentFolder_id
  99. it 'should return the root folder if the path is just a slash', (done)->
  100. path = "/"
  101. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  102. lastFolder.should.deep.equal @rootFolder
  103. assert.equal lastFolder.parentFolder_id, undefined
  104. done()
  105. it 'should make just one folder', (done)->
  106. path = "/differentFolder/"
  107. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  108. folders.length.should.equal 1
  109. lastFolder.name.should.equal "differentFolder"
  110. lastFolder.parentFolder_id.should.equal @parentFolder_id
  111. done()
  112. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  113. path = "level1/level2"
  114. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  115. folders.length.should.equal 1
  116. lastFolder.name.should.equal "level2"
  117. lastFolder.parentFolder_id.should.equal @parentFolder_id
  118. done()
  119. it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
  120. path = "level1/level2/level3"
  121. @ProjectEntityHandler.mkdirp project_id, path,(err, folders, lastFolder) =>
  122. folders.length.should.equal 2
  123. folders[0].name.should.equal "level2"
  124. folders[0].parentFolder_id.should.equal @parentFolder_id
  125. lastFolder.name.should.equal "level3"
  126. lastFolder.parentFolder_id.should.equal @parentFolder_id
  127. done()
  128. it 'should work with slashes either side', (done)->
  129. path = "/level1/level2/level3/"
  130. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  131. folders.length.should.equal 2
  132. folders[0].name.should.equal "level2"
  133. folders[0].parentFolder_id.should.equal @parentFolder_id
  134. lastFolder.name.should.equal "level3"
  135. lastFolder.parentFolder_id.should.equal @parentFolder_id
  136. done()
  137. describe 'deleteEntity', ->
  138. entity_id = "4eecaffcbffa66588e000009"
  139. beforeEach ->
  140. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  141. @tpdsUpdateSender.deleteEntity = sinon.stub().callsArg(1)
  142. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
  143. @ProjectEntityHandler._cleanUpEntity = sinon.stub().callsArg(5)
  144. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  145. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: entity_id }, @path)
  146. describe "deleting from Mongo", ->
  147. beforeEach (done) ->
  148. @ProjectEntityHandler.deleteEntity project_id, entity_id, @type = 'file', userId, done
  149. it "should retreive the path", ->
  150. @projectLocator.findElement.called.should.equal true
  151. options = @projectLocator.findElement.args[0][0]
  152. options.type.should.equal @type
  153. options.project.should.equal @project
  154. options.element_id.should.equal entity_id
  155. it "should remove the element from the database", ->
  156. @ProjectEntityHandler._removeElementFromMongoArray.calledWith(@ProjectModel, project_id, @path.mongo).should.equal true
  157. it "should call the third party data store", ->
  158. options = @tpdsUpdateSender.deleteEntity.args[0][0]
  159. options.project_id.should.equal project_id
  160. options.path.should.equal @path.fileSystem
  161. it "should clean up the entity from the rest of the system", ->
  162. @ProjectEntityHandler._cleanUpEntity
  163. .calledWith(@project, userId, @entity, @type, @path.fileSystem)
  164. .should.equal true
  165. describe "_cleanUpEntity", ->
  166. beforeEach ->
  167. @entity_id = "4eecaffcbffa66588e000009"
  168. @FileStoreHandler.deleteFile = sinon.stub().callsArg(2)
  169. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  170. describe "a file", ->
  171. beforeEach (done) ->
  172. @path = "/file/system/path.png"
  173. @entity = _id: @entity_id
  174. @ProjectEntityHandler._cleanUpEntity @project, userId, @entity, 'file', @path, done
  175. it "should delete the file from FileStoreHandler", ->
  176. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal true
  177. it "should not attempt to delete from the document updater", ->
  178. @documentUpdaterHandler.deleteDoc.called.should.equal false
  179. it "should should send the update to the doc updater", ->
  180. oldFiles = [ file: @entity, path: @path ]
  181. @documentUpdaterHandler.updateProjectStructure
  182. .calledWith(project_id, userId, {oldFiles})
  183. .should.equal true
  184. describe "a doc", ->
  185. beforeEach (done) ->
  186. @path = "/file/system/path.tex"
  187. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(4)
  188. @entity = {_id: @entity_id}
  189. @ProjectEntityHandler._cleanUpEntity @project, userId, @entity, 'doc', @path, done
  190. it "should clean up the doc", ->
  191. @ProjectEntityHandler._cleanUpDoc
  192. .calledWith(@project, userId, @entity, @path)
  193. .should.equal true
  194. describe "a folder", ->
  195. beforeEach (done) ->
  196. @folder =
  197. folders: [
  198. name: "subfolder"
  199. fileRefs: [ @file1 = { _id: "file-id-1", name: "file-name-1"} ]
  200. docs: [ @doc1 = { _id: "doc-id-1", name: "doc-name-1" } ]
  201. folders: []
  202. ]
  203. fileRefs: [ @file2 = { _id: "file-id-2", name: "file-name-2" } ]
  204. docs: [ @doc2 = { _id: "doc-id-2", name: "doc-name-2" } ]
  205. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(4)
  206. @ProjectEntityHandler._cleanUpFile = sinon.stub().callsArg(4)
  207. path = "/folder"
  208. @ProjectEntityHandler._cleanUpEntity @project, userId, @folder, "folder", path, done
  209. it "should clean up all sub files", ->
  210. @ProjectEntityHandler._cleanUpFile
  211. .calledWith(@project, userId, @file1, "/folder/subfolder/file-name-1")
  212. .should.equal true
  213. @ProjectEntityHandler._cleanUpFile
  214. .calledWith(@project, userId, @file2, "/folder/file-name-2")
  215. .should.equal true
  216. it "should clean up all sub docs", ->
  217. @ProjectEntityHandler._cleanUpDoc
  218. .calledWith(@project, userId, @doc1, "/folder/subfolder/doc-name-1")
  219. .should.equal true
  220. @ProjectEntityHandler._cleanUpDoc
  221. .calledWith(@project, userId, @doc2, "/folder/doc-name-2")
  222. .should.equal true
  223. describe 'moveEntity', ->
  224. beforeEach ->
  225. @pathAfterMove = {
  226. fileSystem: "/somewhere/else.txt"
  227. }
  228. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArgWith(3, null, @project)
  229. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, path: @pathAfterMove)
  230. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  231. @tpdsUpdateSender.moveEntity = sinon.stub()
  232. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  233. @ProjectEntityHandler.getAllEntitiesFromProject
  234. .onFirstCall()
  235. .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  236. @ProjectEntityHandler.getAllEntitiesFromProject
  237. .onSecondCall()
  238. .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  239. describe "moving a doc", ->
  240. beforeEach (done) ->
  241. @docId = "4eecaffcbffa66588e000009"
  242. @doc = {lines:["1234","312343d"], rev: "1234"}
  243. @path = {
  244. mongo:"folders[0]"
  245. fileSystem:"/old_folder/somewhere.txt"
  246. }
  247. @destFolder = { name: "folder" }
  248. @destFolderPath = {
  249. mongo: "folders[0]"
  250. fileSystem: "/dest_folder"
  251. }
  252. @projectLocator.findElement = sinon.stub()
  253. @projectLocator.findElement.withArgs({project: @project, element_id: @docId, type: 'docs'})
  254. .callsArgWith(1, null, @doc, @path)
  255. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", userId, done
  256. it 'should find the doc to move', ->
  257. @projectLocator.findElement.calledWith({element_id: @docId, type: "docs", project: @project }).should.equal true
  258. it "should should send the update to the doc updater", ->
  259. @documentUpdaterHandler.updateProjectStructure
  260. .calledWith(project_id, userId, {@oldDocs, @newDocs, @oldFiles, @newFiles})
  261. .should.equal true
  262. it 'should remove the element from its current position', ->
  263. @ProjectEntityHandler._removeElementFromMongoArray
  264. .calledWith(@ProjectModel, project_id, @path.mongo ).should.equal true
  265. it "should put the element back in the new folder", ->
  266. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc, "docs").should.equal true
  267. it 'should tell the third party data store', ->
  268. @tpdsUpdateSender.moveEntity
  269. .calledWith({
  270. project_id: project_id,
  271. startPath: @path.fileSystem
  272. endPath: @pathAfterMove.fileSystem
  273. project_name: @project.name
  274. rev: @doc.rev
  275. })
  276. .should.equal true
  277. describe "moving a folder", ->
  278. beforeEach ->
  279. @folder_id = "folder-to-move"
  280. @move_to_folder_id = "folder-to-move-to"
  281. @folder = { name: "folder" }
  282. @folder_to_move_to = { name: "folder to move to" }
  283. @path = { mongo:"folders[0]" }
  284. @pathToMoveTo = { mongo: "folders[0]" }
  285. @projectLocator.findElement = sinon.stub()
  286. @projectLocator.findElement.withArgs({project: @project, element_id: @folder_id, type: 'folder'})
  287. .callsArgWith(1, null, @folder, @path)
  288. @projectLocator.findElement.withArgs({project: @project, element_id: @move_to_folder_id, type: 'folder'})
  289. .callsArgWith(1, null, @folder_to_move_to, @pathToMoveTo)
  290. describe "when the destination folder is outside the moving folder", ->
  291. beforeEach (done) ->
  292. @path.fileSystem = "/one/src_dir"
  293. @pathToMoveTo.fileSystem = "/two/dest_dir"
  294. @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", userId, done
  295. it 'should find the project then element', ->
  296. @projectLocator.findElement
  297. .calledWith({
  298. element_id: @folder_id,
  299. type: "folder",
  300. project: @project
  301. })
  302. .should.equal true
  303. it "should should send the update to the doc updater", ->
  304. @documentUpdaterHandler.updateProjectStructure
  305. .calledWith(project_id, userId, {@oldDocs, @newDocs, @oldFiles, @newFiles})
  306. .should.equal true
  307. it 'should remove the element from its current position', ->
  308. @ProjectEntityHandler._removeElementFromMongoArray
  309. .calledWith(
  310. @ProjectModel,
  311. project_id,
  312. @path.mongo
  313. )
  314. .should.equal true
  315. it "should put the element back in the new folder", ->
  316. @ProjectEntityHandler._putElement
  317. .calledWith(
  318. @project,
  319. @move_to_folder_id,
  320. @folder,
  321. "folder"
  322. )
  323. .should.equal true
  324. it 'should tell the third party data store', ->
  325. @tpdsUpdateSender.moveEntity
  326. .calledWith({
  327. project_id: project_id,
  328. startPath: @path.fileSystem
  329. endPath: @pathAfterMove.fileSystem
  330. project_name: @project.name,
  331. rev: @folder.rev
  332. })
  333. .should.equal true
  334. describe "when the destination folder is inside the moving folder", ->
  335. beforeEach ->
  336. @path.fileSystem = "/one/two"
  337. @pathToMoveTo.fileSystem = "/one/two/three"
  338. @callback = sinon.stub()
  339. @ProjectEntityHandler.moveEntity project_id, @folder_id, @move_to_folder_id, "folder", userId, @callback
  340. it 'should find the folder we are moving to element', ->
  341. @projectLocator.findElement
  342. .calledWith({
  343. element_id: @move_to_folder_id,
  344. type: "folder",
  345. project: @project
  346. })
  347. .should.equal true
  348. it "should return an error", ->
  349. @callback
  350. .calledWith(new Error("destination folder is a child folder of me"))
  351. .should.equal true
  352. describe '_removeElementFromMongoArray ', ->
  353. beforeEach ->
  354. @mongoPath = "folders[0].folders[5]"
  355. @id = "12344"
  356. @project = 'a project'
  357. @ProjectModel.update = sinon.stub().callsArg(3)
  358. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  359. @ProjectEntityHandler._removeElementFromMongoArray @ProjectModel, @id, @mongoPath, @callback
  360. it 'should unset', ->
  361. update = { '$unset': { } }
  362. update['$unset'][@mongoPath] = 1
  363. @ProjectModel.update
  364. .calledWith({ _id: @id }, update, {})
  365. .should.equal true
  366. it 'should pull', ->
  367. @ProjectModel.findOneAndUpdate
  368. .calledWith({ _id: @id }, { '$pull': { 'folders[0]': null } }, {'new': true})
  369. .should.equal true
  370. it 'should call the callback', ->
  371. @callback.calledWith(null, @project).should.equal true
  372. describe 'getDoc', ->
  373. beforeEach ->
  374. @lines = ["mock", "doc", "lines"]
  375. @rev = 5
  376. @version = 42
  377. @ranges = {"mock": "ranges"}
  378. @DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
  379. describe 'without pathname option', ->
  380. beforeEach ->
  381. @ProjectEntityHandler.getDoc project_id, doc_id, @callback
  382. it "should call the docstore", ->
  383. @DocstoreManager.getDoc
  384. .calledWith(project_id, doc_id)
  385. .should.equal true
  386. it "should call the callback with the lines, version and rev", ->
  387. @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
  388. describe 'with pathname option', ->
  389. beforeEach ->
  390. @project = 'a project'
  391. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  392. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, {}, @path)
  393. @ProjectEntityHandler.getDoc project_id, doc_id, {pathname: true}, @callback
  394. it "should call the project locator", ->
  395. @projectLocator.findElement
  396. .calledWith({project_id: project_id, element_id: doc_id, type: 'doc'})
  397. .should.equal true
  398. it "should call the docstore", ->
  399. @DocstoreManager.getDoc
  400. .calledWith(project_id, doc_id)
  401. .should.equal true
  402. it "should return the pathname if option given", ->
  403. @callback.calledWith(null, @lines, @rev, @version, @ranges, @path.fileSystem).should.equal true
  404. describe 'addDoc', ->
  405. beforeEach ->
  406. @name = "some new doc"
  407. @lines = ['1234','abc']
  408. @path = "/path/to/doc"
  409. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
  410. @callback = sinon.stub()
  411. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  412. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, 0)
  413. @ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, userId, @callback
  414. # Created doc
  415. @doc = @ProjectEntityHandler._putElement.args[0][2]
  416. @doc.name.should.equal @name
  417. expect(@doc.lines).to.be.undefined
  418. it 'should call put element', ->
  419. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc).should.equal true
  420. it 'should return doc and parent folder', ->
  421. @callback.calledWith(null, @doc, folder_id).should.equal true
  422. it 'should call third party data store', ->
  423. @tpdsUpdateSender.addDoc
  424. .calledWith({
  425. project_id: project_id
  426. doc_id: doc_id
  427. path: @path
  428. project_name: @project.name
  429. rev: 0
  430. })
  431. .should.equal true
  432. it "should send the doc lines to the doc store", ->
  433. @DocstoreManager.updateDoc
  434. .calledWith(project_id, @doc._id.toString(), @lines)
  435. .should.equal true
  436. it "should should send the change in project structure to the doc updater", () ->
  437. newDocs = [
  438. doc: @doc
  439. path: @path
  440. docLines: @lines.join('\n')
  441. ]
  442. @documentUpdaterHandler.updateProjectStructure
  443. .calledWith(project_id, userId, {newDocs})
  444. .should.equal true
  445. describe "restoreDoc", ->
  446. beforeEach ->
  447. @name = "doc-name"
  448. @lines = ['1234','abc']
  449. @doc = { "mock": "doc" }
  450. @folder_id = "mock-folder-id"
  451. @callback = sinon.stub()
  452. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, null, @lines)
  453. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  454. @ProjectEntityHandler.restoreDoc project_id, doc_id, @name, @callback
  455. it 'should get the doc lines', ->
  456. @ProjectEntityHandler.getDoc
  457. .calledWith(project_id, doc_id, include_deleted: true)
  458. .should.equal true
  459. it "should add a new doc with these doc lines", ->
  460. @ProjectEntityHandler.addDoc
  461. .calledWith(project_id, null, @name, @lines)
  462. .should.equal true
  463. it "should call the callback with the new folder and doc", ->
  464. @callback.calledWith(null, @doc, @folder_id).should.equal true
  465. describe 'addFile', ->
  466. fileName = "something.jpg"
  467. beforeEach ->
  468. @fileSystemPath = "somehintg"
  469. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem: @fileSystemPath}})
  470. @filePath = "somewhere"
  471. it 'should upload it via the FileStoreHandler', (done)->
  472. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  473. file_id.should.equal "file_id"
  474. passedProject_id.should.equal project_id
  475. filePath.should.equal @filePath
  476. done()
  477. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, userId, (err, fileRef, parentFolder)->
  478. it 'should put file into folder by calling put element', (done)->
  479. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  480. passedProject._id.should.equal project_id
  481. passedFolder_id.should.equal folder_id
  482. passedFileRef.name.should.equal fileName
  483. passedType.should.equal 'file'
  484. done()
  485. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  486. it 'should return doc and parent folder', (done)->
  487. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  488. parentFolder.should.equal folder_id
  489. fileRef.name.should.equal fileName
  490. done()
  491. it 'should call third party data store', (done)->
  492. @project.existsInVersioningApi = true
  493. opts =
  494. path : "/somehwere/idsadsds"
  495. project_id : project_id
  496. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  497. @tpdsUpdateSender.addFile = (options)=>
  498. options.project_id.should.equal project_id
  499. options.path.should.equal opts.path
  500. options.project_name.should.equal @project.name
  501. options.file_id.should.not.be.null
  502. options.rev.should.equal 0
  503. done()
  504. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  505. it "should should send the change in project structure to the doc updater", (done) ->
  506. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
  507. passed_project_id.should.equal project_id
  508. passed_user_id.should.equal userId
  509. { newFiles } = changes
  510. newFiles.length.should.equal 1
  511. newFile = newFiles[0]
  512. newFile.file.name.should.equal fileName
  513. newFile.path.should.equal @fileSystemPath
  514. newFile.url.should.equal @fileUrl
  515. done()
  516. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, () ->
  517. describe 'replaceFile', ->
  518. beforeEach ->
  519. @projectLocator
  520. @file_id = "file_id_here"
  521. @fsPath = "fs_path_here.png"
  522. @fileRef = {rev:3, _id: @file_id, name: @fileName = "fileName"}
  523. @filePaths = {fileSystem: @fileSystemPath="/folder1/file.png", mongo:"folder.1.files.somewhere"}
  524. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  525. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3)
  526. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  527. it 'should find the file', (done)->
  528. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  529. @projectLocator.findElement
  530. .calledWith({element_id:@file_id, type:"file", project: @project})
  531. .should.equal true
  532. done()
  533. it 'should tell the file store handler to upload the file from disk', (done)->
  534. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  535. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  536. done()
  537. it 'should send the file to the tpds with an incremented rev', (done)->
  538. @tpdsUpdateSender.addFile = (options)=>
  539. options.project_id.should.equal project_id
  540. options.path.should.equal @filePaths.fileSystem
  541. options.project_name.should.equal @project.name
  542. options.file_id.should.equal @file_id
  543. options.rev.should.equal @fileRef.rev + 1
  544. done()
  545. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  546. it 'should inc the rev id', (done)->
  547. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  548. conditions._id.should.equal project_id
  549. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  550. done()
  551. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  552. it 'should update the created at date', (done)->
  553. d = new Date()
  554. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  555. conditions._id.should.equal project_id
  556. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  557. differenceInMs.should.be.below(20)
  558. done()
  559. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  560. it "should should send the old and new project structure to the doc updater", (done) ->
  561. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
  562. passed_project_id.should.equal project_id
  563. passed_user_id.should.equal userId
  564. { newFiles } = changes
  565. newFiles.length.should.equal 1
  566. newFile = newFiles[0]
  567. newFile.file.name.should.equal @fileName
  568. newFile.path.should.equal @fileSystemPath
  569. newFile.url.should.equal @fileUrl
  570. done()
  571. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  572. describe 'addFolder', ->
  573. folderName = "folder1234"
  574. beforeEach ->
  575. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  576. it 'should call put element', (done)->
  577. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFolder, passedType, callback)->
  578. passedProject._id.should.equal project_id
  579. passedFolder_id.should.equal folder_id
  580. passedFolder.name.should.equal folderName
  581. passedType.should.equal 'folder'
  582. done()
  583. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  584. it 'should return the folder and parent folder', (done)->
  585. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4)
  586. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  587. parentFolder.should.equal folder_id
  588. folder.name.should.equal folderName
  589. done()
  590. describe 'updateDocLines', ->
  591. beforeEach ->
  592. @lines = ['mock', 'doc', 'lines']
  593. @path = "/somewhere/something.tex"
  594. @doc = {
  595. _id: doc_id
  596. }
  597. @version = 42
  598. @ranges = {"mock":"ranges"}
  599. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  600. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  601. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  602. @projectUpdater.markAsUpdated = sinon.stub()
  603. @callback = sinon.stub()
  604. describe "when the doc has been modified", ->
  605. beforeEach ->
  606. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
  607. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  608. it "should get the project without doc lines", ->
  609. @ProjectGetter.getProjectWithoutDocLines
  610. .calledWith(project_id)
  611. .should.equal true
  612. it "should find the doc", ->
  613. @projectLocator.findElement
  614. .calledWith({
  615. project: @project
  616. type: "docs"
  617. element_id: doc_id
  618. })
  619. .should.equal true
  620. it "should update the doc in the docstore", ->
  621. @DocstoreManager.updateDoc
  622. .calledWith(project_id, doc_id, @lines, @version, @ranges)
  623. .should.equal true
  624. it "should mark the project as updated", ->
  625. @projectUpdater.markAsUpdated
  626. .calledWith(project_id)
  627. .should.equal true
  628. it "should send the doc the to the TPDS", ->
  629. @tpdsUpdateSender.addDoc
  630. .calledWith({
  631. project_id: project_id
  632. project_name: @project.name
  633. doc_id: doc_id
  634. rev: @rev
  635. path: @path
  636. })
  637. .should.equal true
  638. it "should call the callback", ->
  639. @callback.called.should.equal true
  640. describe "when the doc has not been modified", ->
  641. beforeEach ->
  642. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  643. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  644. it "should not mark the project as updated", ->
  645. @projectUpdater.markAsUpdated.called.should.equal false
  646. it "should not send the doc the to the TPDS", ->
  647. @tpdsUpdateSender.addDoc.called.should.equal false
  648. it "should call the callback", ->
  649. @callback.called.should.equal true
  650. describe "when the project is not found", ->
  651. beforeEach ->
  652. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  653. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  654. it "should return a not found error", ->
  655. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  656. describe "when the doc is not found", ->
  657. beforeEach ->
  658. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  659. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  660. it "should log out the error", ->
  661. @logger.error
  662. .calledWith(
  663. project_id: project_id
  664. doc_id: doc_id
  665. lines: @lines
  666. err: new Errors.NotFoundError("doc not found")
  667. "doc not found while updating doc lines"
  668. )
  669. .should.equal true
  670. it "should return a not found error", ->
  671. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  672. describe "getting folders, docs and files", ->
  673. beforeEach ->
  674. @project.rootFolder = [
  675. docs: [@doc1 = {
  676. name : "doc1"
  677. _id : "doc1_id"
  678. }]
  679. fileRefs: [@file1 = {
  680. rev : 1
  681. _id : "file1_id"
  682. name : "file1"
  683. }]
  684. folders: [@folder1 = {
  685. name : "folder1"
  686. docs : [@doc2 = {
  687. name : "doc2"
  688. _id : "doc2_id"
  689. }]
  690. fileRefs : [@file2 = {
  691. rev : 2
  692. name : "file2"
  693. _id : "file2_id"
  694. }]
  695. folders : []
  696. }]
  697. ]
  698. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  699. describe "getAllFolders", ->
  700. beforeEach ->
  701. @callback = sinon.stub()
  702. @ProjectEntityHandler.getAllFolders project_id, @callback
  703. it "should get the project without the docs lines", ->
  704. @ProjectGetter.getProjectWithoutDocLines
  705. .calledWith(project_id)
  706. .should.equal true
  707. it "should call the callback with the folders", ->
  708. @callback
  709. .calledWith(null, {
  710. "/": @project.rootFolder[0]
  711. "/folder1": @folder1
  712. })
  713. .should.equal true
  714. describe "getAllFiles", ->
  715. beforeEach ->
  716. @callback = sinon.stub()
  717. @ProjectEntityHandler.getAllFiles project_id, @callback
  718. it "should call the callback with the files", ->
  719. @callback
  720. .calledWith(null, {
  721. "/file1": @file1
  722. "/folder1/file2": @file2
  723. })
  724. .should.equal true
  725. describe "getAllDocs", ->
  726. beforeEach ->
  727. @docs = [{
  728. _id: @doc1._id
  729. lines: @lines1 = ["one"]
  730. rev: @rev1 = 1
  731. }, {
  732. _id: @doc2._id
  733. lines: @lines2 = ["two"]
  734. rev: @rev2 = 2
  735. }]
  736. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  737. @ProjectEntityHandler.getAllDocs project_id, @callback
  738. it "should get the doc lines and rev from the docstore", ->
  739. @DocstoreManager.getAllDocs
  740. .calledWith(project_id)
  741. .should.equal true
  742. it "should call the callback with the docs with the lines and rev included", ->
  743. @callback
  744. .calledWith(null, {
  745. "/doc1": {
  746. _id: @doc1._id
  747. lines: @lines1
  748. name: @doc1.name
  749. rev: @rev1
  750. }
  751. "/folder1/doc2": {
  752. _id: @doc2._id
  753. lines: @lines2
  754. name: @doc2.name
  755. rev: @rev2
  756. }
  757. })
  758. .should.equal true
  759. describe "getAllFoldersFromProject", ->
  760. beforeEach ->
  761. @callback = sinon.stub()
  762. @ProjectEntityHandler.getAllFoldersFromProject @project, @callback
  763. it "should call the callback with the folders", ->
  764. @callback
  765. .calledWith(null, {
  766. "/": @project.rootFolder[0]
  767. "/folder1": @folder1
  768. })
  769. .should.equal true
  770. describe "getAllDocPathsFromProject", ->
  771. beforeEach ->
  772. @docs = [{
  773. _id: @doc1._id
  774. lines: @lines1 = ["one"]
  775. rev: @rev1 = 1
  776. }, {
  777. _id: @doc2._id
  778. lines: @lines2 = ["two"]
  779. rev: @rev2 = 2
  780. }]
  781. @callback = sinon.stub()
  782. @ProjectEntityHandler.getAllDocPathsFromProject @project, @callback
  783. it "should call the callback with the path for each doc_id", ->
  784. @expected = {}
  785. @expected[@doc1._id] = "/#{@doc1.name}"
  786. @expected[@doc2._id] = "/folder1/#{@doc2.name}"
  787. @callback
  788. .calledWith(null, @expected)
  789. .should.equal true
  790. describe "flushProjectToThirdPartyDataStore", ->
  791. beforeEach (done) ->
  792. @project = {
  793. _id: project_id
  794. name: "Mock project name"
  795. }
  796. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  797. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(1)
  798. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  799. @tpdsUpdateSender.addFile = sinon.stub().callsArg(1)
  800. @docs = {
  801. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  802. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  803. }
  804. @files = {
  805. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  806. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  807. }
  808. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  809. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  810. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  811. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  812. it "should flush the project from the doc updater", ->
  813. @documentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  814. it "should look up the project in mongo", ->
  815. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  816. it "should get all the docs in the project", ->
  817. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  818. it "should get all the files in the project", ->
  819. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  820. it "should flush each doc to the TPDS", ->
  821. for path, doc of @docs
  822. @tpdsUpdateSender.addDoc
  823. .calledWith({
  824. project_id: project_id,
  825. doc_id: doc._id
  826. project_name: @project.name
  827. rev: doc.rev
  828. path: path
  829. })
  830. .should.equal true
  831. it "should flush each file to the TPDS", ->
  832. for path, file of @files
  833. @tpdsUpdateSender.addFile
  834. .calledWith({
  835. project_id: project_id,
  836. file_id: file._id
  837. project_name: @project.name
  838. rev: file.rev
  839. path: path
  840. })
  841. .should.equal true
  842. describe "setRootDoc", ->
  843. it "should call Project.update", ->
  844. @project_id = "project-id-123234adfs"
  845. @rootDoc_id = "root-doc-id-123123"
  846. @ProjectModel.update = sinon.stub()
  847. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  848. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  849. .should.equal true
  850. describe "unsetRootDoc", ->
  851. it "should call Project.update", ->
  852. @project_id = "project-id-123234adfs"
  853. @rootDoc_id = "root-doc-id-123123"
  854. @ProjectModel.update = sinon.stub()
  855. @ProjectEntityHandler.unsetRootDoc @project_id
  856. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  857. .should.equal true
  858. describe 'copyFileFromExistingProjectWithProject', ->
  859. fileName = "something.jpg"
  860. filePath = "dumpFolder/somewhere/image.jpeg"
  861. oldProject_id = "123kljadas"
  862. oldFileRef = {name:fileName, _id:"oldFileRef"}
  863. beforeEach ->
  864. @fileSystemPath = "somehintg"
  865. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem: @fileSystemPath}})
  866. it 'should copy the file in FileStoreHandler', (done)->
  867. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  868. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)=>
  869. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  870. done()
  871. it 'should put file into folder by calling put element', (done)->
  872. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  873. passedProject._id.should.equal project_id
  874. passedFolder_id.should.equal folder_id
  875. passedFileRef.name.should.equal fileName
  876. passedType.should.equal 'file'
  877. done()
  878. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  879. it 'should return doc and parent folder', (done)->
  880. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  881. parentFolder.should.equal folder_id
  882. fileRef.name.should.equal fileName
  883. done()
  884. it 'should call third party data store if versioning is enabled', (done)->
  885. @project.existsInVersioningApi = true
  886. opts =
  887. path : "/somehwere/idsadsds"
  888. project_id : project_id
  889. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  890. @tpdsUpdateSender.addFile = (options)=>
  891. options.project_id.should.equal project_id
  892. options.project_name.should.equal @project.name
  893. options.path.should.equal opts.path
  894. options.file_id.should.not.be.null
  895. options.rev.should.equal 0
  896. done()
  897. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  898. it "should should send the change in project structure to the doc updater", (done) ->
  899. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
  900. passed_project_id.should.equal project_id
  901. passed_user_id.should.equal userId
  902. { newFiles } = changes
  903. newFiles.length.should.equal 1
  904. newFile = newFiles[0]
  905. newFile.file.name.should.equal fileName
  906. newFile.path.should.equal @fileSystemPath
  907. newFile.url.should.equal @fileUrl
  908. done()
  909. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  910. describe "renameEntity", ->
  911. beforeEach ->
  912. @entity_id = "4eecaffcbffa66588e000009"
  913. @entityType = "doc"
  914. @newName = "new.tex"
  915. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  916. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  917. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  918. @ProjectEntityHandler.getAllEntitiesFromProject
  919. .onFirstCall()
  920. .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  921. @ProjectEntityHandler.getAllEntitiesFromProject
  922. .onSecondCall()
  923. .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  924. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  925. @tpdsUpdateSender.moveEntity = sinon.stub()
  926. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  927. @documentUpdaterHandler.updateProjectStructure = sinon.stub().yields()
  928. it "should should send the old and new project structure to the doc updater", (done) ->
  929. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  930. @documentUpdaterHandler.updateProjectStructure
  931. .calledWith(project_id, userId, {@oldDocs, @newDocs, @oldFiles, @newFiles})
  932. .should.equal true
  933. done()
  934. it "should update the name in mongo", (done)->
  935. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  936. @ProjectModel.findOneAndUpdate.calledWith({_id: project_id}, {"$set":{"mongo.path.name": @newName}}, {"new": true}).should.equal true
  937. done()
  938. it "should send the update to the tpds", (done)->
  939. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  940. @tpdsUpdateSender.moveEntity.calledWith({project_id:project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  941. done()
  942. describe "_insertDeletedDocReference", ->
  943. beforeEach ->
  944. @doc =
  945. _id: ObjectId()
  946. name: "test.tex"
  947. @callback = sinon.stub()
  948. @ProjectModel.update = sinon.stub().callsArgWith(3)
  949. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  950. it "should insert the doc into deletedDocs", ->
  951. @ProjectModel.update
  952. .calledWith({
  953. _id: project_id
  954. }, {
  955. $push: {
  956. deletedDocs: {
  957. _id: @doc._id
  958. name: @doc.name
  959. }
  960. }
  961. })
  962. .should.equal true
  963. it "should call the callback", ->
  964. @callback.called.should.equal true
  965. describe "_cleanUpDoc", ->
  966. beforeEach ->
  967. @project =
  968. _id: ObjectId(project_id)
  969. @doc =
  970. _id: ObjectId()
  971. name: "test.tex"
  972. @path = "/path/to/doc"
  973. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  974. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  975. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  976. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  977. @callback = sinon.stub()
  978. describe "when the doc is the root doc", ->
  979. beforeEach ->
  980. @project.rootDoc_id = @doc._id
  981. @ProjectEntityHandler._cleanUpDoc @project, userId, @doc, @path, @callback
  982. it "should unset the root doc", ->
  983. @ProjectEntityHandler.unsetRootDoc
  984. .calledWith(project_id)
  985. .should.equal true
  986. it "should delete the doc in the doc updater", ->
  987. @documentUpdaterHandler.deleteDoc
  988. .calledWith(project_id, @doc._id.toString())
  989. it "should insert the doc into the deletedDocs array", ->
  990. @ProjectEntityHandler._insertDeletedDocReference
  991. .calledWith(@project._id, @doc)
  992. .should.equal true
  993. it "should delete the doc in the doc store", ->
  994. @DocstoreManager.deleteDoc
  995. .calledWith(project_id, @doc._id.toString())
  996. .should.equal true
  997. it "should should send the update to the doc updater", ->
  998. oldDocs = [ doc: @doc, path: @path ]
  999. @documentUpdaterHandler.updateProjectStructure
  1000. .calledWith(project_id, userId, {oldDocs})
  1001. .should.equal true
  1002. it "should call the callback", ->
  1003. @callback.called.should.equal true
  1004. describe "when the doc is not the root doc", ->
  1005. beforeEach ->
  1006. @project.rootDoc_id = ObjectId()
  1007. @ProjectEntityHandler._cleanUpDoc @project, userId, @doc, @path, @callback
  1008. it "should not unset the root doc", ->
  1009. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  1010. it "should call the callback", ->
  1011. @callback.called.should.equal true
  1012. describe "_putElement", ->
  1013. beforeEach ->
  1014. @project_id = project_id
  1015. @project =
  1016. _id: ObjectId(project_id)
  1017. rootFolder: [_id:ObjectId()]
  1018. @folder =
  1019. _id: ObjectId()
  1020. name: "someFolder"
  1021. @doc =
  1022. _id: ObjectId()
  1023. name: "new.tex"
  1024. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  1025. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  1026. @projectLocator.findElement.callsArgWith(1, null, @folder, @path)
  1027. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  1028. describe "updating the project", ->
  1029. it "should use the correct mongo path", (done)->
  1030. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
  1031. @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
  1032. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1033. done()
  1034. it "should return the project in the callback", (done)->
  1035. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
  1036. expect(project).to.equal @project
  1037. done()
  1038. it "should add an s onto the type if not included", (done)->
  1039. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
  1040. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1041. done()
  1042. it "should not call update if element is null", (done)->
  1043. @ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
  1044. @ProjectModel.findOneAndUpdate.called.should.equal false
  1045. done()
  1046. it "should default to root folder insert", (done)->
  1047. @ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
  1048. @projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  1049. done()
  1050. it "should error if the element has no _id", (done)->
  1051. doc =
  1052. name:"something"
  1053. @ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
  1054. @ProjectModel.findOneAndUpdate.called.should.equal false
  1055. done()
  1056. describe "_countElements", ->
  1057. beforeEach ->
  1058. @project.rootFolder[0].docs = [{_id:123}, {_id:345}]
  1059. @project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
  1060. @project.rootFolder[0].folders = [
  1061. {
  1062. docs:
  1063. [{_id:123}, {_id:345}, {_id:456}]
  1064. fileRefs:{}
  1065. folders: [
  1066. {
  1067. docs:[_id:1234],
  1068. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  1069. folders:[
  1070. {
  1071. docs:[{_id:321321}, {_id:123213}]
  1072. fileRefs:[{_id:312321}]
  1073. folders:[]
  1074. }
  1075. ]
  1076. }
  1077. ]
  1078. },{
  1079. docs:[{_id:123}, {_id:32131}]
  1080. fileRefs:[]
  1081. folders:[
  1082. {
  1083. docs:[{_id:3123}]
  1084. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  1085. folders:0
  1086. }
  1087. ]
  1088. }
  1089. ]
  1090. it "should return the correct number", (done)->
  1091. @ProjectEntityHandler._countElements @project, (err, count)->
  1092. count.should.equal 26
  1093. done()
  1094. it "should deal with null folders", (done)->
  1095. @project.rootFolder[0].folders[0].folders = undefined
  1096. @ProjectEntityHandler._countElements @project, (err, count)->
  1097. count.should.equal 17
  1098. done()
  1099. it "should deal with null docs", (done)->
  1100. @project.rootFolder[0].folders[0].docs = undefined
  1101. @ProjectEntityHandler._countElements @project, (err, count)->
  1102. count.should.equal 23
  1103. done()
  1104. it "should deal with null fileRefs", (done)->
  1105. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  1106. @ProjectEntityHandler._countElements @project, (err, count)->
  1107. count.should.equal 23
  1108. done()