ProjectEntityHandlerTests.coffee 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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(2)
  22. addFile:sinon.stub().callsArg(2)
  23. addFolder:sinon.stub().callsArg(2)
  24. @rootFolder =
  25. _id:rootFolderId,
  26. folders:[
  27. {name:"level1", folders:[]}
  28. ]
  29. @ProjectModel = class Project
  30. constructor:(options)->
  31. @._id = project_id
  32. @name = "project_name_here"
  33. @rev = 0
  34. save:(callback)->callback()
  35. rootFolder:[@rootFolder]
  36. @DocModel = class Doc
  37. constructor:(options)->
  38. {@name, @lines} = options
  39. @_id = doc_id
  40. @rev = 0
  41. @FileModel = class File
  42. constructor:(options)->
  43. {@name} = options
  44. @._id = "file_id"
  45. @rev = 0
  46. @FolderModel = class Folder
  47. constructor:(options)->
  48. {@name} = options
  49. @project = new @ProjectModel()
  50. @project.rootFolder = [@rootFolder]
  51. @ProjectModel.findById = (project_id, callback)=> callback(null, @project)
  52. @ProjectModel.getProject = (project_id, fields, callback)=> callback(null, @project)
  53. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:"somehintg"}})
  54. @projectUpdater = markAsUpdated:sinon.stub()
  55. @ProjectEntityHandler = SandboxedModule.require modulePath, requires:
  56. '../../models/Project': Project:@ProjectModel
  57. '../../models/Doc': Doc:@DocModel
  58. '../../models/Folder': Folder:@FolderModel
  59. '../../models/File': File:@FileModel
  60. '../FileStore/FileStoreHandler':@FileStoreHandler
  61. '../ThirdPartyDataStore/TpdsUpdateSender':@tpdsUpdateSender
  62. './ProjectLocator':@projectLocator = {}
  63. '../../Features/DocumentUpdater/DocumentUpdaterHandler':@documentUpdaterHandler = {}
  64. '../Docstore/DocstoreManager': @DocstoreManager = {}
  65. 'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub()}
  66. './ProjectUpdateHandler': @projectUpdater
  67. "./ProjectGetter": @ProjectGetter = {}
  68. describe 'mkdirp', ->
  69. beforeEach ->
  70. @parentFolder_id = "1jnjknjk"
  71. @newFolder = {_id:"newFolder_id_here"}
  72. @lastFolder = {_id:"123das", folders:[]}
  73. @projectLocator.findElementByPath = (project_id, path, cb)=>
  74. @parentFolder = {_id:"parentFolder_id_here"}
  75. lastFolder = path.substring(path.lastIndexOf("/"))
  76. if lastFolder.indexOf("level1") == -1
  77. cb "level1 is not the last foler "
  78. else
  79. cb null, @parentFolder
  80. @ProjectEntityHandler.addFolder = (project_id, parentFolder_id, folderName, sl_req_id, callback)=>
  81. callback null, {name:folderName}, @parentFolder_id
  82. it 'should return the root folder if the path is just a slash', (done)->
  83. path = "/"
  84. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  85. lastFolder.should.deep.equal @rootFolder
  86. assert.equal lastFolder.parentFolder_id, undefined
  87. done()
  88. it 'should make just one folder', (done)->
  89. path = "/differentFolder/"
  90. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  91. folders.length.should.equal 1
  92. lastFolder.name.should.equal "differentFolder"
  93. lastFolder.parentFolder_id.should.equal @parentFolder_id
  94. done()
  95. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  96. path = "level1/level2"
  97. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  98. folders.length.should.equal 1
  99. lastFolder.name.should.equal "level2"
  100. lastFolder.parentFolder_id.should.equal @parentFolder_id
  101. done()
  102. it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
  103. path = "level1/level2/level3"
  104. @ProjectEntityHandler.mkdirp project_id, path,(err, folders, lastFolder) =>
  105. folders.length.should.equal 2
  106. folders[0].name.should.equal "level2"
  107. folders[0].parentFolder_id.should.equal @parentFolder_id
  108. lastFolder.name.should.equal "level3"
  109. lastFolder.parentFolder_id.should.equal @parentFolder_id
  110. done()
  111. it 'should work with slashes either side', (done)->
  112. path = "/level1/level2/level3/"
  113. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  114. folders.length.should.equal 2
  115. folders[0].name.should.equal "level2"
  116. folders[0].parentFolder_id.should.equal @parentFolder_id
  117. lastFolder.name.should.equal "level3"
  118. lastFolder.parentFolder_id.should.equal @parentFolder_id
  119. done()
  120. describe 'deleting an element', ->
  121. entity_id = "4eecaffcbffa66588e000009"
  122. beforeEach ->
  123. @tpdsUpdateSender.deleteEntity = sinon.stub().callsArg(2)
  124. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
  125. @ProjectEntityHandler._cleanUpEntity = sinon.stub().callsArg(3)
  126. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  127. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: entity_id }, @path)
  128. describe "deleting from Mongo", ->
  129. beforeEach (done) ->
  130. @ProjectEntityHandler.deleteEntity project_id, entity_id, @type = 'file', done
  131. it "should retreive the path", ->
  132. @projectLocator.findElement.called.should.equal true
  133. options = @projectLocator.findElement.args[0][0]
  134. options.type.should.equal @type
  135. options.project.should.equal @project
  136. options.element_id.should.equal entity_id
  137. it "should remove the element from the database", ->
  138. @ProjectEntityHandler._removeElementFromMongoArray.calledWith(@ProjectModel, project_id, @path.mongo).should.equal true
  139. it "should call the third party data store", ->
  140. options = @tpdsUpdateSender.deleteEntity.args[0][0]
  141. options.project_id.should.equal project_id
  142. options.path.should.equal @path.fileSystem
  143. it "should clean up the entity from the rest of the system", ->
  144. @ProjectEntityHandler._cleanUpEntity
  145. .calledWith(@project, @entity, @type)
  146. .should.equal true
  147. describe "_cleanUpEntity", ->
  148. beforeEach ->
  149. @entity_id = "4eecaffcbffa66588e000009"
  150. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  151. @FileStoreHandler.deleteFile = sinon.stub().callsArg(2)
  152. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  153. describe "a file", ->
  154. beforeEach (done) ->
  155. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'file', done
  156. it "should delete the file from FileStoreHandler", ->
  157. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal true
  158. it "should not attempt to delete from the document updater", ->
  159. @documentUpdaterHandler.deleteDoc.called.should.equal false
  160. describe "a doc", ->
  161. beforeEach (done) ->
  162. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(3)
  163. @ProjectEntityHandler._cleanUpEntity @project, @entity = {_id: @entity_id}, 'doc', done
  164. it "should clean up the doc", ->
  165. @ProjectEntityHandler._cleanUpDoc
  166. .calledWith(@project, @entity)
  167. .should.equal true
  168. describe "a folder", ->
  169. beforeEach (done) ->
  170. @folder =
  171. folders: [
  172. fileRefs: [ @file1 = {_id: "file-id-1" } ]
  173. docs: [ @doc1 = { _id: "doc-id-1" } ]
  174. folders: []
  175. ]
  176. fileRefs: [ @file2 = { _id: "file-id-2" } ]
  177. docs: [ @doc2 = { _id: "doc-id-2" } ]
  178. @ProjectEntityHandler._cleanUpDoc = sinon.stub().callsArg(3)
  179. @ProjectEntityHandler._cleanUpFile = sinon.stub().callsArg(3)
  180. @ProjectEntityHandler._cleanUpEntity @project, @folder, "folder", done
  181. it "should clean up all sub files", ->
  182. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file1).should.equal true
  183. @ProjectEntityHandler._cleanUpFile.calledWith(@project, @file2).should.equal true
  184. it "should clean up all sub docs", ->
  185. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc1).should.equal true
  186. @ProjectEntityHandler._cleanUpDoc.calledWith(@project, @doc2).should.equal true
  187. describe 'moving an element', ->
  188. beforeEach ->
  189. @docId = "4eecaffcbffa66588e000009"
  190. @doc = {lines:["1234","312343d"], rev: "1234"}
  191. it 'should find the project then element', (done)->
  192. @projectLocator.findElement = (options, callback)=>
  193. options.element_id.should.equal @docId
  194. options.type.should.equal 'docs'
  195. done()
  196. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", ->
  197. it 'should remove the element then add it back in', (done)->
  198. path = {mongo:"folders[0]"}
  199. @projectLocator.findElement = (opts, callback)=>
  200. callback(null, @doc, path)
  201. @ProjectEntityHandler._removeElementFromMongoArray = (model, model_id, path, callback)-> callback()
  202. @ProjectModel.putElement = (passedProject_id, destinationFolder_id, entity, entityType, callback)=>
  203. passedProject_id.should.equal project_id
  204. destinationFolder_id.should.equal folder_id
  205. entity.should.deep.equal @doc
  206. entityType.should.equal 'docs'
  207. done()
  208. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", ->
  209. it 'should tell the third party data store', (done)->
  210. startPath = {fileSystem:"/somewhere.txt"}
  211. endPath = {fileSystem:"/somewhere.txt"}
  212. @projectLocator.findElement = (opts, callback)=>
  213. callback(null, @doc, startPath)
  214. @ProjectEntityHandler._removeElementFromMongoArray = (model, model_id, path, callback)-> callback()
  215. @ProjectModel.putElement = (passedProject_id, destinationFolder_id, entity, entityType, callback)->
  216. callback null, path:endPath
  217. @tpdsUpdateSender.moveEntity = (opts)=>
  218. opts.project_id.should.equal project_id
  219. opts.startPath.should.equal startPath.fileSystem
  220. opts.endPath.should.equal endPath.fileSystem
  221. opts.project_name.should.equal @project.name
  222. opts.rev.should.equal @doc.rev
  223. done()
  224. @ProjectEntityHandler.moveEntity project_id, @docId, folder_id, "docs", ->
  225. describe 'removing element from mongo array', ->
  226. it 'should call update with log the path', (done)->
  227. mongoPath = "folders[0].folders[5]"
  228. id = "12344"
  229. firstUpdate = true
  230. model =
  231. update: (conditions, update, opts, callback)->
  232. if firstUpdate
  233. conditions._id.should.equal id
  234. update.$unset[mongoPath].should.equal 1
  235. firstUpdate = false
  236. callback()
  237. else
  238. conditions._id.should.equal id
  239. assert.deepEqual update, { '$pull': { 'folders[0]': null } }
  240. done()
  241. @ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, ->
  242. describe 'getDoc', ->
  243. beforeEach ->
  244. @lines = ["mock", "doc", "lines"]
  245. @rev = 5
  246. @DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev)
  247. @ProjectEntityHandler.getDoc project_id, doc_id, @callback
  248. it "should call the docstore", ->
  249. @DocstoreManager.getDoc
  250. .calledWith(project_id, doc_id)
  251. .should.equal true
  252. it "should call the callback with the lines, version and rev", ->
  253. @callback.calledWith(null, @lines, @rev).should.equal true
  254. describe 'addDoc', ->
  255. beforeEach ->
  256. @name = "some new doc"
  257. @lines = ['1234','abc']
  258. @path = "/path/to/doc"
  259. @ProjectModel.putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
  260. @callback = sinon.stub()
  261. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(2)
  262. @DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, true, 0)
  263. @ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, @callback
  264. # Created doc
  265. @doc = @ProjectModel.putElement.args[0][2]
  266. @doc.name.should.equal @name
  267. expect(@doc.lines).to.be.undefined
  268. it 'should call put element', ->
  269. @ProjectModel.putElement
  270. .calledWith(project_id, folder_id, @doc)
  271. .should.equal true
  272. it 'should return doc and parent folder', ->
  273. @callback.calledWith(null, @doc, folder_id).should.equal true
  274. it 'should call third party data store', ->
  275. @tpdsUpdateSender.addDoc
  276. .calledWith({
  277. project_id: project_id
  278. doc_id: doc_id
  279. path: @path
  280. project_name: @project.name
  281. rev: 0
  282. })
  283. .should.equal true
  284. it "should send the doc lines to the doc store", ->
  285. @DocstoreManager.updateDoc
  286. .calledWith(project_id, @doc._id.toString(), @lines)
  287. .should.equal true
  288. describe "restoreDoc", ->
  289. beforeEach ->
  290. @name = "doc-name"
  291. @lines = ['1234','abc']
  292. @doc = { "mock": "doc" }
  293. @folder_id = "mock-folder-id"
  294. @callback = sinon.stub()
  295. @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(3, null, @lines)
  296. @ProjectEntityHandler.addDoc = sinon.stub().callsArgWith(4, null, @doc, @folder_id)
  297. @ProjectEntityHandler.restoreDoc project_id, doc_id, @name, @callback
  298. it 'should get the doc lines', ->
  299. @ProjectEntityHandler.getDoc
  300. .calledWith(project_id, doc_id, include_deleted: true)
  301. .should.equal true
  302. it "should add a new doc with these doc lines", ->
  303. @ProjectEntityHandler.addDoc
  304. .calledWith(project_id, null, @name, @lines)
  305. .should.equal true
  306. it "should call the callback with the new folder and doc", ->
  307. @callback.calledWith(null, @doc, @folder_id).should.equal true
  308. describe 'adding file', ->
  309. fileName = "something.jpg"
  310. beforeEach ->
  311. @filePath = "somewhere"
  312. it 'should upload it via the FileStoreHandler', (done)->
  313. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  314. file_id.should.equal "file_id"
  315. passedProject_id.should.equal project_id
  316. filePath.should.equal @filePath
  317. done()
  318. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
  319. it 'should put file into folder by calling put element', (done)->
  320. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  321. passedProject_id.should.equal project_id
  322. passedFolder_id.should.equal folder_id
  323. passedFileRef.name.should.equal fileName
  324. passedType.should.equal 'file'
  325. done()
  326. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  327. it 'should return doc and parent folder', (done)->
  328. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  329. parentFolder.should.equal folder_id
  330. fileRef.name.should.equal fileName
  331. done()
  332. it 'should call third party data store', (done)->
  333. @project.existsInVersioningApi = true
  334. opts =
  335. path : "/somehwere/idsadsds"
  336. project_id : project_id
  337. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  338. @tpdsUpdateSender.addFile = (options)=>
  339. options.project_id.should.equal project_id
  340. options.path.should.equal opts.path
  341. options.project_name.should.equal @project.name
  342. options.file_id.should.not.be.null
  343. options.rev.should.equal 0
  344. done()
  345. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  346. describe 'replacing a file', ->
  347. beforeEach ->
  348. @projectLocator
  349. @file_id = "file_id_here"
  350. @fsPath = "fs_path_here.png"
  351. @fileRef = {rev:3, _id:@file_id}
  352. @filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
  353. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  354. @ProjectModel.update = (_, __, ___, cb)-> cb()
  355. it 'should find the file', (done)->
  356. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  357. @projectLocator.findElement.calledWith({element_id:@file_id, type:"file", project_id:project_id}).should.equal true
  358. done()
  359. it 'should tell the file store handler to upload the file from disk', (done)->
  360. @FileStoreHandler.uploadFileFromDisk = sinon.stub().callsArgWith(3)
  361. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  362. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  363. done()
  364. it 'should send the file to the tpds with an incremented rev', (done)->
  365. @tpdsUpdateSender.addFile = (options)=>
  366. options.project_id.should.equal project_id
  367. options.path.should.equal @filePaths.fileSystem
  368. options.project_name.should.equal @project.name
  369. options.file_id.should.equal @file_id
  370. options.rev.should.equal @fileRef.rev + 1
  371. done()
  372. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  373. it 'should inc the rev id', (done)->
  374. @ProjectModel.update = (conditions, update, options, callback)=>
  375. conditions._id.should.equal project_id
  376. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  377. done()
  378. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  379. it 'should update the created at date', (done)->
  380. d = new Date()
  381. @ProjectModel.update = (conditions, update, options, callback)=>
  382. conditions._id.should.equal project_id
  383. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  384. differenceInMs.should.be.below(10)
  385. done()
  386. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  387. describe 'adding a folder', ->
  388. folderName = "folder1234"
  389. it 'should call put element', (done)->
  390. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFolder, passedType, callback)->
  391. passedProject_id.should.equal project_id
  392. passedFolder_id.should.equal folder_id
  393. passedFolder.name.should.equal folderName
  394. passedType.should.equal 'folder'
  395. done()
  396. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  397. it 'should return the folder and parent folder', (done)->
  398. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  399. parentFolder.should.equal folder_id
  400. folder.name.should.equal folderName
  401. done()
  402. describe 'updateDocLines', ->
  403. beforeEach ->
  404. @lines = ['mock', 'doc', 'lines']
  405. @path = "/somewhere/something.tex"
  406. @doc = {
  407. _id: doc_id
  408. }
  409. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  410. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  411. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  412. @projectUpdater.markAsUpdated = sinon.stub()
  413. @callback = sinon.stub()
  414. describe "when the doc has been modified", ->
  415. beforeEach ->
  416. @DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, true, @rev = 5)
  417. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  418. it "should get the project without doc lines", ->
  419. @ProjectGetter.getProjectWithoutDocLines
  420. .calledWith(project_id)
  421. .should.equal true
  422. it "should find the doc", ->
  423. @projectLocator.findElement
  424. .calledWith({
  425. project: @project
  426. type: "docs"
  427. element_id: doc_id
  428. })
  429. .should.equal true
  430. it "should update the doc in the docstore", ->
  431. @DocstoreManager.updateDoc
  432. .calledWith(project_id, doc_id, @lines)
  433. .should.equal true
  434. it "should mark the project as updated", ->
  435. @projectUpdater.markAsUpdated
  436. .calledWith(project_id)
  437. .should.equal true
  438. it "should send the doc the to the TPDS", ->
  439. @tpdsUpdateSender.addDoc
  440. .calledWith({
  441. project_id: project_id
  442. project_name: @project.name
  443. doc_id: doc_id
  444. rev: @rev
  445. path: @path
  446. })
  447. .should.equal true
  448. it "should call the callback", ->
  449. @callback.called.should.equal true
  450. describe "when the doc has not been modified", ->
  451. beforeEach ->
  452. @DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, false, @rev = 5)
  453. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  454. it "should not mark the project as updated", ->
  455. @projectUpdater.markAsUpdated.called.should.equal false
  456. it "should not send the doc the to the TPDS", ->
  457. @tpdsUpdateSender.addDoc.called.should.equal false
  458. it "should call the callback", ->
  459. @callback.called.should.equal true
  460. describe "when the project is not found", ->
  461. beforeEach ->
  462. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  463. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  464. it "should return a not found error", ->
  465. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  466. describe "when the doc is not found", ->
  467. beforeEach ->
  468. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  469. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  470. it "should log out the error", ->
  471. @logger.error
  472. .calledWith(
  473. project_id: project_id
  474. doc_id: doc_id
  475. lines: @lines
  476. err: new Errors.NotFoundError("doc not found")
  477. "doc not found while updating doc lines"
  478. )
  479. .should.equal true
  480. it "should return a not found error", ->
  481. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  482. describe "getting folders, docs and files", ->
  483. beforeEach ->
  484. @project.rootFolder = [
  485. docs: [@doc1 = {
  486. name : "doc1"
  487. _id : "doc1_id"
  488. }]
  489. fileRefs: [@file1 = {
  490. rev : 1
  491. _id : "file1_id"
  492. name : "file1"
  493. }]
  494. folders: [@folder1 = {
  495. name : "folder1"
  496. docs : [@doc2 = {
  497. name : "doc2"
  498. _id : "doc2_id"
  499. }]
  500. fileRefs : [@file2 = {
  501. rev : 2
  502. name : "file2"
  503. _id : "file2_id"
  504. }]
  505. folders : []
  506. }]
  507. ]
  508. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  509. describe "getAllFolders", ->
  510. beforeEach ->
  511. @callback = sinon.stub()
  512. @ProjectEntityHandler.getAllFolders project_id, @callback
  513. it "should get the project without the docs lines", ->
  514. @ProjectGetter.getProjectWithoutDocLines
  515. .calledWith(project_id)
  516. .should.equal true
  517. it "should call the callback with the folders", ->
  518. @callback
  519. .calledWith(null, {
  520. "/": @project.rootFolder[0]
  521. "/folder1": @folder1
  522. })
  523. .should.equal true
  524. describe "getAllFiles", ->
  525. beforeEach ->
  526. @callback = sinon.stub()
  527. @ProjectEntityHandler.getAllFiles project_id, @callback
  528. it "should call the callback with the files", ->
  529. @callback
  530. .calledWith(null, {
  531. "/file1": @file1
  532. "/folder1/file2": @file2
  533. })
  534. .should.equal true
  535. describe "getAllDocs", ->
  536. beforeEach ->
  537. @docs = [{
  538. _id: @doc1._id
  539. lines: @lines1 = ["one"]
  540. rev: @rev1 = 1
  541. }, {
  542. _id: @doc2._id
  543. lines: @lines2 = ["two"]
  544. rev: @rev2 = 2
  545. }]
  546. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  547. @ProjectEntityHandler.getAllDocs project_id, @callback
  548. it "should get the doc lines and rev from the docstore", ->
  549. @DocstoreManager.getAllDocs
  550. .calledWith(project_id)
  551. .should.equal true
  552. it "should call the callback with the docs with the lines and rev included", ->
  553. @callback
  554. .calledWith(null, {
  555. "/doc1": {
  556. _id: @doc1._id
  557. lines: @lines1
  558. name: @doc1.name
  559. rev: @rev1
  560. }
  561. "/folder1/doc2": {
  562. _id: @doc2._id
  563. lines: @lines2
  564. name: @doc2.name
  565. rev: @rev2
  566. }
  567. })
  568. .should.equal true
  569. describe "flushProjectToThirdPartyDataStore", ->
  570. beforeEach (done) ->
  571. @project = {
  572. _id: project_id
  573. name: "Mock project name"
  574. }
  575. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  576. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(2)
  577. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(2)
  578. @tpdsUpdateSender.addFile = sinon.stub().callsArg(2)
  579. @docs = {
  580. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  581. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  582. }
  583. @files = {
  584. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  585. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  586. }
  587. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  588. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  589. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  590. it "should flush the project from the doc updater", ->
  591. @documentUpdaterHandler.flushProjectToMongo
  592. .calledWith(project_id)
  593. .should.equal true
  594. it "should look up the project in mongo", ->
  595. @ProjectModel.findById
  596. .calledWith(project_id)
  597. .should.equal true
  598. it "should get all the docs in the project", ->
  599. @ProjectEntityHandler.getAllDocs
  600. .calledWith(project_id)
  601. .should.equal true
  602. it "should get all the files in the project", ->
  603. @ProjectEntityHandler.getAllFiles
  604. .calledWith(project_id)
  605. .should.equal true
  606. it "should flush each doc to the TPDS", ->
  607. for path, doc of @docs
  608. @tpdsUpdateSender.addDoc
  609. .calledWith({
  610. project_id: project_id,
  611. doc_id: doc._id
  612. project_name: @project.name
  613. rev: doc.rev
  614. path: path
  615. })
  616. .should.equal true
  617. it "should flush each file to the TPDS", ->
  618. for path, file of @files
  619. @tpdsUpdateSender.addFile
  620. .calledWith({
  621. project_id: project_id,
  622. file_id: file._id
  623. project_name: @project.name
  624. rev: file.rev
  625. path: path
  626. })
  627. .should.equal true
  628. describe "setRootDoc", ->
  629. it "should call Project.update", ->
  630. @project_id = "project-id-123234adfs"
  631. @rootDoc_id = "root-doc-id-123123"
  632. @ProjectModel.update = sinon.stub()
  633. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  634. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  635. .should.equal true
  636. describe "unsetRootDoc", ->
  637. it "should call Project.update", ->
  638. @project_id = "project-id-123234adfs"
  639. @rootDoc_id = "root-doc-id-123123"
  640. @ProjectModel.update = sinon.stub()
  641. @ProjectEntityHandler.unsetRootDoc @project_id
  642. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  643. .should.equal true
  644. describe 'copying file', ->
  645. fileName = "something.jpg"
  646. filePath = "dumpFolder/somewhere/image.jpeg"
  647. oldProject_id = "123kljadas"
  648. oldFileRef = {name:fileName, _id:"oldFileRef"}
  649. it 'should copy the file in FileStoreHandler', (done)->
  650. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  651. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  652. done()
  653. it 'should put file into folder by calling put element', (done)->
  654. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  655. passedProject_id.should.equal project_id
  656. passedFolder_id.should.equal folder_id
  657. passedFileRef.name.should.equal fileName
  658. passedType.should.equal 'file'
  659. done()
  660. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  661. it 'should return doc and parent folder', (done)->
  662. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  663. parentFolder.should.equal folder_id
  664. fileRef.name.should.equal fileName
  665. done()
  666. it 'should call third party data store if versioning is enabled', (done)->
  667. @project.existsInVersioningApi = true
  668. opts =
  669. path : "/somehwere/idsadsds"
  670. project_id : project_id
  671. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  672. @tpdsUpdateSender.addFile = (options)=>
  673. options.project_id.should.equal project_id
  674. options.project_name.should.equal @project.name
  675. options.path.should.equal opts.path
  676. options.file_id.should.not.be.null
  677. options.rev.should.equal 0
  678. done()
  679. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  680. describe "renameEntity", ->
  681. beforeEach ->
  682. @entity_id = "4eecaffcbffa66588e000009"
  683. @entityType = "doc"
  684. @newName = "new.tex"
  685. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  686. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  687. @ProjectModel.update = sinon.stub().callsArgWith(3)
  688. @tpdsUpdateSender.moveEntity = sinon.stub()
  689. it "should update the name in mongo", (done)->
  690. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  691. @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
  692. done()
  693. it "should send the update to the tpds", (done)->
  694. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  695. @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  696. done()
  697. describe "_insertDeletedDocReference", ->
  698. beforeEach ->
  699. @doc =
  700. _id: ObjectId()
  701. name: "test.tex"
  702. @callback = sinon.stub()
  703. @ProjectModel.update = sinon.stub().callsArgWith(3)
  704. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  705. it "should insert the doc into deletedDocs", ->
  706. @ProjectModel.update
  707. .calledWith({
  708. _id: project_id
  709. }, {
  710. $push: {
  711. deletedDocs: {
  712. _id: @doc._id
  713. name: @doc.name
  714. }
  715. }
  716. })
  717. .should.equal true
  718. it "should call the callback", ->
  719. @callback.called.should.equal true
  720. describe "_cleanUpDoc", ->
  721. beforeEach ->
  722. @project =
  723. _id: ObjectId(project_id)
  724. @doc =
  725. _id: ObjectId()
  726. name: "test.tex"
  727. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  728. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  729. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  730. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  731. @callback = sinon.stub()
  732. describe "when the doc is the root doc", ->
  733. beforeEach ->
  734. @project.rootDoc_id = @doc._id
  735. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  736. it "should unset the root doc", ->
  737. @ProjectEntityHandler.unsetRootDoc
  738. .calledWith(project_id)
  739. .should.equal true
  740. it "should delete the doc in the doc updater", ->
  741. @documentUpdaterHandler.deleteDoc
  742. .calledWith(project_id, @doc._id.toString())
  743. it "should insert the doc into the deletedDocs array", ->
  744. @ProjectEntityHandler._insertDeletedDocReference
  745. .calledWith(@project._id, @doc)
  746. .should.equal true
  747. it "should delete the doc in the doc store", ->
  748. @DocstoreManager.deleteDoc
  749. .calledWith(project_id, @doc._id.toString())
  750. .should.equal true
  751. it "should call the callback", ->
  752. @callback.called.should.equal true
  753. describe "when the doc is not the root doc", ->
  754. beforeEach ->
  755. @project.rootDoc_id = ObjectId()
  756. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  757. it "should not unset the root doc", ->
  758. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  759. it "should call the callback", ->
  760. @callback.called.should.equal true