ProjectEntityHandlerTests.coffee 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. 'logger-sharelatex':{log:->}
  61. './ProjectUpdateHandler': @projectUpdater
  62. describe 'mkdirp', ->
  63. beforeEach ->
  64. @parentFolder_id = "1jnjknjk"
  65. @newFolder = {_id:"newFolder_id_here"}
  66. @lastFolder = {_id:"123das", folders:[]}
  67. @projectLocator.findElementByPath = (project_id, path, cb)=>
  68. @parentFolder = {_id:"parentFolder_id_here"}
  69. lastFolder = path.substring(path.lastIndexOf("/"))
  70. if lastFolder.indexOf("level1") == -1
  71. cb "level1 is not the last foler "
  72. else
  73. cb null, @parentFolder
  74. @ProjectEntityHandler.addFolder = (project_id, parentFolder_id, folderName, sl_req_id, callback)=>
  75. callback null, {name:folderName}, @parentFolder_id
  76. it 'should return the root folder if the path is just a slash', (done)->
  77. path = "/"
  78. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  79. lastFolder.should.deep.equal @rootFolder
  80. assert.equal lastFolder.parentFolder_id, undefined
  81. done()
  82. it 'should make just one folder', (done)->
  83. path = "/differentFolder/"
  84. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  85. folders.length.should.equal 1
  86. lastFolder.name.should.equal "differentFolder"
  87. lastFolder.parentFolder_id.should.equal @parentFolder_id
  88. done()
  89. it 'should make the final folder in path if it doesnt exist with one level', (done)->
  90. path = "level1/level2"
  91. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  92. folders.length.should.equal 1
  93. lastFolder.name.should.equal "level2"
  94. lastFolder.parentFolder_id.should.equal @parentFolder_id
  95. done()
  96. it 'should make the final folder in path if it doesnt exist with mutliple levels', (done)->
  97. path = "level1/level2/level3"
  98. @ProjectEntityHandler.mkdirp project_id, path,(err, folders, lastFolder) =>
  99. folders.length.should.equal 2
  100. folders[0].name.should.equal "level2"
  101. folders[0].parentFolder_id.should.equal @parentFolder_id
  102. lastFolder.name.should.equal "level3"
  103. lastFolder.parentFolder_id.should.equal @parentFolder_id
  104. done()
  105. it 'should work with slashes either side', (done)->
  106. path = "/level1/level2/level3/"
  107. @ProjectEntityHandler.mkdirp project_id, path, (err, folders, lastFolder)=>
  108. folders.length.should.equal 2
  109. folders[0].name.should.equal "level2"
  110. folders[0].parentFolder_id.should.equal @parentFolder_id
  111. lastFolder.name.should.equal "level3"
  112. lastFolder.parentFolder_id.should.equal @parentFolder_id
  113. done()
  114. describe 'deleting an element', ->
  115. entity_id = "4eecaffcbffa66588e000009"
  116. beforeEach ->
  117. @tpdsUpdateSender.deleteEntity = sinon.stub().callsArg(2)
  118. @ProjectEntityHandler._removeElementFromMongoArray = sinon.stub().callsArg(3)
  119. @ProjectEntityHandler._cleanUpEntity = sinon.stub().callsArg(3)
  120. @path = mongo: "mongo.path", fileSystem: "/file/system/path"
  121. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: entity_id }, @path)
  122. describe "deleting from Mongo", ->
  123. beforeEach (done) ->
  124. @ProjectEntityHandler.deleteEntity project_id, entity_id, @type = 'file', done
  125. it "should retreive the path", ->
  126. @projectLocator.findElement.called.should.equal true
  127. options = @projectLocator.findElement.args[0][0]
  128. options.type.should.equal @type
  129. options.project.should.equal @project
  130. options.element_id.should.equal entity_id
  131. it "should remove the element from the database", ->
  132. @ProjectEntityHandler._removeElementFromMongoArray.calledWith(@ProjectModel, project_id, @path.mongo).should.equal true
  133. it "should call the third party data store", ->
  134. options = @tpdsUpdateSender.deleteEntity.args[0][0]
  135. options.project_id.should.equal project_id
  136. options.path.should.equal @path.fileSystem
  137. it "should clean up the entity from the rest of the system", ->
  138. @ProjectEntityHandler._cleanUpEntity
  139. .calledWith(@project, @entity, @type)
  140. .should.equal true
  141. describe "_cleanUpEntity", ->
  142. beforeEach ->
  143. @entity_id = "4eecaffcbffa66588e000009"
  144. @documentUpdaterHandler.deleteDoc = sinon.stub().callsArg(2)
  145. @FileStoreHandler.deleteFile = sinon.stub().callsArg(2)
  146. @ProjectEntityHandler.unsetRootDoc = sinon.stub().callsArg(1)
  147. describe "a file", ->
  148. beforeEach (done) ->
  149. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'file', done
  150. it "should delete the file from FileStoreHandler", ->
  151. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal true
  152. it "should not attempt to delete from the document updater", ->
  153. @documentUpdaterHandler.deleteDoc.called.should.equal false
  154. describe "a doc", ->
  155. beforeEach (done) ->
  156. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'doc', done
  157. it "should not attempted to delete from FileStoreHandler", ->
  158. @FileStoreHandler.deleteFile.called.should.equal false
  159. it "should delete the doc from the document updater", ->
  160. @documentUpdaterHandler.deleteDoc.calledWith(project_id, @entity_id).should.equal true
  161. describe "when the entity is the root document", ->
  162. beforeEach (done) ->
  163. @project.rootDoc_id = new ObjectId(@entity_id)
  164. @ProjectEntityHandler._cleanUpEntity @project, _id: @entity_id, 'doc', done
  165. it "should unset the root doc id", ->
  166. @ProjectEntityHandler.unsetRootDoc.calledWith(project_id).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 'adding doc', ->
  242. docName = "some new doc"
  243. docLines = ['1234','abc']
  244. it 'should call put element', (done)->
  245. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedDoc, passedType, callback)->
  246. passedProject_id.should.equal project_id
  247. passedFolder_id.should.equal folder_id
  248. passedDoc.name.should.equal docName
  249. passedDoc.lines[0].should.equal docLines[0]
  250. passedDoc.lines[1].should.equal docLines[1]
  251. passedType.should.equal 'doc'
  252. done()
  253. @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)->
  254. it 'should return doc and parent folder', (done)->
  255. @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)->
  256. parentFolder.should.equal folder_id
  257. doc.name.should.equal docName
  258. done()
  259. it 'should call third party data store', (done)->
  260. fileSystemPath = "/somehwere/#{docName}"
  261. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:fileSystemPath}})
  262. @tpdsUpdateSender.addDoc = (options)=>
  263. options.project_id.should.equal project_id
  264. options.docLines.should.equal docLines
  265. options.path.should.equal fileSystemPath
  266. options.project_name.should.equal @project.name
  267. options.rev.should.equal 0
  268. done()
  269. @ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "",->
  270. describe 'adding file', ->
  271. fileName = "something.jpg"
  272. beforeEach ->
  273. @filePath = "somewhere"
  274. it 'should upload it via the FileStoreHandler', (done)->
  275. @FileStoreHandler.uploadFileFromDisk = (passedProject_id, file_id, filePath, callback)=>
  276. file_id.should.equal "file_id"
  277. passedProject_id.should.equal project_id
  278. filePath.should.equal @filePath
  279. done()
  280. @ProjectEntityHandler.addFile project_id, folder_id, fileName, @filePath, (err, fileRef, parentFolder)->
  281. it 'should put file into folder by calling put element', (done)->
  282. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  283. passedProject_id.should.equal project_id
  284. passedFolder_id.should.equal folder_id
  285. passedFileRef.name.should.equal fileName
  286. passedType.should.equal 'file'
  287. done()
  288. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  289. it 'should return doc and parent folder', (done)->
  290. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  291. parentFolder.should.equal folder_id
  292. fileRef.name.should.equal fileName
  293. done()
  294. it 'should call third party data store', (done)->
  295. @project.existsInVersioningApi = true
  296. opts =
  297. path : "/somehwere/idsadsds"
  298. project_id : project_id
  299. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  300. @tpdsUpdateSender.addFile = (options)=>
  301. options.project_id.should.equal project_id
  302. options.path.should.equal opts.path
  303. options.project_name.should.equal @project.name
  304. options.file_id.should.not.be.null
  305. options.rev.should.equal 0
  306. done()
  307. @ProjectEntityHandler.addFile project_id, folder_id, fileName, {}, (err, fileRef, parentFolder)->
  308. describe 'replacing a file', ->
  309. beforeEach ->
  310. @projectLocator
  311. @file_id = "file_id_here"
  312. @fsPath = "fs_path_here.png"
  313. @fileRef = {rev:3, _id:@file_id}
  314. @filePaths = {fileSystem:"/folder1/file.png", mongo:"folder.1.files.somewhere"}
  315. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @fileRef, @filePaths)
  316. @ProjectModel.update = (_, __, ___, cb)-> cb()
  317. it 'should find the file', (done)->
  318. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  319. @projectLocator.findElement.calledWith({element_id:@file_id, type:"file", project_id:project_id}).should.equal true
  320. done()
  321. it 'should tell the file store handler to upload the file from disk', (done)->
  322. @FileStoreHandler.uploadFileFromDisk = sinon.stub().callsArgWith(3)
  323. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  324. @FileStoreHandler.uploadFileFromDisk.calledWith(project_id, @file_id, @fsPath).should.equal true
  325. done()
  326. it 'should send the file to the tpds with an incremented rev', (done)->
  327. @tpdsUpdateSender.addFile = (options)=>
  328. options.project_id.should.equal project_id
  329. options.path.should.equal @filePaths.fileSystem
  330. options.project_name.should.equal @project.name
  331. options.file_id.should.equal @file_id
  332. options.rev.should.equal @fileRef.rev + 1
  333. done()
  334. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  335. it 'should inc the rev id', (done)->
  336. @ProjectModel.update = (conditions, update, options, callback)=>
  337. conditions._id.should.equal project_id
  338. update.$inc["#{@filePaths.mongo}.rev"].should.equal 1
  339. done()
  340. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  341. it 'should update the created at date', (done)->
  342. d = new Date()
  343. @ProjectModel.update = (conditions, update, options, callback)=>
  344. conditions._id.should.equal project_id
  345. differenceInMs = update.$set["#{@filePaths.mongo}.created"].getTime() - d.getTime()
  346. differenceInMs.should.be.below(10)
  347. done()
  348. @ProjectEntityHandler.replaceFile project_id, @file_id, @fsPath, =>
  349. describe 'adding a folder', ->
  350. folderName = "folder1234"
  351. it 'should call put element', (done)->
  352. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFolder, passedType, callback)->
  353. passedProject_id.should.equal project_id
  354. passedFolder_id.should.equal folder_id
  355. passedFolder.name.should.equal folderName
  356. passedType.should.equal 'folder'
  357. done()
  358. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  359. it 'should return the folder and parent folder', (done)->
  360. @ProjectEntityHandler.addFolder project_id, folder_id, folderName, (err, folder, parentFolder)->
  361. parentFolder.should.equal folder_id
  362. folder.name.should.equal folderName
  363. done()
  364. describe 'updating document lines', ->
  365. docId = "123456"
  366. docLines = ['1234','abc', '543543']
  367. mongoPath = "folders[0].folders[5]"
  368. fileSystemPath = "/somehwere/something.tex"
  369. it 'should find project via getProject', (done)->
  370. @ProjectModel.getProject = (passedProject_id, callback)->
  371. passedProject_id.should.equal project_id
  372. done()
  373. @ProjectEntityHandler.updateDocLines project_id, "", [], ->
  374. it 'should find the doc', (done)->
  375. @projectLocator.findElement = (options, callback)->
  376. options.element_id.should.equal docId
  377. options.type.should.equal 'docs'
  378. done()
  379. @ProjectEntityHandler.updateDocLines project_id, docId, "", ->
  380. it 'should build mongo update statment', (done)->
  381. @projectLocator.findElement = (opts, callback)->
  382. callback(null, {lines:[], rev:0}, {mongo:mongoPath})
  383. @ProjectModel.update = (conditions, update, options, callback)->
  384. conditions._id.should.equal project_id
  385. update.$set["#{mongoPath}.lines"].should.equal docLines
  386. update.$inc["#{mongoPath}.rev"].should.equal 1
  387. done()
  388. @ProjectEntityHandler.updateDocLines project_id, docId, docLines, ->
  389. it 'should call third party data store ', (done)->
  390. rev = 3
  391. @projectLocator.findElement = (opts, callback)->
  392. callback(null, {lines:[],rev:rev}, {fileSystem:fileSystemPath})
  393. @ProjectModel.update = (conditions, update, options, callback)-> callback()
  394. @tpdsUpdateSender.addDoc = (options, _, callback)=>
  395. options.project_id.should.equal project_id
  396. options.docLines.should.equal docLines
  397. options.path.should.equal fileSystemPath
  398. options.project_name.should.equal @project.name
  399. options.rev.should.equal (rev+1)
  400. callback()
  401. @projectUpdater.markAsUpdated.calledWith(project_id).should.equal true
  402. @ProjectEntityHandler.updateDocLines project_id, docId, docLines, done
  403. describe "flushing folders, docs and files", ->
  404. beforeEach ->
  405. @project.rootFolder = [
  406. folders: [{
  407. name : "folder1"
  408. folders : [{
  409. name : "folder2"
  410. folders : [{
  411. name:"folder4",
  412. docs:[{name:"doc3", rev:3,lines:["doc3"]}],
  413. fileRefs:[{_id:"file3_id", rev:3,name:"file3"}],
  414. folders:[]
  415. }]
  416. docs : [{
  417. rev:2
  418. name: "doc2"
  419. lines: ["doc2", "lines"]
  420. }]
  421. fileRefs : []
  422. }]
  423. docs : []
  424. fileRefs : [{
  425. rev:2
  426. name : "file2"
  427. _id : "file2_id"
  428. }]
  429. }, {
  430. name : "folder3"
  431. folders : []
  432. docs : []
  433. fileRefs : []
  434. }]
  435. docs: [{
  436. rev:1
  437. name: "doc1"
  438. lines: ["doc1", "lines"]
  439. }]
  440. fileRefs: [{
  441. rev:1
  442. _id : "file1_id"
  443. name : "file1"
  444. }]
  445. ]
  446. it "should work for a very small project", (done)->
  447. @project.rootFolder[0].folders = []
  448. @ProjectEntityHandler.getAllDocs project_id, (err, docs) =>
  449. docs["/doc1"].name.should.equal "doc1"
  450. @ProjectEntityHandler.getAllFiles project_id, (err, files) =>
  451. files["/file1"].name.should.equal "file1"
  452. done()
  453. it "should be able to get all folders", (done) ->
  454. @ProjectEntityHandler.getAllFolders project_id, (err, folders) ->
  455. should.exist folders["/"]
  456. should.exist folders["/folder1"]
  457. should.exist folders["/folder1/folder2"]
  458. should.exist folders["/folder1/folder2/folder4"]
  459. folders["/folder1/folder2/folder4"].name.should.equal "folder4"
  460. should.exist folders["/folder3"]
  461. done()
  462. it "should be able to get all docs", (done) ->
  463. @ProjectEntityHandler.getAllDocs project_id, (err, docs) ->
  464. docs["/doc1"].name.should.equal "doc1"
  465. docs["/folder1/folder2/doc2"].name.should.equal "doc2"
  466. docs["/folder1/folder2/folder4/doc3"].lines.should.deep.equal ["doc3"]
  467. done()
  468. it "should be able to get all files", (done) ->
  469. @ProjectEntityHandler.getAllFiles project_id, (err, files) ->
  470. files["/file1"].name.should.equal "file1"
  471. files["/folder1/file2"].name.should.equal "file2"
  472. files["/folder1/folder2/folder4/file3"].name.should.equal "file3"
  473. done()
  474. describe "flushProjectToThirdPartyDataStore", ->
  475. beforeEach (done) ->
  476. @addedDocs = {}
  477. @addedFiles = {}
  478. @tpdsUpdateSender.addDoc = (options, _, callback) =>
  479. callback()
  480. sinon.spy @tpdsUpdateSender, "addDoc"
  481. @tpdsUpdateSender.addFile = (options, _, callback) =>
  482. callback()
  483. sinon.spy @tpdsUpdateSender, "addFile"
  484. @documentUpdaterHandler.flushProjectToMongo = (project_id, sl_req_id, callback) ->
  485. callback()
  486. sinon.spy @documentUpdaterHandler, "flushProjectToMongo"
  487. @ProjectEntityHandler.flushProjectToThirdPartyDataStore project_id, (err) -> done()
  488. it "should flush the documents from the document updater", ->
  489. @documentUpdaterHandler.flushProjectToMongo
  490. .calledWith(@project._id).should.equal true
  491. @documentUpdaterHandler.flushProjectToMongo
  492. .calledBefore(@tpdsUpdateSender.addDoc).should.equal true
  493. @documentUpdaterHandler.flushProjectToMongo
  494. .calledBefore(@tpdsUpdateSender.addFile).should.equal true
  495. it "should call addDoc for each doc", ->
  496. @tpdsUpdateSender.addDoc.calledWith(
  497. project_id : @project._id
  498. path : "/doc1"
  499. docLines: ["doc1", "lines"]
  500. project_name: @project.name
  501. rev:1
  502. ).should.equal true
  503. @tpdsUpdateSender.addDoc.calledWith(
  504. project_id : @project._id
  505. path : "/folder1/folder2/doc2"
  506. docLines: ["doc2", "lines"]
  507. project_name: @project.name
  508. rev:2
  509. ).should.equal true
  510. it "should call addFile for each file", ->
  511. @tpdsUpdateSender.addFile.calledWith(
  512. project_id : @project._id
  513. file_id : "file1_id"
  514. path : "/file1"
  515. project_name: @project.name
  516. rev:1
  517. ).should.equal true
  518. @tpdsUpdateSender.addFile.calledWith(
  519. project_id : @project._id
  520. file_id : "file2_id"
  521. path : "/folder1/file2"
  522. project_name: @project.name
  523. rev:2
  524. ).should.equal true
  525. describe "setRootDoc", ->
  526. it "should call Project.update", ->
  527. @project_id = "project-id-123234adfs"
  528. @rootDoc_id = "root-doc-id-123123"
  529. @ProjectModel.update = sinon.stub()
  530. @ProjectEntityHandler.setRootDoc @project_id, @rootDoc_id
  531. @ProjectModel.update.calledWith({_id : @project_id}, {rootDoc_id: @rootDoc_id})
  532. .should.equal true
  533. describe "unsetRootDoc", ->
  534. it "should call Project.update", ->
  535. @project_id = "project-id-123234adfs"
  536. @rootDoc_id = "root-doc-id-123123"
  537. @ProjectModel.update = sinon.stub()
  538. @ProjectEntityHandler.unsetRootDoc @project_id
  539. @ProjectModel.update.calledWith({_id : @project_id}, {$unset : {rootDoc_id: true}})
  540. .should.equal true
  541. describe 'copying file', ->
  542. fileName = "something.jpg"
  543. filePath = "dumpFolder/somewhere/image.jpeg"
  544. oldProject_id = "123kljadas"
  545. oldFileRef = {name:fileName, _id:"oldFileRef"}
  546. it 'should copy the file in FileStoreHandler', (done)->
  547. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)=>
  548. @FileStoreHandler.copyFile.calledWith(oldProject_id, oldFileRef._id, project_id, fileRef._id).should.equal true
  549. done()
  550. it 'should put file into folder by calling put element', (done)->
  551. @ProjectModel.putElement = (passedProject_id, passedFolder_id, passedFileRef, passedType, callback)->
  552. passedProject_id.should.equal project_id
  553. passedFolder_id.should.equal folder_id
  554. passedFileRef.name.should.equal fileName
  555. passedType.should.equal 'file'
  556. done()
  557. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  558. it 'should return doc and parent folder', (done)->
  559. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  560. parentFolder.should.equal folder_id
  561. fileRef.name.should.equal fileName
  562. done()
  563. it 'should call third party data store if versioning is enabled', (done)->
  564. @project.existsInVersioningApi = true
  565. opts =
  566. path : "/somehwere/idsadsds"
  567. project_id : project_id
  568. @ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:opts.path}})
  569. @tpdsUpdateSender.addFile = (options)=>
  570. options.project_id.should.equal project_id
  571. options.project_name.should.equal @project.name
  572. options.path.should.equal opts.path
  573. options.file_id.should.not.be.null
  574. options.rev.should.equal 0
  575. done()
  576. @ProjectEntityHandler.copyFileFromExistingProject project_id, folder_id, oldProject_id, oldFileRef, (err, fileRef, parentFolder)->
  577. describe "renameEntity", ->
  578. beforeEach ->
  579. @entity_id = "4eecaffcbffa66588e000009"
  580. @entityType = "doc"
  581. @newName = "new.tex"
  582. @path = mongo: "mongo.path", fileSystem: "/file/system/old.tex"
  583. @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @entity = { _id: @entity_id, name:"old.tex", rev:4 }, @path)
  584. @ProjectModel.update = sinon.stub().callsArgWith(3)
  585. @tpdsUpdateSender.moveEntity = sinon.stub()
  586. it "should update the name in mongo", (done)->
  587. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  588. @ProjectModel.update.calledWith({_id : @project_id}, {"$set":{"mongo.path.name":@newName}}).should.equal true
  589. done()
  590. it "should send the update to the tpds", (done)->
  591. @ProjectEntityHandler.renameEntity @project_id, @entity_id, @entityType, @newName, =>
  592. @tpdsUpdateSender.moveEntity.calledWith({project_id:@project_id, startPath:@path.fileSystem, endPath:"/file/system/new.tex", project_name:@project.name, rev:4}).should.equal true
  593. done()