ProjectEntityHandlerTests.coffee 28 KB

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