ProjectEntityHandlerTests.coffee 44 KB

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