ProjectEntityHandlerTests.coffee 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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, @entity, @type, @path.fileSystem, userId)
  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, @entity, 'file', @path, userId, 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, @entity, 'doc', @path, userId, done
  190. it "should clean up the doc", ->
  191. @ProjectEntityHandler._cleanUpDoc
  192. .calledWith(@project, @entity, @path, userId)
  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, @folder, "folder", path, userId, done
  209. it "should clean up all sub files", ->
  210. @ProjectEntityHandler._cleanUpFile
  211. .calledWith(@project, @file1, "/folder/subfolder/file-name-1", userId)
  212. .should.equal true
  213. @ProjectEntityHandler._cleanUpFile
  214. .calledWith(@project, @file2, "/folder/file-name-2", userId)
  215. .should.equal true
  216. it "should clean up all sub docs", ->
  217. @ProjectEntityHandler._cleanUpDoc
  218. .calledWith(@project, @doc1, "/folder/subfolder/doc-name-1", userId)
  219. .should.equal true
  220. @ProjectEntityHandler._cleanUpDoc
  221. .calledWith(@project, @doc2, "/folder/doc-name-2", userId)
  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 'addDocWithoutUpdatingHistory', ->
  446. beforeEach ->
  447. @name = "some new doc"
  448. @lines = ['1234','abc']
  449. @path = "/path/to/doc"
  450. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
  451. @callback = sinon.stub()
  452. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  453. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, 0)
  454. @ProjectEntityHandler.addDocWithoutUpdatingHistory project_id, folder_id, @name, @lines, userId, @callback
  455. # Created doc
  456. @doc = @ProjectEntityHandler._putElement.args[0][2]
  457. @doc.name.should.equal @name
  458. expect(@doc.lines).to.be.undefined
  459. it 'should call put element', ->
  460. @ProjectEntityHandler._putElement.calledWith(@project, folder_id, @doc).should.equal true
  461. it 'should return doc and parent folder', ->
  462. @callback.calledWith(null, @doc, folder_id).should.equal true
  463. it 'should call third party data store', ->
  464. @tpdsUpdateSender.addDoc
  465. .calledWith({
  466. project_id: project_id
  467. doc_id: doc_id
  468. path: @path
  469. project_name: @project.name
  470. rev: 0
  471. })
  472. .should.equal true
  473. it "should send the doc lines to the doc store", ->
  474. @DocstoreManager.updateDoc
  475. .calledWith(project_id, @doc._id.toString(), @lines)
  476. .should.equal true
  477. it "should not should send the change in project structure to the doc updater", () ->
  478. @documentUpdaterHandler.updateProjectStructure
  479. .called
  480. .should.equal false
  481. describe "restoreDoc", ->
  482. beforeEach ->
  483. @name = "doc-name"
  484. @lines = ['1234','abc']
  485. @doc = { "mock": "doc" }
  486. @folder_id = "mock-folder-id"
  487. @callback = sinon.stub()
  488. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, null, @lines)
  489. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  490. @ProjectEntityHandler.restoreDoc project_id, doc_id, @name, @callback
  491. it 'should get the doc lines', ->
  492. @ProjectEntityHandler.getDoc
  493. .calledWith(project_id, doc_id, include_deleted: true)
  494. .should.equal true
  495. it "should add a new doc with these doc lines", ->
  496. @ProjectEntityHandler.addDoc
  497. .calledWith(project_id, null, @name, @lines)
  498. .should.equal true
  499. it "should call the callback with the new folder and doc", ->
  500. @callback.calledWith(null, @doc, @folder_id).should.equal true
  501. describe 'addFile', ->
  502. fileName = "something.jpg"
  503. beforeEach ->
  504. @fileSystemPath = "somehintg"
  505. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem: @fileSystemPath}})
  506. @filePath = "somewhere"
  507. it 'should upload it via the FileStoreHandler', (done)->
  508. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  509. file_id.should.equal "file_id"
  510. passedProject_id.should.equal project_id
  511. filePath.should.equal @filePath
  512. done()
  513. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, userId, (err, fileRef, parentFolder)->
  514. it 'should put file into folder by calling put element', (done)->
  515. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  516. passedProject._id.should.equal project_id
  517. passedFolder_id.should.equal folder_id
  518. passedFileRef.name.should.equal fileName
  519. passedType.should.equal 'file'
  520. done()
  521. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  522. it 'should return doc and parent folder', (done)->
  523. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  524. parentFolder.should.equal folder_id
  525. fileRef.name.should.equal fileName
  526. done()
  527. it 'should call third party data store', (done)->
  528. @project.existsInVersioningApi = true
  529. opts =
  530. path : "/somehwere/idsadsds"
  531. project_id : project_id
  532. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  533. @tpdsUpdateSender.addFile = (options)=>
  534. options.project_id.should.equal project_id
  535. options.path.should.equal opts.path
  536. options.project_name.should.equal @project.name
  537. options.file_id.should.not.be.null
  538. options.rev.should.equal 0
  539. done()
  540. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, (err, fileRef, parentFolder)->
  541. it "should should send the change in project structure to the doc updater", (done) ->
  542. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
  543. passed_project_id.should.equal project_id
  544. passed_user_id.should.equal userId
  545. { newFiles } = changes
  546. newFiles.length.should.equal 1
  547. newFile = newFiles[0]
  548. newFile.file.name.should.equal fileName
  549. newFile.path.should.equal @fileSystemPath
  550. newFile.url.should.equal @fileUrl
  551. done()
  552. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, userId, () ->
  553. it "should not send the change in project structure to the doc updater when called as addFileWithoutUpdatingHistory", (done) ->
  554. @documentUpdaterHandler.updateProjectStructure = sinon.stub().yields()
  555. @ProjectEntityHandler.addFileWithoutUpdatingHistory project_id, folder_id, fileName, {}, userId, () =>
  556. @documentUpdaterHandler.updateProjectStructure.called.should.equal false
  557. done()
  558. describe 'replaceFile', ->
  559. beforeEach ->
  560. @projectLocator
  561. @file_id = "file_id_here"
  562. @fsPath = "fs_path_here.png"
  563. @fileRef = {rev:3, _id: @file_id, name: @fileName = "fileName"}
  564. @filePaths = {fileSystem: @fileSystemPath="/folder1/file.png", mongo:"folder.1.files.somewhere"}
  565. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  566. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3)
  567. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  568. it 'should find the file', (done)->
  569. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  570. @projectLocator.findElement
  571. .calledWith({element_id:@file_id, type:"file", project: @project})
  572. .should.equal true
  573. done()
  574. it 'should tell the file store handler to upload the file from disk', (done)->
  575. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  576. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  577. done()
  578. it 'should send the file to the tpds with an incremented rev', (done)->
  579. @tpdsUpdateSender.addFile = (options)=>
  580. options.project_id.should.equal project_id
  581. options.path.should.equal @filePaths.fileSystem
  582. options.project_name.should.equal @project.name
  583. options.file_id.should.equal @file_id
  584. options.rev.should.equal @fileRef.rev + 1
  585. done()
  586. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  587. it 'should inc the rev id', (done)->
  588. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  589. conditions._id.should.equal project_id
  590. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  591. done()
  592. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  593. it 'should update the created at date', (done)->
  594. d = new Date()
  595. @ProjectModel.findOneAndUpdate = (conditions, update, options, callback)=>
  596. conditions._id.should.equal project_id
  597. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  598. differenceInMs.should.be.below(20)
  599. done()
  600. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  601. it "should should send the old and new project structure to the doc updater", (done) ->
  602. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
  603. passed_project_id.should.equal project_id
  604. passed_user_id.should.equal userId
  605. { newFiles } = changes
  606. newFiles.length.should.equal 1
  607. newFile = newFiles[0]
  608. newFile.file.name.should.equal @fileName
  609. newFile.path.should.equal @fileSystemPath
  610. newFile.url.should.equal @fileUrl
  611. done()
  612. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, userId, =>
  613. describe 'addFolder', ->
  614. folderName = "folder1234"
  615. beforeEach ->
  616. @ProjectGetter.getProjectWithOnlyFolders = sinon.stub().callsArgWith(1, null, @project)
  617. it 'should call put element', (done)->
  618. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFolder, passedType, callback)->
  619. passedProject._id.should.equal project_id
  620. passedFolder_id.should.equal folder_id
  621. passedFolder.name.should.equal folderName
  622. passedType.should.equal 'folder'
  623. done()
  624. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  625. it 'should return the folder and parent folder', (done)->
  626. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4)
  627. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  628. parentFolder.should.equal folder_id
  629. folder.name.should.equal folderName
  630. done()
  631. describe 'updateDocLines', ->
  632. beforeEach ->
  633. @lines = ['mock', 'doc', 'lines']
  634. @path = "/somewhere/something.tex"
  635. @doc = {
  636. _id: doc_id
  637. }
  638. @version = 42
  639. @ranges = {"mock":"ranges"}
  640. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  641. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  642. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  643. @projectUpdater.markAsUpdated = sinon.stub()
  644. @callback = sinon.stub()
  645. describe "when the doc has been modified", ->
  646. beforeEach ->
  647. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
  648. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  649. it "should get the project without doc lines", ->
  650. @ProjectGetter.getProjectWithoutDocLines
  651. .calledWith(project_id)
  652. .should.equal true
  653. it "should find the doc", ->
  654. @projectLocator.findElement
  655. .calledWith({
  656. project: @project
  657. type: "docs"
  658. element_id: doc_id
  659. })
  660. .should.equal true
  661. it "should update the doc in the docstore", ->
  662. @DocstoreManager.updateDoc
  663. .calledWith(project_id, doc_id, @lines, @version, @ranges)
  664. .should.equal true
  665. it "should mark the project as updated", ->
  666. @projectUpdater.markAsUpdated
  667. .calledWith(project_id)
  668. .should.equal true
  669. it "should send the doc the to the TPDS", ->
  670. @tpdsUpdateSender.addDoc
  671. .calledWith({
  672. project_id: project_id
  673. project_name: @project.name
  674. doc_id: doc_id
  675. rev: @rev
  676. path: @path
  677. })
  678. .should.equal true
  679. it "should call the callback", ->
  680. @callback.called.should.equal true
  681. describe "when the doc has not been modified", ->
  682. beforeEach ->
  683. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  684. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
  685. it "should not mark the project as updated", ->
  686. @projectUpdater.markAsUpdated.called.should.equal false
  687. it "should not send the doc the to the TPDS", ->
  688. @tpdsUpdateSender.addDoc.called.should.equal false
  689. it "should call the callback", ->
  690. @callback.called.should.equal true
  691. describe "when the project is not found", ->
  692. beforeEach ->
  693. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  694. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  695. it "should return a not found error", ->
  696. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  697. describe "when the doc is not found", ->
  698. beforeEach ->
  699. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  700. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
  701. it "should log out the error", ->
  702. @logger.error
  703. .calledWith(
  704. project_id: project_id
  705. doc_id: doc_id
  706. lines: @lines
  707. err: new Errors.NotFoundError("doc not found")
  708. "doc not found while updating doc lines"
  709. )
  710. .should.equal true
  711. it "should return a not found error", ->
  712. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  713. describe "getting folders, docs and files", ->
  714. beforeEach ->
  715. @project.rootFolder = [
  716. docs: [@doc1 = {
  717. name : "doc1"
  718. _id : "doc1_id"
  719. }]
  720. fileRefs: [@file1 = {
  721. rev : 1
  722. _id : "file1_id"
  723. name : "file1"
  724. }]
  725. folders: [@folder1 = {
  726. name : "folder1"
  727. docs : [@doc2 = {
  728. name : "doc2"
  729. _id : "doc2_id"
  730. }]
  731. fileRefs : [@file2 = {
  732. rev : 2
  733. name : "file2"
  734. _id : "file2_id"
  735. }]
  736. folders : []
  737. }]
  738. ]
  739. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  740. describe "getAllFolders", ->
  741. beforeEach ->
  742. @callback = sinon.stub()
  743. @ProjectEntityHandler.getAllFolders project_id, @callback
  744. it "should get the project without the docs lines", ->
  745. @ProjectGetter.getProjectWithoutDocLines
  746. .calledWith(project_id)
  747. .should.equal true
  748. it "should call the callback with the folders", ->
  749. @callback
  750. .calledWith(null, {
  751. "/": @project.rootFolder[0]
  752. "/folder1": @folder1
  753. })
  754. .should.equal true
  755. describe "getAllFiles", ->
  756. beforeEach ->
  757. @callback = sinon.stub()
  758. @ProjectEntityHandler.getAllFiles project_id, @callback
  759. it "should call the callback with the files", ->
  760. @callback
  761. .calledWith(null, {
  762. "/file1": @file1
  763. "/folder1/file2": @file2
  764. })
  765. .should.equal true
  766. describe "getAllDocs", ->
  767. beforeEach ->
  768. @docs = [{
  769. _id: @doc1._id
  770. lines: @lines1 = ["one"]
  771. rev: @rev1 = 1
  772. }, {
  773. _id: @doc2._id
  774. lines: @lines2 = ["two"]
  775. rev: @rev2 = 2
  776. }]
  777. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  778. @ProjectEntityHandler.getAllDocs project_id, @callback
  779. it "should get the doc lines and rev from the docstore", ->
  780. @DocstoreManager.getAllDocs
  781. .calledWith(project_id)
  782. .should.equal true
  783. it "should call the callback with the docs with the lines and rev included", ->
  784. @callback
  785. .calledWith(null, {
  786. "/doc1": {
  787. _id: @doc1._id
  788. lines: @lines1
  789. name: @doc1.name
  790. rev: @rev1
  791. }
  792. "/folder1/doc2": {
  793. _id: @doc2._id
  794. lines: @lines2
  795. name: @doc2.name
  796. rev: @rev2
  797. }
  798. })
  799. .should.equal true
  800. describe "getAllFoldersFromProject", ->
  801. beforeEach ->
  802. @callback = sinon.stub()
  803. @ProjectEntityHandler.getAllFoldersFromProject @project, @callback
  804. it "should call the callback with the folders", ->
  805. @callback
  806. .calledWith(null, {
  807. "/": @project.rootFolder[0]
  808. "/folder1": @folder1
  809. })
  810. .should.equal true
  811. describe "getAllDocPathsFromProject", ->
  812. beforeEach ->
  813. @docs = [{
  814. _id: @doc1._id
  815. lines: @lines1 = ["one"]
  816. rev: @rev1 = 1
  817. }, {
  818. _id: @doc2._id
  819. lines: @lines2 = ["two"]
  820. rev: @rev2 = 2
  821. }]
  822. @callback = sinon.stub()
  823. @ProjectEntityHandler.getAllDocPathsFromProject @project, @callback
  824. it "should call the callback with the path for each doc_id", ->
  825. @expected = {}
  826. @expected[@doc1._id] = "/#{@doc1.name}"
  827. @expected[@doc2._id] = "/folder1/#{@doc2.name}"
  828. @callback
  829. .calledWith(null, @expected)
  830. .should.equal true
  831. describe "flushProjectToThirdPartyDataStore", ->
  832. beforeEach (done) ->
  833. @project = {
  834. _id: project_id
  835. name: "Mock project name"
  836. }
  837. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  838. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(1)
  839. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  840. @tpdsUpdateSender.addFile = sinon.stub().callsArg(1)
  841. @docs = {
  842. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  843. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  844. }
  845. @files = {
  846. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  847. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  848. }
  849. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  850. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  851. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  852. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  853. it "should flush the project from the doc updater", ->
  854. @documentUpdaterHandler.flushProjectToMongo.calledWith(project_id).should.equal true
  855. it "should look up the project in mongo", ->
  856. @ProjectGetter.getProject.calledWith(project_id).should.equal true
  857. it "should get all the docs in the project", ->
  858. @ProjectEntityHandler.getAllDocs.calledWith(project_id).should.equal true
  859. it "should get all the files in the project", ->
  860. @ProjectEntityHandler.getAllFiles.calledWith(project_id).should.equal true
  861. it "should flush each doc to the TPDS", ->
  862. for path, doc of @docs
  863. @tpdsUpdateSender.addDoc
  864. .calledWith({
  865. project_id: project_id,
  866. doc_id: doc._id
  867. project_name: @project.name
  868. rev: doc.rev
  869. path: path
  870. })
  871. .should.equal true
  872. it "should flush each file to the TPDS", ->
  873. for path, file of @files
  874. @tpdsUpdateSender.addFile
  875. .calledWith({
  876. project_id: project_id,
  877. file_id: file._id
  878. project_name: @project.name
  879. rev: file.rev
  880. path: path
  881. })
  882. .should.equal true
  883. describe "setRootDoc", ->
  884. it "should call Project.update", ->
  885. @project_id = "project-id-123234adfs"
  886. @rootDoc_id = "root-doc-id-123123"
  887. @ProjectModel.update = sinon.stub()
  888. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  889. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  890. .should.equal true
  891. describe "unsetRootDoc", ->
  892. it "should call Project.update", ->
  893. @project_id = "project-id-123234adfs"
  894. @rootDoc_id = "root-doc-id-123123"
  895. @ProjectModel.update = sinon.stub()
  896. @ProjectEntityHandler.unsetRootDoc @project_id
  897. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  898. .should.equal true
  899. describe 'copyFileFromExistingProjectWithProject', ->
  900. fileName = "something.jpg"
  901. filePath = "dumpFolder/somewhere/image.jpeg"
  902. oldProject_id = "123kljadas"
  903. oldFileRef = {name:fileName, _id:"oldFileRef"}
  904. beforeEach ->
  905. @fileSystemPath = "somehintg"
  906. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem: @fileSystemPath}})
  907. it 'should copy the file in FileStoreHandler', (done)->
  908. @ProjectEntityHandler._putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:"somehintg"}})
  909. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)=>
  910. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  911. done()
  912. it 'should put file into folder by calling put element', (done)->
  913. @ProjectEntityHandler._putElement = (passedProject, passedFolder_id, passedFileRef, passedType, callback)->
  914. passedProject._id.should.equal project_id
  915. passedFolder_id.should.equal folder_id
  916. passedFileRef.name.should.equal fileName
  917. passedType.should.equal 'file'
  918. done()
  919. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  920. it 'should return doc and parent folder', (done)->
  921. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  922. parentFolder.should.equal folder_id
  923. fileRef.name.should.equal fileName
  924. done()
  925. it 'should call third party data store if versioning is enabled', (done)->
  926. @project.existsInVersioningApi = true
  927. opts =
  928. path : "/somehwere/idsadsds"
  929. project_id : project_id
  930. @ProjectEntityHandler._putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  931. @tpdsUpdateSender.addFile = (options)=>
  932. options.project_id.should.equal project_id
  933. options.project_name.should.equal @project.name
  934. options.path.should.equal opts.path
  935. options.file_id.should.not.be.null
  936. options.rev.should.equal 0
  937. done()
  938. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  939. it "should should send the change in project structure to the doc updater", (done) ->
  940. @documentUpdaterHandler.updateProjectStructure = (passed_project_id, passed_user_id, changes) =>
  941. passed_project_id.should.equal project_id
  942. passed_user_id.should.equal userId
  943. { newFiles } = changes
  944. newFiles.length.should.equal 1
  945. newFile = newFiles[0]
  946. newFile.file.name.should.equal fileName
  947. newFile.path.should.equal @fileSystemPath
  948. newFile.url.should.equal @fileUrl
  949. done()
  950. @ProjectEntityHandler.copyFileFromExistingProjectWithProject @project, folder_id, oldProject_id, oldFileRef, userId, (err, fileRef, parentFolder)->
  951. describe "renameEntity", ->
  952. beforeEach ->
  953. @entity_id = "4eecaffcbffa66588e000009"
  954. @entityType = "doc"
  955. @newName = "new.tex"
  956. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  957. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  958. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub()
  959. @ProjectEntityHandler.getAllEntitiesFromProject
  960. .onFirstCall()
  961. .callsArgWith(1, null, @oldDocs = ['old-doc'], @oldFiles = ['old-file'])
  962. @ProjectEntityHandler.getAllEntitiesFromProject
  963. .onSecondCall()
  964. .callsArgWith(1, null, @newDocs = ['new-doc'], @newFiles = ['new-file'])
  965. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  966. @tpdsUpdateSender.moveEntity = sinon.stub()
  967. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  968. @documentUpdaterHandler.updateProjectStructure = sinon.stub().yields()
  969. it "should should send the old and new project structure to the doc updater", (done) ->
  970. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  971. @documentUpdaterHandler.updateProjectStructure
  972. .calledWith(project_id, userId, {@oldDocs, @newDocs, @oldFiles, @newFiles})
  973. .should.equal true
  974. done()
  975. it "should update the name in mongo", (done)->
  976. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  977. @ProjectModel.findOneAndUpdate.calledWith({_id: project_id}, {"$set":{"mongo.path.name": @newName}}, {"new": true}).should.equal true
  978. done()
  979. it "should send the update to the tpds", (done)->
  980. @ProjectEntityHandler.renameEntity project_id, @entity_id, @entityType, @newName, userId, =>
  981. @tpdsUpdateSender.moveEntity.calledWith({project_id:project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  982. done()
  983. describe "_insertDeletedDocReference", ->
  984. beforeEach ->
  985. @doc =
  986. _id: ObjectId()
  987. name: "test.tex"
  988. @callback = sinon.stub()
  989. @ProjectModel.update = sinon.stub().callsArgWith(3)
  990. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  991. it "should insert the doc into deletedDocs", ->
  992. @ProjectModel.update
  993. .calledWith({
  994. _id: project_id
  995. }, {
  996. $push: {
  997. deletedDocs: {
  998. _id: @doc._id
  999. name: @doc.name
  1000. }
  1001. }
  1002. })
  1003. .should.equal true
  1004. it "should call the callback", ->
  1005. @callback.called.should.equal true
  1006. describe "_cleanUpDoc", ->
  1007. beforeEach ->
  1008. @project =
  1009. _id: ObjectId(project_id)
  1010. @doc =
  1011. _id: ObjectId()
  1012. name: "test.tex"
  1013. @path = "/path/to/doc"
  1014. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  1015. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  1016. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  1017. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  1018. @callback = sinon.stub()
  1019. describe "when the doc is the root doc", ->
  1020. beforeEach ->
  1021. @project.rootDoc_id = @doc._id
  1022. @ProjectEntityHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  1023. it "should unset the root doc", ->
  1024. @ProjectEntityHandler.unsetRootDoc
  1025. .calledWith(project_id)
  1026. .should.equal true
  1027. it "should delete the doc in the doc updater", ->
  1028. @documentUpdaterHandler.deleteDoc
  1029. .calledWith(project_id, @doc._id.toString())
  1030. it "should insert the doc into the deletedDocs array", ->
  1031. @ProjectEntityHandler._insertDeletedDocReference
  1032. .calledWith(@project._id, @doc)
  1033. .should.equal true
  1034. it "should delete the doc in the doc store", ->
  1035. @DocstoreManager.deleteDoc
  1036. .calledWith(project_id, @doc._id.toString())
  1037. .should.equal true
  1038. it "should should send the update to the doc updater", ->
  1039. oldDocs = [ doc: @doc, path: @path ]
  1040. @documentUpdaterHandler.updateProjectStructure
  1041. .calledWith(project_id, userId, {oldDocs})
  1042. .should.equal true
  1043. it "should call the callback", ->
  1044. @callback.called.should.equal true
  1045. describe "when the doc is not the root doc", ->
  1046. beforeEach ->
  1047. @project.rootDoc_id = ObjectId()
  1048. @ProjectEntityHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  1049. it "should not unset the root doc", ->
  1050. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  1051. it "should call the callback", ->
  1052. @callback.called.should.equal true
  1053. describe "_putElement", ->
  1054. beforeEach ->
  1055. @project_id = project_id
  1056. @project =
  1057. _id: ObjectId(project_id)
  1058. rootFolder: [_id:ObjectId()]
  1059. @folder =
  1060. _id: ObjectId()
  1061. name: "someFolder"
  1062. @doc =
  1063. _id: ObjectId()
  1064. name: "new.tex"
  1065. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  1066. @ProjectGetter.getProject.callsArgWith(2, null, @project)
  1067. @projectLocator.findElement.callsArgWith(1, null, @folder, @path)
  1068. @ProjectModel.findOneAndUpdate = sinon.stub().callsArgWith(3, null, @project)
  1069. describe "updating the project", ->
  1070. it "should use the correct mongo path", (done)->
  1071. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err)=>
  1072. @ProjectModel.findOneAndUpdate.args[0][0]._id.should.equal @project._id
  1073. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1074. done()
  1075. it "should return the project in the callback", (done)->
  1076. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "docs", (err, path, project)=>
  1077. expect(project).to.equal @project
  1078. done()
  1079. it "should add an s onto the type if not included", (done)->
  1080. @ProjectEntityHandler._putElement @project, @folder._id, @doc, "doc", (err)=>
  1081. assert.deepEqual @ProjectModel.findOneAndUpdate.args[0][1].$push[@path.mongo+".docs"], @doc
  1082. done()
  1083. it "should not call update if element is null", (done)->
  1084. @ProjectEntityHandler._putElement @project, @folder._id, null, "doc", (err)=>
  1085. @ProjectModel.findOneAndUpdate.called.should.equal false
  1086. done()
  1087. it "should default to root folder insert", (done)->
  1088. @ProjectEntityHandler._putElement @project, null, @doc, "doc", (err)=>
  1089. @projectLocator.findElement.args[0][0].element_id.should.equal @project.rootFolder[0]._id
  1090. done()
  1091. it "should error if the element has no _id", (done)->
  1092. doc =
  1093. name:"something"
  1094. @ProjectEntityHandler._putElement @project, @folder._id, doc, "doc", (err)=>
  1095. @ProjectModel.findOneAndUpdate.called.should.equal false
  1096. done()
  1097. describe "_countElements", ->
  1098. beforeEach ->
  1099. @project.rootFolder[0].docs = [{_id:123}, {_id:345}]
  1100. @project.rootFolder[0].fileRefs = [{_id:123}, {_id:345}, {_id:456}]
  1101. @project.rootFolder[0].folders = [
  1102. {
  1103. docs:
  1104. [{_id:123}, {_id:345}, {_id:456}]
  1105. fileRefs:{}
  1106. folders: [
  1107. {
  1108. docs:[_id:1234],
  1109. fileRefs:[{_id:23123}, {_id:123213}, {_id:2312}]
  1110. folders:[
  1111. {
  1112. docs:[{_id:321321}, {_id:123213}]
  1113. fileRefs:[{_id:312321}]
  1114. folders:[]
  1115. }
  1116. ]
  1117. }
  1118. ]
  1119. },{
  1120. docs:[{_id:123}, {_id:32131}]
  1121. fileRefs:[]
  1122. folders:[
  1123. {
  1124. docs:[{_id:3123}]
  1125. fileRefs:[{_id:321321}, {_id:321321}, {_id:313122}]
  1126. folders:0
  1127. }
  1128. ]
  1129. }
  1130. ]
  1131. it "should return the correct number", (done)->
  1132. @ProjectEntityHandler._countElements @project, (err, count)->
  1133. count.should.equal 26
  1134. done()
  1135. it "should deal with null folders", (done)->
  1136. @project.rootFolder[0].folders[0].folders = undefined
  1137. @ProjectEntityHandler._countElements @project, (err, count)->
  1138. count.should.equal 17
  1139. done()
  1140. it "should deal with null docs", (done)->
  1141. @project.rootFolder[0].folders[0].docs = undefined
  1142. @ProjectEntityHandler._countElements @project, (err, count)->
  1143. count.should.equal 23
  1144. done()
  1145. it "should deal with null fileRefs", (done)->
  1146. @project.rootFolder[0].folders[0].folders[0].fileRefs = undefined
  1147. @ProjectEntityHandler._countElements @project, (err, count)->
  1148. count.should.equal 23
  1149. done()