ProjectEntityHandlerTests.coffee 47 KB

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