ProjectEntityHandlerTests.coffee 33 KB

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