ProjectEntityHandlerTests.coffee 42 KB

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