ProjectEntityHandlerTests.coffee 45 KB

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