ProjectEntityHandlerTests.coffee 44 KB

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