ProjectEntityHandlerTests.coffee 37 KB

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