ProjectEntityHandlerTests.coffee 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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(2, 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 'adding file', ->
  289. fileName = "something.jpg"
  290. beforeEach ->
  291. @filePath = "somewhere"
  292. it 'should upload it via the FileStoreHandler', (done)->
  293. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  294. file_id.should.equal "file_id"
  295. passedProject_id.should.equal project_id
  296. filePath.should.equal @filePath
  297. done()
  298. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
  299. it 'should put file into folder by calling put element', (done)->
  300. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  301. passedProject_id.should.equal project_id
  302. passedFolder_id.should.equal folder_id
  303. passedFileRef.name.should.equal fileName
  304. passedType.should.equal 'file'
  305. done()
  306. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  307. it 'should return doc and parent folder', (done)->
  308. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  309. parentFolder.should.equal folder_id
  310. fileRef.name.should.equal fileName
  311. done()
  312. it 'should call third party data store', (done)->
  313. @project.existsInVersioningApi = true
  314. opts =
  315. path : "/somehwere/idsadsds"
  316. project_id : project_id
  317. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  318. @tpdsUpdateSender.addFile = (options)=>
  319. options.project_id.should.equal project_id
  320. options.path.should.equal opts.path
  321. options.project_name.should.equal @project.name
  322. options.file_id.should.not.be.null
  323. options.rev.should.equal 0
  324. done()
  325. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  326. describe 'replacing a file', ->
  327. beforeEach ->
  328. @projectLocator
  329. @file_id = "file_id_here"
  330. @fsPath = "fs_path_here.png"
  331. @fileRef = {rev:3, _id:@file_id}
  332. @filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
  333. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  334. @ProjectModel.update = (_, __, ___, cb)-> cb()
  335. it 'should find the file', (done)->
  336. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  337. @projectLocator.findElement.calledWith({element_id:@file_id, type:"file", project_id:project_id}).should.equal true
  338. done()
  339. it 'should tell the file store handler to upload the file from disk', (done)->
  340. @FileStoreHandler.uploadFileFromDisk = sinon.stub().callsArgWith(3)
  341. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  342. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  343. done()
  344. it 'should send the file to the tpds with an incremented rev', (done)->
  345. @tpdsUpdateSender.addFile = (options)=>
  346. options.project_id.should.equal project_id
  347. options.path.should.equal @filePaths.fileSystem
  348. options.project_name.should.equal @project.name
  349. options.file_id.should.equal @file_id
  350. options.rev.should.equal @fileRef.rev + 1
  351. done()
  352. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  353. it 'should inc the rev id', (done)->
  354. @ProjectModel.update = (conditions, update, options, callback)=>
  355. conditions._id.should.equal project_id
  356. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  357. done()
  358. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  359. it 'should update the created at date', (done)->
  360. d = new Date()
  361. @ProjectModel.update = (conditions, update, options, callback)=>
  362. conditions._id.should.equal project_id
  363. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  364. differenceInMs.should.be.below(10)
  365. done()
  366. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  367. describe 'adding a folder', ->
  368. folderName = "folder1234"
  369. it 'should call put element', (done)->
  370. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFolder, passedType, callback)->
  371. passedProject_id.should.equal project_id
  372. passedFolder_id.should.equal folder_id
  373. passedFolder.name.should.equal folderName
  374. passedType.should.equal 'folder'
  375. done()
  376. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  377. it 'should return the folder and parent folder', (done)->
  378. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  379. parentFolder.should.equal folder_id
  380. folder.name.should.equal folderName
  381. done()
  382. describe 'updateDocLines', ->
  383. beforeEach ->
  384. @lines = ['mock', 'doc', 'lines']
  385. @path = "/somewhere/something.tex"
  386. @doc = {
  387. _id: doc_id
  388. }
  389. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  390. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
  391. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
  392. @projectUpdater.markAsUpdated = sinon.stub()
  393. @callback = sinon.stub()
  394. describe "when the doc has been modified", ->
  395. beforeEach ->
  396. @DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, true, @rev = 5)
  397. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  398. it "should get the project without doc lines", ->
  399. @ProjectGetter.getProjectWithoutDocLines
  400. .calledWith(project_id)
  401. .should.equal true
  402. it "should find the doc", ->
  403. @projectLocator.findElement
  404. .calledWith({
  405. project: @project
  406. type: "docs"
  407. element_id: doc_id
  408. })
  409. .should.equal true
  410. it "should update the doc in the docstore", ->
  411. @DocstoreManager.updateDoc
  412. .calledWith(project_id, doc_id, @lines)
  413. .should.equal true
  414. it "should mark the project as updated", ->
  415. @projectUpdater.markAsUpdated
  416. .calledWith(project_id)
  417. .should.equal true
  418. it "should send the doc the to the TPDS", ->
  419. @tpdsUpdateSender.addDoc
  420. .calledWith({
  421. project_id: project_id
  422. project_name: @project.name
  423. doc_id: doc_id
  424. rev: @rev
  425. path: @path
  426. })
  427. .should.equal true
  428. it "should call the callback", ->
  429. @callback.called.should.equal true
  430. describe "when the doc has not been modified", ->
  431. beforeEach ->
  432. @DocstoreManager.updateDoc = sinon.stub().callsArgWith(3, null, false, @rev = 5)
  433. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  434. it "should not mark the project as updated", ->
  435. @projectUpdater.markAsUpdated.called.should.equal false
  436. it "should not send the doc the to the TPDS", ->
  437. @tpdsUpdateSender.addDoc.called.should.equal false
  438. it "should call the callback", ->
  439. @callback.called.should.equal true
  440. describe "when the project is not found", ->
  441. beforeEach ->
  442. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
  443. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  444. it "should return a not found error", ->
  445. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  446. describe "when the doc is not found", ->
  447. beforeEach ->
  448. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
  449. @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @callback
  450. it "should log out the error", ->
  451. @logger.error
  452. .calledWith(
  453. project_id: project_id
  454. doc_id: doc_id
  455. lines: @lines
  456. err: new Errors.NotFoundError("doc not found")
  457. "doc not found while updating doc lines"
  458. )
  459. .should.equal true
  460. it "should return a not found error", ->
  461. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  462. describe "getting folders, docs and files", ->
  463. beforeEach ->
  464. @project.rootFolder = [
  465. docs: [@doc1 = {
  466. name : "doc1"
  467. _id : "doc1_id"
  468. }]
  469. fileRefs: [@file1 = {
  470. rev : 1
  471. _id : "file1_id"
  472. name : "file1"
  473. }]
  474. folders: [@folder1 = {
  475. name : "folder1"
  476. docs : [@doc2 = {
  477. name : "doc2"
  478. _id : "doc2_id"
  479. }]
  480. fileRefs : [@file2 = {
  481. rev : 2
  482. name : "file2"
  483. _id : "file2_id"
  484. }]
  485. folders : []
  486. }]
  487. ]
  488. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
  489. describe "getAllFolders", ->
  490. beforeEach ->
  491. @callback = sinon.stub()
  492. @ProjectEntityHandler.getAllFolders project_id, @callback
  493. it "should get the project without the docs lines", ->
  494. @ProjectGetter.getProjectWithoutDocLines
  495. .calledWith(project_id)
  496. .should.equal true
  497. it "should call the callback with the folders", ->
  498. @callback
  499. .calledWith(null, {
  500. "/": @project.rootFolder[0]
  501. "/folder1": @folder1
  502. })
  503. .should.equal true
  504. describe "getAllFiles", ->
  505. beforeEach ->
  506. @callback = sinon.stub()
  507. @ProjectEntityHandler.getAllFiles project_id, @callback
  508. it "should call the callback with the files", ->
  509. @callback
  510. .calledWith(null, {
  511. "/file1": @file1
  512. "/folder1/file2": @file2
  513. })
  514. .should.equal true
  515. describe "getAllDocs", ->
  516. beforeEach ->
  517. @docs = [{
  518. _id: @doc1._id
  519. lines: @lines1 = ["one"]
  520. rev: @rev1 = 1
  521. }, {
  522. _id: @doc2._id
  523. lines: @lines2 = ["two"]
  524. rev: @rev2 = 2
  525. }]
  526. @DocstoreManager.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  527. @ProjectEntityHandler.getAllDocs project_id, @callback
  528. it "should get the doc lines and rev from the docstore", ->
  529. @DocstoreManager.getAllDocs
  530. .calledWith(project_id)
  531. .should.equal true
  532. it "should call the callback with the docs with the lines and rev included", ->
  533. @callback
  534. .calledWith(null, {
  535. "/doc1": {
  536. _id: @doc1._id
  537. lines: @lines1
  538. name: @doc1.name
  539. rev: @rev1
  540. }
  541. "/folder1/doc2": {
  542. _id: @doc2._id
  543. lines: @lines2
  544. name: @doc2.name
  545. rev: @rev2
  546. }
  547. })
  548. .should.equal true
  549. describe "flushProjectToThirdPartyDataStore", ->
  550. beforeEach (done) ->
  551. @project = {
  552. _id: project_id
  553. name: "Mock project name"
  554. }
  555. @ProjectModel.findById = sinon.stub().callsArgWith(1, null, @project)
  556. @documentUpdaterHandler.flushProjectToMongo = sinon.stub().callsArg(2)
  557. @tpdsUpdateSender.addDoc = sinon.stub().callsArg(2)
  558. @tpdsUpdateSender.addFile = sinon.stub().callsArg(2)
  559. @docs = {
  560. "/doc/one": @doc1 = { _id: "mock-doc-1", lines: ["one"], rev: 5 }
  561. "/doc/two": @doc2 = { _id: "mock-doc-2", lines: ["two"], rev: 6 }
  562. }
  563. @files = {
  564. "/file/one": @file1 = { _id: "mock-file-1", rev: 7 }
  565. "/file/two": @file2 = { _id: "mock-file-2", rev: 8 }
  566. }
  567. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  568. @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
  569. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, () -> done()
  570. it "should flush the project from the doc updater", ->
  571. @documentUpdaterHandler.flushProjectToMongo
  572. .calledWith(project_id)
  573. .should.equal true
  574. it "should look up the project in mongo", ->
  575. @ProjectModel.findById
  576. .calledWith(project_id)
  577. .should.equal true
  578. it "should get all the docs in the project", ->
  579. @ProjectEntityHandler.getAllDocs
  580. .calledWith(project_id)
  581. .should.equal true
  582. it "should get all the files in the project", ->
  583. @ProjectEntityHandler.getAllFiles
  584. .calledWith(project_id)
  585. .should.equal true
  586. it "should flush each doc to the TPDS", ->
  587. for path, doc of @docs
  588. @tpdsUpdateSender.addDoc
  589. .calledWith({
  590. project_id: project_id,
  591. doc_id: doc._id
  592. project_name: @project.name
  593. rev: doc.rev
  594. path: path
  595. })
  596. .should.equal true
  597. it "should flush each file to the TPDS", ->
  598. for path, file of @files
  599. @tpdsUpdateSender.addFile
  600. .calledWith({
  601. project_id: project_id,
  602. file_id: file._id
  603. project_name: @project.name
  604. rev: file.rev
  605. path: path
  606. })
  607. .should.equal true
  608. describe "setRootDoc", ->
  609. it "should call Project.update", ->
  610. @project_id = "project-id-123234adfs"
  611. @rootDoc_id = "root-doc-id-123123"
  612. @ProjectModel.update = sinon.stub()
  613. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  614. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  615. .should.equal true
  616. describe "unsetRootDoc", ->
  617. it "should call Project.update", ->
  618. @project_id = "project-id-123234adfs"
  619. @rootDoc_id = "root-doc-id-123123"
  620. @ProjectModel.update = sinon.stub()
  621. @ProjectEntityHandler.unsetRootDoc @project_id
  622. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  623. .should.equal true
  624. describe 'copying file', ->
  625. fileName = "something.jpg"
  626. filePath = "dumpFolder/somewhere/image.jpeg"
  627. oldProject_id = "123kljadas"
  628. oldFileRef = {name:fileName, _id:"oldFileRef"}
  629. it 'should copy the file in FileStoreHandler', (done)->
  630. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  631. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  632. done()
  633. it 'should put file into folder by calling put element', (done)->
  634. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  635. passedProject_id.should.equal project_id
  636. passedFolder_id.should.equal folder_id
  637. passedFileRef.name.should.equal fileName
  638. passedType.should.equal 'file'
  639. done()
  640. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  641. it 'should return doc and parent folder', (done)->
  642. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  643. parentFolder.should.equal folder_id
  644. fileRef.name.should.equal fileName
  645. done()
  646. it 'should call third party data store if versioning is enabled', (done)->
  647. @project.existsInVersioningApi = true
  648. opts =
  649. path : "/somehwere/idsadsds"
  650. project_id : project_id
  651. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  652. @tpdsUpdateSender.addFile = (options)=>
  653. options.project_id.should.equal project_id
  654. options.project_name.should.equal @project.name
  655. options.path.should.equal opts.path
  656. options.file_id.should.not.be.null
  657. options.rev.should.equal 0
  658. done()
  659. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  660. describe "renameEntity", ->
  661. beforeEach ->
  662. @entity_id = "4eecaffcbffa66588e000009"
  663. @entityType = "doc"
  664. @newName = "new.tex"
  665. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  666. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  667. @ProjectModel.update = sinon.stub().callsArgWith(3)
  668. @tpdsUpdateSender.moveEntity = sinon.stub()
  669. it "should update the name in mongo", (done)->
  670. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  671. @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
  672. done()
  673. it "should send the update to the tpds", (done)->
  674. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  675. @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  676. done()
  677. describe "_insertDeletedDocReference", ->
  678. beforeEach ->
  679. @doc =
  680. _id: ObjectId()
  681. name: "test.tex"
  682. @callback = sinon.stub()
  683. @ProjectModel.update = sinon.stub().callsArgWith(3)
  684. @ProjectEntityHandler._insertDeletedDocReference project_id, @doc, @callback
  685. it "should insert the doc into deletedDocs", ->
  686. @ProjectModel.update
  687. .calledWith({
  688. _id: project_id
  689. }, {
  690. $push: {
  691. deletedDocs: {
  692. _id: @doc._id
  693. name: @doc.name
  694. }
  695. }
  696. })
  697. .should.equal true
  698. it "should call the callback", ->
  699. @callback.called.should.equal true
  700. describe "_cleanUpDoc", ->
  701. beforeEach ->
  702. @project =
  703. _id: ObjectId(project_id)
  704. @doc =
  705. _id: ObjectId()
  706. name: "test.tex"
  707. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  708. @ProjectEntityHandler._insertDeletedDocReference = sinon.stub().callsArg(2)
  709. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  710. @DocstoreManager.deleteDoc = sinon.stub().callsArg(2)
  711. @callback = sinon.stub()
  712. describe "when the doc is the root doc", ->
  713. beforeEach ->
  714. @project.rootDoc_id = @doc._id
  715. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  716. it "should unset the root doc", ->
  717. @ProjectEntityHandler.unsetRootDoc
  718. .calledWith(project_id)
  719. .should.equal true
  720. it "should delete the doc in the doc updater", ->
  721. @documentUpdaterHandler.deleteDoc
  722. .calledWith(project_id, @doc._id.toString())
  723. it "should insert the doc into the deletedDocs array", ->
  724. @ProjectEntityHandler._insertDeletedDocReference
  725. .calledWith(@project._id, @doc)
  726. .should.equal true
  727. it "should delete the doc in the doc store", ->
  728. @DocstoreManager.deleteDoc
  729. .calledWith(project_id, @doc._id.toString())
  730. .should.equal true
  731. it "should call the callback", ->
  732. @callback.called.should.equal true
  733. describe "when the doc is not the root doc", ->
  734. beforeEach ->
  735. @project.rootDoc_id = ObjectId()
  736. @ProjectEntityHandler._cleanUpDoc @project, @doc, @callback
  737. it "should not unset the root doc", ->
  738. @ProjectEntityHandler.unsetRootDoc.called.should.equal false
  739. it "should call the callback", ->
  740. @callback.called.should.equal true