ProjectEntityHandlerTests.coffee 30 KB

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