ProjectEntityHandlerTests.coffee 34 KB

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