ProjectEntityHandlerTests.coffee 43 KB

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