ProjectEntityHandlerTests.coffee 29 KB

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